Refactor user info fetching logic

This commit is contained in:
Alex 2020-03-01 14:30:51 +01:00
parent 6055544313
commit 75a1dee061

98
external/messenger.py vendored
View file

@ -79,16 +79,17 @@ class MessengerBridgeClient(fbchat.Client):
# ---- SEPARATE THREADS FOR INITIAL SYNC & CLIENT LISTEN ---- # ---- SEPARATE THREADS FOR INITIAL SYNC & CLIENT LISTEN ----
class InitialSyncThread(threading.Thread): class InitialSyncThread(threading.Thread):
def __init__(self, client, bridge, *args, **kwargs): def __init__(self, client, bridge, threads, *args, **kwargs):
super(InitialSyncThread, self).__init__(*args, **kwargs) super(InitialSyncThread, self).__init__(*args, **kwargs)
self.client = client self.client = client
self.bridge = bridge self.bridge = bridge
self.threads = threads
def run(self): def run(self):
threads = self.client.fetchThreadList()
sys.stderr.write("fb thread list: {}\n".format(threads)) sys.stderr.write("fb thread list: {}\n".format(threads))
for thread in threads:
for thread in self.threads:
sys.stderr.write("fb thread: {}\n".format(thread)) sys.stderr.write("fb thread: {}\n".format(thread))
if thread.type == ThreadType.GROUP: if thread.type == ThreadType.GROUP:
members = self.client.fetchAllUsersFromThreads([thread]) members = self.client.fetchAllUsersFromThreads([thread])
@ -100,8 +101,6 @@ class InitialSyncThread(threading.Thread):
self.send_room_info(thread, members) self.send_room_info(thread, members)
self.send_room_members(thread, members) self.send_room_members(thread, members)
elif thread.type == ThreadType.USER:
self.bridge.getUserId(thread)
self.backlog_room(thread) self.backlog_room(thread)
@ -134,25 +133,7 @@ class InitialSyncThread(threading.Thread):
def send_room_members(self, thread, members): def send_room_members(self, thread, members):
for member in members: for member in members:
sys.stderr.write("fb thread member: {}\n".format(member)) sys.stderr.write("fb thread member: {}\n".format(member))
self.bridge.write({ self.bridge.ensureJoined(self.bridge.getUserId(member), thread.uid)
"_type": EVENT,
"data": {
"type": EVENT_JOIN,
"author": self.bridge.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": self.bridge.getUserId(member),
"data": user_info,
})
def backlog_room(self, thread): def backlog_room(self, thread):
prev_last_seen = self.bridge.cache_get("last_seen_%s"%thread.uid) prev_last_seen = self.bridge.cache_get("last_seen_%s"%thread.uid)
@ -194,16 +175,32 @@ class MessengerBridge:
def __init__(self): def __init__(self):
self.rev_uid = {} self.rev_uid = {}
self.uid_map = {} self.uid_map = {}
self.joined_map = {}
def getUserId(self, user): def getUserId(self, user):
retval = None
if user.url is not None and not "?" in user.url: if user.url is not None and not "?" in user.url:
user_id = user.url.split("/")[-1] user_id = user.url.split("/")[-1]
self.rev_uid[user_id] = user.uid self.rev_uid[user_id] = user.uid
self.uid_map[user.uid] = user_id retval = user_id
else: else:
self.uid_map[user.uid] = user.uid retval = user.uid
return self.uid_map[user.uid] if user.uid not in self.uid_map:
self.uid_map[user.uid] = retval
user_info = {
"display_name": user.name,
}
if user.photo is not None:
user_info["avatar"] = mediaObjectOfURL(user.photo)
self.bridge.write({
"_type": USER_INFO_UPDATED,
"user": self.bridge.getUserId(user),
"data": user_info,
})
return retval
def getUserIdFromUid(self, uid): def getUserIdFromUid(self, uid):
if uid in self.uid_map: if uid in self.uid_map:
@ -218,7 +215,6 @@ class MessengerBridge:
else: else:
return user_id return user_id
def getUserShortName(self, user): def getUserShortName(self, user):
if user.first_name != None: if user.first_name != None:
return user.first_name return user.first_name
@ -281,17 +277,27 @@ class MessengerBridge:
email, password = cmd["data"]["email"], cmd["data"]["password"] email, password = cmd["data"]["email"], cmd["data"]["password"]
self.client = MessengerBridgeClient(email=email, password=password, max_tries=1) self.client = MessengerBridgeClient(email=email, password=password, max_tries=1)
if self.client.isLoggedIn(): if not self.client.isLoggedIn():
try: return {"_type": "ret_error", "error": "Unable to login (?)"}
f = open(client_file, "wb")
pickle.dump(self.client, f)
f.close()
except:
pass
self.client.setBridge(self) try:
InitialSyncThread(self.client, self).start() f = open(client_file, "wb")
ClientListenThread(self.client).start() pickle.dump(self.client, f)
f.close()
except:
pass
self.client.setBridge(self)
threads = self.client.fetchThreadList()
# ensure we have a correct mapping for bridged user IDs to fb uids
# (this should be fast)
for thread in threads:
if thread.type == ThreadType.GROUP:
self.getUserId(thread)
InitialSyncThread(self.client, self, threads).start()
ClientListenThread(self.client).start()
elif ty == CLOSE: elif ty == CLOSE:
self.close() self.close()
@ -344,6 +350,19 @@ class MessengerBridge:
def cache_put(self, key, value): def cache_put(self, key, value):
self.write({"_type": CACHE_PUT, "key": key, "value": value}) self.write({"_type": CACHE_PUT, "key": key, "value": value})
def ensureJoined(self, userId, room):
key = "{}--{}".format(userId, room)
if not key in self.joined_map:
self.bridge.write({
"_type": EVENT,
"data": {
"type": EVENT_JOIN,
"author": userId,
"room": room,
}
})
self.joined_map[key] = true
def onMessage(self, thread_id, thread_type, message_object, **kwargs): def onMessage(self, thread_id, thread_type, message_object, **kwargs):
if message_object.author == self.client.uid: if message_object.author == self.client.uid:
# Ignore our own messages # Ignore our own messages
@ -358,6 +377,8 @@ class MessengerBridge:
} }
if thread_type == ThreadType.GROUP: if thread_type == ThreadType.GROUP:
event["room"] = thread_id event["room"] = thread_id
self.ensureJoined(author, thread_id)
self.write({"_type": EVENT, "data": event}) self.write({"_type": EVENT, "data": event})
self.cache_put("last_seen_%s"%thread_id, message_object.uid) self.cache_put("last_seen_%s"%thread_id, message_object.uid)
@ -369,7 +390,6 @@ class MessengerBridge:
def onTitleChange(self, *args, **kwargs): def onTitleChange(self, *args, **kwargs):
pass pass
if __name__ == "__main__": if __name__ == "__main__":
bridge = MessengerBridge() bridge = MessengerBridge()
bridge.run() bridge.run()