From 018f4a751ac4bff9113874666a92b4c5d8679af3 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 1 Mar 2020 13:27:29 +0100 Subject: Read backlog; handle messages in the correct order --- external/messenger.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'external') diff --git a/external/messenger.py b/external/messenger.py index 6ec6e9c..792a356 100755 --- a/external/messenger.py +++ b/external/messenger.py @@ -141,7 +141,25 @@ class InitialSyncThread(threading.Thread): }) def backlog_room(self, thread): - pass # TODO + 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) + for m in reversed(messages): + self.bridge.handleMessage(thread, m) @@ -150,11 +168,13 @@ class InitialSyncThread(threading.Thread): class MessengerBridge: def __init__(self): self.rev_uid = {} + self.uid_map = {} def getUserId(self, user): if user.url is not None and not "?" in user.url: user_id = user.url.split("/")[-1] self.rev_uid[user_id] = user.uid + self.uid_map[user.uid] = user_id return user_id else: return user.uid @@ -274,6 +294,20 @@ class MessengerBridge: del self.cache_gets[num] return rep + def handleMessage(self, thread, m): + author = m.author + if author in self.uid_map: + author = self.uid_map[m.author] + + event = { + "type": EVENT_MESSAGE, + "author": author, + "text": m.text, + } + if thread.type == ThreadType.GROUP: + event["room"] = thread.uid + self.write({"_type": EVENT, "data": event}) + if __name__ == "__main__": bridge = MessengerBridge() -- cgit v1.2.3