fix(whatsapp): preserve history across reauthorization

This commit is contained in:
wmq
2026-08-01 23:13:28 +08:00
parent 785a5b196c
commit 00b0ebb775
5 changed files with 210 additions and 12 deletions
+36
View File
@@ -0,0 +1,36 @@
import crypto from 'node:crypto'
export function whatsappCustomerStableId(jid) {
const normalized = String(jid || '').trim().toLowerCase()
const hash = crypto.createHash('sha256').update(normalized).digest('hex').slice(0, 24)
return `wa-customer-${hash}`
}
export function whatsappConversationActivityAt(conversation) {
return Date.parse(
conversation?.lastMessage?.createdAt
|| conversation?.updatedAt
|| conversation?.createdAt
|| '',
) || 0
}
export function isWeakWhatsAppDisplayName(value, jid) {
const name = String(value || '').trim()
const jidName = String(jid || '').split('@')[0].replace(/\D/g, '')
const digits = name.replace(/\D/g, '')
return !name || (digits.length >= 7 && (!jidName || digits === jidName))
}
export function reusableWhatsAppConversations(conversations, activeChannelIds, channelId, jid) {
return conversations
.filter((item) => (
item.channel === 'whatsapp'
&& String(item.whatsappJid || '') === String(jid)
&& (
String(item.channelId || '') === String(channelId)
|| !activeChannelIds.has(String(item.channelId || String(item.id).split(':')[0]))
)
))
.sort((left, right) => whatsappConversationActivityAt(right) - whatsappConversationActivityAt(left))
}