diff options
author | Alex Auvolat <alex@adnab.me> | 2020-03-01 15:48:58 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-03-01 15:48:58 +0100 |
commit | 1d8b7ef84a3487d626fc05194ce869290b7dd30e (patch) | |
tree | b0e4d9af1c5e8111a407c5f84cb5432beb877a24 | |
parent | f251d67c32e001a01c19faac800762ba146c2339 (diff) | |
download | easybridge-1d8b7ef84a3487d626fc05194ce869290b7dd30e.tar.gz easybridge-1d8b7ef84a3487d626fc05194ce869290b7dd30e.zip |
Refactor some && officially integrate Messenger backend :)
-rw-r--r-- | connector/external/config.go | 6 | ||||
-rwxr-xr-x | external/messenger.py | 97 | ||||
-rw-r--r-- | templates/home.html | 7 |
3 files changed, 60 insertions, 50 deletions
diff --git a/connector/external/config.go b/connector/external/config.go index caa2b7e..bf2a5f8 100644 --- a/connector/external/config.go +++ b/connector/external/config.go @@ -45,6 +45,12 @@ func init() { IsPassword: true, Required: true, }, + &ConfigEntry{ + Name: "initial_backlog", + Description: "Maximum number of messages to load when joining a channel", + IsNumeric: true, + Default: "100", + }, }, }) } 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: diff --git a/templates/home.html b/templates/home.html index 1975297..a33b8a0 100644 --- a/templates/home.html +++ b/templates/home.html @@ -34,8 +34,9 @@ <h5 class="mt-4">Add account</h5> -<a class="btn btn-sm btn-dark" href="/add/IRC">IRC</a> -<a class="btn btn-sm btn-warning ml-4" href="/add/XMPP">XMPP</a> -<a class="btn btn-sm btn-info ml-4" href="/add/Mattermost">Mattermost</a> +<a class="btn btn-sm btn-primary mr-4" href="/add/Mattermost">Mattermost</a> +<a class="btn btn-sm btn-warning mr-4" href="/add/XMPP">XMPP</a> +<a class="btn btn-sm btn-dark mr-4" href="/add/IRC">IRC</a> +<a class="btn btn-sm btn-primary mr-4" href="/add/Messenger">Facebook Messenger</a> {{end}} |