diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 4cbbf1f0..6728a78f 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -19,6 +19,7 @@ import { createChannel } from './methods/chats/create-channel' import { createGroup } from './methods/chats/create-group' import { createSupergroup } from './methods/chats/create-supergroup' import { deleteChannel } from './methods/chats/delete-channel' +import { deleteChatPhoto } from './methods/chats/delete-chat-photo' import { deleteGroup } from './methods/chats/delete-group' import { deleteHistory } from './methods/chats/delete-history' import { getChatMember } from './methods/chats/get-chat-member' @@ -27,6 +28,7 @@ import { getChat } from './methods/chats/get-chat' import { getFullChat } from './methods/chats/get-full-chat' import { joinChat } from './methods/chats/join-chat' import { leaveChat } from './methods/chats/leave-chat' +import { setChatPhoto } from './methods/chats/set-chat-photo' import { unarchiveChats } from './methods/chats/unarchive-chats' import { downloadAsBuffer } from './methods/files/download-buffer' import { downloadToFile } from './methods/files/download-file' @@ -416,6 +418,16 @@ export class TelegramClient extends BaseTelegramClient { deleteSupergroup(chatId: InputPeerLike): Promise { return deleteChannel.apply(this, arguments) } + /** + * Delete a chat photo + * + * You must be an administrator and have the appropriate permissions. + * + * @param chatId Chat ID or username + */ + deleteChatPhoto(chatId: InputPeerLike): Promise { + return deleteChatPhoto.apply(this, arguments) + } /** * Delete a legacy group chat for all members * @@ -451,7 +463,7 @@ export class TelegramClient extends BaseTelegramClient { * * @param chatId Chat ID or username * @param userId User ID, username, phone number, `"me"` or `"self"` - * @throws MtCuteNotFoundError In case given user is not a participant of a given chat + * @throws UserNotParticipantError In case given user is not a participant of a given chat */ getChatMember( chatId: InputPeerLike, @@ -512,6 +524,26 @@ export class TelegramClient extends BaseTelegramClient { leaveChat(chatId: InputPeerLike, clear?: boolean): Promise { return leaveChat.apply(this, arguments) } + /** + * Set a new chat photo or video. + * + * You must be an administrator and have the appropriate permissions. + * + * @param chatId Chat ID or username + * @param type Media type (photo or video) + * @param media Input media file + * @param previewSec + * When `type = video`, timestamp in seconds which will be shown + * as a static preview. + */ + setChatPhoto( + chatId: InputPeerLike, + type: 'photo' | 'video', + media: InputFileLike, + previewSec?: number + ): Promise { + return setChatPhoto.apply(this, arguments) + } /** * Unarchive one or more chats * diff --git a/packages/client/src/methods/chats/delete-chat-photo.ts b/packages/client/src/methods/chats/delete-chat-photo.ts new file mode 100644 index 00000000..1fff8afa --- /dev/null +++ b/packages/client/src/methods/chats/delete-chat-photo.ts @@ -0,0 +1,37 @@ +import { TelegramClient } from '../../client' +import { + InputPeerLike, + MtCuteInvalidPeerTypeError, +} from '../../types' +import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' + +/** + * Delete a chat photo + * + * You must be an administrator and have the appropriate permissions. + * + * @param chatId Chat ID or username + * @internal + */ +export async function deleteChatPhoto( + this: TelegramClient, + chatId: InputPeerLike +): Promise { + const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) + if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel')) + throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') + + if (chat._ === 'inputPeerChat') { + await this.call({ + _: 'messages.editChatPhoto', + chatId: chat.chatId, + photo: { _: 'inputChatPhotoEmpty' } + }) + } else { + await this.call({ + _: 'channels.editPhoto', + channel: normalizeToInputChannel(chat)!, + photo: { _: 'inputChatPhotoEmpty' } + }) + } +} diff --git a/packages/client/src/methods/chats/set-chat-photo.ts b/packages/client/src/methods/chats/set-chat-photo.ts new file mode 100644 index 00000000..f56aba9a --- /dev/null +++ b/packages/client/src/methods/chats/set-chat-photo.ts @@ -0,0 +1,70 @@ +import { TelegramClient } from '../../client' +import { + InputFileLike, + InputPeerLike, + isUploadedFile, + MtCuteArgumentError, + MtCuteInvalidPeerTypeError, +} from '../../types' +import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' +import { tl } from '@mtcute/tl' + +/** + * Set a new chat photo or video. + * + * You must be an administrator and have the appropriate permissions. + * + * @param chatId Chat ID or username + * @param type Media type (photo or video) + * @param media Input media file + * @param previewSec + * When `type = video`, timestamp in seconds which will be shown + * as a static preview. + * @internal + */ +export async function setChatPhoto( + this: TelegramClient, + chatId: InputPeerLike, + type: 'photo' | 'video', + media: InputFileLike, + previewSec?: number +): Promise { + const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) + if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel')) + throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') + + let input: tl.TypeInputFile + + if (typeof media === 'string' && media.match(/^https?:\/\//)) { + throw new MtCuteArgumentError("Chat photo can't be external") + } else if (isUploadedFile(media)) { + input = media.inputFile + } else if (typeof media === 'object' && tl.isAnyInputFile(media)) { + input = media + } else { + const uploaded = await this.uploadFile({ + file: media, + }) + input = uploaded.inputFile + } + + const photo: tl.RawInputChatUploadedPhoto = { + _: 'inputChatUploadedPhoto', + [type === 'photo' ? 'file' : 'video']: input, + videoStartTs: previewSec + } + + if (chat._ === 'inputPeerChat') { + await this.call({ + _: 'messages.editChatPhoto', + chatId: chat.chatId, + photo + }) + } else { + await this.call({ + _: 'channels.editPhoto', + channel: normalizeToInputChannel(chat)!, + photo + }) + } +}