fix: store LID-PN mapping from contactAction sync (#2266)
* fix: store LID-PN mapping from contactAction sync * chore: improve testing of sync actions
This commit is contained in:
committed by
GitHub
parent
a89736f89d
commit
8ff01b8bb3
@@ -24,6 +24,7 @@ import { toNumber } from './generics'
|
||||
import type { ILogger } from './logger'
|
||||
import { LT_HASH_ANTI_TAMPERING } from './lt-hash'
|
||||
import { downloadContentFromMessage } from './messages-media'
|
||||
import { emitSyncActionResults, processContactAction } from './sync-action-utils'
|
||||
|
||||
type FetchAppStateSyncKey = (keyId: string) => Promise<proto.Message.IAppStateSyncKeyData | null | undefined>
|
||||
|
||||
@@ -832,14 +833,8 @@ export const processSyncAction = (
|
||||
]
|
||||
})
|
||||
} else if (action?.contactAction) {
|
||||
ev.emit('contacts.upsert', [
|
||||
{
|
||||
id: id!,
|
||||
name: action.contactAction.fullName!,
|
||||
lid: action.contactAction.lidJid || undefined,
|
||||
phoneNumber: action.contactAction.pnJid || undefined
|
||||
}
|
||||
])
|
||||
const results = processContactAction(action.contactAction, id, logger)
|
||||
emitSyncActionResults(ev, results)
|
||||
} else if (action?.pushNameSetting) {
|
||||
const name = action?.pushNameSetting?.name
|
||||
if (name && me?.name !== name) {
|
||||
@@ -935,7 +930,7 @@ export const processSyncAction = (
|
||||
ev.emit('contacts.upsert', [
|
||||
{
|
||||
id: id!,
|
||||
name: action.lidContactAction.fullName!,
|
||||
name: action.lidContactAction.fullName || undefined,
|
||||
lid: id!,
|
||||
phoneNumber: undefined
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
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 || 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user