From 1d8b7ef84a3487d626fc05194ce869290b7dd30e Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 1 Mar 2020 15:48:58 +0100 Subject: Refactor some && officially integrate Messenger backend :) --- external/messenger.py | 97 ++++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 47 deletions(-) (limited to 'external') diff --git a/external/messenger.py b/external/messenger.py index f857b46..cb48410 100755 --- a/external/messenger.py +++ b/external/messenger.py @@ -107,60 +107,31 @@ class InitialSyncThread(threading.Thread): "room": thread.uid, }) - self.send_room_info(thread, members) - self.send_room_members(thread, members) + self.bridge.send_room_info(thread, members) + self.bridge.send_room_members(thread, members) self.backlog_room(thread) - def send_room_info(self, thread, members): - room_info = {} - if thread.name is not None: - room_info["name"] = thread.name - else: - who = [m for m in members if m.uid != self.client.uid] - if len(who) > 3: - room_info["name"] = ", ".join([self.bridge.getUserShortName(m) for m in who[:3]] + ["..."]) - else: - room_info["name"] = ", ".join([self.bridge.getUserShortName(m) for m in who]) - - if thread.photo is not None: - room_info["picture"] = mediaObjectOfURL(thread.photo) - else: - for m in members: - if m.uid != self.client.uid and m.photo is not None: - room_info["picture"] = mediaObjectOfURL(m.photo) - break - - self.bridge.write({ - "_type": ROOM_INFO_UPDATED, - "room": thread.uid, - "data": room_info, - }) - - def send_room_members(self, thread, members): - for member in members: - sys.stderr.write("fb thread member: {}\n".format(member)) - self.bridge.ensureJoined(self.bridge.getUserId(member), thread.uid) - def backlog_room(self, thread): prev_last_seen = self.bridge.cache_get("last_seen_%s"%thread.uid) if prev_last_seen == "": - messages = self.client.fetchThreadMessages(thread.uid, limit=100) - else: - messages = [] - found = False - while not found: - before = None - if len(messages) > 0: - before = messages[-1].timestamp - page = self.client.fetchThreadMessages(thread.uid, before=before, limit=20) - for m in page: - if m.uid == prev_last_seen: - found = True - break - else: - messages.append(m) + prev_last_seen = None + + messages = [] + found = False + while not found: + before = None + if len(messages) > 0: + before = messages[-1].timestamp + page = self.client.fetchThreadMessages(thread.uid, before=before, limit=20) + for m in page: + if m.uid == prev_last_seen or len(messages) > self.bridge.init_backlog_length: + found = True + break + else: + messages.append(m) + for m in reversed(messages): if m.text is None: m.text = "" @@ -189,6 +160,7 @@ class MessengerBridge: self.rev_uid = {} self.uid_map = {} self.joined_map = {} + self.init_backlog_length = 100 def getUserId(self, user): retval = None @@ -276,6 +248,7 @@ class MessengerBridge: def handle_cmd(self, cmd): ty = cmd["_type"] if ty == CONFIGURE: + self.init_backlog_length = int(cmd["data"]["initial_backlog"]) client_file = "/tmp/fbclient_" + hashlib.sha224(cmd["data"]["email"].encode("utf-8")).hexdigest() try: @@ -378,6 +351,36 @@ class MessengerBridge: def cache_put(self, key, value): self.write({"_type": CACHE_PUT, "key": key, "value": value}) + def send_room_info(self, thread, members): + room_info = {} + if thread.name is not None: + room_info["name"] = thread.name + else: + who = [m for m in members if m.uid != self.client.uid] + if len(who) > 3: + room_info["name"] = ", ".join([self.getUserShortName(m) for m in who[:3]] + ["..."]) + else: + room_info["name"] = ", ".join([self.getUserShortName(m) for m in who]) + + if thread.photo is not None: + room_info["picture"] = mediaObjectOfURL(thread.photo) + else: + for m in members: + if m.uid != self.client.uid and m.photo is not None: + room_info["picture"] = mediaObjectOfURL(m.photo) + break + + self.write({ + "_type": ROOM_INFO_UPDATED, + "room": thread.uid, + "data": room_info, + }) + + def send_room_members(self, thread, members): + for member in members: + sys.stderr.write("fb thread member: {}\n".format(member)) + self.ensureJoined(self.getUserId(member), thread.uid) + def ensureJoined(self, userId, room): key = "{}--{}".format(userId, room) if not key in self.joined_map: -- cgit v1.2.3