From 8acad15d7c884b80d938dab160d91e84e8f62207 Mon Sep 17 00:00:00 2001 From: teidesu Date: Sat, 10 Apr 2021 20:30:28 +0300 Subject: [PATCH] fix(client): archiveChats & unarchiveChats and respective bound methods for Chat --- packages/client/src/client.ts | 18 ++++++++++ .../client/src/methods/chats/archive-chats.ts | 33 +++++++++++++++++++ .../src/methods/chats/unarchive-chats.ts | 33 +++++++++++++++++++ packages/client/src/types/peers/chat.ts | 14 ++++++++ 4 files changed, 98 insertions(+) create mode 100644 packages/client/src/methods/chats/archive-chats.ts create mode 100644 packages/client/src/methods/chats/unarchive-chats.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 3ae6eafb..fec87cb3 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -14,10 +14,12 @@ import { signIn } from './methods/auth/sign-in' import { signUp } from './methods/auth/sign-up' import { start } from './methods/auth/start' import { addChatMembers } from './methods/chats/add-chat-members' +import { archiveChats } from './methods/chats/archive-chats' import { getChatPreview } from './methods/chats/get-chat-preview' import { getChat } from './methods/chats/get-chat' import { getFullChat } from './methods/chats/get-full-chat' import { joinChat } from './methods/chats/join-chat' +import { unarchiveChats } from './methods/chats/unarchive-chats' import { downloadAsBuffer } from './methods/files/download-buffer' import { downloadToFile } from './methods/files/download-file' import { downloadAsIterable } from './methods/files/download-iterable' @@ -342,6 +344,14 @@ export class TelegramClient extends BaseTelegramClient { ): Promise { return addChatMembers.apply(this, arguments) } + /** + * Archive one or more chats + * + * @param chats Chat ID(s), username(s), phone number(s), `"me"` or `"self"` + */ + archiveChats(chats: MaybeArray): Promise { + return archiveChats.apply(this, arguments) + } /** * Get preview information about a private chat. * @@ -386,6 +396,14 @@ export class TelegramClient extends BaseTelegramClient { joinChat(chatId: InputPeerLike): Promise { return joinChat.apply(this, arguments) } + /** + * Unarchive one or more chats + * + * @param chats Chat ID(s), username(s), phone number(s), `"me"` or `"self"` + */ + unarchiveChats(chats: MaybeArray): Promise { + return unarchiveChats.apply(this, arguments) + } /** * Download a file and return its contents as a Buffer. * diff --git a/packages/client/src/methods/chats/archive-chats.ts b/packages/client/src/methods/chats/archive-chats.ts new file mode 100644 index 00000000..b6f30fa8 --- /dev/null +++ b/packages/client/src/methods/chats/archive-chats.ts @@ -0,0 +1,33 @@ +import { TelegramClient } from '../../client' +import { MaybeArray } from '@mtcute/core' +import { InputPeerLike } from '../../types' +import { tl } from '@mtcute/tl' +import { normalizeToInputPeer } from '../../utils/peer-utils' + +/** + * Archive one or more chats + * + * @param chats Chat ID(s), username(s), phone number(s), `"me"` or `"self"` + * @internal + */ +export async function archiveChats( + this: TelegramClient, + chats: MaybeArray +): Promise { + if (!Array.isArray(chats)) chats = [chats] + + const folderPeers: tl.TypeInputFolderPeer[] = [] + + for (const chat of chats) { + folderPeers.push({ + _: 'inputFolderPeer', + peer: normalizeToInputPeer(await this.resolvePeer(chat)), + folderId: 1 + }) + } + + await this.call({ + _: 'folders.editPeerFolders', + folderPeers + }) +} diff --git a/packages/client/src/methods/chats/unarchive-chats.ts b/packages/client/src/methods/chats/unarchive-chats.ts new file mode 100644 index 00000000..177f3e23 --- /dev/null +++ b/packages/client/src/methods/chats/unarchive-chats.ts @@ -0,0 +1,33 @@ +import { TelegramClient } from '../../client' +import { MaybeArray } from '@mtcute/core' +import { InputPeerLike } from '../../types' +import { tl } from '@mtcute/tl' +import { normalizeToInputPeer } from '../../utils/peer-utils' + +/** + * Unarchive one or more chats + * + * @param chats Chat ID(s), username(s), phone number(s), `"me"` or `"self"` + * @internal + */ +export async function unarchiveChats( + this: TelegramClient, + chats: MaybeArray +): Promise { + if (!Array.isArray(chats)) chats = [chats] + + const folderPeers: tl.TypeInputFolderPeer[] = [] + + for (const chat of chats) { + folderPeers.push({ + _: 'inputFolderPeer', + peer: normalizeToInputPeer(await this.resolvePeer(chat)), + folderId: 0 + }) + } + + await this.call({ + _: 'folders.editPeerFolders', + folderPeers + }) +} diff --git a/packages/client/src/types/peers/chat.ts b/packages/client/src/types/peers/chat.ts index 0408fc8e..8851e5bf 100644 --- a/packages/client/src/types/peers/chat.ts +++ b/packages/client/src/types/peers/chat.ts @@ -441,6 +441,20 @@ export class Chat { async addMembers(users: MaybeArray, forwardCount?: number): Promise { return this.client.addChatMembers(this.inputPeer, users, forwardCount) } + + /** + * Archive this chat + */ + async archive(): Promise { + return this.client.archiveChats(this.inputPeer) + } + + /** + * Unarchive this chat + */ + async unarchive(): Promise { + return this.client.unarchiveChats(this.inputPeer) + } } makeInspectable(Chat)