From 3fcdd03877fc41115cbc6eef2869ac3f81acd69e Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Sat, 10 Jul 2021 16:12:31 +0300 Subject: [PATCH] feat(dispatcher): lastMessage and lastReceivedMessage getters for Conversation --- packages/dispatcher/src/conversation.ts | 43 +++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/packages/dispatcher/src/conversation.ts b/packages/dispatcher/src/conversation.ts index 22858f05..0e3e1d9e 100644 --- a/packages/dispatcher/src/conversation.ts +++ b/packages/dispatcher/src/conversation.ts @@ -106,14 +106,25 @@ interface QueuedHandler { timeout?: NodeJS.Timeout } +/** + * Represents a conversation inside some chat. + * + * A conversation keeps track of any messages sent or edited + * since it was started and until it was stopped, + * and allows waiting for events in it. + * + * If you need a conversation across multiple chats, + * you should use multiple {@link Conversation} objects + * and synchronize them manually. + */ export class Conversation { private _inputPeer: tl.TypeInputPeer private _chatId: number private _client: TelegramClient private _started = false - private _lastMessage?: number - private _lastReceivedMessage?: number + private _lastMessage: number + private _lastReceivedMessage: number private _queuedNewMessage = new Queue>() private _pendingNewMessages = new Queue() @@ -144,6 +155,31 @@ export class Conversation { return this._inputPeer } + /** + * ID of the very last message in this conversation. + */ + get lastMessage(): number { + if (!this._started) { + throw new MtCuteArgumentError("Conversation hasn't started yet") + } + + return this._lastMessage + } + + /** + * ID of the last incoming message in this conversation. + * + * Note that before any messages were received since the {@link start} + * of the conversation, this will equal to {@link lastMessage} + */ + get lastReceivedMessage(): number { + if (!this._started) { + throw new MtCuteArgumentError("Conversation hasn't started yet") + } + + return this._lastReceivedMessage + } + /** * Start the conversation */ @@ -162,6 +198,9 @@ export class Conversation { this._inputPeer = await client.resolvePeer(this.chat) this._chatId = getMarkedPeerId(this._inputPeer) + const dialog = await client.getPeerDialogs(this._inputPeer) + this._lastMessage = this._lastReceivedMessage = dialog.lastMessage.id + this.dispatcher.on('new_message', this._onNewMessage) this.dispatcher.on('edit_message', this._onEditMessage) this.dispatcher.on('history_read', this._onHistoryRead)