From d9adfe1550dc02faee2a1e74bc40bd0465a58195 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Tue, 5 Apr 2022 00:54:21 -0400 Subject: [PATCH] Send fallback warning message for unsupported inbound message types --- matrix_appservice_kakaotalk/portal.py | 70 ++++++++++++++++----------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/matrix_appservice_kakaotalk/portal.py b/matrix_appservice_kakaotalk/portal.py index 6f035d7..311922b 100644 --- a/matrix_appservice_kakaotalk/portal.py +++ b/matrix_appservice_kakaotalk/portal.py @@ -20,7 +20,6 @@ from typing import ( Any, AsyncGenerator, Awaitable, - Callable, Pattern, cast, ) @@ -67,9 +66,8 @@ from .formatter import kakaotalk_to_matrix, matrix_to_kakaotalk from .kt.types.bson import Long from .kt.types.channel.channel_info import ChannelInfo from .kt.types.channel.channel_type import KnownChannelType, ChannelType -from .kt.types.chat import Chatlog, KnownChatType +from .kt.types.chat import Chatlog, ChatType, KnownChatType from .kt.types.chat.attachment import ( - Attachment, AudioAttachment, #FileAttachment, MediaAttachment, @@ -190,20 +188,7 @@ class Portal(DBPortal, BasePortal): NotificationDisabler.config_enabled = cls.config["bridge.backfill.disable_notifications"] # TODO More - cls._message_type_handler_map: dict[ - KnownChatType, - Callable[ - [ - Portal, - u.User, - IntentAPI, - Attachment | None, - int, - str | None - ], - Awaitable[list[EventID]] - ] - ] = { + cls._message_type_handler_map = { KnownChatType.TEXT: cls._handle_remote_text, KnownChatType.PHOTO: cls._handle_remote_photo, KnownChatType.MULTIPHOTO: cls._handle_remote_multiphoto, @@ -1009,19 +994,18 @@ class Portal(DBPortal, BasePortal): await intent.ensure_joined(self.mxid) self._backfill_leave.add(intent) - handler = self._message_type_handler_map.get(message.type) - if not handler: - self.log.warning(f"No handler for message type {message.type}, falling back to text") - handler = Portal._handle_remote_text + handler = self._message_type_handler_map.get(message.type, Portal._handle_remote_unsupported) event_ids = [ event_id for event_id in await handler( self, - source, - intent, - message.attachment, - message.sendAt, - message.text) + source=source, + intent=intent, + attachment=message.attachment, + timestamp=message.sendAt, + message_text=message.text, + message_type=message.type, + ) if event_id ] if not event_ids: @@ -1039,13 +1023,35 @@ class Portal(DBPortal, BasePortal): ) await self._send_delivery_receipt(event_ids[-1]) - async def _handle_remote_text( + async def _handle_remote_unsupported( self, - source: u.User, intent: IntentAPI, - attachment: None, timestamp: int, message_text: str | None, + message_type: ChatType, + **_ + ) -> Awaitable[list[EventID]]: + try: + type_str = KnownChatType(message_type).name.lower() + except ValueError: + type_str = str(message_type) + self.log.warning("No handler for message type \"%s\" (%s)", + type_str, + f"text = {message_text}" if message_text is not None else "no text", + ) + content = TextMessageEventContent( + msgtype=MessageType.NOTICE, + body=f"\u26a0 Unbridgeable message ({type_str})", + ) + # TODO Replies + return [await self._send_message(intent, content, timestamp=timestamp)] + + async def _handle_remote_text( + self, + intent: IntentAPI, + timestamp: int, + message_text: str, + **_ ) -> list[EventID]: # TODO Handle mentions properly content = await kakaotalk_to_matrix(message_text) @@ -1059,6 +1065,7 @@ class Portal(DBPortal, BasePortal): attachment: PhotoAttachment, timestamp: int, message_text: str | None, + **_ ) -> Awaitable[list[EventID]]: return asyncio.gather(self._handle_remote_uniphoto( source, intent, attachment, timestamp, message_text @@ -1071,6 +1078,7 @@ class Portal(DBPortal, BasePortal): attachment: MultiPhotoAttachment, timestamp: int, message_text: str | None, + **_ ) -> Awaitable[list[EventID]]: # TODO Upload media concurrently, but post messages sequentially return [ @@ -1103,6 +1111,7 @@ class Portal(DBPortal, BasePortal): attachment: PhotoAttachment, timestamp: int, message_text: str | None, + **_ ) -> Awaitable[EventID]: return self._handle_remote_media( source, intent, attachment, timestamp, message_text, @@ -1122,6 +1131,7 @@ class Portal(DBPortal, BasePortal): attachment: VideoAttachment, timestamp: int, message_text: str | None, + **_ ) -> Awaitable[list[EventID]]: return asyncio.gather(self._handle_remote_media( source, intent, attachment, timestamp, message_text, @@ -1140,6 +1150,7 @@ class Portal(DBPortal, BasePortal): attachment: AudioAttachment, timestamp: int, message_text: str | None, + **_ ) -> Awaitable[list[EventID]]: return asyncio.gather(self._handle_remote_media( source, intent, attachment, timestamp, message_text, @@ -1177,6 +1188,7 @@ class Portal(DBPortal, BasePortal): message_text: str | None, info: MediaInfo, msgtype: MessageType, + **_ ) -> EventID: mxc, additional_info, decryption_info = await self._reupload_remote_file( attachment.url,