feat: more efficient message send

This commit is contained in:
Adhiraj Singh
2022-06-16 14:53:23 +05:30
parent ecd8f74800
commit e9f494ec5d

View File

@@ -211,6 +211,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
} }
const assertSessions = async(jids: string[], force: boolean) => { const assertSessions = async(jids: string[], force: boolean) => {
let didFetchNewSession = false
let jidsRequiringFetch: string[] = [] let jidsRequiringFetch: string[] = []
if(force) { if(force) {
jidsRequiringFetch = jids jidsRequiringFetch = jids
@@ -248,15 +249,13 @@ export const makeMessagesSocket = (config: SocketConfig) => {
] ]
}) })
await parseAndInjectE2ESessions(result, authState) await parseAndInjectE2ESessions(result, authState)
return true
didFetchNewSession = true
} }
return false // pre-fetch all sessions to improve efficiency
} // makes a big difference when sending to large groups
// or when there are 100s of devices to push to
const createParticipantNodes = async(jids: string[], bytes: Buffer) => {
await assertSessions(jids, false)
if(authState.keys.isInTransaction()) { if(authState.keys.isInTransaction()) {
await authState.keys.prefetch( await authState.keys.prefetch(
'session', 'session',
@@ -264,7 +263,11 @@ export const makeMessagesSocket = (config: SocketConfig) => {
) )
} }
const nodes = await Promise.all( return didFetchNewSession
}
const createParticipantNodes = (jids: string[], bytes: Buffer) => (
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)
@@ -281,8 +284,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
} }
) )
) )
return nodes )
}
const relayMessage = async( const relayMessage = async(
jid: string, jid: string,
@@ -365,6 +367,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
} }
}) })
await assertSessions(senderKeyJids, false)
participants.push( participants.push(
...(await createParticipantNodes(senderKeyJids, encSenderKeyMsg)) ...(await createParticipantNodes(senderKeyJids, encSenderKeyMsg))
) )
@@ -395,6 +399,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
devices.push(...additionalDevices) devices.push(...additionalDevices)
} }
const allJids: string[] = []
const meJids: string[] = [] const meJids: string[] = []
const otherJids: string[] = [] const otherJids: string[] = []
for(const { user, device } of devices) { for(const { user, device } of devices) {
@@ -405,8 +410,12 @@ export const makeMessagesSocket = (config: SocketConfig) => {
} else { } else {
otherJids.push(jid) otherJids.push(jid)
} }
allJids.push(jid)
} }
await assertSessions(allJids, false)
const [meNodes, otherNodes] = await Promise.all([ const [meNodes, otherNodes] = await Promise.all([
createParticipantNodes(meJids, encodedMeMsg), createParticipantNodes(meJids, encodedMeMsg),
createParticipantNodes(otherJids, encodedMsg) createParticipantNodes(otherJids, encodedMsg)