diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 88ffb0fd..d2a5b5d4 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -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 /** diff --git a/packages/client/src/methods/users/set-profile-photo.ts b/packages/client/src/methods/users/set-profile-photo.ts index e9a1cf0b..a228f62b 100644 --- a/packages/client/src/methods/users/set-profile-photo.ts +++ b/packages/client/src/methods/users/set-profile-photo.ts @@ -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 { + // 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(