fix(client): improve animated profile picture support, fixed related bugs

This commit is contained in:
teidesu 2022-06-27 16:23:02 +03:00
parent 8822de2e1f
commit 16ffca23d2
3 changed files with 38 additions and 21 deletions

View file

@ -22,21 +22,26 @@ export async function setProfilePhoto(
previewSec?: number
): Promise<Photo> {
// try parsing media as file id or input photo
if (tdFileId.isFileIdLike(media) || typeof media === 'object' && tl.isAnyInputPhoto(media)) {
if (
tdFileId.isFileIdLike(media) ||
(typeof media === 'object' && tl.isAnyInputPhoto(media))
) {
if (typeof media === 'string' && media.match(/^https?:\/\//)) {
throw new MtArgumentError('Profile photo can\'t be set from URL.')
throw new MtArgumentError("Profile photo can't be set from URL.")
}
if (tdFileId.isFileIdLike(media)) {
media = fileIdToInputPhoto(media)
if (typeof media !== 'string' || !media.match(/^file:/)) {
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.updateProfilePhoto',
id: media
})
return new Photo(this, res.photo as tl.RawPhoto)
}
const res = await this.call({

View file

@ -78,6 +78,13 @@ export class Photo extends FileLocation {
return new Date(this.raw.date * 1000)
}
/**
* Whether this photo is an animated profile picture
*/
get isAnimated(): boolean {
return !!this.raw.videoSizes?.some(s => s.type === 'u')
}
private _thumbnails?: Thumbnail[]
/**
* Available thumbnails.
@ -90,6 +97,9 @@ export class Photo extends FileLocation {
this._thumbnails = this.raw.sizes.map(
(sz) => new Thumbnail(this.client, this.raw, sz)
)
this.raw.videoSizes?.forEach((sz) =>
this._thumbnails!.push(new Thumbnail(this.client, this.raw, sz))
)
}
return this._thumbnails
@ -116,9 +126,7 @@ export class Photo extends FileLocation {
get fileId(): string {
if (!this._fileId) {
if (!this._bestSize) {
throw new MtArgumentError(
'Cannot get File ID for this photo'
)
throw new MtArgumentError('Cannot get File ID for this photo')
}
this._fileId = this.getThumbnail(this._bestSize.type)!.fileId
@ -135,12 +143,12 @@ export class Photo extends FileLocation {
get uniqueFileId(): string {
if (!this._uniqueFileId) {
if (!this._bestSize) {
throw new MtArgumentError(
'Cannot get File ID for this photo'
)
throw new MtArgumentError('Cannot get File ID for this photo')
}
this._uniqueFileId = this.getThumbnail(this._bestSize.type)!.uniqueFileId
this._uniqueFileId = this.getThumbnail(
this._bestSize.type
)!.uniqueFileId
}
return this._uniqueFileId
@ -166,9 +174,13 @@ export class Photo extends FileLocation {
get inputMedia(): tl.TypeInputMedia {
return {
_: 'inputMediaPhoto',
id: this.inputPhoto
id: this.inputPhoto,
}
}
}
makeInspectable(Photo, ['fileSize', 'dcId', 'width', 'height'], ['inputMedia', 'inputPhoto'])
makeInspectable(
Photo,
['fileSize', 'dcId', 'width', 'height'],
['inputMedia', 'inputPhoto']
)

View file

@ -107,7 +107,7 @@ export class Thumbnail extends FileLocation {
id: media.id,
fileReference: media.fileReference,
accessHash: media.accessHash,
thumbSize: sz.type === 'u' ? '\x00' : sz.type,
thumbSize: sz.type,
}
}
width = sz.w