mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: handle encrypt notification
This commit is contained in:
@@ -5,10 +5,12 @@ import { BaileysEventMap, MessageUserReceipt, SocketConfig, WAMessageStubType }
|
|||||||
import { debouncedTimeout, decodeMessageStanza, delay, encodeBigEndian, generateSignalPubKey, getStatusFromReceiptType, normalizeMessageContent, xmppPreKey, xmppSignedPreKey } from '../Utils'
|
import { debouncedTimeout, decodeMessageStanza, delay, encodeBigEndian, generateSignalPubKey, getStatusFromReceiptType, normalizeMessageContent, xmppPreKey, xmppSignedPreKey } from '../Utils'
|
||||||
import { makeKeyedMutex, makeMutex } from '../Utils/make-mutex'
|
import { makeKeyedMutex, makeMutex } from '../Utils/make-mutex'
|
||||||
import processMessage from '../Utils/process-message'
|
import processMessage from '../Utils/process-message'
|
||||||
import { areJidsSameUser, BinaryNode, BinaryNodeAttributes, getAllBinaryNodeChildren, getBinaryNodeChildren, isJidGroup, jidDecode, jidEncode, jidNormalizedUser } from '../WABinary'
|
import { areJidsSameUser, BinaryNode, BinaryNodeAttributes, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary'
|
||||||
import { makeChatsSocket } from './chats'
|
import { makeChatsSocket } from './chats'
|
||||||
import { extractGroupMetadata } from './groups'
|
import { extractGroupMetadata } from './groups'
|
||||||
|
|
||||||
|
const MIN_PREKEY_COUNT = 5
|
||||||
|
|
||||||
export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||||
const {
|
const {
|
||||||
logger,
|
logger,
|
||||||
@@ -28,6 +30,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
sendReceipt,
|
sendReceipt,
|
||||||
resyncMainAppState,
|
resyncMainAppState,
|
||||||
emitEventsFromMap,
|
emitEventsFromMap,
|
||||||
|
uploadPreKeys
|
||||||
} = sock
|
} = sock
|
||||||
|
|
||||||
/** this mutex ensures that the notifications (receipts, messages etc.) are processed in order */
|
/** this mutex ensures that the notifications (receipts, messages etc.) are processed in order */
|
||||||
@@ -167,7 +170,30 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
return newEvents
|
return newEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
const processNotification = (node: BinaryNode): Partial<proto.IWebMessageInfo> => {
|
const handleEncryptNotification = async(node: BinaryNode) => {
|
||||||
|
const from = node.attrs.from
|
||||||
|
if(from === S_WHATSAPP_NET) {
|
||||||
|
const countChild = getBinaryNodeChild(node, 'count')
|
||||||
|
const count = +countChild.attrs.value
|
||||||
|
const shouldUploadMorePreKeys = count < MIN_PREKEY_COUNT
|
||||||
|
|
||||||
|
logger.debug({ count, shouldUploadMorePreKeys }, 'recv pre-key count')
|
||||||
|
if(shouldUploadMorePreKeys) {
|
||||||
|
await uploadPreKeys()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const identityNode = getBinaryNodeChild(node, 'identity')
|
||||||
|
if(identityNode) {
|
||||||
|
logger.info({ jid: from }, 'identity changed')
|
||||||
|
// not handling right now
|
||||||
|
// signal will override new identity anyway
|
||||||
|
} else {
|
||||||
|
logger.info({ node }, 'unknown encrypt notification')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const processNotification = async(node: BinaryNode): Promise<Partial<proto.IWebMessageInfo>> => {
|
||||||
const result: Partial<proto.IWebMessageInfo> = { }
|
const result: Partial<proto.IWebMessageInfo> = { }
|
||||||
const [child] = getAllBinaryNodeChildren(node)
|
const [child] = getAllBinaryNodeChildren(node)
|
||||||
|
|
||||||
@@ -242,6 +268,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
logger.info({ deviceJids }, 'got my own devices')
|
logger.info({ deviceJids }, 'got my own devices')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
case 'encrypt':
|
||||||
|
handleEncryptNotification(node)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -431,8 +460,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
await sendMessageAck(node, { class: 'notification', type: node.attrs.type })
|
await sendMessageAck(node, { class: 'notification', type: node.attrs.type })
|
||||||
await processingMutex.mutex(
|
await processingMutex.mutex(
|
||||||
remoteJid,
|
remoteJid,
|
||||||
() => {
|
async() => {
|
||||||
const msg = processNotification(node)
|
const msg = await processNotification(node)
|
||||||
if(msg) {
|
if(msg) {
|
||||||
const fromMe = areJidsSameUser(node.attrs.participant || node.attrs.from, authState.creds.me!.id)
|
const fromMe = areJidsSameUser(node.attrs.participant || node.attrs.from, authState.creds.me!.id)
|
||||||
msg.key = {
|
msg.key = {
|
||||||
|
|||||||
@@ -244,9 +244,9 @@ export const makeSocket = ({
|
|||||||
ev.emit('creds.update', update)
|
ev.emit('creds.update', update)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** generates and uploads a set of pre-keys */
|
/** generates and uploads a set of pre-keys to the server */
|
||||||
const uploadPreKeys = async() => {
|
const uploadPreKeys = async(count = INITIAL_PREKEY_COUNT) => {
|
||||||
await assertingPreKeys(INITIAL_PREKEY_COUNT, async preKeys => {
|
await assertingPreKeys(count, async preKeys => {
|
||||||
const node: BinaryNode = {
|
const node: BinaryNode = {
|
||||||
tag: 'iq',
|
tag: 'iq',
|
||||||
attrs: {
|
attrs: {
|
||||||
@@ -602,6 +602,7 @@ export const makeSocket = ({
|
|||||||
logout,
|
logout,
|
||||||
end,
|
end,
|
||||||
onUnexpectedError,
|
onUnexpectedError,
|
||||||
|
uploadPreKeys,
|
||||||
/** Waits for the connection to WA to reach a state */
|
/** Waits for the connection to WA to reach a state */
|
||||||
waitForConnectionUpdate: bindWaitForConnectionUpdate(ev)
|
waitForConnectionUpdate: bindWaitForConnectionUpdate(ev)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user