mtcute/packages/client/src/methods/users/set-profile-photo.ts

53 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-05-09 14:35:58 +03:00
import { TelegramClient } from '../../client'
2021-07-25 21:03:40 +03:00
import { InputFileLike, MtqtArgumentError, Photo } from '../../types'
import { tl } from '@mtqt/tl'
import { fileIdToInputPhoto, tdFileId } from '@mtqt/file-id'
2021-05-09 14:35:58 +03:00
/**
* Set a new profile photo or video.
*
* You can also pass a file ID or an InputPhoto to re-use existing photo.
*
2021-05-09 14:35:58 +03:00
* @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 setProfilePhoto(
this: TelegramClient,
type: 'photo' | 'video',
media: InputFileLike | tl.TypeInputPhoto,
2021-05-09 14:35:58 +03:00
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?:\/\//)) {
2021-07-25 21:03:40 +03:00
throw new MtqtArgumentError('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)
}
2021-05-09 14:35:58 +03:00
const res = await this.call({
_: 'photos.uploadProfilePhoto',
[type === 'photo' ? 'file' : 'video']: await this._normalizeInputFile(
media,
{}
),
videoStartTs: previewSec,
2021-05-09 14:35:58 +03:00
})
return new Photo(this, res.photo as tl.RawPhoto)
}