From b59b8a68c3cf2f7beae0bd96fcc7e043d6bbc1be Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sun, 20 Mar 2022 03:12:17 -0400 Subject: [PATCH] Sync receiver of DM channel at init time for existing portal --- matrix_appservice_kakaotalk/kt/client/client.py | 9 ++++++++- matrix_appservice_kakaotalk/portal.py | 13 +++++++++++-- node/src/client.js | 12 ++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/matrix_appservice_kakaotalk/kt/client/client.py b/matrix_appservice_kakaotalk/kt/client/client.py index b231993..5a40ecd 100644 --- a/matrix_appservice_kakaotalk/kt/client/client.py +++ b/matrix_appservice_kakaotalk/kt/client/client.py @@ -49,7 +49,7 @@ from ..types.request import ( CommandResultDoneValue ) -from .types import PortalChannelInfo +from .types import PortalChannelInfo, UserInfoUnion from .errors import InvalidAccessToken from .error_helper import raise_unsuccessful_response @@ -214,6 +214,13 @@ class Client: channel_id=channel_id.serialize() ) + async def get_participants(self, channel_id: Long) -> list[UserInfoUnion]: + return await self._api_user_request_result( + ResultListType(UserInfoUnion), + "get_participants", + channel_id=channel_id.serialize() + ) + async def get_chats(self, channel_id: Long, sync_from: Long | None, limit: int | None) -> list[Chatlog]: return await self._api_user_request_result( ResultListType(Chatlog), diff --git a/matrix_appservice_kakaotalk/portal.py b/matrix_appservice_kakaotalk/portal.py index 6e7fbf9..bbb928a 100644 --- a/matrix_appservice_kakaotalk/portal.py +++ b/matrix_appservice_kakaotalk/portal.py @@ -397,7 +397,10 @@ class Portal(DBPortal, BasePortal): ) """ - async def _update_participants(self, source: u.User, participants: list[UserInfoUnion]) -> bool: + async def _update_participants(self, source: u.User, participants: list[UserInfoUnion] | None = None) -> bool: + if participants is None: + self.log.debug("Called _update_participants with no participants, fetching them now...") + participants = await source.client.get_participants(self.ktid) changed = False if not self._main_intent: assert self.is_direct, "_main_intent for non-direct chat portal should have been set already" @@ -982,8 +985,14 @@ class Portal(DBPortal, BasePortal): self.by_mxid[self.mxid] = self if not self.is_direct: self._main_intent = self.az.intent + elif self.mxid: + # TODO Save kt_sender in DB instead? Depends on if DM channels are shared... + user = await u.User.get_by_ktid(self.kt_receiver) + assert user, f"Found no user for this portal's receiver of {self.kt_receiver}" + # TODO Should this backfill? Useful for forgotten channels + await self._update_participants(user) else: - self.log.debug("Not setting _main_intent of direct chat until after checking participant list") + self.log.debug("Not setting _main_intent of new direct chat until after checking participant list") @classmethod @async_getter_lock diff --git a/node/src/client.js b/node/src/client.js index f05d8de..7b04ae7 100644 --- a/node/src/client.js +++ b/node/src/client.js @@ -351,6 +351,17 @@ export default class PeerClient { }) } + /** + * @param {Object} req + * @param {string} req.mxid + * @param {Long} req.channel_id + */ + getParticipants = async (req) => { + const userClient = this.#getUser(req.mxid) + const talkChannel = userClient.getChannel(req.channel_id) + return await talkChannel.getAllLatestUserInfo() + } + /** * @param {Object} req * @param {string} req.mxid @@ -458,6 +469,7 @@ export default class PeerClient { get_own_profile: this.getOwnProfile, get_profile: this.getProfile, get_portal_channel_info: this.getPortalChannelInfo, + get_participants: this.getParticipants, get_chats: this.getChats, send_message: this.sendMessage, }[req.command] || this.handleUnknownCommand