11import { LRUCache } from 'lru-cache'
22import type { LIDMapping , SignalKeyStoreWithTransaction } from '../Types'
33import type { ILogger } from '../Utils/logger'
4- import { isLidUser , isPnUser , jidDecode } from '../WABinary'
4+ import { isLidUser , isPnUser , jidDecode , jidNormalizedUser } from '../WABinary'
55
66export 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 {
0 commit comments