diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 52ea6e5a..af611b25 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -68,6 +68,7 @@ import { sendMedia } from './methods/messages/send-media' import { sendPhoto } from './methods/messages/send-photo' import { sendText } from './methods/messages/send-text' import { unpinMessage } from './methods/messages/unpin-message' +import { initTakeoutSession } from './methods/misc/init-takeout-session' import { getParseMode, registerParseMode, @@ -104,6 +105,7 @@ import { PartialExcept, ReplyMarkup, SentCode, + TakeoutSession, TermsOfService, UploadFileLike, UploadedFile, @@ -1031,6 +1033,7 @@ export interface TelegramClient extends BaseTelegramClient { ): Promise /** * Forward one or more messages, optionally including a caption message. + * You can forward no more than 100 messages at once. * * If a caption message was sent, it will be the first message in the resulting array. * @@ -1630,6 +1633,14 @@ export interface TelegramClient extends BaseTelegramClient { * @param messageId Message ID */ unpinMessage(chatId: InputPeerLike, messageId: number): Promise + /** + * Create a new takeout session + * + * @param params Takeout session parameters + */ + initTakeoutSession( + params: Omit + ): Promise /** * Register a given {@link IMessageEntityParser} as a parse mode * for messages. When this method is first called, given parse @@ -1830,6 +1841,7 @@ export class TelegramClient extends BaseTelegramClient { sendPhoto = sendPhoto sendText = sendText unpinMessage = unpinMessage + initTakeoutSession = initTakeoutSession registerParseMode = registerParseMode unregisterParseMode = unregisterParseMode getParseMode = getParseMode diff --git a/packages/client/src/methods/_imports.ts b/packages/client/src/methods/_imports.ts index c5053ac3..6851d68b 100644 --- a/packages/client/src/methods/_imports.ts +++ b/packages/client/src/methods/_imports.ts @@ -25,7 +25,8 @@ import { FileDownloadParameters, Message, ReplyMarkup, - InputMediaLike + InputMediaLike, + TakeoutSession } from '../types' // @copy diff --git a/packages/client/src/methods/misc/init-takeout-session.ts b/packages/client/src/methods/misc/init-takeout-session.ts new file mode 100644 index 00000000..1491ff1a --- /dev/null +++ b/packages/client/src/methods/misc/init-takeout-session.ts @@ -0,0 +1,19 @@ +import { TelegramClient } from '../../client' +import { tl } from '@mtcute/tl' +import { TakeoutSession } from '../../types' + +/** + * Create a new takeout session + * + * @param params Takeout session parameters + * @internal + */ +export async function initTakeoutSession( + this: TelegramClient, + params: Omit +): Promise { + return new TakeoutSession(this, await this.call({ + _: 'account.initTakeoutSession', + ...params + })) +} diff --git a/packages/client/src/types/index.ts b/packages/client/src/types/index.ts index 6c1ebe2f..1ac9b038 100644 --- a/packages/client/src/types/index.ts +++ b/packages/client/src/types/index.ts @@ -4,6 +4,7 @@ export * from './files' export * from './media' export * from './messages' export * from './peers' +export * from './misc' export * from './errors' export { MaybeDynamic } from './utils' diff --git a/packages/client/src/types/misc/index.ts b/packages/client/src/types/misc/index.ts new file mode 100644 index 00000000..dab0e4f9 --- /dev/null +++ b/packages/client/src/types/misc/index.ts @@ -0,0 +1 @@ +export * from './takeout-session' diff --git a/packages/client/src/types/misc/takeout-session.ts b/packages/client/src/types/misc/takeout-session.ts new file mode 100644 index 00000000..481d9bd9 --- /dev/null +++ b/packages/client/src/types/misc/takeout-session.ts @@ -0,0 +1,94 @@ +import { tl } from 'packages/tl' +import { TelegramClient } from '../../client' +import { makeInspectable } from '../utils' + +/** + * Account takeout session + */ +export class TakeoutSession { + private client: TelegramClient + + /** + * Takeout session id + */ + readonly id: tl.Long + + constructor(client: TelegramClient, session: tl.account.RawTakeout) { + this.client = client + this.id = session.id + } + + /** + * Make an API call using this takeout session + * + * This method just wraps the query into `invokeWithTakeout` + * and passes the control down to {@link TelegramClient.call}. + * + * @param message RPC method to call + * @param params Additional call parameters + */ + async call( + message: T, + params?: { + throwFlood: boolean + } + ): Promise { + return this.client.call( + { + _: 'invokeWithTakeout', + takeoutId: this.id, + query: message, + }, + params + ) + } + + /** + * Create a proxy over {@link TelegramClient} + * that will use this takeout session to call methods. + * + * You can optionally provide a function to check if some + * RPC method should be called via a takeout session or directly, + * otherwise all methods are called through the takeout session. + * + * > **Note**: This will return a `Proxy` object that + * > overrides `call` method. Using this method requires + * > that your target environment supports `Proxy` and `Reflect` APIs + * + * @param predicate + * Function that given the RPC call should determine whether + * that call should be called via takeout session or not. + * Returning `true` will use takeout session, `false` will not. + */ + createProxy(predicate?: (obj: tl.TlObject) => boolean): TelegramClient { + const boundCall: TakeoutSession['call'] = predicate + ? (obj, params) => { + if (predicate(obj)) { + return this.call(obj, params) + } + return this.client.call(obj, params) + } + : this.call.bind(this) + + return new Proxy(this.client, { + get(target, prop, receiver) { + if (prop === 'call') return boundCall + return Reflect.get(target, prop, receiver) + }, + }) + } + + /** + * Finish account takeout session + * + * @param success Whether the data was successfully exported + */ + async finish(success = true): Promise { + await this.call({ + _: 'account.finishTakeoutSession', + success, + }) + } +} + +makeInspectable(TakeoutSession)