libsignal,lid-mapping, etc.: Partially fix "No sessions" on hosted JIDs

This commit is contained in:
Rajeh Taher
2025-10-16 18:31:01 +03:00
parent d0feb240fa
commit e41a66b3c4
8 changed files with 37 additions and 30 deletions
+2 -1
View File
@@ -45,6 +45,7 @@ import { makeMutex } from '../Utils/make-mutex'
import {
areJidsSameUser,
type BinaryNode,
binaryNodeToString,
getAllBinaryNodeChildren,
getBinaryNodeChild,
getBinaryNodeChildBuffer,
@@ -1272,7 +1273,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
})
])
} catch (error) {
logger.error({ error, node }, 'error in handling message')
logger.error({ error, node: binaryNodeToString(node) }, 'error in handling message')
}
}
+9 -7
View File
@@ -44,8 +44,8 @@ import {
getBinaryNodeChildren,
getServerFromDomainType,
isJidGroup,
isJidHostedLidUser,
isJidHostedPnUser,
isHostedLidUser,
isHostedPnUser,
isLidUser,
isPnUser,
jidDecode,
@@ -381,6 +381,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
const uniqueJids = [...new Set(jids)] // Deduplicate JIDs
const jidsRequiringFetch: string[] = []
logger.debug({ jids }, 'assertSessions call with jids')
// Check peerSessionsCache and validate sessions using libsignal loadSession
for (const jid of uniqueJids) {
const signalId = signalRepository.jidToSignalProtocolAddress(jid)
@@ -404,10 +406,10 @@ export const makeMessagesSocket = (config: SocketConfig) => {
if (jidsRequiringFetch.length) {
// LID if mapped, otherwise original
const wireJids = [
...jidsRequiringFetch.filter(jid => !!jid.includes('@lid')),
...jidsRequiringFetch.filter(jid => !!jid.includes('@lid') || !!jid.includes('@hosted.lid')),
...(
(await signalRepository.lidMapping.getLIDsForPNs(
jidsRequiringFetch.filter(jid => !!jid.includes('@s.whatsapp.net'))
jidsRequiringFetch.filter(jid => !!jid.includes('@s.whatsapp.net') || !!jid.includes('@hosted'))
)) || []
).map(a => a.lid)
]
@@ -715,8 +717,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
const hasKey = !!senderKeyMap[deviceJid]
if (
(!hasKey || !!participant) &&
!isJidHostedLidUser(deviceJid) &&
!isJidHostedPnUser(deviceJid) &&
!isHostedLidUser(deviceJid) &&
!isHostedPnUser(deviceJid) &&
device.device !== 99
) {
//todo: revamp all this logic
@@ -839,7 +841,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
}
// Check if this is our device (could match either PN or LID user)
const isMe = user === mePnUser || (meLidUser && user === meLidUser)
const isMe = user === mePnUser || user === meLidUser
if (isMe) {
meRecipients.push(jid)
+7 -4
View File
@@ -423,7 +423,7 @@ export const makeSocket = (config: SocketConfig) => {
let lastUploadTime = 0
/** generates and uploads a set of pre-keys to the server */
const uploadPreKeys = async (count = INITIAL_PREKEY_COUNT, retryCount = 0) => {
const uploadPreKeys = async (count = MIN_PREKEY_COUNT, retryCount = 0) => {
// Check minimum interval (except for retries)
if (retryCount === 0) {
const timeSinceLastUpload = Date.now() - lastUploadTime
@@ -457,7 +457,7 @@ export const makeSocket = (config: SocketConfig) => {
logger.info({ count }, 'uploaded pre-keys successfully')
lastUploadTime = Date.now()
} catch (uploadError) {
logger.error({ uploadError, count }, 'Failed to upload pre-keys to server')
logger.error({ uploadError: (uploadError as Error).toString(), count }, 'Failed to upload pre-keys to server')
// Exponential backoff retry (max 3 retries)
if (retryCount < 3) {
@@ -500,13 +500,16 @@ export const makeSocket = (config: SocketConfig) => {
const uploadPreKeysToServerIfRequired = async () => {
try {
let count = 0
const preKeyCount = await getAvailablePreKeysOnServer()
if (preKeyCount == 0) count = INITIAL_PREKEY_COUNT
else count = MIN_PREKEY_COUNT
const { exists: currentPreKeyExists, currentPreKeyId } = await verifyCurrentPreKeyExists()
logger.info(`${preKeyCount} pre-keys found on server`)
logger.info(`Current prekey ID: ${currentPreKeyId}, exists in storage: ${currentPreKeyExists}`)
const lowServerCount = preKeyCount <= MIN_PREKEY_COUNT
const lowServerCount = preKeyCount <= count
const missingCurrentPreKey = !currentPreKeyExists && currentPreKeyId > 0
const shouldUpload = lowServerCount || missingCurrentPreKey
@@ -517,7 +520,7 @@ export const makeSocket = (config: SocketConfig) => {
if (missingCurrentPreKey) reasons.push(`current prekey ${currentPreKeyId} missing from storage`)
logger.info(`Uploading PreKeys due to: ${reasons.join(', ')}`)
await uploadPreKeys()
await uploadPreKeys(count)
} else {
logger.info(`PreKey validation passed - Server: ${preKeyCount}, Current prekey ${currentPreKeyId} exists`)
}