matrix-appservice-kakaotalk/matrix_appservice_kakaotalk/formatter/from_matrix.py

83 lines
3.2 KiB
Python
Raw Normal View History

2022-02-25 02:22:50 -05:00
# matrix-appservice-kakaotalk - A Matrix-KakaoTalk puppeting bridge.
# Copyright (C) 2022 Tulir Asokan, Andrew Ferrazzutti
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
from typing import NamedTuple
2022-04-05 15:44:02 -04:00
from mautrix.appservice import IntentAPI
from mautrix.types import MessageEventContent, RelationType, RoomID
2022-02-25 02:22:50 -05:00
from mautrix.util.logging import TraceLogger
2022-04-05 15:44:02 -04:00
from ..kt.types.bson import Long
from ..kt.types.chat.attachment.reply import ReplyAttachment
from ..kt.client.types import TO_MSGTYPE_MAP
from .. import puppet as pu
2022-02-25 02:22:50 -05:00
from ..db import Message as DBMessage
class SendParams(NamedTuple):
text: str
2022-04-05 15:44:02 -04:00
# TODO Mentions
reply_to: ReplyAttachment
2022-02-25 02:22:50 -05:00
async def matrix_to_kakaotalk(
2022-04-05 15:44:02 -04:00
content: MessageEventContent, room_id: RoomID, log: TraceLogger, intent: IntentAPI
2022-02-25 02:22:50 -05:00
) -> SendParams:
2022-04-05 15:44:02 -04:00
# NOTE By design, this *throws* if user intent can't be matched (i.e. if a reply can't be created)
# TODO Mentions
2022-02-25 02:22:50 -05:00
if content.relates_to.rel_type == RelationType.REPLY:
message = await DBMessage.get_by_mxid(content.relates_to.event_id, room_id)
2022-04-05 15:44:02 -04:00
if not message:
raise ValueError(
2022-02-25 02:22:50 -05:00
f"Couldn't find reply target {content.relates_to.event_id}"
2022-04-05 15:44:02 -04:00
" to bridge text message reply metadata to KakaoTalk"
)
try:
mx_event = await intent.get_event(room_id, message.mxid)
except:
log.exception(f"Failed to find Matrix event for reply target {message.mxid}")
raise
kt_sender = pu.Puppet.get_id_from_mxid(mx_event.sender)
if kt_sender is None:
raise ValueError(
f"Found no KakaoTalk user ID for reply target sender {mx_event.sender}"
2022-02-25 02:22:50 -05:00
)
2022-04-05 15:44:02 -04:00
content.trim_reply_fallback()
reply_to = ReplyAttachment(
# TODO
#mentions=[],
# TODO What are reply URLs for?
#urls=[],
# TODO Set this for emoticon reply, but must first support those
attach_only=False,
# TODO If replying with media works, must set type AND all attachment properties
# But then, the reply object must be an intersection of a ReplyAttachment and something else
#attach_type=TO_MSGTYPE_MAP.get(content.msgtype),
src_logId=message.ktid,
# TODO
src_mentions=[],
# TODO Check if source message needs to be formatted
src_message=mx_event.content.body,
src_type=TO_MSGTYPE_MAP[mx_event.content.msgtype],
src_userId=Long(kt_sender),
)
2022-02-25 02:22:50 -05:00
else:
2022-04-05 15:44:02 -04:00
reply_to = None
return SendParams(text=content.body, reply_to=reply_to)