From 383ea05432d8a566a14084fdf67f7e9588a5d471 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Mon, 11 Apr 2022 04:26:21 -0400 Subject: [PATCH] Add read receipt handler that I forgot about --- matrix_appservice_kakaotalk/db/message.py | 8 ++++---- matrix_appservice_kakaotalk/portal.py | 14 ++++++++++++++ matrix_appservice_kakaotalk/user.py | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/matrix_appservice_kakaotalk/db/message.py b/matrix_appservice_kakaotalk/db/message.py index ace4ed1..1010565 100644 --- a/matrix_appservice_kakaotalk/db/message.py +++ b/matrix_appservice_kakaotalk/db/message.py @@ -84,15 +84,15 @@ class Message: @classmethod async def get_closest_before( - cls, kt_chat: int, kt_receiver: int, timestamp: int + cls, kt_chat: int, kt_receiver: int, ktid: Long ) -> Message | None: q = ( f"SELECT {cls.columns} " - "FROM message WHERE kt_chat=$1 AND kt_receiver=$2 AND timestamp<=$3 AND " + "FROM message WHERE kt_chat=$1 AND kt_receiver=$2 AND ktid<=$3 AND " " ktid IS NOT NULL " - "ORDER BY timestamp DESC LIMIT 1" + "ORDER BY ktid DESC LIMIT 1" ) - row = await cls.db.fetchrow(q, kt_chat, kt_receiver, timestamp) + row = await cls.db.fetchrow(q, kt_chat, kt_receiver, ktid) return cls._from_optional_row(row) _insert_query = ( diff --git a/matrix_appservice_kakaotalk/portal.py b/matrix_appservice_kakaotalk/portal.py index b68744d..ee2cb0a 100644 --- a/matrix_appservice_kakaotalk/portal.py +++ b/matrix_appservice_kakaotalk/portal.py @@ -1277,6 +1277,20 @@ class Portal(DBPortal, BasePortal): await self.main_intent.redact(message.mx_room, message.mxid, timestamp=timestamp) await message.delete() + async def handle_kakaotalk_chat_read(self, source: u.User, sender: p.Puppet, chat_id: Long) -> None: + if not self.mxid: + return + msg = await DBMessage.get_closest_before(self.ktid, self.kt_receiver, chat_id) + if not msg: + return + if not await self._bridge_own_message_pm(source, sender, "read receipt", invite=False): + return + # NOTE No need for timestamp when the read receipt happened, since this is only for live ones + await sender.intent_for(self).mark_read(msg.mx_room, msg.mxid) + self.log.debug( + f"Handled KakaoTalk read receipt from {sender.ktid} up to {chat_id}/{msg.mxid}" + ) + async def handle_kakaotalk_user_join( self, source: u.User, user: p.Puppet ) -> None: diff --git a/matrix_appservice_kakaotalk/user.py b/matrix_appservice_kakaotalk/user.py index 2dfb4dc..cf53342 100644 --- a/matrix_appservice_kakaotalk/user.py +++ b/matrix_appservice_kakaotalk/user.py @@ -736,7 +736,7 @@ class User(DBUser, BaseUser): ) if portal and portal.mxid: await portal.backfill_lock.wait(f"read receipt from {sender_id}") - await portal.handle_kakaotalk_read(self, puppet, chat_id) + await portal.handle_kakaotalk_chat_read(self, puppet, chat_id) @async_time(METRIC_PROFILE_CHANGE) async def on_profile_changed(self, info: OpenLinkChannelUserInfo) -> None: