UNPKG

@whiskeysockets/baileys

Version:

A WebSockets library for interacting with WhatsApp Web

50 lines 2.29 kB
import NodeCache from '@cacheable/node-cache'; import { areJidsSameUser, getBinaryNodeChild, jidDecode } from '../WABinary/index.js'; import { isStringNullOrEmpty } from './generics.js'; export async function handleIdentityChange(node, ctx) { const from = node.attrs.from; if (!from) { return { action: 'invalid_notification' }; } const identityNode = getBinaryNodeChild(node, 'identity'); if (!identityNode) { return { action: 'no_identity_node' }; } ctx.logger.info({ jid: from }, 'identity changed'); const decoded = jidDecode(from); if (decoded?.device && decoded.device !== 0) { ctx.logger.debug({ jid: from, device: decoded.device }, 'ignoring identity change from companion device'); return { action: 'skipped_companion_device', device: decoded.device }; } const isSelfPrimary = ctx.meId && (areJidsSameUser(from, ctx.meId) || (ctx.meLid && areJidsSameUser(from, ctx.meLid))); if (isSelfPrimary) { ctx.logger.info({ jid: from }, 'self primary identity changed'); return { action: 'skipped_self_primary' }; } if (ctx.debounceCache.get(from)) { ctx.logger.debug({ jid: from }, 'skipping identity assert (debounced)'); return { action: 'debounced' }; } ctx.debounceCache.set(from, true); const isOfflineNotification = !isStringNullOrEmpty(node.attrs.offline); const hasExistingSession = await ctx.validateSession(from); if (!hasExistingSession.exists) { ctx.logger.debug({ jid: from }, 'no old session, skipping session refresh'); return { action: 'skipped_no_session' }; } ctx.logger.debug({ jid: from }, 'old session exists, will refresh session'); if (isOfflineNotification) { ctx.logger.debug({ jid: from }, 'skipping session refresh during offline processing'); return { action: 'skipped_offline' }; } ctx.onBeforeSessionRefresh?.(from); try { await ctx.assertSessions([from], true); return { action: 'session_refreshed' }; } catch (error) { ctx.logger.warn({ error, jid: from }, 'failed to assert sessions after identity change'); return { action: 'session_refresh_failed', error }; } } //# sourceMappingURL=identity-change-handler.js.map