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 readonly height: number
private _path?: string private _path?: string
private _media: tl.RawPhoto | tl.RawDocument private _media: tl.RawPhoto | tl.RawDocument | tl.RawStickerSet
constructor( constructor(
client: TelegramClient, client: TelegramClient,
media: tl.RawPhoto | tl.RawDocument, media: tl.RawPhoto | tl.RawDocument | tl.RawStickerSet,
sz: tl.TypePhotoSize sz: tl.TypePhotoSize
) { ) {
switch (sz._) { switch (sz._) {
@ -91,15 +91,27 @@ export class Thumbnail extends FileLocation {
size = Infinity // this doesn't really matter size = Infinity // this doesn't really matter
break break
default: default:
location = { if (media._ === 'stickerSet') {
_: location = {
media._ === 'photo' _: 'inputStickerSetThumb',
? 'inputPhotoFileLocation' stickerset: {
: 'inputDocumentFileLocation', _: 'inputStickerSetID',
id: media.id, id: media.id,
fileReference: media.fileReference, accessHash: media.accessHash
accessHash: media.accessHash, },
thumbSize: sz.type, thumbVersion: media.thumbVersion!
}
} else {
location = {
_:
media._ === 'photo'
? 'inputPhotoFileLocation'
: 'inputDocumentFileLocation',
id: media.id,
fileReference: media.fileReference,
accessHash: media.accessHash,
thumbSize: sz.type,
}
} }
width = sz.w width = sz.w
height = sz.h height = sz.h
@ -107,7 +119,12 @@ export class Thumbnail extends FileLocation {
break break
} }
super(client, location, size, media.dcId) super(
client,
location,
size,
media._ === 'stickerSet' ? media.thumbDcId : media.dcId
)
this.raw = sz this.raw = sz
this.width = width this.width = width
this.height = height this.height = height
@ -157,24 +174,45 @@ export class Thumbnail extends FileLocation {
) )
} }
this._fileId = toFileId({ if (this._media._ === 'stickerSet') {
type: this._fileId = toFileId({
this._media._ === 'photo' type: td.FileType.Thumbnail,
? td.FileType.Photo dcId: this.dcId,
: td.FileType.Thumbnail, fileReference: null,
dcId: this.dcId, location: {
fileReference: this._media.fileReference, _: 'photo',
location: { id: bigInt.zero,
_: 'photo', accessHash: bigInt.zero,
id: this._media.id, source: {
accessHash: this._media.accessHash, _: 'stickerSetThumbnailVersion',
source: { id: this._media.id,
_: 'thumbnail', accessHash: this._media.accessHash,
fileType: td.FileType.Photo, version: this._media.thumbVersion!
thumbnailType: this.raw.type, }
}
})
} 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 return this._fileId
@ -195,20 +233,35 @@ export class Thumbnail extends FileLocation {
) )
} }
this._uniqueFileId = toUniqueFileId( if (this._media._ === 'stickerSet') {
this._media._ === 'photo' this._uniqueFileId = toUniqueFileId(td.FileType.Thumbnail, {
? td.FileType.Photo
: td.FileType.Thumbnail,
{
_: 'photo', _: 'photo',
id: this._media.id, id: bigInt.zero,
source: { source: {
_: 'thumbnail', _: 'stickerSetThumbnailVersion',
fileType: td.FileType.Photo, id: this._media.id,
thumbnailType: this.raw.type, 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 return this._uniqueFileId

View file

@ -1,7 +1,7 @@
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { makeInspectable } from '../utils' import { makeInspectable } from '../utils'
import { Sticker } from '../media' import { Sticker, Thumbnail } from '../media'
import { MtCuteEmptyError, MtCuteTypeAssertionError } from '../errors' import { MtCuteEmptyError, MtCuteTypeAssertionError } from '../errors'
import { parseDocument } from '../media/document-utils' import { parseDocument } from '../media/document-utils'
import { InputFileLike } from '../files' import { InputFileLike } from '../files'
@ -175,6 +175,36 @@ export class StickerSet {
return this._stickers 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. * Find stickers given their emoji.
* *