Files
InfiniteAPI/src/Utils/sync-action-utils.ts
T
Renato Alcara d08a09c35f feat: add inbound username support and USync username protocol (#382)
* feat: add inbound username support and USync username protocol

Aligns with Baileys upstream PR #2480. WhatsApp is rolling out an
inbound username field that maps to the user's LID. This change is
purely additive — it captures and propagates the new optional field
through types, decoders, USync queries, and group/contact events.

Skipped intentionally to preserve InfiniteAPI's LID/PN customization:
- handleGroupNotification in messages-recv.ts (custom LID->PN flow on
  groups.upsert / participants.map). Username for these specific
  events still flows via process-message's emitParticipantsUpdate
  (reads message.key.participantUsername added in decode-wa-message).

Carousel/buttons code (messages.ts, messages-send.ts) untouched.

Co-Authored-By: Renato Alcara
2026-04-25 12:39:31 -03:00

76 lines
1.9 KiB
TypeScript

import { proto } from '../../WAProto/index.js'
import type { BaileysEventEmitter, BaileysEventMap, Contact } from '../Types'
import { isLidUser, isPnUser } from '../WABinary'
import type { ILogger } from './logger'
export type ContactsUpsertResult = {
event: 'contacts.upsert'
data: Contact[]
}
export type LidMappingUpdateResult = {
event: 'lid-mapping.update'
data: BaileysEventMap['lid-mapping.update']
}
export type SyncActionResult = ContactsUpsertResult | LidMappingUpdateResult
/**
* Process contactAction and return events to emit.
* Pure function - no side effects.
*/
export const processContactAction = (
action: proto.SyncActionValue.IContactAction,
id: string | undefined,
logger?: ILogger
): SyncActionResult[] => {
const results: SyncActionResult[] = []
if (!id) {
logger?.warn(
{ hasFullName: !!action.fullName, hasLidJid: !!action.lidJid, hasPnJid: !!action.pnJid },
'contactAction sync: missing id in index'
)
return results
}
const lidJid = action.lidJid
const idIsPn = isPnUser(id)
// PN is in index[1], not in contactAction.pnJid which is usually null
const phoneNumber = idIsPn ? id : action.pnJid || undefined
// Always emit contacts.upsert
results.push({
event: 'contacts.upsert',
data: [
{
id,
name: action.fullName || action.firstName || action.username || undefined,
username: action.username || undefined,
lid: lidJid || undefined,
phoneNumber
}
]
})
// Emit lid-mapping.update if we have valid LID-PN pair
if (lidJid && isLidUser(lidJid) && idIsPn) {
results.push({
event: 'lid-mapping.update',
data: [{ lid: lidJid, pn: id }]
})
}
return results
}
export const emitSyncActionResults = (ev: BaileysEventEmitter, results: SyncActionResult[]): void => {
for (const result of results) {
if (result.event === 'contacts.upsert') {
ev.emit('contacts.upsert', result.data)
} else {
ev.emit('lid-mapping.update', result.data)
}
}
}