Fix replies to relayed users' messages

This commit is contained in:
Andrew Ferrazzutti 2022-04-14 04:24:23 -04:00
parent 770b0e447b
commit 0a0812515c
2 changed files with 17 additions and 14 deletions

View File

@ -35,7 +35,7 @@ from ..kt.types.chat.attachment import ReplyAttachment, MentionStruct
from ..kt.client.types import TO_MSGTYPE_MAP 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 from ..db import Message as DBMessage
@ -90,20 +90,23 @@ class ToKakaoTalkParser(MatrixParser[KakaoTalkFormatString]):
fs = KakaoTalkFormatString fs = KakaoTalkFormatString
async def _get_id_from_mxid(mxid: UserID) -> Long | None: async def _get_id_from_mxid(mxid: UserID, portal: po.Portal) -> Long | None:
user = await u.User.get_by_mxid(mxid, create=False) orig_sender = await u.User.get_by_mxid(mxid, create=False)
if user and user.ktid: if orig_sender and orig_sender.ktid:
return user.ktid return orig_sender.ktid
else: elif orig_sender:
puppet = await pu.Puppet.get_by_mxid(mxid, create=False) sender, _ = await portal.get_relay_sender(orig_sender, "relation")
return puppet.ktid if puppet else None 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( async def matrix_to_kakaotalk(
content: MessageEventContent, content: MessageEventContent,
room_id: RoomID, room_id: RoomID,
log: TraceLogger, log: TraceLogger,
intent: IntentAPI, portal: po.Portal,
skip_reply: bool = False skip_reply: bool = False
) -> SendParams: ) -> SendParams:
# NOTE By design, this *throws* if user intent can't be matched (i.e. if a reply can't be created) # 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" " to bridge text message reply metadata to KakaoTalk"
) )
try: try:
src_event = await intent.get_event(room_id, message.mxid) src_event = await portal.main_intent.get_event(room_id, message.mxid)
except: except:
log.exception(f"Failed to find Matrix event for reply target {message.mxid}") log.exception(f"Failed to find Matrix event for reply target {message.mxid}")
raise 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: if src_kt_sender is None:
raise ValueError( raise ValueError(
f"Found no KakaoTalk user ID for reply target sender {src_event.sender}" f"Found no KakaoTalk user ID for reply target sender {src_event.sender}"
) )
content.trim_reply_fallback() 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: if src_event.content.relates_to.rel_type == RelationType.REPLY:
src_type = KnownChatType.REPLY src_type = KnownChatType.REPLY
src_message = src_converted.text src_message = src_converted.text
@ -158,7 +161,7 @@ async def matrix_to_kakaotalk(
mentions_by_user: dict[Long, MentionStruct] = {} mentions_by_user: dict[Long, MentionStruct] = {}
# Make sure to not create remote mentions for any remote user not in the room # Make sure to not create remote mentions for any remote user not in the room
if parsed.entities: 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 last_offset = 0
at = 0 at = 0
for mention in sorted(parsed.entities, key=lambda entity: entity.offset): for mention in sorted(parsed.entities, key=lambda entity: entity.offset):

View File

@ -927,7 +927,7 @@ class Portal(DBPortal, BasePortal):
async def _send_chat( async def _send_chat(
self, sender: u.User, message: TextMessageEventContent, event_id: EventID | None = None self, sender: u.User, message: TextMessageEventContent, event_id: EventID | None = None
) -> Long: ) -> 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: try:
return await sender.client.send_chat( return await sender.client.send_chat(
self.channel_props, self.channel_props,