diff --git a/mautrix_facebook/portal.py b/mautrix_facebook/portal.py index 24139344686a393c5784bd3519c88aa95898c214..0c59fa940c5b84bc8fd935c124daec062b68d9e6 100644 --- a/mautrix_facebook/portal.py +++ b/mautrix_facebook/portal.py @@ -540,14 +540,18 @@ 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: - event_ids = await asyncio.gather( - *[self._handle_facebook_attachment(intent, attachment, message.reply_to_id) - for attachment in message.attachments]) - event_ids = [event_id for event_id in event_ids if event_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]) @@ -595,7 +599,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) -> Optional[EventID]: + reply_to: str, message_text: 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) @@ -626,6 +630,21 @@ 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