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