diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 1bca767..4acc1e8 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -385,7 +385,7 @@ export const makeChatsSocket = (config: SocketConfig) => { ] }) - const decoded = await extractSyncdPatches(result) // extract from binary node + const decoded = await extractSyncdPatches(result, config?.options) // extract from binary node for(const key in decoded) { const name = key as WAPatchName const { patches, hasMorePatches, snapshot } = decoded[name] @@ -403,7 +403,15 @@ export const makeChatsSocket = (config: SocketConfig) => { // only process if there are syncd patches if(patches.length) { - const { newMutations, state: newState } = await decodePatches(name, patches, states[name], getAppStateSyncKey, onMutation, initialVersionMap[name]) + const { newMutations, state: newState } = await decodePatches( + name, + patches, + states[name], + getAppStateSyncKey, + onMutation, + config.options, + initialVersionMap[name] + ) await authState.keys.set({ 'app-state-sync-version': { [name]: newState } }) @@ -624,6 +632,7 @@ export const makeChatsSocket = (config: SocketConfig) => { initial!, getAppStateSyncKey, onMutation, + config.options, undefined, logger, ) @@ -733,6 +742,7 @@ export const makeChatsSocket = (config: SocketConfig) => { creds: authState.creds, keyStore: authState.keys, logger, + options: config.options, } ) diff --git a/src/Tests/test.media-download.ts b/src/Tests/test.media-download.ts index 0e96d25..f00638c 100644 --- a/src/Tests/test.media-download.ts +++ b/src/Tests/test.media-download.ts @@ -14,7 +14,7 @@ type TestVector = { const TEST_VECTORS: TestVector[] = [ { type: 'image', - message: proto.ImageMessage.decode( + message: proto.Message.ImageMessage.decode( Buffer.from( 'Ck1odHRwczovL21tZy53aGF0c2FwcC5uZXQvZC9mL0FwaHR4WG9fWXZZcDZlUVNSa0tjOHE5d2ozVUpleWdoY3poM3ExX3I0ektnLmVuYxIKaW1hZ2UvanBlZyIgKTuVFyxDc6mTm4GXPlO3Z911Wd8RBeTrPLSWAEdqW8MomcUBQiB7wH5a4nXMKyLOT0A2nFgnnM/DUH8YjQf8QtkCIekaSkogTB+BXKCWDFrmNzozY0DCPn0L4VKd7yG1ZbZwbgRhzVc=', 'base64' @@ -24,7 +24,7 @@ const TEST_VECTORS: TestVector[] = [ }, { type: 'image', - message: proto.ImageMessage.decode( + message: proto.Message.ImageMessage.decode( Buffer.from( 'Ck1odHRwczovL21tZy53aGF0c2FwcC5uZXQvZC9mL0Ftb2tnWkphNWF6QWZxa3dVRzc0eUNUdTlGeWpjMmd5akpqcXNmMUFpZEU5LmVuYxIKaW1hZ2UvanBlZyIg8IS5TQzdzcuvcR7F8HMhWnXmlsV+GOo9JE1/t2k+o9Yoz6o6QiA7kDk8j5KOEQC0kDFE1qW7lBBDYhm5z06N3SirfUj3CUog/CjYF8e670D5wUJwWv2B2mKzDEo8IJLStDv76YmtPfs=', 'base64' diff --git a/src/Utils/chat-utils.ts b/src/Utils/chat-utils.ts index 782fa94..3677bbc 100644 --- a/src/Utils/chat-utils.ts +++ b/src/Utils/chat-utils.ts @@ -1,4 +1,5 @@ import { Boom } from '@hapi/boom' +import { AxiosRequestConfig } from 'axios' import type { Logger } from 'pino' import { proto } from '../../WAProto' import { BaileysEventEmitter, ChatModification, ChatMutation, Contact, InitialAppStateSyncOptions, LastMessageList, LTHashState, WAPatchCreate, WAPatchName } from '../Types' @@ -273,7 +274,10 @@ export const decodeSyncdPatch = async( return result } -export const extractSyncdPatches = async(result: BinaryNode) => { +export const extractSyncdPatches = async( + result: BinaryNode, + options: AxiosRequestConfig +) => { const syncNode = getBinaryNodeChild(result, 'sync') const collectionNodes = getBinaryNodeChildren(syncNode, 'collection') @@ -300,7 +304,7 @@ export const extractSyncdPatches = async(result: BinaryNode) => { const blobRef = proto.ExternalBlobReference.decode( snapshotNode.content! as Buffer ) - const data = await downloadExternalBlob(blobRef) + const data = await downloadExternalBlob(blobRef, options) snapshot = proto.SyncdSnapshot.decode(data) } @@ -328,8 +332,11 @@ export const extractSyncdPatches = async(result: BinaryNode) => { } -export const downloadExternalBlob = async(blob: proto.IExternalBlobReference) => { - const stream = await downloadContentFromMessage(blob, 'md-app-state') +export const downloadExternalBlob = async( + blob: proto.IExternalBlobReference, + options: AxiosRequestConfig +) => { + const stream = await downloadContentFromMessage(blob, 'md-app-state', { options }) const bufferArray: Buffer[] = [] for await (const chunk of stream) { bufferArray.push(chunk) @@ -338,8 +345,11 @@ export const downloadExternalBlob = async(blob: proto.IExternalBlobReference) => return Buffer.concat(bufferArray) } -export const downloadExternalPatch = async(blob: proto.IExternalBlobReference) => { - const buffer = await downloadExternalBlob(blob) +export const downloadExternalPatch = async( + blob: proto.IExternalBlobReference, + options: AxiosRequestConfig +) => { + const buffer = await downloadExternalBlob(blob, options) const syncData = proto.SyncdMutations.decode(buffer) return syncData } @@ -399,6 +409,7 @@ export const decodePatches = async( initial: LTHashState, getAppStateSyncKey: FetchAppStateSyncKey, onMutation: (mut: ChatMutation) => void, + options: AxiosRequestConfig, minimumVersionNumber?: number, logger?: Logger, validateMacs: boolean = true @@ -416,7 +427,7 @@ export const decodePatches = async( const { version, keyId, snapshotMac } = syncd if(syncd.externalMutations) { logger?.trace({ name, version }, 'downloading external patch') - const ref = await downloadExternalPatch(syncd.externalMutations) + const ref = await downloadExternalPatch(syncd.externalMutations, options) logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch') syncd.mutations?.push(...ref.mutations) } diff --git a/src/Utils/history.ts b/src/Utils/history.ts index 533847d..35aa8e0 100644 --- a/src/Utils/history.ts +++ b/src/Utils/history.ts @@ -1,3 +1,4 @@ +import { AxiosRequestConfig } from 'axios' import { promisify } from 'util' import { inflate } from 'zlib' import { proto } from '../../WAProto' @@ -9,8 +10,11 @@ import { downloadContentFromMessage } from './messages-media' const inflatePromise = promisify(inflate) -export const downloadHistory = async(msg: proto.Message.IHistorySyncNotification) => { - const stream = await downloadContentFromMessage(msg, 'md-msg-hist') +export const downloadHistory = async( + msg: proto.Message.IHistorySyncNotification, + options: AxiosRequestConfig +) => { + const stream = await downloadContentFromMessage(msg, 'md-msg-hist', { options }) const bufferArray: Buffer[] = [] for await (const chunk of stream) { bufferArray.push(chunk) @@ -112,9 +116,10 @@ export const processHistoryMessage = ( export const downloadAndProcessHistorySyncNotification = async( msg: proto.Message.IHistorySyncNotification, historyCache: Set, - recvChats: InitialReceivedChatsState + recvChats: InitialReceivedChatsState, + options: AxiosRequestConfig ) => { - const historyMsg = await downloadHistory(msg) + const historyMsg = await downloadHistory(msg, options) return processHistoryMessage(historyMsg, historyCache, recvChats) } diff --git a/src/Utils/process-message.ts b/src/Utils/process-message.ts index ed3f03d..89a9f43 100644 --- a/src/Utils/process-message.ts +++ b/src/Utils/process-message.ts @@ -1,3 +1,4 @@ +import { AxiosRequestConfig } from 'axios' import type { Logger } from 'pino' import { proto } from '../../WAProto' import { AuthenticationCreds, BaileysEventEmitter, Chat, GroupMetadata, InitialReceivedChatsState, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types' @@ -12,6 +13,7 @@ type ProcessMessageContext = { keyStore: SignalKeyStoreWithTransaction ev: BaileysEventEmitter logger?: Logger + options: AxiosRequestConfig } const MSG_MISSED_CALL_TYPES = new Set([ @@ -64,7 +66,7 @@ export const shouldIncrementChatUnread = (message: proto.IWebMessageInfo) => ( const processMessage = async( message: proto.IWebMessageInfo, - { downloadHistory, ev, historyCache, recvChats, creds, keyStore, logger }: ProcessMessageContext + { downloadHistory, ev, historyCache, recvChats, creds, keyStore, logger, options }: ProcessMessageContext ) => { const meId = creds.me!.id const { accountSettings } = creds @@ -95,7 +97,12 @@ const processMessage = async( if(downloadHistory) { const isLatest = !creds.processedHistoryMessages?.length - const { chats, contacts, messages, didProcess } = await downloadAndProcessHistorySyncNotification(histNotification, historyCache, recvChats) + const { chats, contacts, messages, didProcess } = await downloadAndProcessHistorySyncNotification( + histNotification, + historyCache, + recvChats, + options + ) if(chats.length) { ev.emit('chats.set', { chats, isLatest })