From 8a0c9984b50e278ee451a53630b9dbb1c6b91af2 Mon Sep 17 00:00:00 2001 From: teidesu Date: Sat, 8 May 2021 13:19:37 +0300 Subject: [PATCH] feat(client): closePoll method --- packages/client/src/client.ts | 12 ++++ .../methods/files/normalize-input-media.ts | 1 + .../client/src/methods/messages/close-poll.ts | 64 +++++++++++++++++++ .../client/src/types/media/input-media.ts | 6 ++ 4 files changed, 83 insertions(+) create mode 100644 packages/client/src/methods/messages/close-poll.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index fae976c5..447121d1 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -60,6 +60,7 @@ import { _normalizeFileToDocument } from './methods/files/normalize-file-to-docu import { _normalizeInputFile } from './methods/files/normalize-input-file' import { _normalizeInputMedia } from './methods/files/normalize-input-media' import { uploadFile } from './methods/files/upload-file' +import { closePoll } from './methods/messages/close-poll' import { deleteMessages } from './methods/messages/delete-messages' import { editInlineMessage } from './methods/messages/edit-inline-message' import { editMessage } from './methods/messages/edit-message' @@ -1178,6 +1179,16 @@ export interface TelegramClient extends BaseTelegramClient { */ progressCallback?: (uploaded: number, total: number) => void }): Promise + /** + * Close a poll sent by you. + * + * Once closed, poll can't be re-opened, and nobody + * will be able to vote in it + * + * @param chatId Chat ID where this poll was found + * @param message Message ID where this poll was found + */ + closePoll(chatId: InputPeerLike, message: number): Promise /** * Delete messages, including service messages. * @@ -2249,6 +2260,7 @@ export class TelegramClient extends BaseTelegramClient { protected _normalizeInputFile = _normalizeInputFile protected _normalizeInputMedia = _normalizeInputMedia uploadFile = uploadFile + closePoll = closePoll deleteMessages = deleteMessages editInlineMessage = editInlineMessage editMessage = editMessage diff --git a/packages/client/src/methods/files/normalize-input-media.ts b/packages/client/src/methods/files/normalize-input-media.ts index 750f520e..7d82f73d 100644 --- a/packages/client/src/methods/files/normalize-input-media.ts +++ b/packages/client/src/methods/files/normalize-input-media.ts @@ -178,6 +178,7 @@ export async function _normalizeInputMedia( _: 'inputMediaPoll', poll: { _: 'poll', + closed: media.closed, id: bigInt.zero, publicVoters: media.public, multipleChoice: media.multiple, diff --git a/packages/client/src/methods/messages/close-poll.ts b/packages/client/src/methods/messages/close-poll.ts new file mode 100644 index 00000000..a866bda5 --- /dev/null +++ b/packages/client/src/methods/messages/close-poll.ts @@ -0,0 +1,64 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike, MtCuteTypeAssertionError, Poll } from '../../types' +import { tl } from '@mtcute/tl' +import { + createUsersChatsIndex, + normalizeToInputPeer, +} from '../../utils/peer-utils' +import bigInt from 'big-integer' +import { assertTypeIs } from '../../utils/type-assertion' + +/** + * Close a poll sent by you. + * + * Once closed, poll can't be re-opened, and nobody + * will be able to vote in it + * + * @param chatId Chat ID where this poll was found + * @param message Message ID where this poll was found + * @internal + */ +export async function closePoll( + this: TelegramClient, + chatId: InputPeerLike, + message: number +): Promise { + const res = await this.call({ + _: 'messages.editMessage', + peer: normalizeToInputPeer(await this.resolvePeer(chatId)), + id: message, + media: { + _: 'inputMediaPoll', + poll: { + _: 'poll', + id: bigInt.zero, + closed: true, + question: '', + answers: [], + }, + }, + }) + + if (!(res._ === 'updates' || res._ === 'updatesCombined')) + throw new MtCuteTypeAssertionError( + '_findMessageInUpdate', + 'updates | updatesCombined', + res._ + ) + + this._handleUpdate(res, true) + + const upd = res.updates[0] + assertTypeIs('closePoll (@ messages.editMessage)', upd, 'updateMessagePoll') + if (!upd.poll) { + throw new MtCuteTypeAssertionError( + 'closePoll (@ messages.editMessage)', + 'poll', + 'undefined' + ) + } + + const { users } = createUsersChatsIndex(res) + + return new Poll(this, upd.poll, users, upd.results) +} diff --git a/packages/client/src/types/media/input-media.ts b/packages/client/src/types/media/input-media.ts index 3d0d4d92..ec0276e6 100644 --- a/packages/client/src/types/media/input-media.ts +++ b/packages/client/src/types/media/input-media.ts @@ -449,6 +449,12 @@ export interface InputMediaPoll { */ answers: (string | tl.TypePollAnswer)[] + /** + * Whether this is poll is closed + * (i.e. nobody can vote anymore) + */ + closed?: boolean + /** * Whether this is a public poll * (i.e. users who have voted are visible to everyone)