Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0bc9f4a34e | |||
| 2d07ddcc7a |
+43
-56
@@ -417,39 +417,6 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
)
|
||||
}
|
||||
|
||||
const resolveSessionJid = async (jid: string) => {
|
||||
if (isLidUser(jid) || isHostedLidUser(jid)) {
|
||||
return jid
|
||||
}
|
||||
|
||||
if (isPnUser(jid) || isHostedPnUser(jid)) {
|
||||
return await signalRepository.lidMapping.getLIDForPN(jid) || jid
|
||||
}
|
||||
|
||||
return jid
|
||||
}
|
||||
|
||||
const canonicalizeSessionRecipients = async (recipientJids: string[]) => {
|
||||
const uniqueRecipients = [...new Set(recipientJids)]
|
||||
const canonicalRecipients: string[] = []
|
||||
|
||||
for (const jid of uniqueRecipients) {
|
||||
const canonicalJid = await resolveSessionJid(jid)
|
||||
if (!canonicalRecipients.includes(canonicalJid)) {
|
||||
canonicalRecipients.push(canonicalJid)
|
||||
}
|
||||
}
|
||||
|
||||
if (canonicalRecipients.length !== uniqueRecipients.length || canonicalRecipients.some((jid, index) => jid !== uniqueRecipients[index])) {
|
||||
logger.debug(
|
||||
{ before: uniqueRecipients, after: canonicalRecipients },
|
||||
'[SESSION] Canonicalized recipient addressing for session reuse'
|
||||
)
|
||||
}
|
||||
|
||||
return canonicalRecipients
|
||||
}
|
||||
|
||||
const assertSessions = async (jids: string[], force?: boolean) => {
|
||||
let didFetchNewSession = false
|
||||
const uniqueJids = [...new Set(jids)] // Deduplicate JIDs
|
||||
@@ -459,32 +426,22 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
|
||||
// Check peerSessionsCache and validate sessions using libsignal loadSession
|
||||
for (const jid of uniqueJids) {
|
||||
const canonicalJid = await resolveSessionJid(jid)
|
||||
const signalIds = [
|
||||
signalRepository.jidToSignalProtocolAddress(jid),
|
||||
signalRepository.jidToSignalProtocolAddress(canonicalJid)
|
||||
]
|
||||
const cachedSession = signalIds
|
||||
.map(signalId => peerSessionsCache.get(signalId))
|
||||
.find(session => session !== undefined)
|
||||
|
||||
const signalId = signalRepository.jidToSignalProtocolAddress(jid)
|
||||
const cachedSession = peerSessionsCache.get(signalId)
|
||||
if (cachedSession !== undefined) {
|
||||
if (cachedSession && !force) {
|
||||
continue // Session exists in cache
|
||||
}
|
||||
} else {
|
||||
const sessionValidation = await signalRepository.validateSession(canonicalJid)
|
||||
const sessionValidation = await signalRepository.validateSession(jid)
|
||||
const hasSession = sessionValidation.exists
|
||||
for (const signalId of signalIds) {
|
||||
peerSessionsCache.set(signalId, hasSession)
|
||||
}
|
||||
|
||||
peerSessionsCache.set(signalId, hasSession)
|
||||
if (hasSession && !force) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
jidsRequiringFetch.push(canonicalJid)
|
||||
jidsRequiringFetch.push(jid)
|
||||
}
|
||||
|
||||
if (jidsRequiringFetch.length) {
|
||||
@@ -725,6 +682,33 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
return false
|
||||
}
|
||||
|
||||
const canonicalizeCarouselRecipients = async (recipientJids: string[]) => {
|
||||
const uniqueRecipients = [...new Set(recipientJids)]
|
||||
const pnRecipients = uniqueRecipients.filter(jid => isPnUser(jid) || isHostedPnUser(jid))
|
||||
const lidMappings = pnRecipients.length > 0
|
||||
? await signalRepository.lidMapping.getLIDsForPNs(pnRecipients)
|
||||
: []
|
||||
|
||||
const lidByPn = new Map((lidMappings || []).map(({ pn, lid }) => [pn, lid]))
|
||||
const canonicalRecipients: string[] = []
|
||||
|
||||
for (const jid of uniqueRecipients) {
|
||||
const canonicalJid = lidByPn.get(jid) || jid
|
||||
if (!canonicalRecipients.includes(canonicalJid)) {
|
||||
canonicalRecipients.push(canonicalJid)
|
||||
}
|
||||
}
|
||||
|
||||
if (canonicalRecipients.length !== uniqueRecipients.length || canonicalRecipients.some((jid, index) => jid !== uniqueRecipients[index])) {
|
||||
logger.debug(
|
||||
{ before: uniqueRecipients, after: canonicalRecipients },
|
||||
'[CAROUSEL] Canonicalized recipient addressing for session reuse'
|
||||
)
|
||||
}
|
||||
|
||||
return canonicalRecipients
|
||||
}
|
||||
|
||||
const relayMessage = async (
|
||||
jid: string,
|
||||
message: proto.IMessage,
|
||||
@@ -1011,10 +995,13 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
allRecipients.push(jid)
|
||||
}
|
||||
|
||||
|
||||
await assertSessions(allRecipients)
|
||||
const effectiveMeRecipients = await canonicalizeSessionRecipients(meRecipients)
|
||||
const effectiveOtherRecipients = await canonicalizeSessionRecipients(otherRecipients)
|
||||
const isCarouselFanout = isCarouselMessage(message)
|
||||
const effectiveMeRecipients = isCarouselFanout
|
||||
? await canonicalizeCarouselRecipients(meRecipients)
|
||||
: meRecipients
|
||||
const effectiveOtherRecipients = isCarouselFanout
|
||||
? await canonicalizeCarouselRecipients(otherRecipients)
|
||||
: otherRecipients
|
||||
const effectiveAllRecipients = [...effectiveMeRecipients, ...effectiveOtherRecipients]
|
||||
|
||||
await assertSessions(effectiveAllRecipients)
|
||||
@@ -1024,14 +1011,14 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
{ nodes: otherNodes, shouldIncludeDeviceIdentity: s2 }
|
||||
] = await Promise.all([
|
||||
// For own devices: use DSM if available (1:1 chats only)
|
||||
createParticipantNodes(meRecipients, meMsg || message, extraAttrs),
|
||||
createParticipantNodes(otherRecipients, message, extraAttrs, meMsg)
|
||||
createParticipantNodes(effectiveMeRecipients, meMsg || message, extraAttrs),
|
||||
createParticipantNodes(effectiveOtherRecipients, message, extraAttrs, meMsg)
|
||||
])
|
||||
participants.push(...meNodes)
|
||||
participants.push(...otherNodes)
|
||||
|
||||
if (meRecipients.length > 0 || otherRecipients.length > 0) {
|
||||
extraAttrs['phash'] = generateParticipantHashV2([...meRecipients, ...otherRecipients])
|
||||
if (effectiveMeRecipients.length > 0 || effectiveOtherRecipients.length > 0) {
|
||||
extraAttrs['phash'] = generateParticipantHashV2([...effectiveMeRecipients, ...effectiveOtherRecipients])
|
||||
}
|
||||
|
||||
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || s1 || s2
|
||||
|
||||
Reference in New Issue
Block a user