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.
|
|
|
|
*
|
2021-07-02 16:27:08 +03:00
|
|
|
* 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',
|
2021-07-02 16:27:08 +03:00
|
|
|
media: InputFileLike | tl.TypeInputPhoto,
|
2021-05-09 14:35:58 +03:00
|
|
|
previewSec?: number
|
|
|
|
): Promise<Photo> {
|
2021-07-02 16:27:08 +03:00
|
|
|
// 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.')
|
2021-07-02 16:27:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2021-06-06 15:20:41 +03:00
|
|
|
[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)
|
|
|
|
}
|