From aa5c066552924995dbee02d77f9343904a408721 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Tue, 12 Apr 2022 04:30:56 -0400 Subject: [PATCH] Prepend relayed media messages with a new message of who sent it And keep its body text unchanged from its original --- .../example-config.yaml | 8 ++--- matrix_appservice_kakaotalk/portal.py | 34 ++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/matrix_appservice_kakaotalk/example-config.yaml b/matrix_appservice_kakaotalk/example-config.yaml index f0332d8..d4008ee 100644 --- a/matrix_appservice_kakaotalk/example-config.yaml +++ b/matrix_appservice_kakaotalk/example-config.yaml @@ -246,10 +246,10 @@ bridge: m.text: '$sender_displayname: $message' m.notice: '$sender_displayname: $message' m.emote: '* $sender_displayname $message' - m.file: '$sender_displayname sent a file' - m.image: '$sender_displayname sent an image' - m.audio: '$sender_displayname sent an audio file' - m.video: '$sender_displayname sent a video' + m.file: 'File from $sender_displayname: $message' + m.image: 'Image from $sender_displayname: $message' + m.audio: 'Audio from $sender_displayname: $message' + m.video: 'Video from $sender_displayname: $message' m.location: '$sender_displayname sent a location' rpc: diff --git a/matrix_appservice_kakaotalk/portal.py b/matrix_appservice_kakaotalk/portal.py index a19f438..8464396 100644 --- a/matrix_appservice_kakaotalk/portal.py +++ b/matrix_appservice_kakaotalk/portal.py @@ -791,7 +791,12 @@ class Portal(DBPortal, BasePortal): elif not sender.is_connected: raise Exception("not connected to KakaoTalk chats") elif is_relay: - await self.apply_relay_message_format(orig_sender, message) + if not message.msgtype.is_text: + intro_message = TextMessageEventContent(msgtype=MessageType.TEXT, body=message.body) + await self.apply_relay_message_format(orig_sender, intro_message) + await self._send_chat(sender, intro_message) + else: + await self.apply_relay_message_format(orig_sender, message) if message.msgtype == MessageType.TEXT or message.msgtype == MessageType.NOTICE: await self._handle_matrix_text(event_id, sender, message) elif message.msgtype.is_media: @@ -801,6 +806,21 @@ class Portal(DBPortal, BasePortal): else: raise NotImplementedError(f"Unsupported message type {message.msgtype}") + async def _send_chat( + self, sender: u.User, message: TextMessageEventContent, event_id: EventID | None = None + ) -> Chatlog: + converted = await matrix_to_kakaotalk(message, self.mxid, self.log, self.main_intent) + try: + return await sender.client.send_chat( + self.channel_props, + text=converted.text, + reply_to=converted.reply_to, + mentions=converted.mentions, + ) + except CommandException as e: + self.log.debug(f"Error handling Matrix message {event_id if event_id else ''}: {e!s}") + raise + async def _make_dbm(self, event_id: EventID, ktid: Long | None = None) -> DBMessage: dbm = DBMessage( mxid=event_id, @@ -817,17 +837,7 @@ class Portal(DBPortal, BasePortal): async def _handle_matrix_text( self, event_id: EventID, sender: u.User, message: TextMessageEventContent ) -> None: - converted = await matrix_to_kakaotalk(message, self.mxid, self.log, self.main_intent) - try: - chatlog = await sender.client.send_chat( - self.channel_props, - text=converted.text, - reply_to=converted.reply_to, - mentions=converted.mentions, - ) - except CommandException as e: - self.log.debug(f"Error handling Matrix message {event_id}: {e!s}") - raise + chatlog = await self._send_chat(sender, message, event_id) await self._make_dbm(event_id, chatlog.logId) self.log.debug(f"Handled Matrix message {event_id} -> {chatlog.logId}") sender.send_remote_checkpoint(