aboutsummaryrefslogtreecommitdiff
path: root/external
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-03-01 13:27:29 +0100
committerAlex Auvolat <alex@adnab.me>2020-03-01 13:27:29 +0100
commit018f4a751ac4bff9113874666a92b4c5d8679af3 (patch)
tree41670a7c39287344562d2992146cd9c3d6bc94f8 /external
parent810e75a34dddd88279bea1cd2ea38816fe872d52 (diff)
downloadeasybridge-018f4a751ac4bff9113874666a92b4c5d8679af3.tar.gz
easybridge-018f4a751ac4bff9113874666a92b4c5d8679af3.zip
Read backlog; handle messages in the correct order
Diffstat (limited to 'external')
-rwxr-xr-xexternal/messenger.py36
1 files changed, 35 insertions, 1 deletions
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()