From 421bba7f1f883b0a27298998c8ac961e4c03fe37 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sat, 23 Apr 2022 16:48:23 -0400 Subject: [PATCH] Style updates & minor fixes --- .../formatter/from_matrix.py | 1 - .../kt/client/client.py | 76 +++++++++---------- matrix_appservice_kakaotalk/user.py | 31 ++++++-- node/src/client.js | 8 +- 4 files changed, 64 insertions(+), 52 deletions(-) diff --git a/matrix_appservice_kakaotalk/formatter/from_matrix.py b/matrix_appservice_kakaotalk/formatter/from_matrix.py index abe2058..3588cab 100644 --- a/matrix_appservice_kakaotalk/formatter/from_matrix.py +++ b/matrix_appservice_kakaotalk/formatter/from_matrix.py @@ -17,7 +17,6 @@ from __future__ import annotations from typing import NamedTuple -from mautrix.appservice import IntentAPI from mautrix.types import Format, MessageEventContent, RelationType, RoomID, UserID from mautrix.util import utf16_surrogate from mautrix.util.formatter import ( diff --git a/matrix_appservice_kakaotalk/kt/client/client.py b/matrix_appservice_kakaotalk/kt/client/client.py index 0d42ae4..21853ba 100644 --- a/matrix_appservice_kakaotalk/kt/client/client.py +++ b/matrix_appservice_kakaotalk/kt/client/client.py @@ -125,18 +125,18 @@ class Client: ) @classmethod - async def register_device(cls, passcode: str, **req: JSON) -> None: + def register_device(cls, passcode: str, **req: JSON) -> Awaitable[None]: """Register a (fake) device that will be associated with the provided login credentials.""" - await cls._api_request_void("register_device", passcode=passcode, **req) + return cls._api_request_void("register_device", passcode=passcode, **req) @classmethod - async def login(cls, **req: JSON) -> OAuthCredential: + async def login(cls, **req: JSON) -> Awaitable[OAuthCredential]: """ Obtain a session token by logging in with user-provided credentials. Must have first called register_device with these credentials. """ # NOTE Actually returns an auth LoginData, but this only needs an OAuthCredential - return await cls._api_request_result(OAuthCredential, "login", **req) + return cls._api_request_result(OAuthCredential, "login", **req) # endregion @@ -254,8 +254,8 @@ class Client: await self._rpc_client.request("disconnect", mxid=self.user.mxid) await self._on_disconnect(None) - async def get_settings(self) -> SettingsStruct: - return await self._api_user_request_result(SettingsStruct, "get_settings") + def get_settings(self) -> Awaitable[SettingsStruct]: + return self._api_user_request_result(SettingsStruct, "get_settings") async def get_own_profile(self) -> ProfileStruct: profile_req_struct = await self._api_user_request_result(ProfileReqStruct, "get_own_profile") @@ -269,22 +269,22 @@ class Client: ) return profile_req_struct.profile - async def get_portal_channel_info(self, channel_props: ChannelProps) -> PortalChannelInfo: - return await self._api_user_request_result( + def get_portal_channel_info(self, channel_props: ChannelProps) -> Awaitable[PortalChannelInfo]: + return self._api_user_request_result( PortalChannelInfo, "get_portal_channel_info", channel_props=channel_props.serialize(), ) - async def get_participants(self, channel_props: ChannelProps) -> list[UserInfoUnion]: - return await self._api_user_request_result( + def get_participants(self, channel_props: ChannelProps) -> Awaitable[list[UserInfoUnion]]: + return self._api_user_request_result( ResultListType(UserInfoUnion), "get_participants", channel_props=channel_props.serialize(), ) - async def get_chats(self, channel_props: ChannelProps, sync_from: Long | None, limit: int | None) -> list[Chatlog]: - return await self._api_user_request_result( + def get_chats(self, channel_props: ChannelProps, sync_from: Long | None, limit: int | None) -> Awaitable[list[Chatlog]]: + return self._api_user_request_result( ResultListType(Chatlog), "get_chats", channel_props=channel_props.serialize(), @@ -292,8 +292,8 @@ class Client: limit=limit, ) - async def list_friends(self) -> FriendListStruct: - return await self._api_user_request_result( + def list_friends(self) -> Awaitable[FriendListStruct]: + return self._api_user_request_result( FriendListStruct, "list_friends", ) @@ -314,14 +314,14 @@ class Client: await self._rpc_client.request("get_memo_ids", mxid=self.user.mxid) ) - async def send_chat( + def send_chat( self, channel_props: ChannelProps, text: str, reply_to: ReplyAttachment | None, mentions: list[MentionStruct] | None, - ) -> Long: - return await self._api_user_request_result( + ) -> Awaitable[Long]: + return self._api_user_request_result( Long, "send_chat", channel_props=channel_props.serialize(), @@ -330,7 +330,7 @@ class Client: mentions=[m.serialize() for m in mentions] if mentions is not None else None, ) - async def send_media( + def send_media( self, channel_props: ChannelProps, media_type: KnownChatType, @@ -340,8 +340,8 @@ class Client: width: int | None = None, height: int | None = None, ext: str | None = None, - ) -> Long: - return await self._api_user_request_result( + ) -> Awaitable[Long]: + return self._api_user_request_result( Long, "send_media", channel_props=channel_props.serialize(), @@ -353,69 +353,69 @@ class Client: ext=ext, ) - async def delete_chat( + def delete_chat( self, channel_props: ChannelProps, chat_id: Long, - ) -> None: - return await self._api_user_request_void( + ) -> Awaitable[None]: + return self._api_user_request_void( "delete_chat", channel_props=channel_props.serialize(), chat_id=chat_id.serialize(), ) - async def mark_read( + def mark_read( self, channel_props: ChannelProps, read_until_chat_id: Long, - ) -> None: - return await self._api_user_request_void( + ) -> Awaitable[None]: + return self._api_user_request_void( "mark_read", channel_props=channel_props.serialize(), read_until_chat_id=read_until_chat_id.serialize(), ) - async def send_perm( + def send_perm( self, channel_props: ChannelProps, user_id: Long, perm: OpenChannelUserPerm, - ) -> None: - return await self._api_user_request_void( + ) -> Awaitable[None]: + return self._api_user_request_void( "send_perm", channel_props=channel_props.serialize(), user_id=user_id.serialize(), perm=perm, ) - async def set_channel_name( + def set_channel_name( self, channel_props: ChannelProps, name: str, - ) -> None: - return await self._api_user_request_void( + ) -> Awaitable[None]: + return self._api_user_request_void( "set_channel_name", channel_props=channel_props.serialize(), name=name, ) - async def set_channel_description( + def set_channel_description( self, channel_props: ChannelProps, description: str, - ) -> None: - return await self._api_user_request_void( + ) -> Awaitable[None]: + return self._api_user_request_void( "set_channel_description", channel_props=channel_props.serialize(), description=description, ) - async def set_channel_photo( + def set_channel_photo( self, channel_props: ChannelProps, photo_url: str, - ) -> None: - return await self._api_user_request_void( + ) -> Awaitable[None]: + return self._api_user_request_void( "set_channel_photo", channel_props=channel_props.serialize(), photo_url=photo_url, diff --git a/matrix_appservice_kakaotalk/user.py b/matrix_appservice_kakaotalk/user.py index 014fb43..0dadfca 100644 --- a/matrix_appservice_kakaotalk/user.py +++ b/matrix_appservice_kakaotalk/user.py @@ -241,6 +241,7 @@ class User(DBUser, BaseUser): @property def oauth_credential(self) -> OAuthCredential: + assert None not in (self.ktid, self.uuid, self.access_token, self.refresh_token) return OAuthCredential( self.ktid, self.uuid, @@ -383,6 +384,7 @@ class User(DBUser, BaseUser): async def post_login(self, is_startup: bool) -> None: self.log.info("Running post-login actions") + assert self.ktid self._add_to_cache() try: @@ -407,7 +409,7 @@ class User(DBUser, BaseUser): pu.Puppet.get_mxid_from_id(portal.ktid): [portal.mxid] async for portal in po.Portal.get_all_by_receiver(self.ktid) if portal.mxid - } + } if self.ktid else {} @async_time(METRIC_CONNECT_AND_SYNC) async def connect_and_sync(self, sync_count: int | None) -> bool: @@ -503,7 +505,8 @@ class User(DBUser, BaseUser): return self._sync_channel(channel_info) - async def _sync_channel(self, channel_info: ChannelInfo): + async def _sync_channel(self, channel_info: ChannelInfo) -> None: + assert self.ktid portal = await po.Portal.get_by_ktid( channel_info.channelId, kt_receiver=self.ktid, @@ -692,6 +695,7 @@ class User(DBUser, BaseUser): @async_time(METRIC_CHAT) async def on_chat(self, chat: Chatlog, channel_id: Long, channel_type: ChannelType) -> None: + assert self.ktid portal = await po.Portal.get_by_ktid( channel_id, kt_receiver=self.ktid, @@ -712,6 +716,7 @@ class User(DBUser, BaseUser): channel_id: Long, channel_type: ChannelType, ) -> None: + assert self.ktid portal = await po.Portal.get_by_ktid( channel_id, kt_receiver=self.ktid, @@ -731,6 +736,7 @@ class User(DBUser, BaseUser): channel_id: Long, channel_type: ChannelType, ) -> None: + assert self.ktid puppet = await pu.Puppet.get_by_ktid(sender_id) portal = await po.Portal.get_by_ktid( channel_id, @@ -749,13 +755,14 @@ class User(DBUser, BaseUser): channel_id: Long, channel_type: ChannelType, ) -> None: + assert self.ktid portal = await po.Portal.get_by_ktid( channel_id, kt_receiver=self.ktid, kt_type=channel_type, ) - if portal: - await portal.update_info(self, info) + await portal.backfill_lock.wait("meta change") + await portal.update_info(self, info) @async_time(METRIC_PROFILE_CHANGE) async def on_profile_changed(self, info: OpenLinkChannelUserInfo) -> None: @@ -771,6 +778,7 @@ class User(DBUser, BaseUser): channel_id: Long, channel_type: ChannelType, ) -> None: + assert self.ktid portal = await po.Portal.get_by_ktid( channel_id, kt_receiver=self.ktid, @@ -783,18 +791,22 @@ class User(DBUser, BaseUser): await portal.set_power_levels(user_power_levels) @async_time(METRIC_CHANNEL_JOIN) - def on_channel_join(self, channel_info: ChannelInfo) -> Awaitable[None]: - return self._sync_channel(channel_info) + async def on_channel_join(self, channel_info: ChannelInfo) -> None: + assert self.ktid + await self._sync_channel(channel_info) @async_time(METRIC_CHANNEL_LEFT) - async def on_channel_left(self, channel_id: Long, channel_type: ChannelType) -> None: + async def on_channel_left(self, channel_id: Long, channel_type: ChannelType | None) -> None: + assert self.ktid portal = await po.Portal.get_by_ktid( channel_id, kt_receiver=self.ktid, kt_type=channel_type, ) if portal.mxid: - await portal.main_intent.kick_user(portal.mxid, self.mxid, "Left this channel from KakaoTalk") + user = await pu.Puppet.get_by_ktid(self.ktid) + await portal.backfill_lock.wait("channel left") + await portal.handle_kakaotalk_user_left(self, user, user) @async_time(METRIC_CHANNEL_KICKED) async def on_channel_kicked( @@ -804,6 +816,7 @@ class User(DBUser, BaseUser): channel_id: Long, channel_type: ChannelType ) -> None: + assert self.ktid portal = await po.Portal.get_by_ktid( channel_id, kt_receiver=self.ktid, @@ -822,6 +835,7 @@ class User(DBUser, BaseUser): channel_id: Long, channel_type: ChannelType ) -> None: + assert self.ktid portal = await po.Portal.get_by_ktid( channel_id, kt_receiver=self.ktid, @@ -839,6 +853,7 @@ class User(DBUser, BaseUser): channel_id: Long, channel_type: ChannelType ) -> None: + assert self.ktid portal = await po.Portal.get_by_ktid( channel_id, kt_receiver=self.ktid, diff --git a/node/src/client.js b/node/src/client.js index 7975d22..253ce19 100644 --- a/node/src/client.js +++ b/node/src/client.js @@ -29,8 +29,8 @@ import { /** @typedef {import("node-kakao").MentionStruct} MentionStruct */ /** @typedef {import("node-kakao/dist/talk").TalkChannelList} TalkChannelList */ -import pkg from "node-kakao" -const { OpenChannelUserPerm } = pkg +import openlink from "node-kakao/openlink" +const { OpenChannelUserPerm } = openlink import chat from "node-kakao/chat" const { KnownChatType } = chat @@ -317,9 +317,7 @@ class UserClient { this.#talkClient.channelList, channelProps.type ) - const res = await channelList.addChannel({ - channelId: channelProps.id, - }) + const res = await channelList.addChannel({ channelId: channelProps.id }) if (!res.success) { throw new Error(`Unable to add ${channelProps.type} channel ${channelProps.id}`) }