diff --git a/mautrix_facebook/formatter/from_facebook.py b/mautrix_facebook/formatter/from_facebook.py index 2a9eaae2afebcdf1fd6f1ca09118cd0c7888d7ef..bae7b7628129c017243cc0ea8501d9c61dbe21b0 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 0c59fa940c5b84bc8fd935c124daec062b68d9e6..1cdcde02de39aae552edbdd074c34056e728f9d8 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