feat(client): setChatPhoto and deleteChatPhoto methods

This commit is contained in:
teidesu 2021-04-11 15:22:48 +03:00
parent 89dafa570b
commit 173647dda6
3 changed files with 140 additions and 1 deletions

View file

@ -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<void> {
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<void> {
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<void> {
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<void> {
return setChatPhoto.apply(this, arguments)
}
/**
* Unarchive one or more chats
*

View file

@ -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<void> {
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' }
})
}
}

View file

@ -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<void> {
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
})
}
}