Spaces:
Sleeping
Sleeping
File size: 1,898 Bytes
525d39f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import { IncomingMessage } from '../interfaces/whatsapp-engine.interface';
/**
* The subset of whatsapp-web.js `Message` fields we read synchronously to build
* the base of an {@link IncomingMessage}. Declared explicitly so the mapping is
* unit-testable without constructing a full wwebjs `Message`.
*/
export interface RawMessageFields {
id: { _serialized: string };
from: string;
to: string;
body: string;
type: string;
timestamp: number;
fromMe: boolean;
/** Set on group messages: the participant WID that actually sent the message. */
author?: string;
/** Raw wwebjs payload; `notifyName` carries the sender's push name without an extra lookup. */
_data?: { notifyName?: string };
}
/**
* Build the synchronous base of an IncomingMessage from a raw wwebjs message.
* Async enrichment (media, quoted message, saved-contact name) is layered on by
* the adapter; this covers the fields available without an await.
*/
export function buildIncomingMessageBase(msg: RawMessageFields): IncomingMessage {
// For an outgoing (fromMe) message `from` is the account's own JID and `to` is the conversation;
// for an incoming message it's the reverse. So the chat is `to` when fromMe, else `from`.
const chatId = msg.fromMe ? msg.to : msg.from;
const incoming: IncomingMessage = {
id: msg.id._serialized,
from: msg.from,
to: msg.to,
chatId,
body: msg.body,
type: msg.type,
timestamp: msg.timestamp,
fromMe: msg.fromMe,
isGroup: chatId.endsWith('@g.us'),
};
// In a group, `from` is the group JID, so `author` is the only way to know the real sender.
if (msg.author) {
incoming.author = msg.author;
}
// Push name is available synchronously on the raw payload — no contact lookup needed.
const pushName = msg._data?.notifyName;
if (pushName) {
incoming.contact = { pushName };
}
return incoming;
}
|