From 0a0812515cc9cd91f5fe99d1a1493b6989088f0a Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Thu, 14 Apr 2022 04:24:23 -0400 Subject: [PATCH] Fix replies to relayed users' messages --- .../formatter/from_matrix.py | 29 ++++++++++--------- matrix_appservice_kakaotalk/portal.py | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/matrix_appservice_kakaotalk/formatter/from_matrix.py b/matrix_appservice_kakaotalk/formatter/from_matrix.py index 465c58d..abe2058 100644 --- a/matrix_appservice_kakaotalk/formatter/from_matrix.py +++ b/matrix_appservice_kakaotalk/formatter/from_matrix.py @@ -35,7 +35,7 @@ from ..kt.types.chat.attachment import ReplyAttachment, MentionStruct from ..kt.client.types import TO_MSGTYPE_MAP -from .. import puppet as pu, user as u +from .. import portal as po, puppet as pu, user as u from ..db import Message as DBMessage @@ -90,20 +90,23 @@ class ToKakaoTalkParser(MatrixParser[KakaoTalkFormatString]): fs = KakaoTalkFormatString -async def _get_id_from_mxid(mxid: UserID) -> Long | None: - user = await u.User.get_by_mxid(mxid, create=False) - if user and user.ktid: - return user.ktid - else: - puppet = await pu.Puppet.get_by_mxid(mxid, create=False) - return puppet.ktid if puppet else None +async def _get_id_from_mxid(mxid: UserID, portal: po.Portal) -> Long | None: + orig_sender = await u.User.get_by_mxid(mxid, create=False) + if orig_sender and orig_sender.ktid: + return orig_sender.ktid + elif orig_sender: + sender, _ = await portal.get_relay_sender(orig_sender, "relation") + if sender and sender.ktid: + return sender.ktid + puppet = await pu.Puppet.get_by_mxid(mxid, create=False) + return puppet.ktid if puppet else None async def matrix_to_kakaotalk( content: MessageEventContent, room_id: RoomID, log: TraceLogger, - intent: IntentAPI, + portal: po.Portal, skip_reply: bool = False ) -> SendParams: # NOTE By design, this *throws* if user intent can't be matched (i.e. if a reply can't be created) @@ -115,17 +118,17 @@ async def matrix_to_kakaotalk( " to bridge text message reply metadata to KakaoTalk" ) try: - src_event = await intent.get_event(room_id, message.mxid) + src_event = await portal.main_intent.get_event(room_id, message.mxid) except: log.exception(f"Failed to find Matrix event for reply target {message.mxid}") raise - src_kt_sender = await _get_id_from_mxid(src_event.sender) + src_kt_sender = await _get_id_from_mxid(src_event.sender, portal) if src_kt_sender is None: raise ValueError( f"Found no KakaoTalk user ID for reply target sender {src_event.sender}" ) content.trim_reply_fallback() - src_converted = await matrix_to_kakaotalk(src_event.content, room_id, log, intent, skip_reply=True) + src_converted = await matrix_to_kakaotalk(src_event.content, room_id, log, portal, skip_reply=True) if src_event.content.relates_to.rel_type == RelationType.REPLY: src_type = KnownChatType.REPLY src_message = src_converted.text @@ -158,7 +161,7 @@ async def matrix_to_kakaotalk( mentions_by_user: dict[Long, MentionStruct] = {} # Make sure to not create remote mentions for any remote user not in the room if parsed.entities: - joined_members = set(await intent.get_room_members(room_id)) + joined_members = set(await portal.main_intent.get_room_members(room_id)) last_offset = 0 at = 0 for mention in sorted(parsed.entities, key=lambda entity: entity.offset): diff --git a/matrix_appservice_kakaotalk/portal.py b/matrix_appservice_kakaotalk/portal.py index 2b60641..fcafc62 100644 --- a/matrix_appservice_kakaotalk/portal.py +++ b/matrix_appservice_kakaotalk/portal.py @@ -927,7 +927,7 @@ class Portal(DBPortal, BasePortal): async def _send_chat( self, sender: u.User, message: TextMessageEventContent, event_id: EventID | None = None ) -> Long: - converted = await matrix_to_kakaotalk(message, self.mxid, self.log, self.main_intent) + converted = await matrix_to_kakaotalk(message, self.mxid, self.log, self) try: return await sender.client.send_chat( self.channel_props,