From e8773a5c9782851dce6f82ddb438ecd46c01a684 Mon Sep 17 00:00:00 2001 From: Tulir Asokan <tulir@maunium.net> Date: Sun, 5 Apr 2020 02:10:38 +0300 Subject: [PATCH] Combine ShareAttachment links with main message text --- mautrix_facebook/formatter/from_facebook.py | 10 +++++- mautrix_facebook/portal.py | 35 +++++---------------- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/mautrix_facebook/formatter/from_facebook.py b/mautrix_facebook/formatter/from_facebook.py index 2a9eaae..bae7b76 100644 --- a/mautrix_facebook/formatter/from_facebook.py +++ b/mautrix_facebook/formatter/from_facebook.py @@ -17,7 +17,7 @@ from typing import Tuple, List, Optional, Match from html import escape import re -from fbchat import Message +from fbchat import Message, ShareAttachment from mautrix.types import TextMessageEventContent, Format, MessageType @@ -159,6 +159,14 @@ def facebook_to_matrix(message: Message) -> TextMessageEventContent: if i != len(lines) - 1: output.append("<br/>") _handle_codeblock_post(output, *post_args) + links = [attachment for attachment in message.attachments + if isinstance(attachment, ShareAttachment)] + message.attachments = [attachment for attachment in message.attachments + if not isinstance(attachment, ShareAttachment)] + for attachment in links: + if attachment.original_url.rstrip("/") not in message.text: + output.append(f"<br/><a href='{attachment.original_url}'>{attachment.title}</a>") + content.body += f"\n{attachment.title}: {attachment.original_url}" html = "".join(output) html = MENTION_REGEX.sub(_mention_replacer, html) diff --git a/mautrix_facebook/portal.py b/mautrix_facebook/portal.py index 0c59fa9..1cdcde0 100644 --- a/mautrix_facebook/portal.py +++ b/mautrix_facebook/portal.py @@ -540,18 +540,14 @@ class Portal(BasePortal): if message.sticker: event_ids = [await self._handle_facebook_sticker(intent, message.sticker, message.reply_to_id)] + elif len(message.attachments) > 0: + attach_ids = await asyncio.gather( + *[self._handle_facebook_attachment(intent, attachment, message.reply_to_id) + for attachment in message.attachments]) + event_ids += [attach_id for attach_id in attach_ids if attach_id] + if not event_ids and message.text: + event_ids = [await self._handle_facebook_text(intent, message)] else: - if message.text: - event_ids = [await self._handle_facebook_text(intent, message)] - - if len(message.attachments) > 0: - attach_ids = await asyncio.gather( - *[self._handle_facebook_attachment(intent, attachment, message.reply_to_id, - message.text) - for attachment in message.attachments]) - event_ids.extend([attach_id for attach_id in attach_ids if attach_id]) - - if not event_ids: self.log.warning(f"Unhandled Messenger message: {message}") DBMessage.bulk_create(fbid=message.uid, fb_receiver=self.fb_receiver, mx_room=self.mxid, event_ids=[event_id for event_id in event_ids if event_id]) @@ -599,7 +595,7 @@ class Portal(BasePortal): relates_to=self._get_facebook_reply(reply_to))) async def _handle_facebook_attachment(self, intent: IntentAPI, attachment: AttachmentClass, - reply_to: str, message_text: str) -> Optional[EventID]: + reply_to: str) -> Optional[EventID]: if isinstance(attachment, AudioAttachment): mxc, mime, size, decryption_info = await self._reupload_fb_file( attachment.url, intent, attachment.filename, encrypt=self.encrypted) @@ -630,21 +626,6 @@ class Portal(BasePortal): content = await self._convert_facebook_location(intent, attachment) content.relates_to = self._get_facebook_reply(reply_to) event_id = await self._send_message(intent, content) - elif isinstance(attachment, ShareAttachment): - # remove trailing slash for url searching - url = attachment.original_url - if url[-1] == "/": - url = url[0:-1] - - # Prevent sending urls that are already in the original message text - if url not in message_text: - content = TextMessageEventContent( - format=Format.HTML, msgtype=MessageType.TEXT, - body=f"{attachment.title}: {attachment.original_url}", - formatted_body=f"<a href='{attachment.original_url}'>{attachment.title}</a>") - event_id = await self._send_message(intent, content) - else: - return None else: self.log.warning(f"Unsupported attachment type: {attachment}") return None -- GitLab