move appstatesynckey to keys

This commit is contained in:
Adhiraj Singh
2021-09-26 12:35:56 +05:30
parent a46a07fe45
commit 23101f87a1
8 changed files with 56 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
import { decodeSyncdPatch, encodeSyncdPatch } from "../Utils/chat-utils";
import { SocketConfig, WAPresence, PresenceData, Chat, ChatModification, WAMediaUpload } from "../Types";
import { SocketConfig, WAPresence, PresenceData, Chat, ChatModification, WAMediaUpload, ChatMutation } from "../Types";
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, jidNormalizedUser, S_WHATSAPP_NET } from "../WABinary";
import { makeSocket } from "./socket";
import { proto } from '../../WAProto'
@@ -278,7 +278,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
}
}
const processSyncActions = (actions: { action: proto.ISyncActionValue, index: [string, string] }[]) => {
const processSyncActions = (actions: ChatMutation[]) => {
const updates: Partial<Chat>[] = []
for(const { action, index: [_, id] } of actions) {
const update: Partial<Chat> = { id }
@@ -309,7 +309,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
jid: string,
modification: ChatModification
) => {
const patch = encodeSyncdPatch(modification, { remoteJid: jid }, authState)
const patch = await encodeSyncdPatch(modification, { remoteJid: jid }, authState)
const type = 'regular_high'
const ver = authState.creds.appStateVersion![type] || 0
const node: BinaryNode = {
@@ -373,24 +373,24 @@ export const makeChatsSocket = (config: SocketConfig) => {
const patchesNode = getBinaryNodeChild(collectionNode, 'patches')
const patches = getBinaryNodeChildren(patchesNode, 'patch')
const successfulMutations = patches.flatMap(({ content }) => {
const successfulMutations: ChatMutation[] = []
for(const { content } of patches) {
if(content) {
const syncd = proto.SyncdPatch.decode(content! as Uint8Array)
const version = toNumber(syncd.version!.version!)
if(version) {
authState.creds.appStateVersion[name] = Math.max(version, authState.creds.appStateVersion[name])
}
const { mutations, failures } = decodeSyncdPatch(syncd, authState)
const { mutations, failures } = await decodeSyncdPatch(syncd, authState)
if(failures.length) {
logger.info(
{ failures: failures.map(f => ({ trace: f.stack, data: f.data })) },
'failed to decode'
)
}
return mutations
successfulMutations.push(...mutations)
}
return []
})
}
processSyncActions(successfulMutations)
ev.emit('auth-state.update', authState)
}

View File

@@ -99,16 +99,15 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
})
}
const processMessage = (message: proto.IWebMessageInfo, chatUpdate: Partial<Chat>) => {
const processMessage = async(message: proto.IWebMessageInfo, chatUpdate: Partial<Chat>) => {
const protocolMsg = message.message?.protocolMessage
if(protocolMsg) {
switch(protocolMsg.type) {
case proto.ProtocolMessage.ProtocolMessageType.APP_STATE_SYNC_KEY_SHARE:
const newKeys = JSON.parse(JSON.stringify(protocolMsg.appStateSyncKeyShare!.keys))
authState.creds.appStateSyncKeys = [
...(authState.creds.appStateSyncKeys || []),
...newKeys
]
for(const { keyData, keyId } of protocolMsg.appStateSyncKeyShare!.keys || []) {
const str = Buffer.from(keyId.keyId!).toString('base64')
await authState.keys.setAppStateSyncKey(str, keyData)
}
ev.emit('auth-state.update', authState)
break
case proto.ProtocolMessage.ProtocolMessageType.REVOKE:
@@ -417,10 +416,10 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}
})
ev.on('messages.upsert', ({ messages }) => {
ev.on('messages.upsert', async({ messages }) => {
const chat: Partial<Chat> = { id: messages[0].key.remoteJid }
for(const msg of messages) {
processMessage(msg, chat)
await processMessage(msg, chat)
if(!!msg.message && !msg.message!.protocolMessage) {
chat.conversationTimestamp = toNumber(msg.messageTimestamp)
if(!msg.key.fromMe) {