Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/process-message.js
1129 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.shouldIncrementChatUnread = exports.isRealMessage = exports.cleanMessage = void 0;3const WAProto_1 = require("../../WAProto");4const Types_1 = require("../Types");5const Utils_1 = require("../Utils");6const WABinary_1 = require("../WABinary");7const MSG_MISSED_CALL_TYPES = new Set([8Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,9Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE,10Types_1.WAMessageStubType.CALL_MISSED_VIDEO,11Types_1.WAMessageStubType.CALL_MISSED_VOICE12]);13/** Cleans a received message to further processing */14const cleanMessage = (message, meId) => {15// ensure remoteJid and participant doesn't have device or agent in it16message.key.remoteJid = (0, WABinary_1.jidNormalizedUser)(message.key.remoteJid);17message.key.participant = message.key.participant ? (0, WABinary_1.jidNormalizedUser)(message.key.participant) : undefined;18const content = (0, Utils_1.normalizeMessageContent)(message.message);19// if the message has a reaction, ensure fromMe & remoteJid are from our perspective20if (content === null || content === void 0 ? void 0 : content.reactionMessage) {21const msgKey = content.reactionMessage.key;22// if the reaction is from another user23// we've to correctly map the key to this user's perspective24if (!message.key.fromMe) {25// if the sender believed the message being reacted to is not from them26// we've to correct the key to be from them, or some other participant27msgKey.fromMe = !msgKey.fromMe28? (0, WABinary_1.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meId)29// if the message being reacted to, was from them30// fromMe automatically becomes false31: false;32// set the remoteJid to being the same as the chat the message came from33msgKey.remoteJid = message.key.remoteJid;34// set participant of the message35msgKey.participant = msgKey.participant || message.key.participant;36}37}38};39exports.cleanMessage = cleanMessage;40const isRealMessage = (message) => {41const normalizedContent = (0, Utils_1.normalizeMessageContent)(message.message);42return (!!normalizedContent43|| MSG_MISSED_CALL_TYPES.has(message.messageStubType))44&& !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.protocolMessage)45&& !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.reactionMessage);46};47exports.isRealMessage = isRealMessage;48const shouldIncrementChatUnread = (message) => (!message.key.fromMe && !message.messageStubType);49exports.shouldIncrementChatUnread = shouldIncrementChatUnread;50const processMessage = async (message, { downloadHistory, ev, historyCache, recvChats, creds, keyStore, logger }) => {51var _a, _b, _c, _d;52const meId = creds.me.id;53const { accountSettings } = creds;54const chat = { id: (0, WABinary_1.jidNormalizedUser)(message.key.remoteJid) };55if ((0, exports.isRealMessage)(message)) {56chat.conversationTimestamp = (0, Utils_1.toNumber)(message.messageTimestamp);57// only increment unread count if not CIPHERTEXT and from another person58if ((0, exports.shouldIncrementChatUnread)(message)) {59chat.unreadCount = (chat.unreadCount || 0) + 1;60}61if (accountSettings === null || accountSettings === void 0 ? void 0 : accountSettings.unarchiveChats) {62chat.archive = false;63chat.readOnly = false;64}65}66const content = (0, Utils_1.normalizeMessageContent)(message.message);67const protocolMsg = content === null || content === void 0 ? void 0 : content.protocolMessage;68if (protocolMsg) {69switch (protocolMsg.type) {70case WAProto_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:71const histNotification = protocolMsg.historySyncNotification;72logger === null || logger === void 0 ? void 0 : logger.info({ histNotification, id: message.key.id }, 'got history notification');73if (downloadHistory) {74const isLatest = historyCache.size === 0 && !((_a = creds.processedHistoryMessages) === null || _a === void 0 ? void 0 : _a.length);75const { chats, contacts, messages, didProcess } = await (0, Utils_1.downloadAndProcessHistorySyncNotification)(histNotification, historyCache, recvChats);76if (chats.length) {77ev.emit('chats.set', { chats, isLatest });78}79if (messages.length) {80ev.emit('messages.set', { messages, isLatest });81}82if (contacts.length) {83ev.emit('contacts.set', { contacts, isLatest });84}85if (didProcess) {86ev.emit('creds.update', {87processedHistoryMessages: [88...(creds.processedHistoryMessages || []),89{ key: message.key, messageTimestamp: message.messageTimestamp }90]91});92}93}94break;95case WAProto_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:96const keys = protocolMsg.appStateSyncKeyShare.keys;97if (keys === null || keys === void 0 ? void 0 : keys.length) {98let newAppStateSyncKeyId = '';99await keyStore.transaction(async () => {100for (const { keyData, keyId } of keys) {101const strKeyId = Buffer.from(keyId.keyId).toString('base64');102logger === null || logger === void 0 ? void 0 : logger.info({ strKeyId }, 'injecting new app state sync key');103await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });104newAppStateSyncKeyId = strKeyId;105}106});107ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });108}109else {110logger === null || logger === void 0 ? void 0 : logger.info({ protocolMsg }, 'recv app state sync with 0 keys');111}112break;113case WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE:114ev.emit('messages.update', [115{116key: {117...message.key,118id: protocolMsg.key.id119},120update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }121}122]);123break;124case WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:125Object.assign(chat, {126ephemeralSettingTimestamp: (0, Utils_1.toNumber)(message.messageTimestamp),127ephemeralExpiration: protocolMsg.ephemeralExpiration || null128});129break;130}131}132else if (content === null || content === void 0 ? void 0 : content.reactionMessage) {133const reaction = {134...content.reactionMessage,135key: message.key,136};137ev.emit('messages.reaction', [{138reaction,139key: content.reactionMessage.key,140}]);141}142else if (message.messageStubType) {143const jid = message.key.remoteJid;144//let actor = whatsappID (message.participant)145let participants;146const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, participants, action }));147const emitGroupUpdate = (update) => {148ev.emit('groups.update', [{ id: jid, ...update }]);149};150const participantsIncludesMe = () => participants.find(jid => (0, WABinary_1.areJidsSameUser)(meId, jid));151switch (message.messageStubType) {152case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:153case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:154participants = message.messageStubParameters || [];155emitParticipantsUpdate('remove');156// mark the chat read only if you left the group157if (participantsIncludesMe()) {158chat.readOnly = true;159}160break;161case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:162case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:163case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:164participants = message.messageStubParameters || [];165if (participantsIncludesMe()) {166chat.readOnly = false;167}168emitParticipantsUpdate('add');169break;170case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:171participants = message.messageStubParameters || [];172emitParticipantsUpdate('demote');173break;174case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:175participants = message.messageStubParameters || [];176emitParticipantsUpdate('promote');177break;178case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:179const announceValue = (_b = message.messageStubParameters) === null || _b === void 0 ? void 0 : _b[0];180emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });181break;182case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:183const restrictValue = (_c = message.messageStubParameters) === null || _c === void 0 ? void 0 : _c[0];184emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });185break;186case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:187const name = (_d = message.messageStubParameters) === null || _d === void 0 ? void 0 : _d[0];188chat.name = name;189emitGroupUpdate({ subject: name });190break;191}192}193if (Object.keys(chat).length > 1) {194ev.emit('chats.update', [chat]);195}196};197exports.default = processMessage;198199200