From f5de6e8b2d674b49e44064de37ee6edcc5841efa Mon Sep 17 00:00:00 2001 From: LiaoYijun Date: Thu, 9 Jan 2025 15:09:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?perf:=20=E5=A2=9E=E5=8A=A0=E5=AE=A2?= =?UTF-8?q?=E4=BA=BA=E7=BE=A4=E5=8F=91=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wai-server/core/baileys/index.js | 92 +++++++++++++++++++------------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/wai-server/core/baileys/index.js b/wai-server/core/baileys/index.js index 7144439..d14ca09 100644 --- a/wai-server/core/baileys/index.js +++ b/wai-server/core/baileys/index.js @@ -79,10 +79,13 @@ const createWhatsApp = async phone => { } const messageType = Object.keys(msg.message)[0]; - const remoteNo = decodeJid(msg.key.remoteJid); + // 如果是群发(status@broadcast),participant 是发送人,不然则是 remoteJid + const remoteNo = isJidStatusBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); const externalId = externalIdCache.get(msg.key.id); + const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid); + const conversationType = isPersonal ? 'individual' : 'group'; const isGroup = isJidGroup(msg.key.remoteJid); - let groupSubject = groupSubjectCache.get(msg.key.remoteJid) + let groupSubject = groupSubjectCache.get(msg.key.remoteJid); if (isGroup && groupSubject === undefined) { const groupMetadata = await waSocket.groupMetadata(msg.key.remoteJid); groupSubject = groupMetadata.subject; @@ -111,7 +114,7 @@ const createWhatsApp = async phone => { body: text, }, conversation: { - type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', + type: conversationType, name: groupSubject, }, customerProfile: { @@ -148,7 +151,7 @@ const createWhatsApp = async phone => { link_original: imageMessage.url, }, conversation: { - type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', + type: conversationType, name: groupSubject, }, customerProfile: { @@ -166,34 +169,41 @@ const createWhatsApp = async phone => { for (const msg of upsert.messages) { if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) { const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text; - const fromWhatsAppNo = decodeJid(msg.key.remoteJid); - + // 如果是群发(status@broadcast),participant 是发送人,不然则是 remoteJid + const remoteNo = isJidStatusBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); const externalId = externalIdCache.get(msg.key.id); - if (msg.key.fromMe) { - waEmitter.emit('message:updated', { - id: msg.key.id, - externalId, - status: formatStatus(msg.status), - direction: 'outbound', - from: whatsAppNo, - to: fromWhatsAppNo, - type: 'text', - text: { - body: text, - }, - conversation: { - type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', - }, - customerProfile: { - id: decodeJid(msg.participant), - name: msg.pushName, - }, - whatsAppNo, - fromMe: msg.key.fromMe, - eventSource: serverConfig.name + '.messages.upsert.append', - updateTime: formatTimestamp(msg.messageTimestamp), - }); - } + const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid); + const conversationType = isPersonal ? 'individual' : 'group'; + + const emitEventName = msg.key.fromMe ? 'message:updated' : 'message:received'; + const msgDirection = msg.key.fromMe ? 'outbound' : 'inbound'; + const msgFrom = msg.key.fromMe ? whatsAppNo : remoteNo; + const msgTo = msg.key.fromMe ? remoteNo : whatsAppNo; + const msgStatus = msg.status === undefined ? '' : formatStatus(msg.status); + + waEmitter.emit(emitEventName, { + id: msg.key.id, + externalId, + status: msgStatus, + direction: msgDirection, + from: msgFrom, + to: msgTo, + type: 'text', + text: { + body: text, + }, + conversation: { + type: conversationType, + }, + customerProfile: { + id: decodeJid(msg.participant), + name: msg.pushName, + }, + whatsAppNo, + fromMe: msg.key.fromMe, + eventSource: serverConfig.name + '.messages.upsert.append', + updateTime: formatTimestamp(msg.messageTimestamp), + }); } } } @@ -209,16 +219,26 @@ const createWhatsApp = async phone => { if (ignore) continue; + // 如果是群发(status@broadcast),participant 是发送人,不然则是 remoteJid + const remoteNo = isJidStatusBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); const externalId = externalIdCache.get(msg.key.id); + const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid); + const conversationType = isPersonal ? 'individual' : 'group'; + + const msgDirection = msg.key.fromMe ? 'outbound' : 'inbound'; + const msgFrom = msg.key.fromMe ? whatsAppNo : remoteNo; + const msgTo = msg.key.fromMe ? remoteNo : whatsAppNo; + const msgStatus = msg.status === undefined ? '' : formatStatus(msg.status); + waEmitter.emit('message:updated', { id: msg.key.id, externalId, - status: formatStatus(msg.update?.status), - direction: msg.key.fromMe ? 'outbound' : 'inbound', - from: msg.key.fromMe ? whatsAppNo : decodeJid(msg.key.remoteJid), - to: msg.key.fromMe ? decodeJid(msg.key.remoteJid) : whatsAppNo, + status: msgStatus, + direction: msgDirection, + from: msgFrom, + to: msgTo, conversation: { - type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', + type: conversationType, }, customerProfile: { id: decodeJid(msg.key.participant), From a1ed0df17c056ba4ffdf060337810a1849d6d05f Mon Sep 17 00:00:00 2001 From: LiaoYijun Date: Thu, 9 Jan 2025 15:36:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E7=BE=A4=E5=8F=91?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wai-server/core/baileys/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/wai-server/core/baileys/index.js b/wai-server/core/baileys/index.js index d14ca09..a71b5fc 100644 --- a/wai-server/core/baileys/index.js +++ b/wai-server/core/baileys/index.js @@ -7,7 +7,7 @@ const { makeInMemoryStore, useMultiFileAuthState, downloadMediaMessage, - isJidUser, isJidGroup + isJidUser, isJidGroup, isJidBroadcast } = require('@whiskeysockets/baileys'); const fs = require('fs'); const path = require('path'); @@ -80,12 +80,12 @@ const createWhatsApp = async phone => { const messageType = Object.keys(msg.message)[0]; // 如果是群发(status@broadcast),participant 是发送人,不然则是 remoteJid - const remoteNo = isJidStatusBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); + const remoteNo = isJidBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); const externalId = externalIdCache.get(msg.key.id); - const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid); + const isPersonal = isJidUser(msg.key.remoteJid) || isJidBroadcast(msg.key.remoteJid); const conversationType = isPersonal ? 'individual' : 'group'; const isGroup = isJidGroup(msg.key.remoteJid); - let groupSubject = groupSubjectCache.get(msg.key.remoteJid); + let groupSubject = groupSubjectCache.get(msg.key.remoteJid); if (isGroup && groupSubject === undefined) { const groupMetadata = await waSocket.groupMetadata(msg.key.remoteJid); groupSubject = groupMetadata.subject; @@ -170,9 +170,9 @@ const createWhatsApp = async phone => { if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) { const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text; // 如果是群发(status@broadcast),participant 是发送人,不然则是 remoteJid - const remoteNo = isJidStatusBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); + const remoteNo = isJidBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); const externalId = externalIdCache.get(msg.key.id); - const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid); + const isPersonal = isJidUser(msg.key.remoteJid) || isJidBroadcast(msg.key.remoteJid); const conversationType = isPersonal ? 'individual' : 'group'; const emitEventName = msg.key.fromMe ? 'message:updated' : 'message:received'; @@ -220,9 +220,9 @@ const createWhatsApp = async phone => { if (ignore) continue; // 如果是群发(status@broadcast),participant 是发送人,不然则是 remoteJid - const remoteNo = isJidStatusBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); + const remoteNo = isJidBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); const externalId = externalIdCache.get(msg.key.id); - const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid); + const isPersonal = isJidUser(msg.key.remoteJid) || isJidBroadcast(msg.key.remoteJid); const conversationType = isPersonal ? 'individual' : 'group'; const msgDirection = msg.key.fromMe ? 'outbound' : 'inbound';