Revert "refactor: flatten participant encryption flow for lint"
This reverts commit a790bf616d.
This commit is contained in:
+38
-65
@@ -74,6 +74,8 @@ import {
|
|||||||
import { USyncQuery, USyncUser } from '../WAUSync'
|
import { USyncQuery, USyncUser } from '../WAUSync'
|
||||||
import { makeNewsletterSocket } from './newsletter'
|
import { makeNewsletterSocket } from './newsletter'
|
||||||
|
|
||||||
|
/* eslint-disable max-depth */
|
||||||
|
|
||||||
export const makeMessagesSocket = (config: SocketConfig) => {
|
export const makeMessagesSocket = (config: SocketConfig) => {
|
||||||
const {
|
const {
|
||||||
logger,
|
logger,
|
||||||
@@ -599,57 +601,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
return patchedMessage
|
return patchedMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
const encryptPatchedMessageForRecipient = async ({
|
/* eslint-disable max-depth */
|
||||||
jid,
|
|
||||||
patchedMessage,
|
|
||||||
dsmMessage,
|
|
||||||
meId,
|
|
||||||
meLid,
|
|
||||||
meLidUser,
|
|
||||||
extraAttrs,
|
|
||||||
onPkmsg
|
|
||||||
}: {
|
|
||||||
jid: string
|
|
||||||
patchedMessage: proto.IMessage
|
|
||||||
dsmMessage: proto.IMessage | undefined
|
|
||||||
meId: string
|
|
||||||
meLid: string | undefined
|
|
||||||
meLidUser: string | null | undefined
|
|
||||||
extraAttrs: BinaryNode['attrs'] | undefined
|
|
||||||
onPkmsg: () => void
|
|
||||||
}) => {
|
|
||||||
if (!jid) return null
|
|
||||||
|
|
||||||
try {
|
|
||||||
const msgToEncrypt = resolveDsmMessageForRecipient(jid, patchedMessage, dsmMessage, meId, meLid, meLidUser)
|
|
||||||
const bytes = encodeWAMessage(msgToEncrypt)
|
|
||||||
const mutexKey = jid
|
|
||||||
|
|
||||||
return await encryptionMutex.mutex(mutexKey, async () => {
|
|
||||||
const { type, ciphertext } = await signalRepository.encryptMessage({ jid, data: bytes })
|
|
||||||
|
|
||||||
if (type === 'pkmsg') {
|
|
||||||
onPkmsg()
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
tag: 'to',
|
|
||||||
attrs: { jid },
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
tag: 'enc',
|
|
||||||
attrs: { v: '2', type, ...(extraAttrs || {}) },
|
|
||||||
content: ciphertext
|
|
||||||
}
|
|
||||||
]
|
|
||||||
} as BinaryNode
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
logger.error({ jid, err }, 'Failed to encrypt for recipient')
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const createParticipantNodes = async (
|
const createParticipantNodes = async (
|
||||||
recipientJids: string[],
|
recipientJids: string[],
|
||||||
message: proto.IMessage,
|
message: proto.IMessage,
|
||||||
@@ -670,22 +622,42 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
if (!meId) throw new Boom('Not authenticated', { statusCode: 401 })
|
if (!meId) throw new Boom('Not authenticated', { statusCode: 401 })
|
||||||
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 onPkmsg = () => {
|
|
||||||
shouldIncludeDeviceIdentity = true
|
|
||||||
}
|
|
||||||
|
|
||||||
const encryptionPromises = (patchedMessages as { recipientJid: string; message: proto.IMessage }[]).map(
|
const encryptionPromises = (patchedMessages as { recipientJid: string; message: proto.IMessage }[]).map(
|
||||||
({ recipientJid: jid, message: patchedMessage }: { recipientJid: string; message: proto.IMessage }) =>
|
async ({ recipientJid: jid, message: patchedMessage }: { recipientJid: string; message: proto.IMessage }) => {
|
||||||
encryptPatchedMessageForRecipient({
|
try {
|
||||||
jid,
|
if (!jid) return null
|
||||||
patchedMessage,
|
|
||||||
dsmMessage,
|
const msgToEncrypt = resolveDsmMessageForRecipient(jid, patchedMessage, dsmMessage, meId, meLid, meLidUser)
|
||||||
meId,
|
const bytes = encodeWAMessage(msgToEncrypt)
|
||||||
meLid,
|
const mutexKey = jid
|
||||||
meLidUser,
|
|
||||||
extraAttrs,
|
const node = await encryptionMutex.mutex(mutexKey, async () => {
|
||||||
onPkmsg
|
const { type, ciphertext } = await signalRepository.encryptMessage({ jid, data: bytes })
|
||||||
})
|
|
||||||
|
if (type === 'pkmsg') {
|
||||||
|
shouldIncludeDeviceIdentity = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
tag: 'to',
|
||||||
|
attrs: { jid },
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
tag: 'enc',
|
||||||
|
attrs: { v: '2', type, ...(extraAttrs || {}) },
|
||||||
|
content: ciphertext
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return node
|
||||||
|
} catch (err) {
|
||||||
|
logger.error({ jid, err }, 'Failed to encrypt for recipient')
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const nodes = (await Promise.all(encryptionPromises)).filter(node => node !== null) as BinaryNode[]
|
const nodes = (await Promise.all(encryptionPromises)).filter(node => node !== null) as BinaryNode[]
|
||||||
@@ -697,6 +669,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
return { nodes, shouldIncludeDeviceIdentity }
|
return { nodes, shouldIncludeDeviceIdentity }
|
||||||
}
|
}
|
||||||
|
/* eslint-enable max-depth */
|
||||||
|
|
||||||
// Interactive message detection and binary node injection
|
// Interactive message detection and binary node injection
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user