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> {
if (!this._thumbnails) {
this._thumbnails = this.raw.thumbs
? this.raw.thumbs.map(
(sz) => new Thumbnail(this.client, this.raw, sz)
)
: []
const arr: Thumbnail[] = []
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

View file

@ -76,6 +76,13 @@ export class Sticker extends RawDocument {
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.
*

View file

@ -31,7 +31,14 @@ export class Thumbnail extends FileLocation {
static readonly THUMB_STRIP = 'i'
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
@ -51,7 +58,7 @@ export class Thumbnail extends FileLocation {
constructor(
client: TelegramClient,
media: tl.RawPhoto | tl.RawDocument | tl.RawStickerSet,
sz: tl.TypePhotoSize
sz: tl.TypePhotoSize | tl.TypeVideoSize
) {
switch (sz._) {
case 'photoSizeEmpty':
@ -87,9 +94,9 @@ export class Thumbnail extends FileLocation {
stickerset: {
_: 'inputStickerSetID',
id: media.id,
accessHash: media.accessHash
accessHash: media.accessHash,
},
thumbVersion: media.thumbVersion!
thumbVersion: media.thumbVersion!,
}
} else {
location = {
@ -100,12 +107,15 @@ export class Thumbnail extends FileLocation {
id: media.id,
fileReference: media.fileReference,
accessHash: media.accessHash,
thumbSize: sz.type,
thumbSize: sz.type === 'u' ? '\x00' : sz.type,
}
}
width = sz.w
height = sz.h
size = sz._ === 'photoSize' ? sz.size : Math.max(...sz.sizes)
size =
sz._ === 'photoSizeProgressive'
? Math.max(...sz.sizes)
: sz.size
break
}
@ -125,6 +135,10 @@ export class Thumbnail extends FileLocation {
}
}
get isVideo(): boolean {
return this.raw._ === 'videoSize'
}
/**
* Thumbnail type
*/
@ -152,12 +166,16 @@ export class Thumbnail extends FileLocation {
/**
* Get TDLib and Bot API compatible File ID
* representing this thumbnail.
*
* > **Note:** You can't use this file id to send a thumbnail,
* > only to download it.
*/
get fileId(): string {
if (!this._fileId) {
if (
this.raw._ !== 'photoSize' &&
this.raw._ !== 'photoSizeProgressive'
this.raw._ !== 'photoSizeProgressive' &&
this.raw._ !== 'videoSize'
) {
throw new MtArgumentError(
`Cannot generate a file ID for "${this.raw.type}"`
@ -177,9 +195,9 @@ export class Thumbnail extends FileLocation {
_: 'stickerSetThumbnailVersion',
id: this._media.id,
accessHash: this._media.accessHash,
version: this._media.thumbVersion!
}
}
version: this._media.thumbVersion!,
},
},
})
} else {
this._fileId = toFileId({
@ -195,10 +213,12 @@ export class Thumbnail extends FileLocation {
accessHash: this._media.accessHash,
source: {
_: 'thumbnail',
fileType: this._media._ === 'photo'
? td.FileType.Photo
: td.FileType.Thumbnail,
thumbnailType: this.raw.type,
fileType:
this._media._ === 'photo'
? td.FileType.Photo
: 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.raw._ !== 'photoSize' &&
this.raw._ !== 'photoSizeProgressive'
this.raw._ !== 'photoSizeProgressive' &&
this.raw._ !== 'videoSize'
) {
throw new MtArgumentError(
`Cannot generate a unique file ID for "${this.raw.type}"`
@ -231,8 +252,8 @@ export class Thumbnail extends FileLocation {
_: 'stickerSetThumbnailVersion',
id: this._media.id,
accessHash: this._media.accessHash,
version: this._media.thumbVersion!
}
version: this._media.thumbVersion!,
},
})
} else {
this._uniqueFileId = toUniqueFileId(
@ -244,10 +265,11 @@ export class Thumbnail extends FileLocation {
id: this._media.id,
source: {
_: 'thumbnail',
fileType: this._media._ === 'photo'
? td.FileType.Photo
: td.FileType.Thumbnail,
thumbnailType: this.raw.type,
fileType:
this._media._ === 'photo'
? td.FileType.Photo
: 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
* (e.g. >2gb file or fullscreen sticker)
* that was forwarded without author by a non-premium user
*/
get isPremiumMedia(): boolean {
get isForwardedPremiumMedia(): boolean {
return (
this.raw._ === 'message' &&
this.raw.media?._ === 'messageMediaDocument' &&