feat(client): renamed inputMediaTl to inputMedia and added it to every kind of media

also fixed imports/exports
This commit is contained in:
teidesu 2021-05-08 17:53:59 +03:00
parent 002d949a13
commit cb01d8250e
16 changed files with 209 additions and 50 deletions

View file

@ -48,4 +48,4 @@ export class Audio extends RawDocument {
} }
} }
makeInspectable(Audio, ['fileSize', 'dcId']) makeInspectable(Audio, ['fileSize', 'dcId'], ['inputMedia'])

View file

@ -38,6 +38,18 @@ export class Contact {
get userId(): number { get userId(): number {
return this.obj.userId return this.obj.userId
} }
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}
*/
get inputMedia(): tl.TypeInputMedia {
return {
...this.obj,
_: 'inputMediaContact',
}
}
} }
makeInspectable(Contact) makeInspectable(Contact, undefined, ['inputMedia'])

View file

@ -159,6 +159,21 @@ export class Dice {
get value(): number { get value(): number {
return this.obj.value return this.obj.value
} }
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}
*
* Note that when you use this media, a new `value`
* will be generated!
*/
get inputMedia(): tl.TypeInputMedia {
return {
_: 'inputMediaDice',
emoticon: this.obj.emoticon
}
}
} }
makeInspectable(Dice) makeInspectable(Dice, undefined, ['inputMedia'])

View file

@ -111,29 +111,16 @@ export class RawDocument extends FileLocation {
/** /**
* Input media TL object generated from this object, * Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} * to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}
*/ */
get inputMediaTl(): tl.TypeInputMedia { get inputMedia(): tl.TypeInputMedia {
return { return {
_: 'inputMediaDocument', _: 'inputMediaDocument',
id: this.inputDocument id: this.inputDocument
} }
} }
/**
* Input media object generated from this object,
* to be used with {@link TelegramClient.sendMedia}
*/
get inputMedia(): InputMediaLike {
return {
// type is only really used for creating tl.InputMedia,
// but since we are providing it directly, we can use `auto`
type: 'auto',
file: this.inputMediaTl
// other fields are not needed since it's a forwarded media
}
}
protected _fileIdType(): td.FileType { protected _fileIdType(): td.FileType {
return td.FileType.Document return td.FileType.Document
} }
@ -185,4 +172,4 @@ export class RawDocument extends FileLocation {
*/ */
export class Document extends RawDocument {} export class Document extends RawDocument {}
makeInspectable(Document, ['fileSize', 'dcId']) makeInspectable(Document, ['fileSize', 'dcId'], ['inputMedia'])

View file

@ -79,6 +79,22 @@ export class Game {
return this._animation return this._animation
} }
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}
*/
get inputMedia(): tl.TypeInputMedia {
return {
_: 'inputMediaGame',
id: {
_: 'inputGameID',
id: this.game.id,
accessHash: this.game.accessHash
}
}
}
} }
makeInspectable(Game) makeInspectable(Game, undefined, ['inputMedia'])

View file

@ -2,6 +2,7 @@ import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { makeInspectable } from '../utils' import { makeInspectable } from '../utils'
import { WebDocument } from '../files/web-document' import { WebDocument } from '../files/web-document'
import { MtCuteArgumentError } from '../errors'
/** /**
* An invoice * An invoice
@ -90,6 +91,20 @@ export class Invoice {
get startParam(): string { get startParam(): string {
return this.raw.startParam return this.raw.startParam
} }
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}.
*
* Invoice can't provide an input media, since some
* of the data is not available to the user,
* which is required to send it. This getter
* is only provided to allow using `msg.media.inputMedia`
*/
get inputMedia(): tl.TypeInputMedia {
throw new MtCuteArgumentError('Invoice cannot provide an InputMedia')
}
} }
makeInspectable(Invoice) makeInspectable(Invoice, undefined, ['inputMedia'])

View file

@ -84,6 +84,23 @@ export class Location {
scale: params.scale ?? 1, scale: params.scale ?? 1,
}) })
} }
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}
*/
get inputMedia(): tl.TypeInputMedia {
return {
_: 'inputMediaGeoPoint',
geoPoint: {
_: 'inputGeoPoint',
lat: this.geo.lat,
long: this.geo.long,
accuracyRadius: this.geo.accuracyRadius
}
}
}
} }
export class LiveLocation extends Location { export class LiveLocation extends Location {
@ -108,8 +125,32 @@ export class LiveLocation extends Location {
return this.live.period return this.live.period
} }
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}
*
* Note that using this will result in an
* independent live geolocation, which
* will not be auto-updated with the current
*/
get inputMedia(): tl.TypeInputMedia {
return {
_: 'inputMediaGeoLive',
geoPoint: {
_: 'inputGeoPoint',
lat: this.geo.lat,
long: this.geo.long,
accuracyRadius: this.geo.accuracyRadius
},
heading: this.live.heading,
period: this.live.period,
proximityNotificationRadius: this.live.proximityNotificationRadius
}
}
// todo: api to subscribe for real-time updates // todo: api to subscribe for real-time updates
} }
makeInspectable(Location) makeInspectable(Location, undefined, ['inputMedia'])
makeInspectable(LiveLocation) makeInspectable(LiveLocation, undefined, ['inputMedia'])

View file

@ -109,10 +109,10 @@ export class Photo extends FileLocation {
/** /**
* Input media generated from this object, * Input media generated from this object,
* to be used in {@link TelegramClient.sendPhoto} * to be used in {@link InputMediaLike} and
* and {@link InputMediaLike} * {@link TelegramClient.sendMedia}
*/ */
get inputMediaTl(): tl.TypeInputMedia { get inputMedia(): tl.TypeInputMedia {
return { return {
_: 'inputMediaPhoto', _: 'inputMediaPhoto',
id: { id: {
@ -123,20 +123,6 @@ export class Photo extends FileLocation {
}, },
} }
} }
/**
* Input media object generated from this object,
* to be used with {@link TelegramClient.sendMedia}
*/
get inputMedia(): InputMediaLike {
return {
// type is only really used for creating tl.InputMedia,
// but since we are providing it directly, we can use `auto`
type: 'auto',
file: this.inputMediaTl,
// other fields are not needed since it's a forwarded media
}
}
} }
makeInspectable(Photo, ['fileSize', 'dcId', 'width', 'height']) makeInspectable(Photo, ['fileSize', 'dcId', 'width', 'height'], ['inputMedia'])

View file

@ -2,6 +2,7 @@ import { makeInspectable } from '../utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { MessageEntity } from '../messages' import { MessageEntity } from '../messages'
import bigInt from 'big-integer'
export namespace Poll { export namespace Poll {
export interface PollAnswer { export interface PollAnswer {
@ -180,6 +181,38 @@ export class Poll {
.getParseMode(parseMode) .getParseMode(parseMode)
.unparse(this.solution, this.solutionEntities!) .unparse(this.solution, this.solutionEntities!)
} }
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}
*
* A few notes:
* - Using this will result in an
* independent poll, which will not
* be auto-updated with the current.
* - If this is a quiz, a normal poll
* will be returned since the client does not
* know the correct answer.
* - This always returns a non-closed poll,
* even if the current poll was closed
*/
get inputMedia(): tl.TypeInputMedia {
return {
_: 'inputMediaPoll',
poll: {
_: 'poll',
closed: false,
id: bigInt.zero,
publicVoters: this.raw.publicVoters,
multipleChoice: this.raw.multipleChoice,
question: this.raw.question,
answers: this.raw.answers,
closePeriod: this.raw.closePeriod,
closeDate: this.raw.closeDate
},
}
}
} }
makeInspectable(Poll) makeInspectable(Poll, undefined, ['inputMedia'])

View file

@ -158,4 +158,4 @@ export class Sticker extends RawDocument {
} }
} }
makeInspectable(Sticker, ['fileSize', 'dcId']) makeInspectable(Sticker, ['fileSize', 'dcId'], ['inputMedia'])

View file

@ -3,6 +3,7 @@ import { Location } from './location'
import { assertTypeIs } from '../../utils/type-assertion' import { assertTypeIs } from '../../utils/type-assertion'
import { makeInspectable } from '../utils' import { makeInspectable } from '../utils'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import bigInt from 'big-integer'
export namespace Venue { export namespace Venue {
export interface VenueSource { export interface VenueSource {
@ -76,6 +77,38 @@ export class Venue {
type: this.raw.venueType, type: this.raw.venueType,
} }
} }
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}
*
* A few notes:
* - Using this will result in an
* independent poll, which will not
* be auto-updated with the current.
* - If this is a quiz, a normal poll
* will be returned since the client does not
* know the correct answer.
* - This always returns a non-closed poll,
* even if the current poll was closed
*/
get inputMedia(): tl.TypeInputMedia {
return {
_: 'inputMediaVenue',
geoPoint: {
_: 'inputGeoPoint',
lat: (this.raw.geo as tl.RawGeoPoint).lat,
long: (this.raw.geo as tl.RawGeoPoint).long,
accuracyRadius: (this.raw.geo as tl.RawGeoPoint).accuracyRadius,
},
title: this.raw.title,
address: this.raw.address,
provider: this.raw.provider,
venueId: this.raw.venueId,
venueType: this.raw.venueType
}
}
} }
makeInspectable(Venue) makeInspectable(Venue, undefined, ['inputMedia'])

View file

@ -88,4 +88,4 @@ export class Video extends RawDocument {
} }
} }
makeInspectable(Video, ['fileSize', 'dcId']) makeInspectable(Video, ['fileSize', 'dcId'], ['inputMedia'])

View file

@ -39,4 +39,4 @@ export class Voice extends RawDocument {
} }
} }
makeInspectable(Voice, ['fileSize', 'dcId']) makeInspectable(Voice, ['fileSize', 'dcId'], ['inputMedia'])

View file

@ -4,6 +4,7 @@ import { TelegramClient } from '../../client'
import { RawDocument } from './document' import { RawDocument } from './document'
import { parseDocument } from './document-utils' import { parseDocument } from './document-utils'
import { makeInspectable } from '../utils' import { makeInspectable } from '../utils'
import { MtCuteArgumentError } from '../errors'
/** /**
* Web page preview. * Web page preview.
@ -206,6 +207,19 @@ export class WebPage {
return this._document return this._document
} }
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} and
* {@link TelegramClient.sendMedia}.
*
* WebPage can't provide an input media, since some
* can only be auto-generated from a link. This getter
* is only provided to allow using `msg.media.inputMedia`
*/
get inputMedia(): tl.TypeInputMedia {
throw new MtCuteArgumentError('WebPage cannot provide an InputMedia')
}
} }
makeInspectable(WebPage) makeInspectable(WebPage, undefined, ['inputMedia'])

View file

@ -6,6 +6,7 @@ export * from './chat-preview'
export { InputChatPermissions } from './chat-permissions' export { InputChatPermissions } from './chat-permissions'
export * from './chat-member' export * from './chat-member'
export * from './chat-invite-link' export * from './chat-invite-link'
export * from './typing-status'
/** /**
* Peer types that have one-to-one relation to tl.Peer* types. * Peer types that have one-to-one relation to tl.Peer* types.

View file

@ -1,7 +1,13 @@
import { BasicPeerType, Chat, MtCuteUnsupportedError, TelegramClient, User } from '@mtcute/client' import {
BasicPeerType,
Chat,
MtCuteUnsupportedError,
TelegramClient,
User,
TypingStatus,
} from '@mtcute/client'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { getBarePeerId, MAX_CHANNEL_ID } from '@mtcute/core' import { getBarePeerId, MAX_CHANNEL_ID } from '@mtcute/core'
import { TypingStatus } from '@mtcute/client/src/types/peers/typing-status'
import { makeInspectable } from '@mtcute/client/src/types/utils' import { makeInspectable } from '@mtcute/client/src/types/utils'
/** /**