From be506f5ed72fcc48ac2f031f596f39b29b15ff7a Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Mon, 5 Jul 2021 16:06:49 +0300 Subject: [PATCH] feat(client): stickerset thumbnails also fixed a few issues with file ids for thumbnails --- packages/client/src/types/media/thumbnail.ts | 135 ++++++++++++------ packages/client/src/types/misc/sticker-set.ts | 32 ++++- 2 files changed, 125 insertions(+), 42 deletions(-) diff --git a/packages/client/src/types/media/thumbnail.ts b/packages/client/src/types/media/thumbnail.ts index 06068a8e..8796a4b7 100644 --- a/packages/client/src/types/media/thumbnail.ts +++ b/packages/client/src/types/media/thumbnail.ts @@ -56,11 +56,11 @@ export class Thumbnail extends FileLocation { readonly height: number private _path?: string - private _media: tl.RawPhoto | tl.RawDocument + private _media: tl.RawPhoto | tl.RawDocument | tl.RawStickerSet constructor( client: TelegramClient, - media: tl.RawPhoto | tl.RawDocument, + media: tl.RawPhoto | tl.RawDocument | tl.RawStickerSet, sz: tl.TypePhotoSize ) { switch (sz._) { @@ -91,15 +91,27 @@ export class Thumbnail extends FileLocation { size = Infinity // this doesn't really matter break default: - location = { - _: - media._ === 'photo' - ? 'inputPhotoFileLocation' - : 'inputDocumentFileLocation', - id: media.id, - fileReference: media.fileReference, - accessHash: media.accessHash, - thumbSize: sz.type, + if (media._ === 'stickerSet') { + location = { + _: 'inputStickerSetThumb', + stickerset: { + _: 'inputStickerSetID', + id: media.id, + accessHash: media.accessHash + }, + thumbVersion: media.thumbVersion! + } + } else { + location = { + _: + media._ === 'photo' + ? 'inputPhotoFileLocation' + : 'inputDocumentFileLocation', + id: media.id, + fileReference: media.fileReference, + accessHash: media.accessHash, + thumbSize: sz.type, + } } width = sz.w height = sz.h @@ -107,7 +119,12 @@ export class Thumbnail extends FileLocation { break } - super(client, location, size, media.dcId) + super( + client, + location, + size, + media._ === 'stickerSet' ? media.thumbDcId : media.dcId + ) this.raw = sz this.width = width this.height = height @@ -157,24 +174,45 @@ export class Thumbnail extends FileLocation { ) } - this._fileId = toFileId({ - type: - this._media._ === 'photo' - ? td.FileType.Photo - : td.FileType.Thumbnail, - dcId: this.dcId, - fileReference: this._media.fileReference, - location: { - _: 'photo', - id: this._media.id, - accessHash: this._media.accessHash, - source: { - _: 'thumbnail', - fileType: td.FileType.Photo, - thumbnailType: this.raw.type, + if (this._media._ === 'stickerSet') { + this._fileId = toFileId({ + type: td.FileType.Thumbnail, + dcId: this.dcId, + fileReference: null, + location: { + _: 'photo', + id: bigInt.zero, + accessHash: bigInt.zero, + source: { + _: 'stickerSetThumbnailVersion', + id: this._media.id, + accessHash: this._media.accessHash, + version: this._media.thumbVersion! + } + } + }) + } else { + this._fileId = toFileId({ + type: + this._media._ === 'photo' + ? td.FileType.Photo + : td.FileType.Thumbnail, + dcId: this.dcId, + fileReference: this._media.fileReference, + location: { + _: 'photo', + id: this._media.id, + accessHash: this._media.accessHash, + source: { + _: 'thumbnail', + fileType: this._media._ === 'photo' + ? td.FileType.Photo + : td.FileType.Thumbnail, + thumbnailType: this.raw.type, + }, }, - }, - }) + }) + } } return this._fileId @@ -195,20 +233,35 @@ export class Thumbnail extends FileLocation { ) } - this._uniqueFileId = toUniqueFileId( - this._media._ === 'photo' - ? td.FileType.Photo - : td.FileType.Thumbnail, - { + if (this._media._ === 'stickerSet') { + this._uniqueFileId = toUniqueFileId(td.FileType.Thumbnail, { _: 'photo', - id: this._media.id, + id: bigInt.zero, source: { - _: 'thumbnail', - fileType: td.FileType.Photo, - thumbnailType: this.raw.type, - }, - } - ) + _: 'stickerSetThumbnailVersion', + id: this._media.id, + accessHash: this._media.accessHash, + version: this._media.thumbVersion! + } + }) + } else { + this._uniqueFileId = toUniqueFileId( + this._media._ === 'photo' + ? td.FileType.Photo + : td.FileType.Thumbnail, + { + _: 'photo', + id: this._media.id, + source: { + _: 'thumbnail', + fileType: this._media._ === 'photo' + ? td.FileType.Photo + : td.FileType.Thumbnail, + thumbnailType: this.raw.type, + }, + } + ) + } } return this._uniqueFileId diff --git a/packages/client/src/types/misc/sticker-set.ts b/packages/client/src/types/misc/sticker-set.ts index 8cec7fb7..2daa6ab1 100644 --- a/packages/client/src/types/misc/sticker-set.ts +++ b/packages/client/src/types/misc/sticker-set.ts @@ -1,7 +1,7 @@ import { TelegramClient } from '../../client' import { tl } from '@mtcute/tl' import { makeInspectable } from '../utils' -import { Sticker } from '../media' +import { Sticker, Thumbnail } from '../media' import { MtCuteEmptyError, MtCuteTypeAssertionError } from '../errors' import { parseDocument } from '../media/document-utils' import { InputFileLike } from '../files' @@ -175,6 +175,36 @@ export class StickerSet { return this._stickers } + private _thumbnails?: Thumbnail[] + /** + * Available stickerset thumbnails. + * + * Returns empty array if not available + * (i.e. first sticker should be used as thumbnail) + */ + get thumbnails(): ReadonlyArray { + if (!this._thumbnails) { + this._thumbnails = this.brief.thumbs?.map( + (sz) => new Thumbnail(this.client, this.brief, sz) + ) ?? [] + } + + return this._thumbnails + } + + /** + * Get a stickerset thumbnail by its type. + * + * Thumbnail types are described in the + * [Telegram docs](https://core.telegram.org/api/files#image-thumbnail-types), + * and are also available as static members of {@link Thumbnail} for convenience. + * + * @param type Thumbnail type + */ + getThumbnail(type: string): Thumbnail | null { + return this.thumbnails.find((it) => it.raw.type === type) ?? null + } + /** * Find stickers given their emoji. *