fix: move DSM helper out of participant loop
This commit is contained in:
+22
-79
@@ -439,39 +439,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) => {
|
const assertSessions = async (jids: string[], force?: boolean) => {
|
||||||
let didFetchNewSession = false
|
let didFetchNewSession = false
|
||||||
const uniqueJids = [...new Set(jids)] // Deduplicate JIDs
|
const uniqueJids = [...new Set(jids)] // Deduplicate JIDs
|
||||||
@@ -481,32 +448,22 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
// Check peerSessionsCache and validate sessions using libsignal loadSession
|
// Check peerSessionsCache and validate sessions using libsignal loadSession
|
||||||
for (const jid of uniqueJids) {
|
for (const jid of uniqueJids) {
|
||||||
const canonicalJid = await resolveSessionJid(jid)
|
const signalId = signalRepository.jidToSignalProtocolAddress(jid)
|
||||||
const signalIds = [
|
const cachedSession = peerSessionsCache.get(signalId)
|
||||||
signalRepository.jidToSignalProtocolAddress(jid),
|
|
||||||
signalRepository.jidToSignalProtocolAddress(canonicalJid)
|
|
||||||
]
|
|
||||||
const cachedSession = signalIds
|
|
||||||
.map(signalId => peerSessionsCache.get(signalId))
|
|
||||||
.find(session => session !== undefined)
|
|
||||||
|
|
||||||
if (cachedSession !== undefined) {
|
if (cachedSession !== undefined) {
|
||||||
if (cachedSession && !force) {
|
if (cachedSession && !force) {
|
||||||
continue // Session exists in cache
|
continue // Session exists in cache
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const sessionValidation = await signalRepository.validateSession(canonicalJid)
|
const sessionValidation = await signalRepository.validateSession(jid)
|
||||||
const hasSession = sessionValidation.exists
|
const hasSession = sessionValidation.exists
|
||||||
for (const signalId of signalIds) {
|
peerSessionsCache.set(signalId, hasSession)
|
||||||
peerSessionsCache.set(signalId, hasSession)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasSession && !force) {
|
if (hasSession && !force) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
jidsRequiringFetch.push(canonicalJid)
|
jidsRequiringFetch.push(jid)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (jidsRequiringFetch.length) {
|
if (jidsRequiringFetch.length) {
|
||||||
@@ -623,7 +580,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
dsmMessage: proto.IMessage | undefined,
|
dsmMessage: proto.IMessage | undefined,
|
||||||
meId: string,
|
meId: string,
|
||||||
meLid: string | undefined,
|
meLid: string | undefined,
|
||||||
meLidUser: string | null
|
meLidUser: string | null | undefined
|
||||||
) => {
|
) => {
|
||||||
if (!dsmMessage) {
|
if (!dsmMessage) {
|
||||||
return patchedMessage
|
return patchedMessage
|
||||||
@@ -641,6 +598,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
return patchedMessage
|
return patchedMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
const createParticipantNodes = async (
|
const createParticipantNodes = async (
|
||||||
recipientJids: string[],
|
recipientJids: string[],
|
||||||
message: proto.IMessage,
|
message: proto.IMessage,
|
||||||
@@ -662,26 +620,6 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
const meLid = authState.creds.me?.lid
|
const meLid = authState.creds.me?.lid
|
||||||
const meLidUser = meLid ? (jidDecode(meLid)?.user ?? null) : null
|
const meLidUser = meLid ? (jidDecode(meLid)?.user ?? null) : null
|
||||||
|
|
||||||
const encryptForRecipient = async (jid: string, bytes: Uint8Array) => {
|
|
||||||
const { type, ciphertext } = await signalRepository.encryptMessage({ jid, data: bytes })
|
|
||||||
|
|
||||||
return {
|
|
||||||
type,
|
|
||||||
node: {
|
|
||||||
tag: 'to',
|
|
||||||
attrs: { jid },
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
tag: 'enc',
|
|
||||||
attrs: { v: '2', type, ...(extraAttrs || {}) },
|
|
||||||
content: ciphertext
|
|
||||||
}
|
|
||||||
]
|
|
||||||
} as BinaryNode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const encryptionPromises = (patchedMessages as { recipientJid: string; message: proto.IMessage }[]).map(
|
const encryptionPromises = (patchedMessages as { recipientJid: string; message: proto.IMessage }[]).map(
|
||||||
async ({ recipientJid: jid, message: patchedMessage }: { recipientJid: string; message: proto.IMessage }) => {
|
async ({ recipientJid: jid, message: patchedMessage }: { recipientJid: string; message: proto.IMessage }) => {
|
||||||
try {
|
try {
|
||||||
@@ -692,13 +630,23 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
const mutexKey = jid
|
const mutexKey = jid
|
||||||
|
|
||||||
const node = await encryptionMutex.mutex(mutexKey, async () => {
|
const node = await encryptionMutex.mutex(mutexKey, async () => {
|
||||||
const { type, node } = await encryptForRecipient(jid, bytes)
|
const { type, ciphertext } = await signalRepository.encryptMessage({ jid, data: bytes })
|
||||||
|
|
||||||
if (type === 'pkmsg') {
|
if (type === 'pkmsg') {
|
||||||
shouldIncludeDeviceIdentity = true
|
shouldIncludeDeviceIdentity = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return node
|
return {
|
||||||
|
tag: 'to',
|
||||||
|
attrs: { jid },
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
tag: 'enc',
|
||||||
|
attrs: { v: '2', type, ...(extraAttrs || {}) },
|
||||||
|
content: ciphertext
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return node
|
return node
|
||||||
@@ -1261,18 +1209,13 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
allRecipients.push(jid)
|
allRecipients.push(jid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isCarouselFanout = isCarouselMessage(message)
|
||||||
const isCarouselRecipientFlow = isCarouselMessage(message)
|
const effectiveMeRecipients = isCarouselFanout
|
||||||
const recipientsForMe = isCarouselRecipientFlow
|
|
||||||
? await canonicalizeCarouselRecipients(meRecipients)
|
? await canonicalizeCarouselRecipients(meRecipients)
|
||||||
: meRecipients
|
: meRecipients
|
||||||
const recipientsForOthers = isCarouselRecipientFlow
|
const effectiveOtherRecipients = isCarouselFanout
|
||||||
? await canonicalizeCarouselRecipients(otherRecipients)
|
? await canonicalizeCarouselRecipients(otherRecipients)
|
||||||
: otherRecipients
|
: otherRecipients
|
||||||
|
|
||||||
await assertSessions(allRecipients)
|
|
||||||
const effectiveMeRecipients = await canonicalizeSessionRecipients(recipientsForMe)
|
|
||||||
const effectiveOtherRecipients = await canonicalizeSessionRecipients(recipientsForOthers)
|
|
||||||
const effectiveAllRecipients = [...effectiveMeRecipients, ...effectiveOtherRecipients]
|
const effectiveAllRecipients = [...effectiveMeRecipients, ...effectiveOtherRecipients]
|
||||||
|
|
||||||
await assertSessions(effectiveAllRecipients)
|
await assertSessions(effectiveAllRecipients)
|
||||||
|
|||||||
Reference in New Issue
Block a user