Send fallback warning message for unsupported inbound message types

This commit is contained in:
Andrew Ferrazzutti 2022-04-05 00:54:21 -04:00
parent cc5f3f13c3
commit d9adfe1550
1 changed files with 41 additions and 29 deletions

View File

@ -20,7 +20,6 @@ from typing import (
Any, Any,
AsyncGenerator, AsyncGenerator,
Awaitable, Awaitable,
Callable,
Pattern, Pattern,
cast, cast,
) )
@ -67,9 +66,8 @@ from .formatter import kakaotalk_to_matrix, matrix_to_kakaotalk
from .kt.types.bson import Long from .kt.types.bson import Long
from .kt.types.channel.channel_info import ChannelInfo from .kt.types.channel.channel_info import ChannelInfo
from .kt.types.channel.channel_type import KnownChannelType, ChannelType 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 ( from .kt.types.chat.attachment import (
Attachment,
AudioAttachment, AudioAttachment,
#FileAttachment, #FileAttachment,
MediaAttachment, MediaAttachment,
@ -190,20 +188,7 @@ class Portal(DBPortal, BasePortal):
NotificationDisabler.config_enabled = cls.config["bridge.backfill.disable_notifications"] NotificationDisabler.config_enabled = cls.config["bridge.backfill.disable_notifications"]
# TODO More # TODO More
cls._message_type_handler_map: dict[ cls._message_type_handler_map = {
KnownChatType,
Callable[
[
Portal,
u.User,
IntentAPI,
Attachment | None,
int,
str | None
],
Awaitable[list[EventID]]
]
] = {
KnownChatType.TEXT: cls._handle_remote_text, KnownChatType.TEXT: cls._handle_remote_text,
KnownChatType.PHOTO: cls._handle_remote_photo, KnownChatType.PHOTO: cls._handle_remote_photo,
KnownChatType.MULTIPHOTO: cls._handle_remote_multiphoto, KnownChatType.MULTIPHOTO: cls._handle_remote_multiphoto,
@ -1009,19 +994,18 @@ class Portal(DBPortal, BasePortal):
await intent.ensure_joined(self.mxid) await intent.ensure_joined(self.mxid)
self._backfill_leave.add(intent) self._backfill_leave.add(intent)
handler = self._message_type_handler_map.get(message.type) handler = self._message_type_handler_map.get(message.type, Portal._handle_remote_unsupported)
if not handler:
self.log.warning(f"No handler for message type {message.type}, falling back to text")
handler = Portal._handle_remote_text
event_ids = [ event_ids = [
event_id for event_id in event_id for event_id in
await handler( await handler(
self, self,
source, source=source,
intent, intent=intent,
message.attachment, attachment=message.attachment,
message.sendAt, timestamp=message.sendAt,
message.text) message_text=message.text,
message_type=message.type,
)
if event_id if event_id
] ]
if not event_ids: if not event_ids:
@ -1039,13 +1023,35 @@ class Portal(DBPortal, BasePortal):
) )
await self._send_delivery_receipt(event_ids[-1]) await self._send_delivery_receipt(event_ids[-1])
async def _handle_remote_text( async def _handle_remote_unsupported(
self, self,
source: u.User,
intent: IntentAPI, intent: IntentAPI,
attachment: None,
timestamp: int, timestamp: int,
message_text: str | None, 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]: ) -> list[EventID]:
# TODO Handle mentions properly # TODO Handle mentions properly
content = await kakaotalk_to_matrix(message_text) content = await kakaotalk_to_matrix(message_text)
@ -1059,6 +1065,7 @@ class Portal(DBPortal, BasePortal):
attachment: PhotoAttachment, attachment: PhotoAttachment,
timestamp: int, timestamp: int,
message_text: str | None, message_text: str | None,
**_
) -> Awaitable[list[EventID]]: ) -> Awaitable[list[EventID]]:
return asyncio.gather(self._handle_remote_uniphoto( return asyncio.gather(self._handle_remote_uniphoto(
source, intent, attachment, timestamp, message_text source, intent, attachment, timestamp, message_text
@ -1071,6 +1078,7 @@ class Portal(DBPortal, BasePortal):
attachment: MultiPhotoAttachment, attachment: MultiPhotoAttachment,
timestamp: int, timestamp: int,
message_text: str | None, message_text: str | None,
**_
) -> Awaitable[list[EventID]]: ) -> Awaitable[list[EventID]]:
# TODO Upload media concurrently, but post messages sequentially # TODO Upload media concurrently, but post messages sequentially
return [ return [
@ -1103,6 +1111,7 @@ class Portal(DBPortal, BasePortal):
attachment: PhotoAttachment, attachment: PhotoAttachment,
timestamp: int, timestamp: int,
message_text: str | None, message_text: str | None,
**_
) -> Awaitable[EventID]: ) -> Awaitable[EventID]:
return self._handle_remote_media( return self._handle_remote_media(
source, intent, attachment, timestamp, message_text, source, intent, attachment, timestamp, message_text,
@ -1122,6 +1131,7 @@ class Portal(DBPortal, BasePortal):
attachment: VideoAttachment, attachment: VideoAttachment,
timestamp: int, timestamp: int,
message_text: str | None, message_text: str | None,
**_
) -> Awaitable[list[EventID]]: ) -> Awaitable[list[EventID]]:
return asyncio.gather(self._handle_remote_media( return asyncio.gather(self._handle_remote_media(
source, intent, attachment, timestamp, message_text, source, intent, attachment, timestamp, message_text,
@ -1140,6 +1150,7 @@ class Portal(DBPortal, BasePortal):
attachment: AudioAttachment, attachment: AudioAttachment,
timestamp: int, timestamp: int,
message_text: str | None, message_text: str | None,
**_
) -> Awaitable[list[EventID]]: ) -> Awaitable[list[EventID]]:
return asyncio.gather(self._handle_remote_media( return asyncio.gather(self._handle_remote_media(
source, intent, attachment, timestamp, message_text, source, intent, attachment, timestamp, message_text,
@ -1177,6 +1188,7 @@ class Portal(DBPortal, BasePortal):
message_text: str | None, message_text: str | None,
info: MediaInfo, info: MediaInfo,
msgtype: MessageType, msgtype: MessageType,
**_
) -> EventID: ) -> EventID:
mxc, additional_info, decryption_info = await self._reupload_remote_file( mxc, additional_info, decryption_info = await self._reupload_remote_file(
attachment.url, attachment.url,