feat(client): set profile photo from file id or input photo

This commit is contained in:
teidesu 2021-07-02 16:27:08 +03:00
parent d0e3ebda80
commit 94a65b1a74
2 changed files with 26 additions and 3 deletions

View file

@ -3096,6 +3096,8 @@ export interface TelegramClient extends BaseTelegramClient {
/**
* Set a new profile photo or video.
*
* You can also pass a file ID or an InputPhoto to re-use existing photo.
*
* @param type Media type (photo or video)
* @param media Input media file
* @param previewSec
@ -3104,7 +3106,7 @@ export interface TelegramClient extends BaseTelegramClient {
*/
setProfilePhoto(
type: 'photo' | 'video',
media: InputFileLike,
media: InputFileLike | tl.TypeInputPhoto,
previewSec?: number
): Promise<Photo>
/**

View file

@ -1,10 +1,13 @@
import { TelegramClient } from '../../client'
import { InputFileLike, Photo } from '../../types'
import { InputFileLike, MtCuteArgumentError, Photo } from '../../types'
import { tl } from '@mtcute/tl'
import { fileIdToInputPhoto, tdFileId } from '../../../../file-id/dist'
/**
* Set a new profile photo or video.
*
* You can also pass a file ID or an InputPhoto to re-use existing photo.
*
* @param type Media type (photo or video)
* @param media Input media file
* @param previewSec
@ -15,9 +18,27 @@ import { tl } from '@mtcute/tl'
export async function setProfilePhoto(
this: TelegramClient,
type: 'photo' | 'video',
media: InputFileLike,
media: InputFileLike | tl.TypeInputPhoto,
previewSec?: number
): Promise<Photo> {
// try parsing media as file id or input photo
if (tdFileId.isFileIdLike(media) || typeof media === 'object' && tl.isAnyInputPhoto(media)) {
if (typeof media === 'string' && media.match(/^https?:\/\//)) {
throw new MtCuteArgumentError('Profile photo can\'t be set from URL.')
}
if (tdFileId.isFileIdLike(media)) {
media = fileIdToInputPhoto(media)
}
const res = await this.call({
_: 'photos.updateProfilePhoto',
id: media
})
return new Photo(this, res.photo as tl.RawPhoto)
}
const res = await this.call({
_: 'photos.uploadProfilePhoto',
[type === 'photo' ? 'file' : 'video']: await this._normalizeInputFile(