feat(client): readHistory method and related bound methods

This commit is contained in:
teidesu 2021-05-12 22:31:29 +03:00
parent 4f40571455
commit 8cf4ce5533
5 changed files with 84 additions and 1 deletions

View file

@ -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<void>
/**
* 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<void>
/**
* 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

View file

@ -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<void> {
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))
}
}

View file

@ -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))
}
}

View file

@ -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<void> {
return this.client.readHistory(this.chat.inputPeer, this.raw.id, clearMentions)
}
}
makeInspectable(Message, ['empty', 'isScheduled'], ['link'])

View file

@ -545,6 +545,16 @@ export class Chat {
async unarchive(): Promise<void> {
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<void> {
return this.client.readHistory(this.inputPeer, message, clearMentions)
}
}
makeInspectable(Chat, [], ['user'])