diff --git a/src/LegacySocket/chats.ts b/src/LegacySocket/chats.ts index 3500b2b..b516d2f 100644 --- a/src/LegacySocket/chats.ts +++ b/src/LegacySocket/chats.ts @@ -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' diff --git a/src/Types/Chat.ts b/src/Types/Chat.ts index 8bd0fe5..c8fd072 100644 --- a/src/Types/Chat.ts +++ b/src/Types/Chat.ts @@ -36,7 +36,7 @@ export type Chat = Omit & { * 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[] +export type LastMessageList = Pick[] | proto.ISyncActionMessageRange export type ChatModification = { diff --git a/src/Utils/chat-utils.ts b/src/Utils/chat-utils.ts index f6aaf71..5989290 100644 --- a/src/Utils/chat-utils.ts +++ b/src/Utils/chat-utils.ts @@ -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 }