feat: updated to TL layer 129
This commit is contained in:
parent
d50a25eab9
commit
2daf497cc4
10 changed files with 167 additions and 56 deletions
|
@ -57,28 +57,6 @@ export class FileLocation {
|
|||
this.dcId = dcId
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
static fromDeprecated(
|
||||
client: TelegramClient,
|
||||
peer: tl.TypeInputPeer,
|
||||
loc: tl.RawFileLocationToBeDeprecated,
|
||||
dcId?: number,
|
||||
big?: boolean
|
||||
): FileLocation {
|
||||
return new FileLocation(
|
||||
client,
|
||||
{
|
||||
_: 'inputPeerPhotoFileLocation',
|
||||
peer,
|
||||
volumeId: loc.volumeId,
|
||||
localId: loc.localId,
|
||||
big,
|
||||
},
|
||||
undefined,
|
||||
dcId
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a file and return it as an iterable, which yields file contents
|
||||
* in chunks of a given size. Order of the chunks is guaranteed to be
|
||||
|
|
|
@ -10,6 +10,7 @@ import { MtCuteArgumentError, MtCuteTypeAssertionError } from '../errors'
|
|||
import { assertTypeIs } from '../../utils/type-assertion'
|
||||
import { makeInspectable } from '../utils'
|
||||
import { tdFileId as td, toFileId, toUniqueFileId } from '@mtcute/file-id'
|
||||
import bigInt from 'big-integer'
|
||||
|
||||
/**
|
||||
* One size of some thumbnail
|
||||
|
@ -147,20 +148,28 @@ export class Thumbnail extends FileLocation {
|
|||
*/
|
||||
get fileId(): string {
|
||||
if (!this._fileId) {
|
||||
if (this.raw._ !== 'photoSize' && this.raw._ !== 'photoSizeProgressive') {
|
||||
throw new MtCuteArgumentError(`Cannot generate a file ID for "${this.raw.type}"`)
|
||||
if (
|
||||
this.raw._ !== 'photoSize' &&
|
||||
this.raw._ !== 'photoSizeProgressive'
|
||||
) {
|
||||
throw new MtCuteArgumentError(
|
||||
`Cannot generate a file ID for "${this.raw.type}"`
|
||||
)
|
||||
}
|
||||
|
||||
this._fileId = toFileId({
|
||||
type: this._media._ === 'photo' ? td.FileType.Photo : td.FileType.Thumbnail,
|
||||
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,
|
||||
volumeId: this.raw.location.volumeId,
|
||||
localId: this.raw.location.localId,
|
||||
volumeId: bigInt.zero,
|
||||
localId: 0,
|
||||
source: {
|
||||
_: 'thumbnail',
|
||||
fileType: td.FileType.Photo,
|
||||
|
@ -179,15 +188,24 @@ export class Thumbnail extends FileLocation {
|
|||
*/
|
||||
get uniqueFileId(): string {
|
||||
if (!this._uniqueFileId) {
|
||||
if (this.raw._ !== 'photoSize' && this.raw._ !== 'photoSizeProgressive') {
|
||||
throw new MtCuteArgumentError(`Cannot generate a unique file ID for "${this.raw.type}"`)
|
||||
if (
|
||||
this.raw._ !== 'photoSize' &&
|
||||
this.raw._ !== 'photoSizeProgressive'
|
||||
) {
|
||||
throw new MtCuteArgumentError(
|
||||
`Cannot generate a unique file ID for "${this.raw.type}"`
|
||||
)
|
||||
}
|
||||
|
||||
this._uniqueFileId = toUniqueFileId(td.FileType.Photo, {
|
||||
_: 'photo',
|
||||
volumeId: this.raw.location.volumeId,
|
||||
localId: this.raw.location.localId
|
||||
})
|
||||
this._uniqueFileId = toUniqueFileId(
|
||||
this._media._ === 'photo'
|
||||
? td.FileType.Photo
|
||||
: td.FileType.Thumbnail,
|
||||
{
|
||||
_: 'common',
|
||||
id: this._media.id,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
return this._uniqueFileId
|
||||
|
|
|
@ -1,7 +1,109 @@
|
|||
import { tl } from '@mtcute/tl'
|
||||
import { FileLocation } from '../files/file-location'
|
||||
import { FileLocation } from '../files'
|
||||
import { TelegramClient } from '../../client'
|
||||
import { makeInspectable } from '../utils'
|
||||
import { strippedPhotoToJpg } from '../../utils/file-utils'
|
||||
import { tdFileId, toFileId, toUniqueFileId } from '@mtcute/file-id'
|
||||
import bigInt from 'big-integer'
|
||||
import { MAX_CHANNEL_ID } from '@mtcute/core'
|
||||
import { MtCuteArgumentError } from '../errors'
|
||||
|
||||
/**
|
||||
* A size of a chat photo
|
||||
*/
|
||||
export class ChatPhotoSize extends FileLocation {
|
||||
readonly obj: tl.RawUserProfilePhoto | tl.RawChatPhoto
|
||||
readonly peer: tl.TypeInputPeer
|
||||
readonly big: boolean
|
||||
|
||||
constructor (
|
||||
client: TelegramClient,
|
||||
peer: tl.TypeInputPeer,
|
||||
obj: tl.RawUserProfilePhoto | tl.RawChatPhoto,
|
||||
big: boolean,
|
||||
) {
|
||||
super(client, {
|
||||
_: 'inputPeerPhotoFileLocation',
|
||||
peer,
|
||||
photoId: obj.photoId,
|
||||
big,
|
||||
}, undefined, obj.dcId)
|
||||
|
||||
this.peer = peer
|
||||
this.obj = obj
|
||||
this.big = big
|
||||
}
|
||||
|
||||
private _fileId?: string
|
||||
|
||||
/**
|
||||
* TDLib and Bot API compatible File ID representing this size
|
||||
*/
|
||||
get fileId (): string {
|
||||
if (!this._fileId) {
|
||||
const peer = this.peer
|
||||
|
||||
let id: number
|
||||
let hash: tl.Long
|
||||
switch (peer._) {
|
||||
case 'inputPeerUser':
|
||||
id = peer.userId
|
||||
hash = peer.accessHash
|
||||
break
|
||||
case 'inputPeerChat':
|
||||
id = -peer.chatId
|
||||
hash = bigInt.zero
|
||||
break
|
||||
case 'inputPeerChannel':
|
||||
id = MAX_CHANNEL_ID - peer.channelId
|
||||
hash = peer.accessHash
|
||||
break
|
||||
default:
|
||||
// should not happen
|
||||
throw new MtCuteArgumentError('Input peer was invalid')
|
||||
}
|
||||
|
||||
this._fileId = toFileId({
|
||||
dcId: this.obj.dcId,
|
||||
type: tdFileId.FileType.ProfilePhoto,
|
||||
fileReference: null,
|
||||
location: {
|
||||
_: 'photo',
|
||||
id: this.obj.photoId,
|
||||
accessHash: bigInt.zero,
|
||||
volumeId: bigInt.zero,
|
||||
localId: 0,
|
||||
source: {
|
||||
_: 'dialogPhoto',
|
||||
big: this.big,
|
||||
id: bigInt(id),
|
||||
accessHash: hash
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return this._fileId
|
||||
}
|
||||
|
||||
private _uniqueFileId?: string
|
||||
/**
|
||||
* TDLib and Bot API compatible unique File ID representing this size
|
||||
*/
|
||||
get uniqueFileId(): string {
|
||||
if (!this._uniqueFileId) {
|
||||
// todo: check how tdlib handles big/small photos here
|
||||
this._uniqueFileId = toUniqueFileId(tdFileId.FileType.ProfilePhoto, {
|
||||
_: 'common',
|
||||
id: this.obj.photoId
|
||||
})
|
||||
}
|
||||
|
||||
return this._uniqueFileId
|
||||
}
|
||||
}
|
||||
|
||||
makeInspectable(ChatPhotoSize, ['dcId', 'big'])
|
||||
|
||||
/**
|
||||
* A chat photo
|
||||
|
@ -11,48 +113,61 @@ export class ChatPhoto {
|
|||
readonly obj: tl.RawUserProfilePhoto | tl.RawChatPhoto
|
||||
readonly peer: tl.TypeInputPeer
|
||||
|
||||
constructor(
|
||||
constructor (
|
||||
client: TelegramClient,
|
||||
peer: tl.TypeInputPeer,
|
||||
obj: tl.RawUserProfilePhoto | tl.RawChatPhoto
|
||||
obj: tl.RawUserProfilePhoto | tl.RawChatPhoto,
|
||||
) {
|
||||
this.client = client
|
||||
this.peer = peer
|
||||
this.obj = obj
|
||||
}
|
||||
|
||||
private _smallFile?: FileLocation
|
||||
private _smallFile?: ChatPhotoSize
|
||||
|
||||
/** Chat photo file location in small resolution (160x160) */
|
||||
get small(): FileLocation {
|
||||
get small (): ChatPhotoSize {
|
||||
if (!this._smallFile) {
|
||||
this._smallFile = FileLocation.fromDeprecated(
|
||||
this._smallFile = new ChatPhotoSize(
|
||||
this.client,
|
||||
this.peer,
|
||||
this.obj.photoSmall,
|
||||
this.obj.dcId
|
||||
this.obj,
|
||||
false
|
||||
)
|
||||
}
|
||||
|
||||
return this._smallFile
|
||||
}
|
||||
|
||||
private _bigFile?: FileLocation
|
||||
|
||||
private _bigFile: ChatPhotoSize
|
||||
/** Chat photo file location in big resolution (640x640) */
|
||||
get big(): FileLocation {
|
||||
get big (): ChatPhotoSize {
|
||||
if (!this._bigFile) {
|
||||
this._bigFile = FileLocation.fromDeprecated(
|
||||
this._bigFile = new ChatPhotoSize(
|
||||
this.client,
|
||||
this.peer,
|
||||
this.obj.photoBig,
|
||||
this.obj.dcId,
|
||||
this.obj,
|
||||
true
|
||||
)
|
||||
}
|
||||
|
||||
return this._bigFile
|
||||
}
|
||||
|
||||
private _thumb?: Buffer
|
||||
|
||||
/**
|
||||
* Chat photo preview in *very* small resolution, if available
|
||||
*/
|
||||
get thumb (): Buffer | null {
|
||||
if (!this.obj.strippedThumb) return null
|
||||
|
||||
if (!this._thumb) {
|
||||
this._thumb = strippedPhotoToJpg(this.obj.strippedThumb)
|
||||
}
|
||||
|
||||
return this._thumb
|
||||
}
|
||||
}
|
||||
|
||||
makeInspectable(ChatPhoto)
|
||||
|
|
|
@ -114,8 +114,7 @@ export function fileIdToInputFileLocation(
|
|||
_: 'inputPeerPhotoFileLocation',
|
||||
big: loc.source.big,
|
||||
peer: dialogPhotoToInputPeer(loc.source),
|
||||
volumeId: loc.volumeId,
|
||||
localId: loc.localId,
|
||||
photoId: loc.id,
|
||||
}
|
||||
case 'stickerSetThumbnail':
|
||||
return {
|
||||
|
@ -125,8 +124,7 @@ export function fileIdToInputFileLocation(
|
|||
id: loc.source.id,
|
||||
accessHash: loc.source.accessHash,
|
||||
},
|
||||
volumeId: loc.volumeId,
|
||||
localId: loc.localId,
|
||||
thumbVersion: 0 // todo: check how tdlib stores this
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -71,6 +71,7 @@ function parsePhotoFileLocation(
|
|||
reader: BinaryReader,
|
||||
version: number
|
||||
): td.RawPhotoRemoteFileLocation {
|
||||
// todo: check how tdlib handles volume ids
|
||||
return {
|
||||
_: 'photo',
|
||||
id: reader.long(),
|
||||
|
|
|
@ -43,6 +43,7 @@ export function toFileId(
|
|||
writer.long(loc.accessHash)
|
||||
break
|
||||
case 'photo':
|
||||
// todo: check how tdlib handles volume ids
|
||||
writer.long(loc.id)
|
||||
writer.long(loc.accessHash)
|
||||
writer.long(loc.volumeId)
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
> TL schema and related utils used for MTCute.
|
||||
|
||||
Generated from TL layer **126** (last updated on 11.04.2021).
|
||||
Generated from TL layer **129** (last updated on 31.05.2021).
|
||||
|
||||
## About
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -408,7 +408,7 @@ async function addDocumentation(obj) {
|
|||
let onlyBotsCanUse =
|
||||
botsCanUse && (
|
||||
!!target.description.match(/[,;]( for)? bots only$/)
|
||||
|| target.throws.some((it) => it.code === 'USER_BOT_REQUIRED')
|
||||
|| (target.throws && target.throws.some((it) => it.code === 'USER_BOT_REQUIRED'))
|
||||
)
|
||||
|
||||
target.available = onlyBotsCanUse
|
||||
|
|
Loading…
Reference in a new issue