fix: send message speed, lid logic, remove messages-send useless functions (#1794)

* fix: remove redundant migration

* fix: remove deduplicatelidpnjids

* fix: assertsessions complexity

* fix: assertsessions call

* fix: remove getencryptionjid

* fix: add wirejid to injecte2esession, remove libsignal lid migration

* fix: logger lid-mapping

* fix: injecte2esession decoded approach

* fix: changes made by @AstroX11

* fix: lint

* fix: lint

* fix: lint

* Revert "fix: injecte2esession decoded approach"

This reverts commit 4e368296ed084398b8a173ec117dc2478e481748.

* fix: centralize getlidforpn calls in messages-send

* fix: add resolvechunklids to signal

* fix: remove useless arrays

* fix: assertsessions logic

* fix: lint

* Revert "fix: assertsessions logic"

This reverts commit 65b0730891c91573edcab1c96af010db54382fba.

* fix: remove onwhatsapp fallback to prevent rate limit, migrate sessions when lid-map is created, new migration logic and user devices key

* fix: migrate session devices, socket creation

* fix: add back decryptionjid validation

* fix: add resolveSignalAddress to get lid

* fix: type error migration

* fix: lint

* fix: device level cache, proposed changes
This commit is contained in:
Gustavo Quadri
2025-09-26 00:53:15 -03:00
committed by GitHub
parent e965385841
commit ae456cb342
11 changed files with 340 additions and 518 deletions
+19 -47
View File
@@ -1,6 +1,6 @@
import { LRUCache } from 'lru-cache'
import type { SignalKeyStoreWithTransaction } from '../Types'
import logger from '../Utils/logger'
import type { ILogger } from '../Utils/logger'
import { isLidUser, isPnUser, jidDecode } from '../WABinary'
export class LIDMappingStore {
@@ -10,28 +10,11 @@ export class LIDMappingStore {
updateAgeOnGet: true
})
private readonly keys: SignalKeyStoreWithTransaction
private onWhatsAppFunc?: (...jids: string[]) => Promise<
| {
jid: string
exists: boolean
lid: string
}[]
| undefined
>
private readonly logger: ILogger
constructor(
keys: SignalKeyStoreWithTransaction,
onWhatsAppFunc?: (...jids: string[]) => Promise<
| {
jid: string
exists: boolean
lid: string
}[]
| undefined
>
) {
constructor(keys: SignalKeyStoreWithTransaction, logger: ILogger) {
this.keys = keys
this.onWhatsAppFunc = onWhatsAppFunc // needed to get LID from PN if not found
this.logger = logger
}
/**
@@ -42,7 +25,7 @@ export class LIDMappingStore {
const pairMap: { [_: string]: string } = {}
for (const { lid, pn } of pairs) {
if (!((isLidUser(lid) && isPnUser(pn)) || (isPnUser(lid) && isLidUser(pn)))) {
logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`)
this.logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`)
continue
}
@@ -54,10 +37,9 @@ export class LIDMappingStore {
const pnUser = pnDecoded.user
const lidUser = lidDecoded.user
// Check if mapping already exists (cache first, then database)
let existingLidUser = this.mappingCache.get(`pn:${pnUser}`)
if (!existingLidUser) {
// Cache miss - check database
this.logger.trace(`Cache miss for PN user ${pnUser}; checking database`)
const stored = await this.keys.get('lid-mapping', [pnUser])
existingLidUser = stored[pnUser]
if (existingLidUser) {
@@ -68,25 +50,24 @@ export class LIDMappingStore {
}
if (existingLidUser === lidUser) {
logger.debug({ pnUser, lidUser }, 'LID mapping already exists, skipping')
this.logger.debug({ pnUser, lidUser }, 'LID mapping already exists, skipping')
continue
}
pairMap[pnUser] = lidUser
}
logger.trace({ pairMap }, `Storing ${Object.keys(pairMap).length} pn mappings`)
this.logger.trace({ pairMap }, `Storing ${Object.keys(pairMap).length} pn mappings`)
await this.keys.transaction(async () => {
for (const [pnUser, lidUser] of Object.entries(pairMap)) {
await this.keys.set({
'lid-mapping': {
[pnUser]: lidUser, // "554396160286" -> "102765716062358"
[`${lidUser}_reverse`]: pnUser // "102765716062358_reverse" -> "554396160286"
[pnUser]: lidUser,
[`${lidUser}_reverse`]: pnUser
}
})
// Update cache with both directions
this.mappingCache.set(`pn:${pnUser}`, lidUser)
this.mappingCache.set(`lid:${lidUser}`, pnUser)
}
@@ -112,26 +93,17 @@ export class LIDMappingStore {
lidUser = stored[pnUser]
if (lidUser) {
// Cache the database result
this.mappingCache.set(`pn:${pnUser}`, lidUser)
this.mappingCache.set(`lid:${lidUser}`, pnUser)
} else {
// Not in database - try USync
logger.trace(`No LID mapping found for PN user ${pnUser}; getting from USync`)
const { exists, lid } = (await this.onWhatsAppFunc?.(pn))?.[0]! // this function already adds LIDs to mapping
if (exists && lid) {
lidUser = jidDecode(lid)?.user
if (lidUser) {
// Cache the USync result
this.mappingCache.set(`pn:${pnUser}`, lidUser)
}
} else {
return null
}
this.logger.trace(`No LID mapping found for PN user ${pnUser}`)
return null
}
}
if (typeof lidUser !== 'string' || !lidUser) {
logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`)
lidUser = lidUser.toString()
if (!lidUser) {
this.logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`)
return null
}
@@ -139,7 +111,7 @@ export class LIDMappingStore {
const pnDevice = decoded.device !== undefined ? decoded.device : 0
const deviceSpecificLid = `${lidUser}:${pnDevice}@lid`
logger.trace(`getLIDForPN: ${pn}${deviceSpecificLid} (user mapping with device ${pnDevice})`)
this.logger.trace(`getLIDForPN: ${pn}${deviceSpecificLid} (user mapping with device ${pnDevice})`)
return deviceSpecificLid
}
@@ -162,7 +134,7 @@ export class LIDMappingStore {
pnUser = stored[`${lidUser}_reverse`]
if (!pnUser || typeof pnUser !== 'string') {
logger.trace(`No reverse mapping found for LID user: ${lidUser}`)
this.logger.trace(`No reverse mapping found for LID user: ${lidUser}`)
return null
}
@@ -173,7 +145,7 @@ export class LIDMappingStore {
const lidDevice = decoded.device !== undefined ? decoded.device : 0
const pnJid = `${pnUser}:${lidDevice}@s.whatsapp.net`
logger.trace(`Found reverse mapping: ${lid}${pnJid}`)
this.logger.trace(`Found reverse mapping: ${lid}${pnJid}`)
return pnJid
}
}