mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
refactor: update chat modifications to be uniform across MD + legacy
This commit is contained in:
@@ -179,7 +179,7 @@ export type BaileysEventMap = {
|
||||
/** auth credentials updated -- some pre key state, device ID etc. */
|
||||
'creds.update': Partial<AuthenticationCreds>
|
||||
/** set chats (history sync), messages are reverse chronologically sorted */
|
||||
'chats.set': { chats: Chat[], messages: WAMessage[] }
|
||||
'chats.set': { chats: Chat[], messages: WAMessage[], contacts: Contact[] }
|
||||
/** upsert chats */
|
||||
'chats.upsert': Chat[]
|
||||
/** update the given chats */
|
||||
@@ -495,7 +495,7 @@ WA uses an encrypted form of communication to send chat/app updates. This has be
|
||||
- Archive a chat
|
||||
``` ts
|
||||
const lastMsgInChat = await getLastMessageInChat('123456@s.whatsapp.net') // implement this on your end
|
||||
await sock.chatModify({ archive: true }, '123456@s.whatsapp.net', [lastMsgInChat])
|
||||
await sock.chatModify({ archive: true, lastMessages: [lastMsgInChat] }, '123456@s.whatsapp.net')
|
||||
```
|
||||
- Mute/unmute a chat
|
||||
``` ts
|
||||
@@ -508,7 +508,7 @@ WA uses an encrypted form of communication to send chat/app updates. This has be
|
||||
``` ts
|
||||
const lastMsgInChat = await getLastMessageInChat('123456@s.whatsapp.net') // implement this on your end
|
||||
// mark it unread
|
||||
await sock.chatModify({ markRead: false }, '123456@s.whatsapp.net', [lastMsgInChat])
|
||||
await sock.chatModify({ markRead: false, lastMessages: [lastMsgInChat] }, '123456@s.whatsapp.net')
|
||||
```
|
||||
|
||||
- Delete message for me
|
||||
|
||||
@@ -294,24 +294,30 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
||||
* Modify a given chat (archive, pin etc.)
|
||||
* @param jid the ID of the person/group you are modifiying
|
||||
*/
|
||||
chatModify: async( modification: ChatModification, jid: string, chatInfo: Pick<Chat, 'mute' | 'pin'>, index?: WAMessageKey) => {
|
||||
chatModify: async(modification: ChatModification, jid: string, chatInfo: Pick<Chat, 'mute' | 'pin'>, timestampNow?: number) => {
|
||||
let chatAttrs: BinaryNode['attrs'] = { jid: jid }
|
||||
let data: BinaryNode[] | undefined = undefined
|
||||
const stamp = unixTimestampSeconds()
|
||||
|
||||
timestampNow = timestampNow || unixTimestampSeconds()
|
||||
|
||||
if('archive' in modification) {
|
||||
chatAttrs.type = modification.archive ? 'archive' : 'unarchive'
|
||||
const indexKey = modification.lastMessages[modification.lastMessages.length-1].key
|
||||
if(indexKey) {
|
||||
chatAttrs.index = indexKey.id
|
||||
chatAttrs.owner = indexKey.fromMe ? 'true' : 'false'
|
||||
}
|
||||
} else if('pin' in modification) {
|
||||
chatAttrs.type = 'pin'
|
||||
if(modification.pin) {
|
||||
chatAttrs.pin = stamp.toString()
|
||||
chatAttrs.pin = timestampNow.toString()
|
||||
} else {
|
||||
chatAttrs.previous = chatInfo.pin!.toString()
|
||||
}
|
||||
} else if('mute' in modification) {
|
||||
chatAttrs.type = 'mute'
|
||||
if(modification.mute) {
|
||||
chatAttrs.mute = (stamp + modification.mute).toString()
|
||||
chatAttrs.mute = (timestampNow + modification.mute).toString()
|
||||
} else {
|
||||
chatAttrs.previous = chatInfo.mute!.toString()
|
||||
}
|
||||
@@ -335,12 +341,8 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
||||
}
|
||||
))
|
||||
} else if('markRead' in modification) {
|
||||
return chatRead(index!, modification.markRead ? 0 : -1)
|
||||
}
|
||||
|
||||
if(index) {
|
||||
chatAttrs.index = index.id
|
||||
chatAttrs.owner = index.fromMe ? 'true' : 'false'
|
||||
const indexKey = modification.lastMessages[modification.lastMessages.length-1].key
|
||||
return chatRead(indexKey, modification.markRead ? 0 : -1)
|
||||
}
|
||||
|
||||
const node = { tag: 'chat', attrs: chatAttrs, content: data }
|
||||
|
||||
@@ -541,8 +541,8 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
* lastMessages must be sorted in reverse chronologically
|
||||
* requires the last messages till the last message received; required for archive & unread
|
||||
*/
|
||||
const chatModify = (mod: ChatModification, jid: string, lastMessages: Pick<proto.IWebMessageInfo, 'key' | 'messageTimestamp'>[]) => {
|
||||
const patch = chatModificationToAppPatch(mod, jid, lastMessages)
|
||||
const chatModify = (mod: ChatModification, jid: string) => {
|
||||
const patch = chatModificationToAppPatch(mod, jid)
|
||||
return appPatch(patch)
|
||||
}
|
||||
|
||||
|
||||
@@ -27,9 +27,17 @@ export type Chat = Omit<proto.IConversation, 'messages'> & {
|
||||
pin?: number | null
|
||||
archive?: boolean
|
||||
}
|
||||
/**
|
||||
* the last messages in a chat, sorted reverse-chronologically
|
||||
* for MD modifications, the last message in the array must be the last message recv in the chat
|
||||
* */
|
||||
export type LastMessageList = Pick<proto.IWebMessageInfo, 'key' | 'messageTimestamp'>[]
|
||||
|
||||
export type ChatModification =
|
||||
{ archive: boolean } |
|
||||
{
|
||||
archive: boolean
|
||||
lastMessages: LastMessageList
|
||||
} |
|
||||
{
|
||||
pin: boolean
|
||||
} |
|
||||
@@ -48,5 +56,6 @@ export type ChatModification =
|
||||
} |
|
||||
{
|
||||
markRead: boolean
|
||||
lastMessages: LastMessageList
|
||||
} |
|
||||
{ delete: true }
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { aesDecrypt, hmacSign, aesEncrypt, hkdf } from "./crypto"
|
||||
import { WAPatchCreate, ChatMutation, WAPatchName, LTHashState, ChatModification, SignalKeyStore } from "../Types"
|
||||
import { WAPatchCreate, ChatMutation, WAPatchName, LTHashState, ChatModification, LastMessageList } from "../Types"
|
||||
import { proto } from '../../WAProto'
|
||||
import { LT_HASH_ANTI_TAMPERING } from './lt-hash'
|
||||
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren } from '../WABinary'
|
||||
@@ -418,11 +418,10 @@ export const decodePatches = async(
|
||||
|
||||
export const chatModificationToAppPatch = (
|
||||
mod: ChatModification,
|
||||
jid: string,
|
||||
lastMessages: Pick<proto.IWebMessageInfo, 'key' | 'messageTimestamp'>[]
|
||||
jid: string
|
||||
) => {
|
||||
const OP = proto.SyncdMutation.SyncdMutationSyncdOperation
|
||||
const getMessageRange = () => {
|
||||
const getMessageRange = (lastMessages: LastMessageList) => {
|
||||
if(!lastMessages?.length) {
|
||||
throw new Boom('Expected last message to be not from me', { statusCode: 400 })
|
||||
}
|
||||
@@ -455,7 +454,7 @@ export const chatModificationToAppPatch = (
|
||||
syncAction: {
|
||||
archiveChatAction: {
|
||||
archived: !!mod.archive,
|
||||
messageRange: getMessageRange()
|
||||
messageRange: getMessageRange(mod.lastMessages)
|
||||
}
|
||||
},
|
||||
index: ['archive', jid],
|
||||
@@ -468,7 +467,7 @@ export const chatModificationToAppPatch = (
|
||||
syncAction: {
|
||||
markChatAsReadAction: {
|
||||
read: mod.markRead,
|
||||
messageRange: getMessageRange()
|
||||
messageRange: getMessageRange(mod.lastMessages)
|
||||
}
|
||||
},
|
||||
index: ['markChatAsRead', jid],
|
||||
|
||||
Reference in New Issue
Block a user