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,6 +91,17 @@ export class Thumbnail extends FileLocation {
size = Infinity // this doesn't really matter
break
default:
if (media._ === 'stickerSet') {
location = {
_: 'inputStickerSetThumb',
stickerset: {
_: 'inputStickerSetID',
id: media.id,
accessHash: media.accessHash
},
thumbVersion: media.thumbVersion!
}
} else {
location = {
_:
media._ === 'photo'
@ -101,13 +112,19 @@ export class Thumbnail extends FileLocation {
accessHash: media.accessHash,
thumbSize: sz.type,
}
}
width = sz.w
height = sz.h
size = sz._ === 'photoSize' ? sz.size : Math.max(...sz.sizes)
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,6 +174,24 @@ export class Thumbnail extends FileLocation {
)
}
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'
@ -170,12 +205,15 @@ export class Thumbnail extends FileLocation {
accessHash: this._media.accessHash,
source: {
_: 'thumbnail',
fileType: td.FileType.Photo,
fileType: this._media._ === 'photo'
? td.FileType.Photo
: td.FileType.Thumbnail,
thumbnailType: this.raw.type,
},
},
})
}
}
return this._fileId
}
@ -195,6 +233,18 @@ export class Thumbnail extends FileLocation {
)
}
if (this._media._ === 'stickerSet') {
this._uniqueFileId = toUniqueFileId(td.FileType.Thumbnail, {
_: 'photo',
id: bigInt.zero,
source: {
_: 'stickerSetThumbnailVersion',
id: this._media.id,
accessHash: this._media.accessHash,
version: this._media.thumbVersion!
}
})
} else {
this._uniqueFileId = toUniqueFileId(
this._media._ === 'photo'
? td.FileType.Photo
@ -204,12 +254,15 @@ export class Thumbnail extends FileLocation {
id: this._media.id,
source: {
_: 'thumbnail',
fileType: td.FileType.Photo,
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.
*