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 { debouncedTimeout, unixTimestampSeconds } from '../Utils/generics'
|
||||||
import { BinaryNode, jidNormalizedUser } from '../WABinary'
|
import { BinaryNode, jidNormalizedUser } from '../WABinary'
|
||||||
import makeAuthSocket from './auth'
|
import makeAuthSocket from './auth'
|
||||||
@@ -309,6 +309,14 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
|||||||
|
|
||||||
timestampNow = timestampNow || unixTimestampSeconds()
|
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) {
|
if('archive' in modification) {
|
||||||
chatAttrs.type = modification.archive ? 'archive' : 'unarchive'
|
chatAttrs.type = modification.archive ? 'archive' : 'unarchive'
|
||||||
} else if('pin' in modification) {
|
} else if('pin' in modification) {
|
||||||
@@ -345,14 +353,14 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
|||||||
}
|
}
|
||||||
))
|
))
|
||||||
} else if('markRead' in modification) {
|
} 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)
|
return chatRead(indexKey, modification.markRead ? 0 : -1)
|
||||||
} else if('delete' in modification) {
|
} else if('delete' in modification) {
|
||||||
chatAttrs.type = 'delete'
|
chatAttrs.type = 'delete'
|
||||||
}
|
}
|
||||||
|
|
||||||
if('lastMessages' in modification) {
|
if('lastMessages' in modification) {
|
||||||
const indexKey = modification.lastMessages[modification.lastMessages.length - 1].key
|
const indexKey = getIndexKey(modification.lastMessages)
|
||||||
if(indexKey) {
|
if(indexKey) {
|
||||||
chatAttrs.index = indexKey.id
|
chatAttrs.index = indexKey.id
|
||||||
chatAttrs.owner = indexKey.fromMe ? 'true' : 'false'
|
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
|
* 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
|
* 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 =
|
export type ChatModification =
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -447,32 +447,38 @@ export const chatModificationToAppPatch = (
|
|||||||
) => {
|
) => {
|
||||||
const OP = proto.SyncdMutation.SyncdMutationSyncdOperation
|
const OP = proto.SyncdMutation.SyncdMutationSyncdOperation
|
||||||
const getMessageRange = (lastMessages: LastMessageList) => {
|
const getMessageRange = (lastMessages: LastMessageList) => {
|
||||||
const lastMsg = lastMessages[lastMessages.length - 1]
|
let messageRange: proto.ISyncActionMessageRange
|
||||||
const messageRange: proto.ISyncActionMessageRange = {
|
if(Array.isArray(lastMessages)) {
|
||||||
lastMessageTimestamp: lastMsg?.messageTimestamp,
|
const lastMsg = lastMessages[lastMessages.length - 1]
|
||||||
messages: lastMessages?.length ? lastMessages.map(
|
messageRange = {
|
||||||
m => {
|
lastMessageTimestamp: lastMsg?.messageTimestamp,
|
||||||
if(!m.key?.id || !m.key?.remoteJid) {
|
messages: lastMessages?.length ? lastMessages.map(
|
||||||
throw new Boom('Incomplete key', { statusCode: 400, data: m })
|
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) {
|
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 })
|
throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
|
if(!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
|
||||||
throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
|
throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
|
||||||
}
|
}
|
||||||
|
|
||||||
if(m.key.participant) {
|
if(m.key.participant) {
|
||||||
m.key = { ...m.key }
|
m.key = { ...m.key }
|
||||||
m.key.participant = jidNormalizedUser(m.key.participant)
|
m.key.participant = jidNormalizedUser(m.key.participant)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
) : undefined
|
) : undefined
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
messageRange = lastMessages
|
||||||
}
|
}
|
||||||
|
|
||||||
return messageRange
|
return messageRange
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user