From cc8c974e23e203259d0bcee370e9e686682b241b Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Sun, 25 Jul 2021 14:00:51 +0300 Subject: [PATCH] feat(client): getDiscussionMessage method --- packages/client/src/client.ts | 27 +++++++++++- .../messages/get-discussion-message.ts | 43 ++++++++++++++++++- packages/client/src/types/messages/message.ts | 10 +++++ 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index b9c646ba..9eeaf1fd 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -98,7 +98,10 @@ import { editInlineMessage } from './methods/messages/edit-inline-message' import { editMessage } from './methods/messages/edit-message' import { _findMessageInUpdate } from './methods/messages/find-in-update' import { forwardMessages } from './methods/messages/forward-messages' -import { _getDiscussionMessage } from './methods/messages/get-discussion-message' +import { + _getDiscussionMessage, + getDiscussionMessage, +} from './methods/messages/get-discussion-message' import { getHistory } from './methods/messages/get-history' import { getMessageGroup } from './methods/messages/get-message-group' import { getMessagesUnsafe } from './methods/messages/get-messages-unsafe' @@ -680,7 +683,7 @@ export interface TelegramClient extends BaseTelegramClient { * Whether the results should be displayed as a gallery instead * of a vertical list. Only applicable to some media types. * - * Defaults to `false` + * Defaults to `true` */ gallery?: boolean @@ -2201,6 +2204,25 @@ export interface TelegramClient extends BaseTelegramClient { clearDraft?: boolean } ): Promise> + // public version of the same method because why not + /** + * Get discussion message for some channel post. + * + * Returns `null` if the post does not have a discussion + * message. + * + * This method might throw `FLOOD_WAIT_X` error in case + * the discussion message was not *yet* created. Error + * is usually handled by the client, but if you disabled that, + * you'll need to handle it manually. + * + * @param peer Channel where the post was found + * @param message ID of the channel post + */ + getDiscussionMessage( + peer: InputPeerLike, + message: number + ): Promise /** * Retrieve a chunk of the chat history. * @@ -3588,6 +3610,7 @@ export class TelegramClient extends BaseTelegramClient { protected _findMessageInUpdate = _findMessageInUpdate forwardMessages = forwardMessages protected _getDiscussionMessage = _getDiscussionMessage + getDiscussionMessage = getDiscussionMessage getHistory = getHistory getMessageGroup = getMessageGroup getMessagesUnsafe = getMessagesUnsafe diff --git a/packages/client/src/methods/messages/get-discussion-message.ts b/packages/client/src/methods/messages/get-discussion-message.ts index bb82ad12..bc141edf 100644 --- a/packages/client/src/methods/messages/get-discussion-message.ts +++ b/packages/client/src/methods/messages/get-discussion-message.ts @@ -1,6 +1,7 @@ import { TelegramClient } from '../../client' -import { InputPeerLike } from '../../types' +import { InputPeerLike, Message } from '../../types' import { tl } from '@mtcute/tl' +import { createUsersChatsIndex } from '../../utils/peer-utils' /** @internal */ export async function _getDiscussionMessage( @@ -34,3 +35,43 @@ export async function _getDiscussionMessage( msg.id, ] } + +// public version of the same method because why not + +/** + * Get discussion message for some channel post. + * + * Returns `null` if the post does not have a discussion + * message. + * + * This method might throw `FLOOD_WAIT_X` error in case + * the discussion message was not *yet* created. Error + * is usually handled by the client, but if you disabled that, + * you'll need to handle it manually. + * + * @param peer Channel where the post was found + * @param message ID of the channel post + * @internal + */ +export async function getDiscussionMessage( + this: TelegramClient, + peer: InputPeerLike, + message: number +): Promise { + const inputPeer = await this.resolvePeer(peer) + + const res = await this.call({ + _: 'messages.getDiscussionMessage', + peer: inputPeer, + msgId: message, + }) + + if (!res.messages.length || res.messages[0]._ === 'messageEmpty') + // no discussion message (i guess?), return the same msg + return null + + const msg = res.messages[0] + const { users, chats } = createUsersChatsIndex(res) + + return new Message(this, msg, users, chats) +} diff --git a/packages/client/src/types/messages/message.ts b/packages/client/src/types/messages/message.ts index aa5742d6..071fec8e 100644 --- a/packages/client/src/types/messages/message.ts +++ b/packages/client/src/types/messages/message.ts @@ -870,6 +870,16 @@ export class Message { return this.client.getMessageGroup(this.chat.inputPeer, this.raw.id) } + /** + * Get discussion message for some channel post. + * + * Returns `null` if the post does not have a discussion + * message. + */ + async getDiscussionMessage(): Promise { + return this.client.getDiscussionMessage(this.chat.inputPeer, this.raw.id) + } + /** * Read history in the chat up until this message *