fix: optimize LID migration performance and introduce cache (#1782)

* chore: lid-map cache, bulk migration, fixes

* fix: type error

* fix: signalrepository with lid type, remove check from assertsessions, lid-mapping store level deduplication

* fix: migrations logs

* fix: remove migrationsops lenght check

* fix: skip deletesession if cached or existing migrations

* chore: remove check migrated session

* fix: remove return null loadsession

* fix: getLIDforPN call and jidsrequiringfetch

* fix: longer lid map cache TTL

* fix: lint

* fix: lid type

* fix: lid socket type
This commit is contained in:
Gustavo Quadri
2025-09-14 15:58:59 -03:00
committed by GitHub
parent 42c980778e
commit 194b557b0f
10 changed files with 241 additions and 220 deletions
+7 -6
View File
@@ -1,6 +1,7 @@
import { Boom } from '@hapi/boom'
import { proto } from '../../WAProto/index.js'
import type { SignalRepository, WAMessage, WAMessageKey } from '../Types'
import type { WAMessage, WAMessageKey } from '../Types'
import type { SignalRepositoryWithLIDStore } from '../Types/Signal'
import {
areJidsSameUser,
type BinaryNode,
@@ -16,26 +17,26 @@ import {
import { unpadRandomMax16 } from './generics'
import type { ILogger } from './logger'
const getDecryptionJid = async (sender: string, repository: SignalRepository): Promise<string> => {
const getDecryptionJid = async (sender: string, repository: SignalRepositoryWithLIDStore): Promise<string> => {
if (!sender.includes('@s.whatsapp.net')) {
return sender
}
return (await repository.getLIDMappingStore().getLIDForPN(sender))!
return (await repository.lidMapping.getLIDForPN(sender))!
}
const storeMappingFromEnvelope = async (
stanza: BinaryNode,
sender: string,
decryptionJid: string,
repository: SignalRepository,
repository: SignalRepositoryWithLIDStore,
logger: ILogger
): Promise<void> => {
const { senderAlt } = extractAddressingContext(stanza)
if (senderAlt && isLidUser(senderAlt) && isPnUser(sender) && decryptionJid === sender) {
try {
await repository.storeLIDPNMapping(senderAlt, sender)
await repository.lidMapping.storeLIDPNMappings([{ lid: senderAlt, pn: sender }])
logger.debug({ sender, senderAlt }, 'Stored LID mapping from envelope')
} catch (error) {
logger.warn({ sender, senderAlt, error }, 'Failed to store LID mapping')
@@ -206,7 +207,7 @@ export const decryptMessageNode = (
stanza: BinaryNode,
meId: string,
meLid: string,
repository: SignalRepository,
repository: SignalRepositoryWithLIDStore,
logger: ILogger
) => {
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid)
+3 -4
View File
@@ -10,7 +10,7 @@ import type {
RequestJoinAction,
RequestJoinMethod,
SignalKeyStoreWithTransaction,
SignalRepository
SignalRepositoryWithLIDStore
} from '../Types'
import { WAMessageStubType } from '../Types'
import { getContentType, normalizeMessageContent } from '../Utils/messages'
@@ -28,7 +28,7 @@ type ProcessMessageContext = {
ev: BaileysEventEmitter
logger?: ILogger
options: AxiosRequestConfig<{}>
signalRepository: SignalRepository
signalRepository: SignalRepositoryWithLIDStore
}
const REAL_MSG_STUB_TYPES = new Set([
@@ -304,7 +304,6 @@ const processMessage = async (
])
break
case proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
const lidMappingStore = signalRepository.getLIDMappingStore()
const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload!
const { pnToLidMappings, chatDbMigrationTimestamp } =
proto.LIDMigrationMappingSyncPayload.decode(encodedPayload)
@@ -315,7 +314,7 @@ const processMessage = async (
pairs.push({ lid: `${lid}@lid`, pn: `${pn}@s.whatsapp.net` })
}
await lidMappingStore.storeLIDPNMappings(pairs)
await signalRepository.lidMapping.storeLIDPNMappings(pairs)
}
} else if (content?.reactionMessage) {
const reaction: proto.IReaction = {
+2 -2
View File
@@ -1,5 +1,5 @@
import { KEY_BUNDLE_TYPE } from '../Defaults'
import type { SignalRepository } from '../Types'
import type { SignalRepositoryWithLIDStore } from '../Types'
import type {
AuthenticationCreds,
AuthenticationState,
@@ -85,7 +85,7 @@ export const xmppPreKey = (pair: KeyPair, id: number): BinaryNode => ({
]
})
export const parseAndInjectE2ESessions = async (node: BinaryNode, repository: SignalRepository) => {
export const parseAndInjectE2ESessions = async (node: BinaryNode, repository: SignalRepositoryWithLIDStore) => {
const extractKey = (key: BinaryNode) =>
key
? {