diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 90e413e..e9dbf26 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -1,5 +1,5 @@ import { SocketConfig, WAPresence, PresenceData, Chat, WAPatchCreate, WAMediaUpload, ChatMutation, WAPatchName, LTHashState, ChatModification, Contact } from "../Types"; -import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, jidNormalizedUser, S_WHATSAPP_NET } from "../WABinary"; +import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, jidNormalizedUser, S_WHATSAPP_NET, reduceBinaryNodeToDictionary } from "../WABinary"; import { proto } from '../../WAProto' import { generateProfilePicture, toNumber, encodeSyncdPatch, decodePatches, extractSyncdPatches, chatModificationToAppPatch, decodeSyncdSnapshot, newLTHashState } from "../Utils"; import { makeMessagesSocket } from "./messages-send"; @@ -465,6 +465,56 @@ export const makeChatsSocket = (config: SocketConfig) => { } ) } + /** sending abt props may fix QR scan fail if server expects */ + const fetchAbt = async() => { + const abtNode = await query({ + tag: 'iq', + attrs: { + to: S_WHATSAPP_NET, + xmlns: 'abt', + type: 'get', + id: generateMessageTag(), + }, + content: [ + { tag: 'props', attrs: { protocol: '1' } } + ] + }) + + const propsNode = getBinaryNodeChild(abtNode, 'props') + + let props: { [_: string]: string } = { } + if(propsNode) { + props = reduceBinaryNodeToDictionary(propsNode, 'prop') + } + logger.debug('fetched abt') + + return props + } + /** sending non-abt props may fix QR scan fail if server expects */ + const fetchProps = async() => { + const resultNode = await query({ + tag: 'iq', + attrs: { + to: S_WHATSAPP_NET, + xmlns: 'w', + type: 'get', + id: generateMessageTag(), + }, + content: [ + { tag: 'props', attrs: { } } + ] + }) + + const propsNode = getBinaryNodeChild(resultNode, 'props') + + let props: { [_: string]: string } = { } + if(propsNode) { + props = reduceBinaryNodeToDictionary(propsNode, 'prop') + } + logger.debug('fetched props') + + return props + } /** * modify a chat -- mark unread, read etc. * lastMessages must be sorted in reverse chronologically @@ -514,6 +564,8 @@ export const makeChatsSocket = (config: SocketConfig) => { sendPresenceUpdate('available') fetchBlocklist() fetchPrivacySettings() + fetchAbt() + fetchProps() } }) diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index eb45b51..cd988b7 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -1,9 +1,9 @@ import got from "got" import { Boom } from "@hapi/boom" -import { SocketConfig, MediaConnInfo, AnyMessageContent, MiscMessageGenerationOptions, WAMediaUploadFunction, MessageRelayOptions, WAMessageKey } from "../Types" +import { SocketConfig, MediaConnInfo, AnyMessageContent, MiscMessageGenerationOptions, WAMediaUploadFunction, MessageRelayOptions } from "../Types" import { encodeWAMessage, generateMessageID, generateWAMessage, encryptSenderKeyMsgSignalProto, encryptSignalProto, extractDeviceJids, jidToSignalProtocolAddress, parseAndInjectE2ESession } from "../Utils" -import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET, BinaryNodeAttributes, JidWithDevice } from '../WABinary' +import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET, BinaryNodeAttributes, JidWithDevice, reduceBinaryNodeToDictionary } from '../WABinary' import { proto } from "../../WAProto" import { WA_DEFAULT_EPHEMERAL, DEFAULT_ORIGIN, MEDIA_PATH_MAP } from "../Defaults" import { makeGroupsSocket } from "./groups" @@ -41,13 +41,7 @@ export const makeMessagesSocket = (config: SocketConfig) => { { tag: 'privacy', attrs: { } } ] }) - const nodes = getBinaryNodeChildren(result, 'category') - privacySettings = nodes.reduce( - (dict, { attrs }) => { - dict[attrs.name] = attrs.value - return dict - }, { } as { [_: string]: string } - ) + privacySettings = reduceBinaryNodeToDictionary(result, 'category') } return privacySettings } diff --git a/src/WABinary/index.ts b/src/WABinary/index.ts index e9d677c..644a870 100644 --- a/src/WABinary/index.ts +++ b/src/WABinary/index.ts @@ -308,5 +308,16 @@ export const assertNodeErrorFree = (node: BinaryNode) => { } } +export const reduceBinaryNodeToDictionary = (node: BinaryNode, tag: string) => { + const nodes = getBinaryNodeChildren(node, tag) + const dict = nodes.reduce( + (dict, { attrs }) => { + dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value + return dict + }, { } as { [_: string]: string } + ) + return dict +} + export * from './jid-utils' export { Binary } from '../../WABinary/Binary' \ No newline at end of file