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,22 +22,27 @@ export async function setProfilePhoto(
previewSec?: number previewSec?: number
): Promise<Photo> { ): Promise<Photo> {
// try parsing media as file id or input 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?:\/\//)) { 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 (typeof media !== 'string' || !media.match(/^file:/)) {
if (tdFileId.isFileIdLike(media)) { if (tdFileId.isFileIdLike(media)) {
media = fileIdToInputPhoto(media) media = fileIdToInputPhoto(media)
} }
const res = await this.call({ const res = await this.call({
_: 'photos.updateProfilePhoto', _: 'photos.updateProfilePhoto',
id: media id: media,
}) })
return new Photo(this, res.photo as tl.RawPhoto) return new Photo(this, res.photo as tl.RawPhoto)
} }
}
const res = await this.call({ const res = await this.call({
_: 'photos.uploadProfilePhoto', _: 'photos.uploadProfilePhoto',

View file

@ -78,6 +78,13 @@ export class Photo extends FileLocation {
return new Date(this.raw.date * 1000) 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[] private _thumbnails?: Thumbnail[]
/** /**
* Available thumbnails. * Available thumbnails.
@ -90,6 +97,9 @@ export class Photo extends FileLocation {
this._thumbnails = this.raw.sizes.map( this._thumbnails = this.raw.sizes.map(
(sz) => new Thumbnail(this.client, this.raw, sz) (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 return this._thumbnails
@ -116,9 +126,7 @@ export class Photo extends FileLocation {
get fileId(): string { get fileId(): string {
if (!this._fileId) { if (!this._fileId) {
if (!this._bestSize) { if (!this._bestSize) {
throw new MtArgumentError( throw new MtArgumentError('Cannot get File ID for this photo')
'Cannot get File ID for this photo'
)
} }
this._fileId = this.getThumbnail(this._bestSize.type)!.fileId this._fileId = this.getThumbnail(this._bestSize.type)!.fileId
@ -135,12 +143,12 @@ export class Photo extends FileLocation {
get uniqueFileId(): string { get uniqueFileId(): string {
if (!this._uniqueFileId) { if (!this._uniqueFileId) {
if (!this._bestSize) { if (!this._bestSize) {
throw new MtArgumentError( throw new MtArgumentError('Cannot get File ID for this photo')
'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 return this._uniqueFileId
@ -166,9 +174,13 @@ export class Photo extends FileLocation {
get inputMedia(): tl.TypeInputMedia { get inputMedia(): tl.TypeInputMedia {
return { return {
_: 'inputMediaPhoto', _: '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, id: media.id,
fileReference: media.fileReference, fileReference: media.fileReference,
accessHash: media.accessHash, accessHash: media.accessHash,
thumbSize: sz.type === 'u' ? '\x00' : sz.type, thumbSize: sz.type,
} }
} }
width = sz.w width = sz.w