import NodeCache from '@cacheable/node-cache' import { Boom } from '@hapi/boom' import { proto } from '../../WAProto/index.js' import { DEFAULT_CACHE_TTLS, WA_DEFAULT_EPHEMERAL } from '../Defaults' import type { AnyMessageContent, MediaConnInfo, MessageReceiptType, MessageRelayOptions, MiscMessageGenerationOptions, SocketConfig, WAMessageKey } from '../Types' import { WAMessageAddressingMode } from '../Types' import { aggregateMessageKeysNotFromMe, assertMediaContent, bindWaitForEvent, decryptMediaRetryData, encodeNewsletterMessage, encodeSignedDeviceIdentity, encodeWAMessage, encryptMediaRetryRequest, extractDeviceJids, generateMessageIDV2, generateParticipantHashV2, generateWAMessage, getStatusCodeForMediaRetry, getUrlFromDirectPath, getWAUploadToServer, MessageRetryManager, normalizeMessageContent, parseAndInjectE2ESessions, unixTimestampSeconds } from '../Utils' import { getUrlInfo } from '../Utils/link-preview' import { makeKeyedMutex } from '../Utils/make-mutex' import { areJidsSameUser, type BinaryNode, type BinaryNodeAttributes, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isPnUser, jidDecode, jidEncode, jidNormalizedUser, type JidWithDevice, S_WHATSAPP_NET } from '../WABinary' import { USyncQuery, USyncUser } from '../WAUSync' import { makeNewsletterSocket } from './newsletter' export const makeMessagesSocket = (config: SocketConfig) => { const { logger, linkPreviewImageThumbnailWidth, generateHighQualityLinkPreview, options: axiosOptions, patchMessageBeforeSending, cachedGroupMetadata, enableRecentMessageCache, maxMsgRetryCount } = config const sock = makeNewsletterSocket(config) const { ev, authState, processingMutex, signalRepository, upsertMessage, query, fetchPrivacySettings, sendNode, groupMetadata, groupToggleEphemeral } = sock const userDevicesCache = config.userDevicesCache || new NodeCache({ stdTTL: DEFAULT_CACHE_TTLS.USER_DEVICES, // 5 minutes useClones: false }) const peerSessionsCache = new NodeCache({ stdTTL: DEFAULT_CACHE_TTLS.USER_DEVICES, useClones: false }) // Initialize message retry manager if enabled const messageRetryManager = enableRecentMessageCache ? new MessageRetryManager(logger, maxMsgRetryCount) : null // Prevent race conditions in Signal session encryption by user const encryptionMutex = makeKeyedMutex() let mediaConn: Promise const refreshMediaConn = async (forceGet = false) => { const media = await mediaConn if (!media || forceGet || new Date().getTime() - media.fetchDate.getTime() > media.ttl * 1000) { mediaConn = (async () => { const result = await query({ tag: 'iq', attrs: { type: 'set', xmlns: 'w:m', to: S_WHATSAPP_NET }, content: [{ tag: 'media_conn', attrs: {} }] }) const mediaConnNode = getBinaryNodeChild(result, 'media_conn')! const node: MediaConnInfo = { hosts: getBinaryNodeChildren(mediaConnNode, 'host').map(({ attrs }) => ({ hostname: attrs.hostname!, maxContentLengthBytes: +attrs.maxContentLengthBytes! })), auth: mediaConnNode.attrs.auth!, ttl: +mediaConnNode.attrs.ttl!, fetchDate: new Date() } logger.debug('fetched media conn') return node })() } return mediaConn } /** * generic send receipt function * used for receipts of phone call, read, delivery etc. * */ const sendReceipt = async ( jid: string, participant: string | undefined, messageIds: string[], type: MessageReceiptType ) => { if (!messageIds || messageIds.length === 0) { throw new Boom('missing ids in receipt') } const node: BinaryNode = { tag: 'receipt', attrs: { id: messageIds[0]! } } const isReadReceipt = type === 'read' || type === 'read-self' if (isReadReceipt) { node.attrs.t = unixTimestampSeconds().toString() } if (type === 'sender' && isPnUser(jid)) { node.attrs.recipient = jid node.attrs.to = participant! } else { node.attrs.to = jid if (participant) { node.attrs.participant = participant } } if (type) { node.attrs.type = type } const remainingMessageIds = messageIds.slice(1) if (remainingMessageIds.length) { node.content = [ { tag: 'list', attrs: {}, content: remainingMessageIds.map(id => ({ tag: 'item', attrs: { id } })) } ] } logger.debug({ attrs: node.attrs, messageIds }, 'sending receipt for messages') await sendNode(node) } /** Correctly bulk send receipts to multiple chats, participants */ const sendReceipts = async (keys: WAMessageKey[], type: MessageReceiptType) => { const recps = aggregateMessageKeysNotFromMe(keys) for (const { jid, participant, messageIds } of recps) { await sendReceipt(jid, participant, messageIds, type) } } /** Bulk read messages. Keys can be from different chats & participants */ const readMessages = async (keys: WAMessageKey[]) => { const privacySettings = await fetchPrivacySettings() // based on privacy settings, we have to change the read type const readType = privacySettings.readreceipts === 'all' ? 'read' : 'read-self' await sendReceipts(keys, readType) } /** Device info with wire JID format for envelope addressing */ type DeviceWithWireJid = JidWithDevice & { wireJid: string } const resolveSessionJids = async (jids: string[]): Promise> => { const uniquePnJids = Array.from(new Set(jids.filter(isPnUser))) if (!uniquePnJids.length) { return new Map() } const lookups = await Promise.all( uniquePnJids.map(async pnJid => { try { const resolved = await signalRepository.lidMapping.getLIDForPN(pnJid) return resolved ? ([pnJid, resolved] as const) : [pnJid, pnJid] } catch (error) { logger.warn({ pnJid, error }, 'Failed to resolve LID mapping for PN JID') return [pnJid, pnJid] } }) ) const sessionMap = new Map() for (const entry of lookups) { if (entry) { sessionMap.set(entry[0], entry[1]) } } return sessionMap } /** Fetch all the devices we've to send a message to */ const getUSyncDevices = async ( jids: string[], useCache: boolean, ignoreZeroDevices: boolean ): Promise => { const deviceResults: DeviceWithWireJid[] = [] if (!useCache) { logger.debug('not using cache for devices') } const toFetch: string[] = [] const jidsWithUser = jids .map(jid => { const decoded = jidDecode(jid) const user = decoded?.user const device = decoded?.device const isExplicitDevice = typeof device === 'number' && device >= 0 if (isExplicitDevice && user) { deviceResults.push({ user, device, wireJid: jid // again this makes no sense }) return null } jid = jidNormalizedUser(jid) return { jid, user } }) .filter(jid => jid !== null) let mgetDevices: undefined | Record if (useCache && userDevicesCache.mget) { const usersToFetch = jidsWithUser.map(j => j?.user).filter(Boolean) as string[] mgetDevices = await userDevicesCache.mget(usersToFetch) } for (const { jid, user } of jidsWithUser) { if (useCache) { const devices = mgetDevices?.[user!] || (userDevicesCache.mget ? undefined : ((await userDevicesCache.get(user!)) as JidWithDevice[])) if (devices) { const isLidJid = jid.includes('@lid') const devicesWithWire = devices.map(d => ({ ...d, wireJid: isLidJid ? jidEncode(d.user, 'lid', d.device) : jidEncode(d.user, 's.whatsapp.net', d.device) })) deviceResults.push(...devicesWithWire) logger.trace({ user }, 'using cache for devices') } else { toFetch.push(jid) } } else { toFetch.push(jid) } } if (!toFetch.length) { return deviceResults } const requestedLidUsers = new Set() for (const jid of toFetch) { if (jid.includes('@lid')) { const user = jidDecode(jid)?.user if (user) requestedLidUsers.add(user) } } const query = new USyncQuery().withContext('message').withDeviceProtocol() for (const jid of toFetch) { query.withUser(new USyncUser().withId(jid)) // todo: investigate - the idea here is that should have an inline lid field with the lid being the pn equivalent } const result = await sock.executeUSyncQuery(query) if (result) { const extracted = extractDeviceJids(result?.list, authState.creds.me!.id, ignoreZeroDevices) const deviceMap: { [_: string]: JidWithDevice[] } = {} for (const item of extracted) { deviceMap[item.user] = deviceMap[item.user] || [] deviceMap[item.user]?.push(item) } // Process each user's devices as a group for bulk LID migration for (const [user, userDevices] of Object.entries(deviceMap)) { const isLidUser = requestedLidUsers.has(user) // Process all devices for this user for (const item of userDevices) { const finalWireJid = isLidUser ? jidEncode(user, 'lid', item.device) : jidEncode(item.user, 's.whatsapp.net', item.device) deviceResults.push({ ...item, wireJid: finalWireJid }) logger.debug( { user: item.user, device: item.device, finalWireJid, usedLid: isLidUser }, 'Processed device with LID priority' ) } } if (userDevicesCache.mset) { // if the cache supports mset, we can set all devices in one go await userDevicesCache.mset(Object.entries(deviceMap).map(([key, value]) => ({ key, value }))) } else { for (const key in deviceMap) { if (deviceMap[key]) await userDevicesCache.set(key, deviceMap[key]) } } const userDeviceUpdates: { [userId: string]: string[] } = {} for (const [userId, devices] of Object.entries(deviceMap)) { if (devices && devices.length > 0) { userDeviceUpdates[userId] = devices.map(d => d.device?.toString() || '0') } } if (Object.keys(userDeviceUpdates).length > 0) { try { await authState.keys.set({ 'device-list': userDeviceUpdates }) logger.debug( { userCount: Object.keys(userDeviceUpdates).length }, 'stored user device lists for bulk migration' ) } catch (error) { logger.warn({ error }, 'failed to store user device lists') } } } return deviceResults } const assertSessions = async (jids: string[]) => { let didFetchNewSession = false const uniqueJids = [...new Set(jids)] // Deduplicate JIDs const jidsRequiringFetch: string[] = [] // Check peerSessionsCache and authState.keys for (const jid of uniqueJids) { const signalId = signalRepository.jidToSignalProtocolAddress(jid) const cachedSession = peerSessionsCache.get(signalId) if (cachedSession !== undefined) { if (cachedSession) { continue // Session exists in cache } } else { const sessions = await authState.keys.get('session', [signalId]) const hasSession = !!sessions[signalId] peerSessionsCache.set(signalId, hasSession) if (hasSession) { continue } } jidsRequiringFetch.push(jid) } if (jidsRequiringFetch.length) { logger.debug({ jidsRequiringFetch }, 'fetching sessions') const result = await query({ tag: 'iq', attrs: { xmlns: 'encrypt', type: 'get', to: S_WHATSAPP_NET }, content: [ { tag: 'key', attrs: {}, content: jidsRequiringFetch.map(jid => ({ tag: 'user', attrs: { jid } })) } ] }) await parseAndInjectE2ESessions(result, signalRepository) didFetchNewSession = true // Cache fetched sessions for (const jid of jidsRequiringFetch) { const signalId = signalRepository.jidToSignalProtocolAddress(jid) peerSessionsCache.set(signalId, true) } } return didFetchNewSession } const sendPeerDataOperationMessage = async ( pdoMessage: proto.Message.IPeerDataOperationRequestMessage ): Promise => { //TODO: for later, abstract the logic to send a Peer Message instead of just PDO - useful for App State Key Resync with phone if (!authState.creds.me?.id) { throw new Boom('Not authenticated') } const protocolMessage: proto.IMessage = { protocolMessage: { peerDataOperationRequestMessage: pdoMessage, type: proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_MESSAGE } } const meJid = jidNormalizedUser(authState.creds.me.id) const msgId = await relayMessage(meJid, protocolMessage, { additionalAttributes: { category: 'peer', push_priority: 'high_force' } }) return msgId } const createParticipantNodesWithSessionMap = async ( recipientWireJids: string[], sessionMap: Map, message: proto.IMessage, extraAttrs?: BinaryNode['attrs'], dsmMessage?: proto.IMessage ) => { if (!recipientWireJids.length) { return { nodes: [] as BinaryNode[], shouldIncludeDeviceIdentity: false } } const patched = await patchMessageBeforeSending(message, recipientWireJids) const patchedMessages = Array.isArray(patched) ? patched : recipientWireJids.map(jid => ({ recipientJid: jid, message: patched })) let shouldIncludeDeviceIdentity = false const meId = authState.creds.me!.id const meLid = authState.creds.me?.lid const meLidUser = meLid ? jidDecode(meLid)?.user : null const encryptionPromises = (patchedMessages as any).map( async ({ recipientJid: wireJid, message: patchedMessage }: any) => { if (!wireJid) return null wireJid = sessionMap.get(wireJid) ?? wireJid let msgToEncrypt = patchedMessage if (dsmMessage) { const { user: targetUser } = jidDecode(wireJid)! const { user: ownPnUser } = jidDecode(meId)! const ownLidUser = meLidUser const isOwnUser = targetUser === ownPnUser || (ownLidUser && targetUser === ownLidUser) const isExactSenderDevice = wireJid === meId || (meLid && wireJid === meLid) if (isOwnUser && !isExactSenderDevice) { msgToEncrypt = dsmMessage logger.debug({ wireJid, targetUser }, 'Using DSM for own device') } } const bytes = encodeWAMessage(msgToEncrypt) const mutexKey = wireJid const node = await encryptionMutex.mutex(mutexKey, async () => { const { type, ciphertext } = await signalRepository.encryptMessage({ jid: wireJid, data: bytes }) if (type === 'pkmsg') { shouldIncludeDeviceIdentity = true } return { tag: 'to', attrs: { jid: wireJid }, content: [ { tag: 'enc', attrs: { v: '2', type, ...(extraAttrs || {}) }, content: ciphertext } ] } }) return node } ) const nodes = (await Promise.all(encryptionPromises)).filter(node => node !== null) as BinaryNode[] return { nodes, shouldIncludeDeviceIdentity } } const createParticipantNodes = async ( recipientWireJids: string[], message: proto.IMessage, extraAttrs?: BinaryNode['attrs'], dsmMessage?: proto.IMessage ) => { const sessionMap = await resolveSessionJids(recipientWireJids) return createParticipantNodesWithSessionMap(recipientWireJids, sessionMap, message, extraAttrs, dsmMessage) } const relayMessage = async ( jid: string, message: proto.IMessage, { messageId: msgId, participant, additionalAttributes, additionalNodes, useUserDevicesCache, useCachedGroupMetadata, statusJidList }: MessageRelayOptions ) => { const meId = authState.creds.me!.id const meLid = authState.creds.me?.lid let shouldIncludeDeviceIdentity = false const statusJid = 'status@broadcast' const { user, server } = jidDecode(jid)! const isGroup = server === 'g.us' const isStatus = jid === statusJid const isLid = server === 'lid' const isNewsletter = server === 'newsletter' const finalJid = jid // ADDRESSING CONSISTENCY: Match own identity to conversation context // TODO: investigate if this is true let ownId = meId if (isLid && meLid) { ownId = meLid logger.debug({ to: jid, ownId }, 'Using LID identity for @lid conversation') } else { logger.debug({ to: jid, ownId }, 'Using PN identity for @s.whatsapp.net conversation') } msgId = msgId || generateMessageIDV2(sock.user?.id) useUserDevicesCache = useUserDevicesCache !== false useCachedGroupMetadata = useCachedGroupMetadata !== false && !isStatus const participants: BinaryNode[] = [] const destinationJid = !isStatus ? finalJid : statusJid const binaryNodeContent: BinaryNode[] = [] const devices: DeviceWithWireJid[] = [] const meMsg: proto.IMessage = { deviceSentMessage: { destinationJid, message }, messageContextInfo: message.messageContextInfo } const extraAttrs: BinaryNodeAttributes = {} if (participant) { if (!isGroup && !isStatus) { additionalAttributes = { ...additionalAttributes, device_fanout: 'false' } } const { user, device } = jidDecode(participant.jid)! devices.push({ user, device, wireJid: participant.jid }) } await authState.keys.transaction(async () => { const mediaType = getMediaType(message) if (mediaType) { extraAttrs['mediatype'] = mediaType } if (isNewsletter) { const patched = patchMessageBeforeSending ? await patchMessageBeforeSending(message, []) : message const bytes = encodeNewsletterMessage(patched as proto.IMessage) binaryNodeContent.push({ tag: 'plaintext', attrs: {}, content: bytes }) const stanza: BinaryNode = { tag: 'message', attrs: { to: jid, id: msgId, type: getMessageType(message), ...(additionalAttributes || {}) }, content: binaryNodeContent } logger.debug({ msgId }, `sending newsletter message to ${jid}`) await sendNode(stanza) return } if (normalizeMessageContent(message)?.pinInChatMessage) { extraAttrs['decrypt-fail'] = 'hide' // todo: expand for reactions and other types } if (isGroup || isStatus) { const [groupData, senderKeyMap] = await Promise.all([ (async () => { let groupData = useCachedGroupMetadata && cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined // todo: should we rely on the cache specially if the cache is outdated and the metadata has new fields? if (groupData && Array.isArray(groupData?.participants)) { logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata') } else if (!isStatus) { groupData = await groupMetadata(jid) } return groupData })(), (async () => { if (!participant && !isStatus) { const result = await authState.keys.get('sender-key-memory', [jid]) // TODO: check out what if the sender key memory doesn't include the LID stuff now? return result[jid] || {} } return {} })() ]) if (!participant) { const participantsList = groupData && !isStatus ? groupData.participants.map(p => p.id) : [] if (isStatus && statusJidList) { participantsList.push(...statusJidList) } if (!isStatus) { const groupAddressingMode = groupData?.addressingMode || (isLid ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN) additionalAttributes = { ...additionalAttributes, addressing_mode: groupAddressingMode } } const additionalDevices = await getUSyncDevices(participantsList, !!useUserDevicesCache, false) devices.push(...additionalDevices) } if (groupData?.ephemeralDuration && groupData.ephemeralDuration > 0) { additionalAttributes = { ...additionalAttributes, expiration: groupData.ephemeralDuration.toString() } } const patched = await patchMessageBeforeSending(message) if (Array.isArray(patched)) { throw new Boom('Per-jid patching is not supported in groups') } const bytes = encodeWAMessage(patched) const groupAddressingMode = groupData?.addressingMode || (isLid ? 'lid' : 'pn') const groupSenderIdentity = groupAddressingMode === 'lid' && meLid ? meLid : meId const { ciphertext, senderKeyDistributionMessage } = await signalRepository.encryptGroupMessage({ group: destinationJid, data: bytes, meId: groupSenderIdentity }) const deviceSessionMap = await resolveSessionJids(devices.map(d => d.wireJid)) const senderKeyRecipients: string[] = [] for (const device of devices) { const deviceJid = device.wireJid const hasKey = !!senderKeyMap[deviceJid] if (!hasKey || !!participant) { senderKeyRecipients.push(deviceJid) senderKeyMap[deviceJid] = true } } if (senderKeyRecipients.length) { logger.debug({ senderKeyJids: senderKeyRecipients }, 'sending new sender key') const senderKeyMsg: proto.IMessage = { senderKeyDistributionMessage: { axolotlSenderKeyDistributionMessage: senderKeyDistributionMessage, groupId: destinationJid } } const senderKeySessionTargets = senderKeyRecipients.map(jid => deviceSessionMap.get(jid) ?? jid) await assertSessions(senderKeySessionTargets) const result = await createParticipantNodesWithSessionMap( senderKeyRecipients, deviceSessionMap, senderKeyMsg, extraAttrs ) shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || result.shouldIncludeDeviceIdentity participants.push(...result.nodes) } binaryNodeContent.push({ tag: 'enc', attrs: { v: '2', type: 'skmsg', ...extraAttrs }, content: ciphertext }) await authState.keys.set({ 'sender-key-memory': { [jid]: senderKeyMap } }) } else { const { user: ownUser } = jidDecode(ownId)! if (!participant) { const targetUserServer = isLid ? 'lid' : 's.whatsapp.net' devices.push({ user, device: 0, wireJid: jidEncode(user, targetUserServer, 0) }) // Own user matches conversation addressing mode if (user !== ownUser) { const ownUserServer = isLid ? 'lid' : 's.whatsapp.net' const ownUserForAddressing = isLid && meLid ? jidDecode(meLid)!.user : jidDecode(meId)!.user devices.push({ user: ownUserForAddressing, device: 0, wireJid: jidEncode(ownUserForAddressing, ownUserServer, 0) }) } if (additionalAttributes?.['category'] !== 'peer') { // Clear placeholders and enumerate actual devices devices.length = 0 // Use conversation-appropriate sender identity const senderIdentity = isLid && meLid ? jidEncode(jidDecode(meLid)?.user!, 'lid', undefined) : jidEncode(jidDecode(meId)?.user!, 's.whatsapp.net', undefined) // Enumerate devices for sender and target with consistent addressing const sessionDevices = await getUSyncDevices([senderIdentity, jid], true, false) devices.push(...sessionDevices) logger.debug( { deviceCount: devices.length, devices: devices.map(d => `${d.user}:${d.device}@${jidDecode(d.wireJid)?.server}`) }, 'Device enumeration complete with unified addressing' ) } } const allRecipients: string[] = [] const meRecipients: string[] = [] const otherRecipients: string[] = [] const { user: mePnUser } = jidDecode(meId)! const { user: meLidUser } = meLid ? jidDecode(meLid)! : { user: null } for (const { user, wireJid } of devices) { const isExactSenderDevice = wireJid === meId || (meLid && wireJid === meLid) if (isExactSenderDevice) { logger.debug({ wireJid, meId, meLid }, 'Skipping exact sender device (whatsmeow pattern)') continue } // Check if this is our device (could match either PN or LID user) const isMe = user === mePnUser || (meLidUser && user === meLidUser) if (isMe) { meRecipients.push(wireJid) } else { otherRecipients.push(wireJid) } allRecipients.push(wireJid) } const deviceSessionMap = await resolveSessionJids(devices.map(d => d.wireJid)) const sessionTargets = allRecipients.map(jid => deviceSessionMap.get(jid) ?? jid) await assertSessions(sessionTargets) const [ { nodes: meNodes, shouldIncludeDeviceIdentity: s1 }, { nodes: otherNodes, shouldIncludeDeviceIdentity: s2 } ] = await Promise.all([ // For own devices: use DSM if available (1:1 chats only) createParticipantNodesWithSessionMap(meRecipients, deviceSessionMap, meMsg || message, extraAttrs), createParticipantNodesWithSessionMap(otherRecipients, deviceSessionMap, message, extraAttrs, meMsg) ]) participants.push(...meNodes) participants.push(...otherNodes) if (meRecipients.length > 0 || otherRecipients.length > 0) { extraAttrs['phash'] = generateParticipantHashV2([...meRecipients, ...otherRecipients]) } shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || s1 || s2 } if (participants.length) { if (additionalAttributes?.['category'] === 'peer') { const peerNode = participants[0]?.content?.[0] as BinaryNode if (peerNode) { binaryNodeContent.push(peerNode) // push only enc } } else { binaryNodeContent.push({ tag: 'participants', attrs: {}, content: participants }) } } const stanza: BinaryNode = { tag: 'message', attrs: { id: msgId, to: destinationJid, type: getMessageType(message), ...(additionalAttributes || {}) }, content: binaryNodeContent } // if the participant to send to is explicitly specified (generally retry recp) // ensure the message is only sent to that person // if a retry receipt is sent to everyone -- it'll fail decryption for everyone else who received the msg if (participant) { if (isJidGroup(destinationJid)) { stanza.attrs.to = destinationJid stanza.attrs.participant = participant.jid } else if (areJidsSameUser(participant.jid, meId)) { stanza.attrs.to = participant.jid stanza.attrs.recipient = destinationJid } else { stanza.attrs.to = participant.jid } } else { stanza.attrs.to = destinationJid } if (shouldIncludeDeviceIdentity) { ;(stanza.content as BinaryNode[]).push({ tag: 'device-identity', attrs: {}, content: encodeSignedDeviceIdentity(authState.creds.account!, true) }) logger.debug({ jid }, 'adding device identity') } if (additionalNodes && additionalNodes.length > 0) { ;(stanza.content as BinaryNode[]).push(...additionalNodes) } logger.debug({ msgId }, `sending message to ${participants.length} devices`) await sendNode(stanza) // Add message to retry cache if enabled if (messageRetryManager && !participant) { messageRetryManager.addRecentMessage(destinationJid, msgId, message) } }, meId) return msgId } const getMessageType = (message: proto.IMessage) => { if (message.pollCreationMessage || message.pollCreationMessageV2 || message.pollCreationMessageV3) { return 'poll' } if (message.eventMessage) { return 'event' } if (getMediaType(message) !== '') { return 'media' } return 'text' } const getMediaType = (message: proto.IMessage) => { if (message.imageMessage) { return 'image' } else if (message.videoMessage) { return message.videoMessage.gifPlayback ? 'gif' : 'video' } else if (message.audioMessage) { return message.audioMessage.ptt ? 'ptt' : 'audio' } else if (message.contactMessage) { return 'vcard' } else if (message.documentMessage) { return 'document' } else if (message.contactsArrayMessage) { return 'contact_array' } else if (message.liveLocationMessage) { return 'livelocation' } else if (message.stickerMessage) { return 'sticker' } else if (message.listMessage) { return 'list' } else if (message.listResponseMessage) { return 'list_response' } else if (message.buttonsResponseMessage) { return 'buttons_response' } else if (message.orderMessage) { return 'order' } else if (message.productMessage) { return 'product' } else if (message.interactiveResponseMessage) { return 'native_flow_response' } else if (message.groupInviteMessage) { return 'url' } return '' } const getPrivacyTokens = async (jids: string[]) => { const t = unixTimestampSeconds().toString() const result = await query({ tag: 'iq', attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'privacy' }, content: [ { tag: 'tokens', attrs: {}, content: jids.map(jid => ({ tag: 'token', attrs: { jid: jidNormalizedUser(jid), t, type: 'trusted_contact' } })) } ] }) return result } const waUploadToServer = getWAUploadToServer(config, refreshMediaConn) const waitForMsgMediaUpdate = bindWaitForEvent(ev, 'messages.media-update') return { ...sock, getPrivacyTokens, assertSessions, relayMessage, sendReceipt, sendReceipts, readMessages, refreshMediaConn, waUploadToServer, fetchPrivacySettings, sendPeerDataOperationMessage, createParticipantNodes, getUSyncDevices, messageRetryManager, updateMediaMessage: async (message: proto.IWebMessageInfo) => { const content = assertMediaContent(message.message) const mediaKey = content.mediaKey! const meId = authState.creds.me!.id const node = await encryptMediaRetryRequest(message.key, mediaKey, meId) let error: Error | undefined = undefined await Promise.all([ sendNode(node), waitForMsgMediaUpdate(async update => { const result = update.find(c => c.key.id === message.key.id) if (result) { if (result.error) { error = result.error } else { try { const media = await decryptMediaRetryData(result.media!, mediaKey, result.key.id!) if (media.result !== proto.MediaRetryNotification.ResultType.SUCCESS) { const resultStr = proto.MediaRetryNotification.ResultType[media.result] throw new Boom(`Media re-upload failed by device (${resultStr})`, { data: media, statusCode: getStatusCodeForMediaRetry(media.result) || 404 }) } content.directPath = media.directPath content.url = getUrlFromDirectPath(content.directPath) logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful') } catch (err: any) { error = err } } return true } }) ]) if (error) { throw error } ev.emit('messages.update', [{ key: message.key, update: { message: message.message } }]) return message }, sendMessage: async (jid: string, content: AnyMessageContent, options: MiscMessageGenerationOptions = {}) => { const userJid = authState.creds.me!.id if ( typeof content === 'object' && 'disappearingMessagesInChat' in content && typeof content['disappearingMessagesInChat'] !== 'undefined' && isJidGroup(jid) ) { const { disappearingMessagesInChat } = content const value = typeof disappearingMessagesInChat === 'boolean' ? disappearingMessagesInChat ? WA_DEFAULT_EPHEMERAL : 0 : disappearingMessagesInChat await groupToggleEphemeral(jid, value) } else { const fullMsg = await generateWAMessage(jid, content, { logger, userJid, getUrlInfo: text => getUrlInfo(text, { thumbnailWidth: linkPreviewImageThumbnailWidth, fetchOpts: { timeout: 3_000, ...(axiosOptions || {}) }, logger, uploadImage: generateHighQualityLinkPreview ? waUploadToServer : undefined }), //TODO: CACHE getProfilePicUrl: sock.profilePictureUrl, getCallLink: sock.createCallLink, upload: waUploadToServer, mediaCache: config.mediaCache, options: config.options, messageId: generateMessageIDV2(sock.user?.id), ...options }) const isEventMsg = 'event' in content && !!content.event const isDeleteMsg = 'delete' in content && !!content.delete const isEditMsg = 'edit' in content && !!content.edit const isPinMsg = 'pin' in content && !!content.pin const isPollMessage = 'poll' in content && !!content.poll const additionalAttributes: BinaryNodeAttributes = {} const additionalNodes: BinaryNode[] = [] // required for delete if (isDeleteMsg) { // if the chat is a group, and I am not the author, then delete the message as an admin if (isJidGroup(content.delete?.remoteJid as string) && !content.delete?.fromMe) { additionalAttributes.edit = '8' } else { additionalAttributes.edit = '7' } } else if (isEditMsg) { additionalAttributes.edit = '1' } else if (isPinMsg) { additionalAttributes.edit = '2' } else if (isPollMessage) { additionalNodes.push({ tag: 'meta', attrs: { polltype: 'creation' } } as BinaryNode) } else if (isEventMsg) { additionalNodes.push({ tag: 'meta', attrs: { event_type: 'creation' } } as BinaryNode) } await relayMessage(jid, fullMsg.message!, { messageId: fullMsg.key.id!, useCachedGroupMetadata: options.useCachedGroupMetadata, additionalAttributes, statusJidList: options.statusJidList, additionalNodes }) if (config.emitOwnEvents) { process.nextTick(() => { processingMutex.mutex(() => upsertMessage(fullMsg, 'append')) }) } return fullMsg } } } }