Handle attachments, sometimes

This commit is contained in:
Alex 2020-03-01 15:19:09 +01:00
parent 2eda975741
commit b453406299

49
external/messenger.py vendored
View file

@ -7,6 +7,8 @@ import threading
import queue import queue
import pickle import pickle
import time import time
import traceback
from urllib.parse import unquote as UrlUnquote
import hashlib import hashlib
@ -55,6 +57,12 @@ def mediaObjectOfURL(url):
"url": url, "url": url,
} }
def stripFbLinkPrefix(url):
PREFIX = "https://l.facebook.com/l.php?u="
if url[:len(PREFIX)] == PREFIX:
return UrlUnquote(url[len(PREFIX):].split('&')[0])
else:
return url
# ---- MESSENGER CLIENT CLASS THAT HANDLES EVENTS ---- # ---- MESSENGER CLIENT CLASS THAT HANDLES EVENTS ----
@ -180,14 +188,13 @@ class MessengerBridge:
def getUserId(self, user): def getUserId(self, user):
retval = None retval = None
if user.url is not None and not "?" in user.url: if user.url is not None and not "?" in user.url:
user_id = user.url.split("/")[-1] retval = user.url.split("/")[-1]
self.rev_uid[user_id] = user.uid
retval = user_id
else: else:
retval = user.uid retval = user.uid
if user.uid not in self.uid_map: if user.uid not in self.uid_map:
self.uid_map[user.uid] = retval self.uid_map[user.uid] = retval
self.rev_uid[retval] = user.uid
user_info = { user_info = {
"display_name": user.name, "display_name": user.name,
@ -246,6 +253,7 @@ class MessengerBridge:
if "_type" not in rep: if "_type" not in rep:
rep["_type"] = REP_OK rep["_type"] = REP_OK
except Exception as e: except Exception as e:
sys.stderr.write("{}\n".format(traceback.format_exc()))
rep = { rep = {
"_type": REP_ERROR, "_type": REP_ERROR,
"error": "{}".format(e) "error": "{}".format(e)
@ -293,7 +301,7 @@ class MessengerBridge:
# ensure we have a correct mapping for bridged user IDs to fb uids # ensure we have a correct mapping for bridged user IDs to fb uids
# (this should be fast) # (this should be fast)
for thread in threads: for thread in threads:
if thread.type == ThreadType.GROUP: if thread.type == ThreadType.USER:
self.getUserId(thread) self.getUserId(thread)
InitialSyncThread(self.client, self, threads).start() InitialSyncThread(self.client, self, threads).start()
@ -312,15 +320,30 @@ class MessengerBridge:
elif ty == SEND: elif ty == SEND:
event = cmd["data"] event = cmd["data"]
if event["type"] in [EVENT_MESSAGE, EVENT_ACTION]: if event["type"] in [EVENT_MESSAGE, EVENT_ACTION]:
# TODO: attachments attachments = []
if "attachments" in event and isinstance(event["attachments"], list):
for at in event["attachments"]:
if "url" in at:
attachments.append(at["url"])
else:
# TODO
sys.stdout.write("Unhandled: attachment without URL")
msg = Message(event["text"]) msg = Message(event["text"])
if event["type"] == EVENT_ACTION: if event["type"] == EVENT_ACTION:
msg.text = "* " + event["text"] msg.text = "* " + event["text"]
if event["room"] != "": if event["room"] != "":
if len(attachments) > 0:
msg_id = self.client.sendRemoteFiles(attachments, message=msg, thread_id=event["room"], thread_type=ThreadType.GROUP)
else:
msg_id = self.client.send(msg, thread_id=event["room"], thread_type=ThreadType.GROUP) msg_id = self.client.send(msg, thread_id=event["room"], thread_type=ThreadType.GROUP)
elif event["recipient"] != "": elif event["recipient"] != "":
uid = self.revUserId(event["recipient"]) uid = self.revUserId(event["recipient"])
sys.stderr.write("Sending to {}\n".format(uid))
if len(attachments) > 0:
msg_id = self.client.sendRemoteFiles(attachments, message=msg, thread_id=uid, thread_type=ThreadType.USER)
else:
msg_id = self.client.send(msg, thread_id=uid, thread_type=ThreadType.USER) msg_id = self.client.send(msg, thread_id=uid, thread_type=ThreadType.USER)
else: else:
return {"_type": REP_ERROR, "error": "Invalid message"} return {"_type": REP_ERROR, "error": "Invalid message"}
@ -378,6 +401,9 @@ class MessengerBridge:
"text": message_object.text, "text": message_object.text,
"attachments": [] "attachments": []
} }
if event["text"] is None:
event["text"] = ""
for at in message_object.attachments: for at in message_object.attachments:
if isinstance(at, ImageAttachment): if isinstance(at, ImageAttachment):
full_url = self.client.fetchImageUrl(at.uid) full_url = self.client.fetchImageUrl(at.uid)
@ -389,10 +415,17 @@ class MessengerBridge:
"height": at.height, "height": at.height,
}, },
}) })
elif isinstance(at, FileAttachment) or isinstance(at, AudioAttachment): elif isinstance(at, FileAttachment):
url = stripFbLinkPrefix(at.url)
event["attachments"].append({ event["attachments"].append({
"filename": at.url.split("?")[0].split("/")[-1], "filename": at.name,
"url": at.url, "url": url,
})
elif isinstance(at, AudioAttachment):
url = stripFbLinkPrefix(at.url)
event["attachments"].append({
"filename": at.filename,
"url": url,
}) })
else: else:
event["text"] += "\nUnhandled attachment: {}".format(at) event["text"] += "\nUnhandled attachment: {}".format(at)