Skip to content

Commit 1afc566

Browse files
committed
lid-mapping, messages-send: Batch getLIDForPN calls
1 parent 50b36ec commit 1afc566

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/Signal/lid-mapping.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LRUCache } from 'lru-cache'
22
import type { LIDMapping, SignalKeyStoreWithTransaction } from '../Types'
33
import type { ILogger } from '../Utils/logger'
4-
import { isLidUser, isPnUser, jidDecode } from '../WABinary'
4+
import { isLidUser, isPnUser, jidDecode, jidNormalizedUser } from '../WABinary'
55

66
export class LIDMappingStore {
77
private readonly mappingCache = new LRUCache<string, string>({
@@ -89,7 +89,7 @@ export class LIDMappingStore {
8989
}
9090

9191
async getLIDsForPNs(pns: string[]): Promise<LIDMapping[] | null> {
92-
const usyncFetch: string[] = []
92+
const usyncFetch: { [_: string]: number[] } = {}
9393
// mapped from pn to lid mapping to prevent duplication in results later
9494
const successfulPairs: { [_: string]: LIDMapping } = {}
9595
for (const pn of pns) {
@@ -112,7 +112,14 @@ export class LIDMappingStore {
112112
this.mappingCache.set(`lid:${lidUser}`, pnUser)
113113
} else {
114114
this.logger.trace(`No LID mapping found for PN user ${pnUser}; batch getting from USync`)
115-
usyncFetch.push(pn)
115+
const device = decoded.device || 0
116+
const normalizedPn = jidNormalizedUser(pn)
117+
if (!usyncFetch[normalizedPn]) {
118+
usyncFetch[normalizedPn] = [device]
119+
} else {
120+
usyncFetch[normalizedPn].push(device)
121+
}
122+
116123
continue
117124
}
118125
}
@@ -125,20 +132,32 @@ export class LIDMappingStore {
125132

126133
// Push the PN device ID to the LID to maintain device separation
127134
const pnDevice = decoded.device !== undefined ? decoded.device : 0
128-
const deviceSpecificLid = `${lidUser}:${pnDevice}@lid`
135+
const deviceSpecificLid = `${lidUser}${!!pnDevice ? `:${pnDevice}` : ``}@lid`
129136

130137
this.logger.trace(`getLIDForPN: ${pn}${deviceSpecificLid} (user mapping with device ${pnDevice})`)
131138
successfulPairs[pn] = { lid: deviceSpecificLid, pn }
132139
}
133140

134-
if (usyncFetch.length > 0) {
135-
const result = await this.pnToLIDFunc?.(usyncFetch) // this function already adds LIDs to mapping
141+
if (Object.keys(usyncFetch).length > 0) {
142+
const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch)) // this function already adds LIDs to mapping
136143
if (result && result.length > 0) {
137144
this.storeLIDPNMappings(result)
138145
for (const pair of result) {
146+
const pnUser = jidDecode(pair.pn)?.user
147+
if (!pnUser) continue
139148
const lidUser = jidDecode(pair.lid)?.user
140-
if (lidUser) {
141-
successfulPairs[pair.pn] = pair
149+
if (!lidUser) continue
150+
151+
for (const device of usyncFetch[pair.pn]!) {
152+
const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@lid`
153+
154+
this.logger.trace(
155+
`getLIDForPN: USYNC success for ${pair.pn}${deviceSpecificLid} (user mapping with device ${device})`
156+
)
157+
158+
const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@s.whatsapp.net`
159+
160+
successfulPairs[deviceSpecificPn] = { lid: deviceSpecificLid, pn: deviceSpecificPn }
142161
}
143162
}
144163
} else {

src/Socket/messages-send.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,14 @@ export const makeMessagesSocket = (config: SocketConfig) => {
392392

393393
if (jidsRequiringFetch.length) {
394394
// LID if mapped, otherwise original
395-
const wireJids = await Promise.all(
396-
jidsRequiringFetch.map(async jid => {
397-
if (jid.includes('@s.whatsapp.net')) {
398-
const lid = await signalRepository.lidMapping.getLIDForPN(jid)
399-
return lid ? lid : jid
400-
}
401-
402-
return jid
403-
})
404-
)
395+
const wireJids = [
396+
...jidsRequiringFetch.filter(jid => !!jid.includes('@lid')),
397+
...(
398+
(await signalRepository.lidMapping.getLIDsForPNs(
399+
jidsRequiringFetch.filter(jid => !!jid.includes('@s.whatsapp.net'))
400+
)) || []
401+
).map(a => a.lid)
402+
]
405403

406404
logger.debug({ jidsRequiringFetch, wireJids }, 'fetching sessions')
407405
const result = await query({

0 commit comments

Comments
 (0)