feat(client): stickerset thumbnails

also fixed a few issues with file ids for thumbnails
This commit is contained in:
teidesu 2021-07-05 16:06:49 +03:00
parent 0d32731b7a
commit be506f5ed7
2 changed files with 125 additions and 42 deletions

View file

@ -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

View file

@ -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<Thumbnail> {
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.
*