From db3337834557068b6a54cda6a42765dc66c16839 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sat, 23 Apr 2022 17:01:39 -0400 Subject: [PATCH] Support inbound channel adding, and log remaining events Also use more consistent grammar for event log messages --- .../kt/client/client.py | 5 +++ matrix_appservice_kakaotalk/user.py | 17 ++++++- node/src/client.js | 44 ++++++++++++++++--- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/matrix_appservice_kakaotalk/kt/client/client.py b/matrix_appservice_kakaotalk/kt/client/client.py index 21853ba..fb07e36 100644 --- a/matrix_appservice_kakaotalk/kt/client/client.py +++ b/matrix_appservice_kakaotalk/kt/client/client.py @@ -515,6 +515,11 @@ class Client: str(data["channelType"]), ) + def _on_channel_added(self, data: dict[str, JSON]) -> Awaitable[None]: + return self.user.on_channel_added( + ChannelInfo.deserialize(data["channelInfo"]), + ) + def _on_channel_join(self, data: dict[str, JSON]) -> Awaitable[None]: return self.user.on_channel_join( ChannelInfo.deserialize(data["channelInfo"]), diff --git a/matrix_appservice_kakaotalk/user.py b/matrix_appservice_kakaotalk/user.py index 0dadfca..8862fbb 100644 --- a/matrix_appservice_kakaotalk/user.py +++ b/matrix_appservice_kakaotalk/user.py @@ -58,6 +58,7 @@ METRIC_CHAT_READ = Summary("bridge_on_chat_read", "calls to on_chat_read") METRIC_CHANNEL_META_CHANGE = Summary("bridge_on_channel_meta_change", "calls to on_channel_meta_change") METRIC_PROFILE_CHANGE = Summary("bridge_on_profile_changed", "calls to on_profile_changed") METRIC_PERM_CHANGE = Summary("bridge_on_perm_changed", "calls to on_perm_changed") +METRIC_CHANNEL_ADDED = Summary("bridge_on_channel_added", "calls to on_channel_added") METRIC_CHANNEL_JOIN = Summary("bridge_on_channel_join", "calls to on_channel_join") METRIC_CHANNEL_LEFT = Summary("bridge_on_channel_left", "calls to on_channel_left") METRIC_CHANNEL_KICKED = Summary("bridge_on_channel_kicked", "calls to on_channel_kicked") @@ -790,10 +791,24 @@ class User(DBUser, BaseUser): user_power_levels = await po.Portal.get_mapped_ktid_power_levels(user_id, power_level) await portal.set_power_levels(user_power_levels) + @async_time(METRIC_CHANNEL_ADDED) + def on_channel_added(self, channel_info: ChannelInfo) -> Awaitable[None]: + return self._sync_channel(channel_info) + @async_time(METRIC_CHANNEL_JOIN) async def on_channel_join(self, channel_info: ChannelInfo) -> None: assert self.ktid - await self._sync_channel(channel_info) + portal = await po.Portal.get_by_ktid( + channel_info.channelId, + kt_receiver=self.ktid, + kt_type=channel_info.type, + ) + if portal.mxid: + user = await pu.Puppet.get_by_ktid(self.ktid) + await portal.backfill_lock.wait("channel join") + await portal.handle_kakaotalk_user_join(self, user) + else: + await self._sync_channel(channel_info) @async_time(METRIC_CHANNEL_LEFT) async def on_channel_left(self, channel_id: Long, channel_type: ChannelType | None) -> None: diff --git a/node/src/client.js b/node/src/client.js index 253ce19..426416c 100644 --- a/node/src/client.js +++ b/node/src/client.js @@ -118,7 +118,7 @@ class UserClient { this.peerClient = peerClient this.#talkClient.on("chat", (data, channel) => { - this.log(`${data.chat.logId} received in channel ${channel.channelId}`) + this.log(`Chat ${data.chat.logId} received in channel ${channel.channelId}`) this.write("chat", { //is_sequential: true, // TODO Make sequential per user & channel (if it isn't already) chatlog: data.chat, @@ -128,7 +128,7 @@ class UserClient { }) this.#talkClient.on("chat_deleted", (feedChatlog, channel, feed) => { - this.log(`${feed.logId} deleted in channel ${channel.channelId} by user ${feedChatlog.sender.userId}`) + this.log(`Chat ${feed.logId} deleted in channel ${channel.channelId} by user ${feedChatlog.sender.userId}`) this.write("chat_deleted", { chatId: feed.logId, senderId: feedChatlog.sender.userId, @@ -150,7 +150,7 @@ class UserClient { }) this.#talkClient.on("chat_read", (chat, channel, reader) => { - this.log(`${chat.logId} read in channel ${channel.channelId} by ${reader.userId}`) + this.log(`Chat ${chat.logId} read in channel ${channel.channelId} by ${reader.userId}`) this.write("chat_read", { chatId: chat.logId, senderId: reader.userId, @@ -159,8 +159,13 @@ class UserClient { }) }) + this.#talkClient.on("chat_event", (channel, author, type, count, chat) => { + // TODO Figure out if this is can ever be for anything other than hearts on Shouts + this.log(`Event ${type} (count = ${count}) on chat ${chat.logId} in channel ${channel.channelId} sent by user ${author.userId}`) + }) + this.#talkClient.on("profile_changed", (channel, lastInfo, user) => { - this.log(`Profile of ${user.userId} changed (channel: ${channel ? channel.channelId : "None"})`) + this.log(`Profile of ${user.userId} changed (in channel ${channel ? channel.channelId : "None"})`) this.write("profile_changed", { info: user, /* TODO Is this ever a per-channel profile change? @@ -182,6 +187,18 @@ class UserClient { }) }) + this.#talkClient.on("host_handover", (channel, lastLink, link) => { + // TODO Find how or if this relates to permissions + this.log(`Host of channel ${channel.channelId} changed from ${lastLink.linkOwner.nickname} to ${link.linkOwner.nickname}`) + }) + + this.#talkClient.on("channel_added", channel => { + this.log(`Added channel ${channel.channelId}`) + this.write("channel_added", { + channelInfo: channel.info, + }) + }) + this.#talkClient.on("channel_join", channel => { this.log(`Joined channel ${channel.channelId}`) this.write("channel_join", { @@ -198,7 +215,8 @@ class UserClient { }) this.#talkClient.on("channel_kicked", (kickedLog, channel, feed) => { - this.log(`User ${feed.member.userId} kicked from channel ${channel.channelId} by user ${kickedLog.sender.userId}`) + // TODO Confirm whether this can refer to any user that was kicked, or only to the current user + this.log(`Kicked from channel ${channel.channelId}`) this.write("channel_kicked", { userId: feed.member.userId, senderId: kickedLog.sender.userId, @@ -207,6 +225,21 @@ class UserClient { }) }) + this.#talkClient.on("channel_link_deleted", (feedChatLog, channel, feed) => { + // TODO Figure out what this means + this.log(`Channel link deleted in channel ${channel.channelId}: feed=${JSON.stringify(feed)}, feedChatLog=${JSON.stringify(feedChatLog)}`) + }) + + this.#talkClient.on("link_created", link => { + // TODO Figure out what this means + this.log(`Link created: ${JSON.stringify(link)}`) + }) + + this.#talkClient.on("link_deleted", link => { + // TODO Figure out what this means + this.log(`Link deleted: ${JSON.stringify(link)}`) + }) + this.#talkClient.on("user_join", (joinLog, channel, user, feed) => { this.log(`User ${user.userId} joined channel ${channel.channelId}`) this.write("user_join", { @@ -226,6 +259,7 @@ class UserClient { }) this.#talkClient.on("meta_change", (channel, type, newMeta) => { + // TODO Handle announcements as pinned messages this.log(`Channel ${channel.channelId} metadata changed`) })