feat: updated to TL layer 129

This commit is contained in:
teidesu 2021-05-31 22:36:26 +03:00
parent d50a25eab9
commit 2daf497cc4
10 changed files with 167 additions and 56 deletions

View file

@ -57,28 +57,6 @@ export class FileLocation {
this.dcId = dcId 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 * 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 * in chunks of a given size. Order of the chunks is guaranteed to be

View file

@ -10,6 +10,7 @@ import { MtCuteArgumentError, MtCuteTypeAssertionError } from '../errors'
import { assertTypeIs } from '../../utils/type-assertion' import { assertTypeIs } from '../../utils/type-assertion'
import { makeInspectable } from '../utils' import { makeInspectable } from '../utils'
import { tdFileId as td, toFileId, toUniqueFileId } from '@mtcute/file-id' import { tdFileId as td, toFileId, toUniqueFileId } from '@mtcute/file-id'
import bigInt from 'big-integer'
/** /**
* One size of some thumbnail * One size of some thumbnail
@ -147,20 +148,28 @@ export class Thumbnail extends FileLocation {
*/ */
get fileId(): string { get fileId(): string {
if (!this._fileId) { if (!this._fileId) {
if (this.raw._ !== 'photoSize' && this.raw._ !== 'photoSizeProgressive') { if (
throw new MtCuteArgumentError(`Cannot generate a file ID for "${this.raw.type}"`) this.raw._ !== 'photoSize' &&
this.raw._ !== 'photoSizeProgressive'
) {
throw new MtCuteArgumentError(
`Cannot generate a file ID for "${this.raw.type}"`
)
} }
this._fileId = toFileId({ 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, dcId: this.dcId,
fileReference: this._media.fileReference, fileReference: this._media.fileReference,
location: { location: {
_: 'photo', _: 'photo',
id: this._media.id, id: this._media.id,
accessHash: this._media.accessHash, accessHash: this._media.accessHash,
volumeId: this.raw.location.volumeId, volumeId: bigInt.zero,
localId: this.raw.location.localId, localId: 0,
source: { source: {
_: 'thumbnail', _: 'thumbnail',
fileType: td.FileType.Photo, fileType: td.FileType.Photo,
@ -179,15 +188,24 @@ export class Thumbnail extends FileLocation {
*/ */
get uniqueFileId(): string { get uniqueFileId(): string {
if (!this._uniqueFileId) { if (!this._uniqueFileId) {
if (this.raw._ !== 'photoSize' && this.raw._ !== 'photoSizeProgressive') { if (
throw new MtCuteArgumentError(`Cannot generate a unique file ID for "${this.raw.type}"`) 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, { this._uniqueFileId = toUniqueFileId(
_: 'photo', this._media._ === 'photo'
volumeId: this.raw.location.volumeId, ? td.FileType.Photo
localId: this.raw.location.localId : td.FileType.Thumbnail,
}) {
_: 'common',
id: this._media.id,
}
)
} }
return this._uniqueFileId return this._uniqueFileId

View file

@ -1,7 +1,109 @@
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { FileLocation } from '../files/file-location' import { FileLocation } from '../files'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { makeInspectable } from '../utils' 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 * A chat photo
@ -11,48 +113,61 @@ export class ChatPhoto {
readonly obj: tl.RawUserProfilePhoto | tl.RawChatPhoto readonly obj: tl.RawUserProfilePhoto | tl.RawChatPhoto
readonly peer: tl.TypeInputPeer readonly peer: tl.TypeInputPeer
constructor( constructor (
client: TelegramClient, client: TelegramClient,
peer: tl.TypeInputPeer, peer: tl.TypeInputPeer,
obj: tl.RawUserProfilePhoto | tl.RawChatPhoto obj: tl.RawUserProfilePhoto | tl.RawChatPhoto,
) { ) {
this.client = client this.client = client
this.peer = peer this.peer = peer
this.obj = obj this.obj = obj
} }
private _smallFile?: FileLocation private _smallFile?: ChatPhotoSize
/** Chat photo file location in small resolution (160x160) */ /** Chat photo file location in small resolution (160x160) */
get small(): FileLocation { get small (): ChatPhotoSize {
if (!this._smallFile) { if (!this._smallFile) {
this._smallFile = FileLocation.fromDeprecated( this._smallFile = new ChatPhotoSize(
this.client, this.client,
this.peer, this.peer,
this.obj.photoSmall, this.obj,
this.obj.dcId false
) )
} }
return this._smallFile return this._smallFile
} }
private _bigFile?: FileLocation private _bigFile: ChatPhotoSize
/** Chat photo file location in big resolution (640x640) */ /** Chat photo file location in big resolution (640x640) */
get big(): FileLocation { get big (): ChatPhotoSize {
if (!this._bigFile) { if (!this._bigFile) {
this._bigFile = FileLocation.fromDeprecated( this._bigFile = new ChatPhotoSize(
this.client, this.client,
this.peer, this.peer,
this.obj.photoBig, this.obj,
this.obj.dcId,
true true
) )
} }
return this._bigFile 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) makeInspectable(ChatPhoto)

View file

@ -114,8 +114,7 @@ export function fileIdToInputFileLocation(
_: 'inputPeerPhotoFileLocation', _: 'inputPeerPhotoFileLocation',
big: loc.source.big, big: loc.source.big,
peer: dialogPhotoToInputPeer(loc.source), peer: dialogPhotoToInputPeer(loc.source),
volumeId: loc.volumeId, photoId: loc.id,
localId: loc.localId,
} }
case 'stickerSetThumbnail': case 'stickerSetThumbnail':
return { return {
@ -125,8 +124,7 @@ export function fileIdToInputFileLocation(
id: loc.source.id, id: loc.source.id,
accessHash: loc.source.accessHash, accessHash: loc.source.accessHash,
}, },
volumeId: loc.volumeId, thumbVersion: 0 // todo: check how tdlib stores this
localId: loc.localId,
} }
} }

View file

@ -71,6 +71,7 @@ function parsePhotoFileLocation(
reader: BinaryReader, reader: BinaryReader,
version: number version: number
): td.RawPhotoRemoteFileLocation { ): td.RawPhotoRemoteFileLocation {
// todo: check how tdlib handles volume ids
return { return {
_: 'photo', _: 'photo',
id: reader.long(), id: reader.long(),

View file

@ -43,6 +43,7 @@ export function toFileId(
writer.long(loc.accessHash) writer.long(loc.accessHash)
break break
case 'photo': case 'photo':
// todo: check how tdlib handles volume ids
writer.long(loc.id) writer.long(loc.id)
writer.long(loc.accessHash) writer.long(loc.accessHash)
writer.long(loc.volumeId) writer.long(loc.volumeId)

View file

@ -2,7 +2,7 @@
> TL schema and related utils used for MTCute. > 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 ## About

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -408,7 +408,7 @@ async function addDocumentation(obj) {
let onlyBotsCanUse = let onlyBotsCanUse =
botsCanUse && ( botsCanUse && (
!!target.description.match(/[,;]( for)? bots only$/) !!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 target.available = onlyBotsCanUse