perf: nicer "shouldIncludeDeviceIdentity" flag computation

This commit is contained in:
Adhiraj Singh
2022-06-17 13:16:41 +05:30
parent 399b4d3cb8
commit 7863c0e4c2
2 changed files with 25 additions and 17 deletions

View File

@@ -266,11 +266,16 @@ export const makeMessagesSocket = (config: SocketConfig) => {
return didFetchNewSession return didFetchNewSession
} }
const createParticipantNodes = (jids: string[], bytes: Buffer) => ( const createParticipantNodes = async(jids: string[], bytes: Buffer) => {
Promise.all( let shouldIncludeDeviceIdentity = false
const nodes = await Promise.all(
jids.map( jids.map(
async jid => { async jid => {
const { type, ciphertext } = await encryptSignalProto(jid, bytes, authState) const { type, ciphertext } = await encryptSignalProto(jid, bytes, authState)
if(type === 'pkmsg') {
shouldIncludeDeviceIdentity = true
}
const node: BinaryNode = { const node: BinaryNode = {
tag: 'to', tag: 'to',
attrs: { jid }, attrs: { jid },
@@ -284,7 +289,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
} }
) )
) )
) return { nodes, shouldIncludeDeviceIdentity }
}
const relayMessage = async( const relayMessage = async(
jid: string, jid: string,
@@ -293,6 +299,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
) => { ) => {
const meId = authState.creds.me!.id const meId = authState.creds.me!.id
let shouldIncludeDeviceIdentity = false
const { user, server } = jidDecode(jid) const { user, server } = jidDecode(jid)
const isGroup = server === 'g.us' const isGroup = server === 'g.us'
msgId = msgId || generateMessageID() msgId = msgId || generateMessageID()
@@ -376,9 +384,10 @@ export const makeMessagesSocket = (config: SocketConfig) => {
await assertSessions(senderKeyJids, false) await assertSessions(senderKeyJids, false)
participants.push( const result = await createParticipantNodes(senderKeyJids, encSenderKeyMsg)
...(await createParticipantNodes(senderKeyJids, encSenderKeyMsg)) shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || result.shouldIncludeDeviceIdentity
)
participants.push(...result.nodes)
} }
binaryNodeContent.push({ binaryNodeContent.push({
@@ -423,12 +432,17 @@ export const makeMessagesSocket = (config: SocketConfig) => {
await assertSessions(allJids, false) await assertSessions(allJids, false)
const [meNodes, otherNodes] = await Promise.all([ const [
{ nodes: meNodes, shouldIncludeDeviceIdentity: s1 },
{ nodes: otherNodes, shouldIncludeDeviceIdentity: s2 }
] = await Promise.all([
createParticipantNodes(meJids, encodedMeMsg), createParticipantNodes(meJids, encodedMeMsg),
createParticipantNodes(otherJids, encodedMsg) createParticipantNodes(otherJids, encodedMsg)
]) ])
participants.push(...meNodes) participants.push(...meNodes)
participants.push(...otherNodes) participants.push(...otherNodes)
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || s1 || s2
} }
if(participants.length) { if(participants.length) {
@@ -465,11 +479,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
stanza.attrs.to = destinationJid stanza.attrs.to = destinationJid
} }
const shouldHaveIdentity = !!participants.find( if(shouldIncludeDeviceIdentity) {
participant => (participant.content! as BinaryNode[]).find(n => n.attrs.type === 'pkmsg')
)
if(shouldHaveIdentity) {
(stanza.content as BinaryNode[]).push({ (stanza.content as BinaryNode[]).push({
tag: 'device-identity', tag: 'device-identity',
attrs: { }, attrs: { },

View File

@@ -175,11 +175,9 @@ export const encryptSignalProto = async(user: string, buffer: Buffer, auth: Sign
const addr = jidToSignalProtocolAddress(user) const addr = jidToSignalProtocolAddress(user)
const cipher = new libsignal.SessionCipher(signalStorage(auth), addr) const cipher = new libsignal.SessionCipher(signalStorage(auth), addr)
const { type, body } = await cipher.encrypt(buffer) const { type: sigType, body } = await cipher.encrypt(buffer)
return { const type = sigType === 3 ? 'pkmsg' : 'msg'
type: type === 3 ? 'pkmsg' : 'msg', return { type, ciphertext: Buffer.from(body, 'binary') }
ciphertext: Buffer.from(body, 'binary')
}
} }
export const encryptSenderKeyMsgSignalProto = async(group: string, data: Uint8Array | Buffer, meId: string, auth: SignalAuthState) => { export const encryptSenderKeyMsgSignalProto = async(group: string, data: Uint8Array | Buffer, meId: string, auth: SignalAuthState) => {