From f0cf8a50a19176a23d362fcc7f6b1a1271c91737 Mon Sep 17 00:00:00 2001 From: teidesu Date: Sat, 10 Apr 2021 20:06:33 +0300 Subject: [PATCH] feat(client): addChatMembers method and related bound method for Chat --- packages/client/src/client.ts | 17 ++++++ .../src/methods/chats/add-chat-members.ts | 56 +++++++++++++++++++ packages/client/src/types/peers/chat.ts | 15 ++++- 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 packages/client/src/methods/chats/add-chat-members.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 45ddb3c3..3ae6eafb 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -13,6 +13,7 @@ import { signInBot } from './methods/auth/sign-in-bot' 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 { getChatPreview } from './methods/chats/get-chat-preview' import { getChat } from './methods/chats/get-chat' import { getFullChat } from './methods/chats/get-full-chat' @@ -325,6 +326,22 @@ export class TelegramClient extends BaseTelegramClient { }): Promise { return start.apply(this, arguments) } + /** + * Add new members to a group, supergroup or channel. + * + * @param chatId ID of the chat or its username + * @param users ID(s) of the users, their username(s) or phone(s). + * @param forwardCount(default: `100`) + * Number of old messages to be forwarded (0-100). + * Only applicable to legacy groups, ignored for supergroups and channels + */ + addChatMembers( + chatId: InputPeerLike, + users: MaybeArray, + forwardCount?: number + ): Promise { + return addChatMembers.apply(this, arguments) + } /** * Get preview information about a private chat. * diff --git a/packages/client/src/methods/chats/add-chat-members.ts b/packages/client/src/methods/chats/add-chat-members.ts new file mode 100644 index 00000000..1042267b --- /dev/null +++ b/packages/client/src/methods/chats/add-chat-members.ts @@ -0,0 +1,56 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types' +import { MaybeArray } from '@mtcute/core' +import { + normalizeToInputChannel, + normalizeToInputPeer, + normalizeToInputUser, +} from '../../utils/peer-utils' +import { tl } from '@mtcute/tl' + +/** + * Add new members to a group, supergroup or channel. + * + * @param chatId ID of the chat or its username + * @param users ID(s) of the users, their username(s) or phone(s). + * @param forwardCount + * Number of old messages to be forwarded (0-100). + * Only applicable to legacy groups, ignored for supergroups and channels + * @internal + */ +export async function addChatMembers( + this: TelegramClient, + chatId: InputPeerLike, + users: MaybeArray, + forwardCount = 100 +): Promise { + const chat = await this.resolvePeer(chatId) + const input = normalizeToInputPeer(chat) + + if (!Array.isArray(users)) users = [users] + + if (input._ === 'inputPeerChat') { + for (const user of users) { + const p = normalizeToInputUser(await this.resolvePeer(user)) + if (!p) continue + + await this.call({ + _: 'messages.addChatUser', + chatId: input.chatId, + userId: p, + fwdLimit: forwardCount, + }) + } + } else if (input._ === 'inputPeerChannel') { + await this.call({ + _: 'channels.inviteToChannel', + channel: normalizeToInputChannel(chat)!, + users: await Promise.all( + (users as InputPeerLike[]).map((u) => + this.resolvePeer(u).then(normalizeToInputUser) + ) + ).then((res) => res.filter(Boolean)) as tl.TypeInputUser[], + fwdLimit: forwardCount, + }) + } else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') +} diff --git a/packages/client/src/types/peers/chat.ts b/packages/client/src/types/peers/chat.ts index 03453baf..0408fc8e 100644 --- a/packages/client/src/types/peers/chat.ts +++ b/packages/client/src/types/peers/chat.ts @@ -2,9 +2,10 @@ import { ChatPhoto } from './chat-photo' import { tl } from '@mtcute/tl' import { ChatPermissions } from './chat-permissions' import { TelegramClient } from '../../client' -import { getMarkedPeerId } from '@mtcute/core' +import { getMarkedPeerId, MaybeArray } from '@mtcute/core' import { MtCuteArgumentError, MtCuteTypeAssertionError } from '../errors' import { makeInspectable } from '../utils' +import { InputPeerLike } from './index' export namespace Chat { /** @@ -428,6 +429,18 @@ export class Chat { async join(): Promise { await this.client.joinChat(this.inputPeer) } + + /** + * Add user(s) to this chat + * + * @param users ID(s) of the users, their username(s) or phone(s). + * @param forwardCount + * Number of old messages to be forwarded (0-100). + * Only applicable to legacy groups, ignored for supergroups and channels + */ + async addMembers(users: MaybeArray, forwardCount?: number): Promise { + return this.client.addChatMembers(this.inputPeer, users, forwardCount) + } } makeInspectable(Chat)