diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index c4546f97..d8b50d83 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -87,6 +87,7 @@ import { getMessages } from './methods/messages/get-messages' import { iterHistory } from './methods/messages/iter-history' import { _parseEntities } from './methods/messages/parse-entities' import { pinMessage } from './methods/messages/pin-message' +import { readHistory } from './methods/messages/read-history' import { searchGlobal } from './methods/messages/search-global' import { searchMessages } from './methods/messages/search-messages' import { sendCopy } from './methods/messages/send-copy' @@ -1922,6 +1923,18 @@ export interface TelegramClient extends BaseTelegramClient { notify?: boolean, bothSides?: boolean ): Promise + /** + * Mark chat history as read. + * + * @param chatId Chat ID + * @param message (default: `0`) Message up until which to read history (by default everything is read) + * @param clearMentions (default: `false`) Whether to also clear all mentions in the chat + */ + readHistory( + chatId: InputPeerLike, + message?: number, + clearMentions?: boolean + ): Promise /** * Search for messages globally from all of your chats * @@ -2936,6 +2949,7 @@ export class TelegramClient extends BaseTelegramClient { iterHistory = iterHistory protected _parseEntities = _parseEntities pinMessage = pinMessage + readHistory = readHistory searchGlobal = searchGlobal searchMessages = searchMessages sendCopy = sendCopy diff --git a/packages/client/src/methods/messages/read-history.ts b/packages/client/src/methods/messages/read-history.ts new file mode 100644 index 00000000..8992bbf5 --- /dev/null +++ b/packages/client/src/methods/messages/read-history.ts @@ -0,0 +1,50 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike } from '../../types' +import { isInputPeerChannel, normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' +import { createDummyUpdate } from '../../utils/updates-utils' + +/** + * Mark chat history as read. + * + * @param chatId Chat ID + * @param message Message up until which to read history (by default everything is read) + * @param clearMentions Whether to also clear all mentions in the chat + * @internal + */ +export async function readHistory( + this: TelegramClient, + chatId: InputPeerLike, + message = 0, + clearMentions = false +): Promise { + const peer = normalizeToInputPeer(await this.resolvePeer(chatId)) + + if (clearMentions) { + const res = await this.call({ + _: 'messages.readMentions', + peer + }) + + if (isInputPeerChannel(peer)) { + this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount, peer.channelId)) + } else { + this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount)) + } + } + + if (isInputPeerChannel(peer)) { + await this.call({ + _: 'channels.readHistory', + channel: normalizeToInputChannel(peer), + maxId: message + }) + } else { + const res = await this.call({ + _: 'messages.readHistory', + peer, + maxId: message + }) + this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount)) + } + +} diff --git a/packages/client/src/methods/messages/unpin-all-messages.ts b/packages/client/src/methods/messages/unpin-all-messages.ts index 16066620..dee6771e 100644 --- a/packages/client/src/methods/messages/unpin-all-messages.ts +++ b/packages/client/src/methods/messages/unpin-all-messages.ts @@ -23,6 +23,6 @@ export async function unpinAllMessages( if (isInputPeerChannel(peer)) { this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount, peer.channelId)) } else { - createDummyUpdate(res.pts, res.ptsCount) + this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount)) } } diff --git a/packages/client/src/types/messages/message.ts b/packages/client/src/types/messages/message.ts index 3e81d1c6..49360f5b 100644 --- a/packages/client/src/types/messages/message.ts +++ b/packages/client/src/types/messages/message.ts @@ -687,6 +687,15 @@ export class Message { return this.client.getMessageGroup(this.chat.inputPeer, this.raw.id) } + + /** + * Read history in the chat up until this message + * + * @param clearMentions Whether to also clear mentions + */ + async read(clearMentions = false): Promise { + return this.client.readHistory(this.chat.inputPeer, this.raw.id, clearMentions) + } } makeInspectable(Message, ['empty', 'isScheduled'], ['link']) diff --git a/packages/client/src/types/peers/chat.ts b/packages/client/src/types/peers/chat.ts index 9c05c3a1..f83a7934 100644 --- a/packages/client/src/types/peers/chat.ts +++ b/packages/client/src/types/peers/chat.ts @@ -545,6 +545,16 @@ export class Chat { async unarchive(): Promise { return this.client.unarchiveChats(this.inputPeer) } + + /** + * Read history in this chat + * + * @param message Message up until which to read history (by default everything is read) + * @param clearMentions Whether to also clear all mentions in the chat + */ + async readHistory(message = 0, clearMentions = false): Promise { + return this.client.readHistory(this.inputPeer, message, clearMentions) + } } makeInspectable(Chat, [], ['user'])