Fix replies to relayed users' messages
This commit is contained in:
parent
770b0e447b
commit
0a0812515c
|
@ -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,11 +90,14 @@ 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:
|
||||
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
|
||||
|
||||
|
@ -103,7 +106,7 @@ 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):
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue