aboutsummaryrefslogtreecommitdiff
path: root/external/messenger.py
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-02-29 20:47:44 +0100
committerAlex Auvolat <alex@adnab.me>2020-02-29 20:47:44 +0100
commit1316a3031b829daa28eda205bd3cdf14b192a0b4 (patch)
tree71d4844e21456652918cb02594a9386a87db92b8 /external/messenger.py
parent74314696328a52779b83777eddac6bb506a3846b (diff)
downloadeasybridge-1316a3031b829daa28eda205bd3cdf14b192a0b4.tar.gz
easybridge-1316a3031b829daa28eda205bd3cdf14b192a0b4.zip
Load member lists and avatar (low res) from facebook
Diffstat (limited to 'external/messenger.py')
-rwxr-xr-xexternal/messenger.py85
1 files changed, 84 insertions, 1 deletions
diff --git a/external/messenger.py b/external/messenger.py
index 495bca9..8403a03 100755
--- a/external/messenger.py
+++ b/external/messenger.py
@@ -2,10 +2,13 @@
import sys
import json
+import signal
+import threading
import hashlib
-import gevent
+
import fbchat
+from fbchat.models import *
# ---- MESSAGE TYPES ----
@@ -35,21 +38,96 @@ CACHE_GET = "cache_get"
REP_OK = "rep_ok"
REP_ERROR = "rep_error"
+# Event types
+EVENT_JOIN = "join"
+EVENT_LEAVE = "leave"
+EVENT_MESSAGE = "message"
+EVENT_ACTION = "action"
+
# ---- MESSENGER CLIENT CLASS THAT HANDLES EVENTS ----
+def getUserId(user):
+ if user.url is not None and not "?" in user.url:
+ return user.url.split("/")[-1]
+ else:
+ return user.uid
+
+def mediaObjectOfURL(url):
+ return {
+ "filename": url.split("?")[0].split("/")[-1],
+ "url": url,
+ }
+
+
class MessengerBridgeClient(fbchat.Client):
def __init__(self, bridge, *args, **kwargs):
self.bridge = bridge
super(MessengerBridgeClient, self).__init__(*args, **kwargs)
+class InitialSyncThread(threading.Thread):
+ def __init__(self, client, bridge, *args, **kwargs):
+ super(InitialSyncThread, self).__init__(*args, **kwargs)
+
+ self.client = client
+ self.bridge = bridge
+
+ def run(self):
+ threads = self.client.fetchThreadList()
+ sys.stderr.write("fb thread list: {}\n".format(threads))
+ for thread in threads:
+ sys.stderr.write("fb thread: {}\n".format(thread))
+ if thread.type != ThreadType.GROUP:
+ continue
+ self.bridge.write({
+ "_type": JOINED,
+ "room": thread.uid,
+ })
+
+ room_info = {
+ "name": thread.name,
+ }
+ if thread.photo is not None:
+ room_info["picture"] = mediaObjectOfURL(thread.photo)
+ self.bridge.write({
+ "_type": ROOM_INFO_UPDATED,
+ "room": thread.uid,
+ "data": room_info,
+ })
+
+ members = self.client.fetchAllUsersFromThreads([thread])
+ for member in members:
+ sys.stderr.write("fb thread member: {}\n".format(member))
+ self.bridge.write({
+ "_type": EVENT,
+ "data": {
+ "type": EVENT_JOIN,
+ "author": getUserId(member),
+ "room": thread.uid,
+ }
+ })
+
+ user_info = {
+ "display_name": member.name,
+ }
+ if member.photo is not None:
+ user_info["avatar"] = mediaObjectOfURL(member.photo)
+ self.bridge.write({
+ "_type": USER_INFO_UPDATED,
+ "user": getUserId(member),
+ "data": user_info,
+ })
+
# TODO: handle events
# ---- MAIN LOOP THAT HANDLES REQUESTS FROM BRIDGE ----
class MessengerBridge:
+ def __init__(self):
+ pass
+
def run(self):
self.client = None
self.keep_running = True
@@ -104,10 +182,15 @@ class MessengerBridge:
except:
pass
+ InitialSyncThread(self.client, self).start()
+
elif ty == CLOSE:
self.client.logout()
self.keep_running = False
+ elif ty == GET_USER:
+ return {"_type": REP_OK, "user": self.client.uid}
+
else:
return {"_type": REP_ERROR, "error": "Not implemented"}