Handle attachments, sometimes

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

53
external/messenger.py vendored
View file

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