feat: add readMessages function

This commit is contained in:
Adhiraj Singh
2022-04-06 09:30:32 +05:30
parent 6d4fc87169
commit 5aa64f2c39
3 changed files with 44 additions and 10 deletions

View File

@@ -526,6 +526,7 @@ export const getDevice = (id: string) => {
return deviceType
}
/** Upserts a receipt in the message */
export const updateMessageWithReceipt = (msg: WAMessage, receipt: MessageUserReceipt) => {
msg.userReceipt = msg.userReceipt || []
const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid)
@@ -536,6 +537,27 @@ export const updateMessageWithReceipt = (msg: WAMessage, receipt: MessageUserRec
}
}
/** Given a list of message keys, aggregates them by chat & sender. Useful for sending read receipts in bulk */
export const aggregateMessageKeysNotFromMe = (keys: proto.IMessageKey[]) => {
const keyMap: { [id: string]: { jid: string, participant: string | undefined, messageIds: string[] } } = { }
for(const { remoteJid, id, participant, fromMe } of keys) {
if(!fromMe) {
const uqKey = `${remoteJid}:${participant || ''}`
if(!keyMap[uqKey]) {
keyMap[uqKey] = {
jid: remoteJid,
participant,
messageIds: []
}
}
keyMap[uqKey].messageIds.push(id)
}
}
return Object.values(keyMap)
}
/**
* Downloads the given message. Throws an error if it's not a media message
*/