Refactor some && officially integrate Messenger backend :)

This commit is contained in:
Alex 2020-03-01 15:48:58 +01:00
parent f251d67c32
commit 1d8b7ef84a
3 changed files with 60 additions and 50 deletions

View file

@ -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",
},
},
})
}

97
external/messenger.py vendored
View file

@ -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:

View file

@ -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}}