mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Added function to change read status for chat, updated connect
This commit is contained in:
@@ -41,16 +41,49 @@ export class WAConnection extends Base {
|
||||
return info
|
||||
}
|
||||
/**
|
||||
* Read/unread messages of a chat; will mark the entire chat read by default
|
||||
* Marks a chat as read/unread; updates the chat object too
|
||||
* @param jid the ID of the person/group whose message you want to mark read
|
||||
* @param unread unreads the chat, if true
|
||||
*/
|
||||
async chatRead (jid: string, type: 'unread' | 'read' = 'read') {
|
||||
jid = whatsappID (jid)
|
||||
const chat = this.assertChatGet (jid)
|
||||
|
||||
if (type === 'unread') await this.sendReadReceipt (jid, null, -2)
|
||||
else if (chat.count !== 0) {
|
||||
let messageID: string
|
||||
|
||||
let messages: WAMessage[]
|
||||
let cursor: any
|
||||
|
||||
messages = chat.messages
|
||||
cursor = messages[messages.length-1]?.key
|
||||
|
||||
do {
|
||||
const m = messages.reverse().find (m => !m.key.fromMe)
|
||||
if (m) messageID = m.key.id
|
||||
|
||||
const obj = await this.loadMessages (jid, 10, cursor)
|
||||
messages = obj.messages
|
||||
cursor = obj.cursor
|
||||
|
||||
if (messages.length === 0) throw new BaileysError ('no valid message found to read', { status: 404 })
|
||||
} while (!messageID)
|
||||
|
||||
await this.sendReadReceipt (jid, messageID, Math.abs(chat.count))
|
||||
}
|
||||
|
||||
chat.count = type === 'unread' ? -1 : 0
|
||||
this.emit ('chat-update', {jid, count: chat.count})
|
||||
}
|
||||
/**
|
||||
* Sends a read receipt for a given message;
|
||||
* does not update the chat do @see chatRead
|
||||
* @param jid the ID of the person/group whose message you want to mark read
|
||||
* @param messageID optionally, the message ID
|
||||
* @param count number of messages to read, set to < 0 to unread a message
|
||||
*/
|
||||
async sendReadReceipt(jid: string, messageID?: string, count?: number) {
|
||||
jid = whatsappID (jid)
|
||||
const chat = this.chats.get(jid)
|
||||
count = count || Math.abs(chat?.count || 1)
|
||||
|
||||
async sendReadReceipt(jid: string, messageID: string, count: number) {
|
||||
const attributes = {
|
||||
jid: jid,
|
||||
count: count.toString(),
|
||||
@@ -58,10 +91,6 @@ export class WAConnection extends Base {
|
||||
owner: messageID ? 'false' : null
|
||||
}
|
||||
const read = await this.setQuery ([['read', attributes, null]])
|
||||
if (chat) {
|
||||
chat.count = count > 0 ? Math.max(chat.count-count, 0) : -1
|
||||
this.emit ('chat-update', {jid, count: chat.count})
|
||||
}
|
||||
return read
|
||||
}
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user