diff --git a/ROADMAP.md b/ROADMAP.md index 1d791a8..9eec13b 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -87,6 +87,9 @@ * [x] Remove friend * [ ] Manage favourite friends * [ ] Manage hidden friends + * [x] KakaoTalk ID management + * [x] Set/Change ID + * [x] Make ID searchable/hidden [1] Sometimes fails with "Invalid body" error [2] Only recently-sent KakaoTalk messages can be deleted diff --git a/matrix_appservice_kakaotalk/commands/kakaotalk.py b/matrix_appservice_kakaotalk/commands/kakaotalk.py index 8b70e3d..093f787 100644 --- a/matrix_appservice_kakaotalk/commands/kakaotalk.py +++ b/matrix_appservice_kakaotalk/commands/kakaotalk.py @@ -15,7 +15,7 @@ # along with this program. If not, see . from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Awaitable import asyncio from mautrix.bridge.commands import HelpSection, command_handler @@ -36,6 +36,7 @@ from .typehint import CommandEvent from ..kt.client.errors import CommandException +SECTION_ACCOUNT = HelpSection("Account management", 35, "") SECTION_FRIENDS = HelpSection("Friends management", 40, "") SECTION_CHANNELS = HelpSection("Channel management", 45, "") @@ -46,6 +47,84 @@ if TYPE_CHECKING: from ..kt.types.bson import Long +_CMD_CONFIRM_CHANGE_ID = "confirm-change-id" + +@command_handler( + needs_auth=True, + management_only=False, + help_section=SECTION_ACCOUNT, + help_text="Set or change your KakaoTalk ID", + help_args="<_KakaoTalk ID_>", + aliases=["set-id"], +) +async def change_id(evt: CommandEvent) -> None: + if len(evt.args) != 1: + await evt.reply(f"**Usage:** `$cmdprefix+sp {evt.command} `") + return + new_id = evt.args[0] + if len(new_id) > 20: + await evt.reply("ID must not exceed 20 characters. Please choose a shorter ID.") + return + await evt.mark_read() + if await evt.sender.client.can_change_uuid(new_id): + await evt.reply( + "Once set, your KakaoTalk ID can be changed only once! " + f"If you are sure that you want to change it, type `$cmdprefix+sp {_CMD_CONFIRM_CHANGE_ID}`. " + "Otherwise, type `$cmdprefix+sp cancel`." + ) + evt.sender.command_status = { + "action": "ID change", + "room_id": evt.room_id, + "next": _confirm_change_id, + "new_id": new_id, + } + else: + await evt.reply( + f"Cannot change KakaoTalk ID to `{new_id}`. " + "That ID might already be in use or have restricted characters. " + "Either try a different ID, or try again later." + ) + +async def _confirm_change_id(evt: CommandEvent) -> None: + assert evt.sender.command_status + new_id = evt.sender.command_status.pop("new_id") + evt.sender.command_status = None + await evt.mark_read() + try: + await evt.sender.client.change_uuid(new_id) + except CommandException: + await evt.reply(f"Failed to change KakaoTalk ID to `{new_id}`. Try again later.") + else: + await evt.reply(f"Successfully changed ID to `{new_id}`") + + +@command_handler( + needs_auth=True, + management_only=False, + help_section=SECTION_ACCOUNT, + help_text="Allow others to search by your KakaoTalk ID", +) +def make_id_searchable(evt: CommandEvent) -> Awaitable[None]: + return _set_id_searchable(evt, True) + +@command_handler( + needs_auth=True, + management_only=False, + help_section=SECTION_ACCOUNT, + help_text="Prevent others from searching by your KakaoTalk ID", +) +def make_id_hidden(evt: CommandEvent) -> Awaitable[None]: + return _set_id_searchable(evt, False) + +async def _set_id_searchable(evt: CommandEvent, searchable: bool) -> None: + await evt.mark_read() + try: + await evt.sender.client.set_uuid_searchable(searchable) + except RPCError as e: + await evt.reply(str(e)) + else: + await evt.reply(f"Successfully made KakaoTalk ID {'searchable' if searchable else 'hidden'}") + async def _get_search_result_puppet(source: u.User, friend_struct: FriendStruct) -> pu.Puppet: puppet = await pu.Puppet.get_by_ktid(friend_struct.userId) if not puppet.name_set: diff --git a/matrix_appservice_kakaotalk/kt/client/client.py b/matrix_appservice_kakaotalk/kt/client/client.py index e4783a4..2a45fbb 100644 --- a/matrix_appservice_kakaotalk/kt/client/client.py +++ b/matrix_appservice_kakaotalk/kt/client/client.py @@ -324,6 +324,20 @@ class Client: unread_chat_ids=[c.serialize() for c in unread_chat_ids], ) + async def can_change_uuid(self, uuid: str) -> bool: + try: + await self._api_user_request_void("can_change_uuid", uuid=uuid) + except CommandException: + return False + else: + return True + + def change_uuid(self, uuid: str) -> Awaitable[None]: + return self._api_user_request_void("change_uuid", uuid=uuid) + + def set_uuid_searchable(self, searchable: bool) -> Awaitable[None]: + return self._api_user_request_void("set_uuid_searchable", searchable=searchable) + def list_friends(self) -> Awaitable[FriendListStruct]: return self._api_user_request_result( FriendListStruct, diff --git a/matrix_appservice_kakaotalk/user.py b/matrix_appservice_kakaotalk/user.py index e6c83cd..2cf2c0d 100644 --- a/matrix_appservice_kakaotalk/user.py +++ b/matrix_appservice_kakaotalk/user.py @@ -278,8 +278,8 @@ class User(DBUser, BaseUser): self.log.warning(f"UUID mismatch: expected {self.uuid}, got {oauth_credential.deviceUUID}") self.uuid = oauth_credential.deviceUUID - async def get_own_info(self) -> SettingsStruct | None: - if self._client and (not self._logged_in_info or self._logged_in_info_time + 60 * 60 < time.monotonic()): + async def get_own_info(self, *, force: bool = False) -> SettingsStruct | None: + if force or self._client and (not self._logged_in_info or self._logged_in_info_time + 60 * 60 < time.monotonic()): self._logged_in_info = await self._client.get_settings() self._logged_in_info_time = time.monotonic() return self._logged_in_info diff --git a/node/src/client.js b/node/src/client.js index 7cada60..f053b02 100644 --- a/node/src/client.js +++ b/node/src/client.js @@ -834,6 +834,51 @@ export default class PeerClient { return makeCommandResult(receipts) } + /** + * @param {Object} req + * @param {string} req.mxid + * @param {string} req.uuid + */ + canChangeUUID = async (req) => { + return await this.#getUser(req.mxid).serviceClient.canChangeUUID(req.uuid) + } + + /** + * @param {Object} req + * @param {string} req.mxid + * @param {string} req.uuid + */ + changeUUID = async (req) => { + const serviceClient = this.#getUser(req.mxid).serviceClient + + const checkRes = await serviceClient.canChangeUUID(req.uuid) + if (!checkRes.success) return checkRes + + return await serviceClient.changeUUID(req.uuid) + } + + /** + * @param {Object} req + * @param {string} req.mxid + * @param {boolean} req.searchable + */ + setUUIDSearchable = async (req) => { + const serviceClient = this.#getUser(req.mxid).serviceClient + const moreRes = await serviceClient.requestMoreSettings() + if (!moreRes.success) { + throw new ProtocolError("Error checking status of KakaoTalk ID") + } + if (!moreRes.result.uuid) { + throw new ProtocolError("You do not yet have a KakaoTalk ID") + } + if (req.searchable == moreRes.result.uuidSearchable) { + throw new ProtocolError(`Your KakaoTalk ID is already ${req.searchable ? "searchable" : "hidden"}`) + } + return await serviceClient.updateSettings({ + uuid_searchable: req.searchable, + }) + } + /** * @param {Object} req * @param {string} req.mxid @@ -1174,6 +1219,9 @@ export default class PeerClient { get_participants: this.getParticipants, get_chats: this.getChats, get_read_receipts: this.getReadReceipts, + can_change_uuid: this.canChangeUUID, + change_uuid: this.changeUUID, + set_uuid_searchable: this.setUUIDSearchable, list_friends: this.listFriends, edit_friend: this.editFriend, edit_friend_by_uuid: this.editFriendByUUID, @@ -1205,11 +1253,15 @@ export default class PeerClient { } } else { resp.command = "error" - resp.error = err instanceof ProtocolError ? err.message : err.toString() - this.log(`Error handling request ${resp.id} ${err.stack}`) - // TODO Check if session is broken. If it is, close the PeerClient + if (err instanceof ProtocolError) { + resp.error = err.message + } else { + resp.error = err.toString() + this.log(`Error handling request ${resp.id} ${err.stack}`) + } } } + // TODO Check if session is broken. If it is, close the PeerClient await this.write(resp) }