general: Bring back USYNC calls for LID getting, and improve

This commit is contained in:
Rajeh Taher
2025-10-03 00:04:39 +03:00
parent 1060f44bff
commit 592f70b81d
6 changed files with 119 additions and 77 deletions
+32 -18
View File
@@ -12,7 +12,7 @@ import {
NOISE_WA_HEADER,
UPLOAD_TIMEOUT
} from '../Defaults'
import type { SocketConfig } from '../Types'
import type { LIDMapping, SocketConfig } from '../Types'
import { DisconnectReason } from '../Types'
import {
addTransactionCapability,
@@ -258,13 +258,13 @@ export const makeSocket = (config: SocketConfig) => {
return usyncQuery.parseUSyncQueryResult(result)
}
const onWhatsApp = async (...jids: string[]) => {
let usyncQuery = new USyncQuery().withLIDProtocol()
const onWhatsApp = async (...phoneNumber: string[]) => {
let usyncQuery = new USyncQuery()
let contactEnabled = false
for (const jid of jids) {
for (const jid of phoneNumber) {
if (isLidUser(jid)) {
// usyncQuery.withUser(new USyncUser().withLid(jid)) // intentional but needs JID too
logger?.warn('LIDs are not supported with onWhatsApp')
continue
} else {
if (!contactEnabled) {
@@ -284,27 +284,41 @@ export const makeSocket = (config: SocketConfig) => {
const results = await executeUSyncQuery(usyncQuery)
if (results) {
const lidOnly = results.list.filter(a => !!a.lid)
if (lidOnly.length > 0) {
const pairs = lidOnly.map(a => ({ pn: a.id, lid: a.lid as string }))
await signalRepository.lidMapping.storeLIDPNMappings(pairs)
for (const { pn, lid } of pairs) {
await signalRepository.migrateSession(pn, lid)
}
}
return results.list
.filter(a => !!a.contact)
.map(({ contact, id, lid }) => ({ jid: id, exists: contact as boolean, lid: lid as string }))
return results.list.filter(a => !!a.contact).map(({ contact, id }) => ({ jid: id, exists: contact as boolean }))
}
}
const pnFromLIDUSync = async (jids: string[]): Promise<LIDMapping[] | undefined> => {
let usyncQuery = new USyncQuery().withLIDProtocol().withContext('background')
for (const jid of jids) {
if (isLidUser(jid)) {
logger?.warn('LID user found in LID fetch call')
continue
} else {
usyncQuery.withUser(new USyncUser().withId(jid))
}
}
if (usyncQuery.users.length === 0) {
return [] // return early without forcing an empty query
}
const results = await executeUSyncQuery(usyncQuery)
if (results) {
return results.list.filter(a => !!a.lid).map(({ lid, id }) => ({ pn: id, lid: lid as string }))
}
return []
}
const ev = makeEventBuffer(logger)
const { creds } = authState
// add transaction capability
const keys = addTransactionCapability(authState.keys, logger, transactionOpts)
const signalRepository = makeSignalRepository({ creds, keys }, logger, onWhatsApp)
const signalRepository = makeSignalRepository({ creds, keys }, logger, pnFromLIDUSync)
let lastDateRecv: Date
let epoch = 1