From 7b96687daecefbfaef00f2cdcc3fa8d3d5a29838 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sun, 10 Apr 2022 02:25:36 -0400 Subject: [PATCH] Inbound read receipts But not while backfilling --- .../kt/client/client.py | 15 ++++++++------ matrix_appservice_kakaotalk/portal.py | 6 ++---- matrix_appservice_kakaotalk/user.py | 20 +++++++++++++++++++ node/src/client.js | 11 ++++++---- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/matrix_appservice_kakaotalk/kt/client/client.py b/matrix_appservice_kakaotalk/kt/client/client.py index 387f434..6c4e268 100644 --- a/matrix_appservice_kakaotalk/kt/client/client.py +++ b/matrix_appservice_kakaotalk/kt/client/client.py @@ -403,16 +403,19 @@ class Client: str(data["channelType"]), ) + async def _on_chat_read(self, data: dict[str, JSON]) -> None: + await self.user.on_chat_read( + Long.deserialize(data["chatId"]), + Long.deserialize(data["senderId"]), + Long.deserialize(data["channelId"]), + str(data["channelType"]), + ) + async def _on_profile_changed(self, data: dict[str, JSON]) -> None: await self.user.on_profile_changed( OpenLinkChannelUserInfo.deserialize(data["info"]), ) - """ TODO - async def _on_receipt(self, data: Dict[str, JSON]) -> None: - await self.user.on_receipt(Receipt.deserialize(data["receipt"])) - """ - async def _on_listen_disconnect(self, data: dict[str, JSON]) -> None: try: res = KickoutRes.deserialize(data) @@ -436,8 +439,8 @@ class Client: def _start_listen(self) -> None: self._add_event_handler("chat", self._on_chat) self._add_event_handler("chat_deleted", self._on_chat_deleted) + self._add_event_handler("chat_read", self._on_chat_read) self._add_event_handler("profile_changed", self._on_profile_changed) - # TODO many more listeners self._add_event_handler("disconnected", self._on_listen_disconnect) self._add_event_handler("switch_server", self._on_switch_server) self._add_event_handler("error", self._on_error) diff --git a/matrix_appservice_kakaotalk/portal.py b/matrix_appservice_kakaotalk/portal.py index 577d5c3..dbfd14a 100644 --- a/matrix_appservice_kakaotalk/portal.py +++ b/matrix_appservice_kakaotalk/portal.py @@ -527,8 +527,7 @@ class Portal(DBPortal, BasePortal): if did_join and self.is_direct: await source.update_direct_chats({self.main_intent.mxid: [self.mxid]}) - # TODO - #await self._sync_read_receipts(info.read_receipts.nodes) + # TODO Sync read receipts? """ async def _sync_read_receipts(self, receipts: list[None]) -> None: @@ -702,8 +701,7 @@ class Portal(DBPortal, BasePortal): except Exception: self.log.exception("Failed to backfill new portal") - # TODO - #await self._sync_read_receipts(info.read_receipts.nodes) + # TODO Sync read receipts? return self.mxid diff --git a/matrix_appservice_kakaotalk/user.py b/matrix_appservice_kakaotalk/user.py index 34e96a1..de35358 100644 --- a/matrix_appservice_kakaotalk/user.py +++ b/matrix_appservice_kakaotalk/user.py @@ -52,6 +52,7 @@ from .kt.types.packet.chat.kickout import KnownKickoutType, KickoutRes METRIC_CONNECT_AND_SYNC = Summary("bridge_connect_and_sync", "calls to connect_and_sync") METRIC_CHAT = Summary("bridge_on_chat", "calls to on_chat") METRIC_CHAT_DELETED = Summary("bridge_on_chat_deleted", "calls to on_chat_deleted") +METRIC_CHAT_READ = Summary("bridge_on_chat_read", "calls to on_chat_read") METRIC_PROFILE_CHANGE = Summary("bridge_on_profile_changed", "calls to on_profile_changed") METRIC_LOGGED_IN = Gauge("bridge_logged_in", "Users logged into the bridge") METRIC_CONNECTED = Gauge("bridge_connected", "Bridge users connected to KakaoTalk") @@ -703,6 +704,25 @@ class User(DBUser, BaseUser): puppet = await pu.Puppet.get_by_ktid(sender_id) await portal.handle_kakaotalk_chat_delete(puppet, chat_id, timestamp) + @async_time(METRIC_CHAT_READ) + async def on_chat_read( + self, + chat_id: Long, + sender_id: Long, + channel_id: Long, + channel_type: ChannelType, + ) -> None: + puppet = await pu.Puppet.get_by_ktid(sender_id) + portal = await po.Portal.get_by_ktid( + channel_id, + kt_receiver=self.ktid, + kt_type=channel_type, + create=False, + ) + 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) + @async_time(METRIC_PROFILE_CHANGE) async def on_profile_changed(self, info: OpenLinkChannelUserInfo) -> None: puppet = await pu.Puppet.get_by_ktid(info.userId) diff --git a/node/src/client.js b/node/src/client.js index 894346b..a74d8ca 100644 --- a/node/src/client.js +++ b/node/src/client.js @@ -114,12 +114,15 @@ class UserClient { }) }) - /* TODO Many more listeners this.#talkClient.on("chat_read", (chat, channel, reader) => { - this.log(`chat_read in channel ${channel.channelId}`) - //chat.logId + this.log(`${chat.logId} read in channel ${channel.channelId} by ${reader.userId}`) + return this.write("chat_read", { + chatId: chat.logId, + senderId: reader.userId, + channelId: channel.channelId, + channelType: channel.info.type, + }) }) - */ this.#talkClient.on("profile_changed", (channel, lastInfo, user) => { this.log(`Profile of ${user.userId} changed (channel: ${channel})`)