mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
chore: add ability to specify ISyncActionMessageRange directly
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { BaileysEventMap, Chat, ChatModification, Contact, LegacySocketConfig, PresenceData, WABusinessProfile, WAFlag, WAMessageKey, WAMessageUpdate, WAMetric, WAPresence } from '../Types'
|
||||
import { BaileysEventMap, Chat, ChatModification, Contact, LastMessageList, LegacySocketConfig, PresenceData, WABusinessProfile, WAFlag, WAMessageKey, WAMessageUpdate, WAMetric, WAPresence } from '../Types'
|
||||
import { debouncedTimeout, unixTimestampSeconds } from '../Utils/generics'
|
||||
import { BinaryNode, jidNormalizedUser } from '../WABinary'
|
||||
import makeAuthSocket from './auth'
|
||||
@@ -309,6 +309,14 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
||||
|
||||
timestampNow = timestampNow || unixTimestampSeconds()
|
||||
|
||||
const getIndexKey = (list: LastMessageList) => {
|
||||
if(Array.isArray(list)) {
|
||||
return list[list.length - 1].key
|
||||
}
|
||||
|
||||
return list.messages?.[list.messages?.length - 1]?.key
|
||||
}
|
||||
|
||||
if('archive' in modification) {
|
||||
chatAttrs.type = modification.archive ? 'archive' : 'unarchive'
|
||||
} else if('pin' in modification) {
|
||||
@@ -345,14 +353,14 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
||||
}
|
||||
))
|
||||
} else if('markRead' in modification) {
|
||||
const indexKey = modification.lastMessages[modification.lastMessages.length - 1].key
|
||||
const indexKey = getIndexKey(modification.lastMessages)
|
||||
return chatRead(indexKey, modification.markRead ? 0 : -1)
|
||||
} else if('delete' in modification) {
|
||||
chatAttrs.type = 'delete'
|
||||
}
|
||||
|
||||
if('lastMessages' in modification) {
|
||||
const indexKey = modification.lastMessages[modification.lastMessages.length - 1].key
|
||||
const indexKey = getIndexKey(modification.lastMessages)
|
||||
if(indexKey) {
|
||||
chatAttrs.index = indexKey.id
|
||||
chatAttrs.owner = indexKey.fromMe ? 'true' : 'false'
|
||||
|
||||
@@ -36,7 +36,7 @@ export type Chat = Omit<proto.IConversation, 'messages'> & {
|
||||
* the last messages in a chat, sorted reverse-chronologically. That is, the latest message should be first in the chat
|
||||
* for MD modifications, the last message in the array (i.e. the earlist message) must be the last message recv in the chat
|
||||
* */
|
||||
export type LastMessageList = Pick<proto.IWebMessageInfo, 'key' | 'messageTimestamp'>[]
|
||||
export type LastMessageList = Pick<proto.IWebMessageInfo, 'key' | 'messageTimestamp'>[] | proto.ISyncActionMessageRange
|
||||
|
||||
export type ChatModification =
|
||||
{
|
||||
|
||||
@@ -447,32 +447,38 @@ export const chatModificationToAppPatch = (
|
||||
) => {
|
||||
const OP = proto.SyncdMutation.SyncdMutationSyncdOperation
|
||||
const getMessageRange = (lastMessages: LastMessageList) => {
|
||||
const lastMsg = lastMessages[lastMessages.length - 1]
|
||||
const messageRange: proto.ISyncActionMessageRange = {
|
||||
lastMessageTimestamp: lastMsg?.messageTimestamp,
|
||||
messages: lastMessages?.length ? lastMessages.map(
|
||||
m => {
|
||||
if(!m.key?.id || !m.key?.remoteJid) {
|
||||
throw new Boom('Incomplete key', { statusCode: 400, data: m })
|
||||
}
|
||||
let messageRange: proto.ISyncActionMessageRange
|
||||
if(Array.isArray(lastMessages)) {
|
||||
const lastMsg = lastMessages[lastMessages.length - 1]
|
||||
messageRange = {
|
||||
lastMessageTimestamp: lastMsg?.messageTimestamp,
|
||||
messages: lastMessages?.length ? lastMessages.map(
|
||||
m => {
|
||||
if(!m.key?.id || !m.key?.remoteJid) {
|
||||
throw new Boom('Incomplete key', { statusCode: 400, data: m })
|
||||
}
|
||||
|
||||
if(isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
|
||||
throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
|
||||
}
|
||||
if(isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
|
||||
throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
|
||||
}
|
||||
|
||||
if(!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
|
||||
throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
|
||||
}
|
||||
if(!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
|
||||
throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
|
||||
}
|
||||
|
||||
if(m.key.participant) {
|
||||
m.key = { ...m.key }
|
||||
m.key.participant = jidNormalizedUser(m.key.participant)
|
||||
}
|
||||
if(m.key.participant) {
|
||||
m.key = { ...m.key }
|
||||
m.key.participant = jidNormalizedUser(m.key.participant)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
) : undefined
|
||||
return m
|
||||
}
|
||||
) : undefined
|
||||
}
|
||||
} else {
|
||||
messageRange = lastMessages
|
||||
}
|
||||
|
||||
return messageRange
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user