feat(client): improve support for premium stickers and video thumbnails

This commit is contained in:
teidesu 2022-06-21 20:53:57 +03:00
parent 1ac0cd8530
commit 0396710b18
4 changed files with 62 additions and 27 deletions

View file

@ -67,11 +67,16 @@ export class RawDocument extends FileLocation {
*/ */
get thumbnails(): ReadonlyArray<Thumbnail> { get thumbnails(): ReadonlyArray<Thumbnail> {
if (!this._thumbnails) { if (!this._thumbnails) {
this._thumbnails = this.raw.thumbs const arr: Thumbnail[] = []
? this.raw.thumbs.map(
(sz) => new Thumbnail(this.client, this.raw, sz) this.raw.thumbs?.forEach((sz) =>
) arr.push(new Thumbnail(this.client, this.raw, sz))
: [] )
this.raw.videoThumbs?.forEach((sz) =>
arr.push(new Thumbnail(this.client, this.raw, sz))
)
this._thumbnails = arr
} }
return this._thumbnails return this._thumbnails

View file

@ -76,6 +76,13 @@ export class Sticker extends RawDocument {
return this.attr2?._ === 'documentAttributeVideo' return this.attr2?._ === 'documentAttributeVideo'
} }
/**
* Whether this sticker is a video (WEBM) sticker
*/
get isPremiumSticker(): boolean {
return !!this.raw.videoThumbs?.some(s => s.type === 'f')
}
/** /**
* Whether this sticker is a valid sticker. * Whether this sticker is a valid sticker.
* *

View file

@ -31,7 +31,14 @@ export class Thumbnail extends FileLocation {
static readonly THUMB_STRIP = 'i' static readonly THUMB_STRIP = 'i'
static readonly THUMB_OUTLINE = 'j' static readonly THUMB_OUTLINE = 'j'
readonly raw: tl.TypePhotoSize /** Animated profile pictures preview */
static readonly THUMB_VIDEO_PROFILE = 'u'
/** Trimmed and downscaled video previews */
static readonly THUMB_VIDEO_PREVIEW = 'v'
/** Fullscreen animation for Premium stickers */
static readonly THUMB_VIDEO_FULLSCREEN = 'f'
readonly raw: tl.TypePhotoSize | tl.TypeVideoSize
/** /**
* Thumbnail width * Thumbnail width
@ -51,7 +58,7 @@ export class Thumbnail extends FileLocation {
constructor( constructor(
client: TelegramClient, client: TelegramClient,
media: tl.RawPhoto | tl.RawDocument | tl.RawStickerSet, media: tl.RawPhoto | tl.RawDocument | tl.RawStickerSet,
sz: tl.TypePhotoSize sz: tl.TypePhotoSize | tl.TypeVideoSize
) { ) {
switch (sz._) { switch (sz._) {
case 'photoSizeEmpty': case 'photoSizeEmpty':
@ -87,9 +94,9 @@ export class Thumbnail extends FileLocation {
stickerset: { stickerset: {
_: 'inputStickerSetID', _: 'inputStickerSetID',
id: media.id, id: media.id,
accessHash: media.accessHash accessHash: media.accessHash,
}, },
thumbVersion: media.thumbVersion! thumbVersion: media.thumbVersion!,
} }
} else { } else {
location = { location = {
@ -100,12 +107,15 @@ 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, thumbSize: sz.type === 'u' ? '\x00' : sz.type,
} }
} }
width = sz.w width = sz.w
height = sz.h height = sz.h
size = sz._ === 'photoSize' ? sz.size : Math.max(...sz.sizes) size =
sz._ === 'photoSizeProgressive'
? Math.max(...sz.sizes)
: sz.size
break break
} }
@ -125,6 +135,10 @@ export class Thumbnail extends FileLocation {
} }
} }
get isVideo(): boolean {
return this.raw._ === 'videoSize'
}
/** /**
* Thumbnail type * Thumbnail type
*/ */
@ -152,12 +166,16 @@ export class Thumbnail extends FileLocation {
/** /**
* Get TDLib and Bot API compatible File ID * Get TDLib and Bot API compatible File ID
* representing this thumbnail. * representing this thumbnail.
*
* > **Note:** You can't use this file id to send a thumbnail,
* > only to download it.
*/ */
get fileId(): string { get fileId(): string {
if (!this._fileId) { if (!this._fileId) {
if ( if (
this.raw._ !== 'photoSize' && this.raw._ !== 'photoSize' &&
this.raw._ !== 'photoSizeProgressive' this.raw._ !== 'photoSizeProgressive' &&
this.raw._ !== 'videoSize'
) { ) {
throw new MtArgumentError( throw new MtArgumentError(
`Cannot generate a file ID for "${this.raw.type}"` `Cannot generate a file ID for "${this.raw.type}"`
@ -177,9 +195,9 @@ export class Thumbnail extends FileLocation {
_: 'stickerSetThumbnailVersion', _: 'stickerSetThumbnailVersion',
id: this._media.id, id: this._media.id,
accessHash: this._media.accessHash, accessHash: this._media.accessHash,
version: this._media.thumbVersion! version: this._media.thumbVersion!,
} },
} },
}) })
} else { } else {
this._fileId = toFileId({ this._fileId = toFileId({
@ -195,10 +213,12 @@ export class Thumbnail extends FileLocation {
accessHash: this._media.accessHash, accessHash: this._media.accessHash,
source: { source: {
_: 'thumbnail', _: 'thumbnail',
fileType: this._media._ === 'photo' fileType:
? td.FileType.Photo this._media._ === 'photo'
: td.FileType.Thumbnail, ? td.FileType.Photo
thumbnailType: this.raw.type, : td.FileType.Thumbnail,
thumbnailType:
this.raw.type === 'u' ? '\x00' : this.raw.type,
}, },
}, },
}) })
@ -216,7 +236,8 @@ export class Thumbnail extends FileLocation {
if (!this._uniqueFileId) { if (!this._uniqueFileId) {
if ( if (
this.raw._ !== 'photoSize' && this.raw._ !== 'photoSize' &&
this.raw._ !== 'photoSizeProgressive' this.raw._ !== 'photoSizeProgressive' &&
this.raw._ !== 'videoSize'
) { ) {
throw new MtArgumentError( throw new MtArgumentError(
`Cannot generate a unique file ID for "${this.raw.type}"` `Cannot generate a unique file ID for "${this.raw.type}"`
@ -231,8 +252,8 @@ export class Thumbnail extends FileLocation {
_: 'stickerSetThumbnailVersion', _: 'stickerSetThumbnailVersion',
id: this._media.id, id: this._media.id,
accessHash: this._media.accessHash, accessHash: this._media.accessHash,
version: this._media.thumbVersion! version: this._media.thumbVersion!,
} },
}) })
} else { } else {
this._uniqueFileId = toUniqueFileId( this._uniqueFileId = toUniqueFileId(
@ -244,10 +265,11 @@ export class Thumbnail extends FileLocation {
id: this._media.id, id: this._media.id,
source: { source: {
_: 'thumbnail', _: 'thumbnail',
fileType: this._media._ === 'photo' fileType:
? td.FileType.Photo this._media._ === 'photo'
: td.FileType.Thumbnail, ? td.FileType.Photo
thumbnailType: this.raw.type, : td.FileType.Thumbnail,
thumbnailType: this.raw.type === 'u' ? '\x00' : this.raw.type,
}, },
} }
) )

View file

@ -440,8 +440,9 @@ export class Message {
/** /**
* Whether this is a premium media * Whether this is a premium media
* (e.g. >2gb file or fullscreen sticker) * (e.g. >2gb file or fullscreen sticker)
* that was forwarded without author by a non-premium user
*/ */
get isPremiumMedia(): boolean { get isForwardedPremiumMedia(): boolean {
return ( return (
this.raw._ === 'message' && this.raw._ === 'message' &&
this.raw.media?._ === 'messageMediaDocument' && this.raw.media?._ === 'messageMediaDocument' &&