messages-send: clean up group message send logic

This commit is contained in:
Rajeh Taher
2025-10-02 21:35:53 +03:00
parent 0af6c2f298
commit 929693e2a1
+32 -26
View File
@@ -11,7 +11,6 @@ import type {
SocketConfig,
WAMessageKey
} from '../Types'
import { WAMessageAddressingMode } from '../Types'
import {
aggregateMessageKeysNotFromMe,
assertMediaContent,
@@ -307,8 +306,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
// Process all devices for this user
for (const item of userDevices) {
const finalJid = isLidUser
? jidEncode(user, item.server == 'hosted' ? 'hosted.lid' : 'lid', item.device)
: jidEncode(item.user, item.server == 'hosted' ? 'hosted' :'s.whatsapp.net', item.device)
? jidEncode(user, item.server === 'hosted' ? 'hosted.lid' : 'lid', item.device)
: jidEncode(item.user, item.server === 'hosted' ? 'hosted' : 's.whatsapp.net', item.device)
deviceResults.push({
...item,
@@ -554,17 +553,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
const isNewsletter = server === 'newsletter'
const finalJid = jid
// ADDRESSING CONSISTENCY: Match own identity to conversation context
// TODO: investigate if this is true
let ownId = meId
if (isLid && meLid) {
ownId = meLid
logger.debug({ to: jid, ownId }, 'Using LID identity for @lid conversation')
} else {
logger.debug({ to: jid, ownId }, 'Using PN identity for @s.whatsapp.net conversation')
}
msgId = msgId || generateMessageIDV2(sock.user?.id)
msgId = msgId || generateMessageIDV2(meId)
useUserDevicesCache = useUserDevicesCache !== false
useCachedGroupMetadata = useCachedGroupMetadata !== false && !isStatus
@@ -636,13 +625,15 @@ export const makeMessagesSocket = (config: SocketConfig) => {
if (groupData && Array.isArray(groupData?.participants)) {
logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata')
} else if (!isStatus) {
groupData = await groupMetadata(jid)
groupData = await groupMetadata(jid) // TODO: start storing group participant list + addr mode in Signal & stop relying on this
}
return groupData
})(),
(async () => {
if (!participant && !isStatus) {
// what if sender memory is less accurate than the cached metadata
// on participant change in group, we should do sender memory manipulation
const result = await authState.keys.get('sender-key-memory', [jid]) // TODO: check out what if the sender key memory doesn't include the LID stuff now?
return result[jid] || {}
}
@@ -652,14 +643,18 @@ export const makeMessagesSocket = (config: SocketConfig) => {
])
if (!participant) {
const participantsList = groupData && !isStatus ? groupData.participants.map(p => p.id) : []
if (isStatus && statusJidList) {
participantsList.push(...statusJidList)
}
const participantsList = []
if (isStatus) {
if (statusJidList?.length) participantsList.push(...statusJidList)
} else {
// default to LID based groups
let groupAddressingMode = 'lid'
if (groupData) {
participantsList.push(...groupData.participants.map(p => p.id))
groupAddressingMode = groupData?.addressingMode || groupAddressingMode
}
if (!isStatus) {
const groupAddressingMode =
groupData?.addressingMode || (isLid ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN)
// default to lid addressing mode in a group
additionalAttributes = {
...additionalAttributes,
addressing_mode: groupAddressingMode
@@ -683,7 +678,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
}
const bytes = encodeWAMessage(patched)
const groupAddressingMode = groupData?.addressingMode || (isLid ? 'lid' : 'pn')
const groupAddressingMode = additionalAttributes?.['addressing_mode'] || groupData?.addressingMode || 'lid'
const groupSenderIdentity = groupAddressingMode === 'lid' && meLid ? meLid : meId
const { ciphertext, senderKeyDistributionMessage } = await signalRepository.encryptGroupMessage({
@@ -696,7 +691,9 @@ export const makeMessagesSocket = (config: SocketConfig) => {
for (const device of devices) {
const deviceJid = device.jid
const hasKey = !!senderKeyMap[deviceJid]
if (!hasKey && !isRetryResend) {
if (!hasKey || !!participant) {
//todo: revamp all this logic
// the goal is to follow with what I said above for each group, and instead of a true false map of ids, we can set an array full of those the app has already sent pkmsgs
senderKeyRecipients.push(deviceJid)
senderKeyMap[deviceJid] = true
}
@@ -746,6 +743,16 @@ export const makeMessagesSocket = (config: SocketConfig) => {
await authState.keys.set({ 'sender-key-memory': { [jid]: senderKeyMap } })
}
} else {
// ADDRESSING CONSISTENCY: Match own identity to conversation context
// TODO: investigate if this is true
let ownId = meId
if (isLid && meLid) {
ownId = meLid
logger.debug({ to: jid, ownId }, 'Using LID identity for @lid conversation')
} else {
logger.debug({ to: jid, ownId }, 'Using PN identity for @s.whatsapp.net conversation')
}
const { user: ownUser } = jidDecode(ownId)!
if (!participant) {
@@ -753,10 +760,9 @@ export const makeMessagesSocket = (config: SocketConfig) => {
devices.push({
user,
device: 0,
jid: jidEncode(user, targetUserServer, 0)
jid: jidEncode(user, targetUserServer, 0) // rajeh, todo: this entire logic is convoluted and weird.
})
// Own user matches conversation addressing mode
if (user !== ownUser) {
const ownUserServer = isLid ? 'lid' : 's.whatsapp.net'
const ownUserForAddressing = isLid && meLid ? jidDecode(meLid)!.user : jidDecode(meId)!.user