@aldy11/baileys 0.0.0-stage → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/README.md +270 -2
- package/WAProto/index.d.ts +15933 -0
- package/WAProto/index.js +115857 -0
- package/index.d.ts +129 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.js +120 -0
- package/lib/Signal/Group/ciphertext-message.js +15 -0
- package/lib/Signal/Group/group-session-builder.js +64 -0
- package/lib/Signal/Group/group_cipher.js +96 -0
- package/lib/Signal/Group/index.js +57 -0
- package/lib/Signal/Group/keyhelper.js +55 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.js +34 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +69 -0
- package/lib/Signal/Group/sender-key-name.js +51 -0
- package/lib/Signal/Group/sender-key-record.js +53 -0
- package/lib/Signal/Group/sender-key-state.js +99 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +218 -0
- package/lib/Socket/Client/index.js +18 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +72 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.js +996 -0
- package/lib/Socket/groups.js +332 -0
- package/lib/Socket/index.js +10 -0
- package/lib/Socket/messages-recv.js +1134 -0
- package/lib/Socket/messages-send.js +1087 -0
- package/lib/Socket/newsletter.js +251 -0
- package/lib/Socket/socket.js +646 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +8 -0
- package/lib/Store/make-in-memory-store.js +439 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.js +7 -0
- package/lib/Types/Newsletter.js +18 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +42 -0
- package/lib/Utils/auth-utils.js +199 -0
- package/lib/Utils/browser-utils.js +40 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.js +730 -0
- package/lib/Utils/crypto.js +193 -0
- package/lib/Utils/decode-wa-message.js +218 -0
- package/lib/Utils/event-buffer.js +518 -0
- package/lib/Utils/event-stream.js +63 -0
- package/lib/Utils/generics.js +438 -0
- package/lib/Utils/history.js +94 -0
- package/lib/Utils/index.js +36 -0
- package/lib/Utils/link-preview.js +126 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/message-retry-manager.js +185 -0
- package/lib/Utils/messages-media.js +879 -0
- package/lib/Utils/messages.js +2212 -0
- package/lib/Utils/noise-handler.js +150 -0
- package/lib/Utils/power-features.js +416 -0
- package/lib/Utils/process-message.js +385 -0
- package/lib/Utils/signal.js +186 -0
- package/lib/Utils/use-multi-file-auth-state.js +124 -0
- package/lib/Utils/validate-connection.js +229 -0
- package/lib/WABinary/constants.js +1303 -0
- package/lib/WABinary/decode.js +265 -0
- package/lib/WABinary/encode.js +250 -0
- package/lib/WABinary/generic-utils.js +110 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.js +85 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.js +26 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.js +48 -0
- package/package.json +50 -4
|
@@ -0,0 +1,1087 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.makeMessagesSocket = void 0;
|
|
7
|
+
const boom_1 = require("@hapi/boom");
|
|
8
|
+
const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
10
|
+
const WAProto_1 = require("../../WAProto");
|
|
11
|
+
const Defaults_1 = require("../Defaults");
|
|
12
|
+
const Utils_1 = require("../Utils");
|
|
13
|
+
const link_preview_1 = require("../Utils/link-preview");
|
|
14
|
+
const WABinary_1 = require("../WABinary");
|
|
15
|
+
const WAUSync_1 = require("../WAUSync");
|
|
16
|
+
const newsletter_1 = require("./newsletter");
|
|
17
|
+
const makeMessagesSocket = (config) => {
|
|
18
|
+
const { logger, linkPreviewImageThumbnailWidth, generateHighQualityLinkPreview, options: axiosOptions, patchMessageBeforeSending, cachedGroupMetadata, albumMessageItemDelayMs = 0, } = config;
|
|
19
|
+
const sock = (0, newsletter_1.makeNewsletterSocket)(config);
|
|
20
|
+
const { ev, authState, processingMutex, signalRepository, upsertMessage, query, fetchPrivacySettings, sendNode, groupMetadata, groupToggleEphemeral, } = sock;
|
|
21
|
+
const userDevicesCache = config.userDevicesCache || new node_cache_1.default({
|
|
22
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.USER_DEVICES, // 5 minutes
|
|
23
|
+
useClones: false
|
|
24
|
+
});
|
|
25
|
+
let mediaConn;
|
|
26
|
+
const refreshMediaConn = async (forceGet = false) => {
|
|
27
|
+
const media = await mediaConn;
|
|
28
|
+
if (!media || forceGet || (new Date().getTime() - media.fetchDate.getTime()) > media.ttl * 1000) {
|
|
29
|
+
mediaConn = (async () => {
|
|
30
|
+
const result = await query({
|
|
31
|
+
tag: 'iq',
|
|
32
|
+
attrs: {
|
|
33
|
+
type: 'set',
|
|
34
|
+
xmlns: 'w:m',
|
|
35
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
36
|
+
},
|
|
37
|
+
content: [{ tag: 'media_conn', attrs: {} }]
|
|
38
|
+
});
|
|
39
|
+
const mediaConnNode = (0, WABinary_1.getBinaryNodeChild)(result, 'media_conn');
|
|
40
|
+
const node = {
|
|
41
|
+
hosts: (0, WABinary_1.getBinaryNodeChildren)(mediaConnNode, 'host').map(({ attrs }) => ({
|
|
42
|
+
hostname: attrs.hostname,
|
|
43
|
+
maxContentLengthBytes: +attrs.maxContentLengthBytes,
|
|
44
|
+
})),
|
|
45
|
+
auth: mediaConnNode.attrs.auth,
|
|
46
|
+
ttl: +mediaConnNode.attrs.ttl,
|
|
47
|
+
fetchDate: new Date()
|
|
48
|
+
};
|
|
49
|
+
logger.debug('fetched media conn');
|
|
50
|
+
return node;
|
|
51
|
+
})();
|
|
52
|
+
}
|
|
53
|
+
return mediaConn;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* generic send receipt function
|
|
57
|
+
* used for receipts of phone call, read, delivery etc.
|
|
58
|
+
* */
|
|
59
|
+
const sendReceipt = async (jid, participant, messageIds, type) => {
|
|
60
|
+
const node = {
|
|
61
|
+
tag: 'receipt',
|
|
62
|
+
attrs: {
|
|
63
|
+
id: messageIds[0],
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
const isReadReceipt = type === 'read' || type === 'read-self';
|
|
67
|
+
if (isReadReceipt) {
|
|
68
|
+
node.attrs.t = (0, Utils_1.unixTimestampSeconds)().toString();
|
|
69
|
+
}
|
|
70
|
+
if (type === 'sender' && (0, WABinary_1.isJidUser)(jid)) {
|
|
71
|
+
node.attrs.recipient = jid;
|
|
72
|
+
node.attrs.to = participant;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
node.attrs.to = jid;
|
|
76
|
+
if (participant) {
|
|
77
|
+
node.attrs.participant = participant;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (type) {
|
|
81
|
+
node.attrs.type = (0, WABinary_1.isJidNewsletter)(jid) ? 'read-self' : type;
|
|
82
|
+
}
|
|
83
|
+
const remainingMessageIds = messageIds.slice(1);
|
|
84
|
+
if (remainingMessageIds.length) {
|
|
85
|
+
node.content = [
|
|
86
|
+
{
|
|
87
|
+
tag: 'list',
|
|
88
|
+
attrs: {},
|
|
89
|
+
content: remainingMessageIds.map(id => ({
|
|
90
|
+
tag: 'item',
|
|
91
|
+
attrs: { id }
|
|
92
|
+
}))
|
|
93
|
+
}
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
logger.debug({ attrs: node.attrs, messageIds }, 'sending receipt for messages');
|
|
97
|
+
await sendNode(node);
|
|
98
|
+
};
|
|
99
|
+
/** Correctly bulk send receipts to multiple chats, participants */
|
|
100
|
+
const sendReceipts = async (keys, type) => {
|
|
101
|
+
const recps = (0, Utils_1.aggregateMessageKeysNotFromMe)(keys);
|
|
102
|
+
for (const { jid, participant, messageIds } of recps) {
|
|
103
|
+
await sendReceipt(jid, participant, messageIds, type);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
/** Bulk read messages. Keys can be from different chats & participants */
|
|
107
|
+
const readMessages = async (keys) => {
|
|
108
|
+
const privacySettings = await fetchPrivacySettings();
|
|
109
|
+
// based on privacy settings, we have to change the read type
|
|
110
|
+
const readType = privacySettings.readreceipts === 'all' ? 'read' : 'read-self';
|
|
111
|
+
await sendReceipts(keys, readType);
|
|
112
|
+
};
|
|
113
|
+
/** Fetch all the devices we've to send a message to */
|
|
114
|
+
/** Checks whether a JID matches this exact running device across PN and LID formats. */
|
|
115
|
+
const isOwnDeviceJid = (jid) => {
|
|
116
|
+
var _a, _b;
|
|
117
|
+
const target = (0, WABinary_1.jidDecode)(jid);
|
|
118
|
+
if (!(target === null || target === void 0 ? void 0 : target.user)) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
const targetDevice = target.device || 0;
|
|
122
|
+
const ownJids = [(_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id, (_b = authState.creds.me) === null || _b === void 0 ? void 0 : _b.lid];
|
|
123
|
+
return ownJids.some((ownJid) => {
|
|
124
|
+
const own = ownJid ? (0, WABinary_1.jidDecode)(ownJid) : undefined;
|
|
125
|
+
return !!own && own.user === target.user && (own.device || 0) === targetDevice;
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
const getUSyncDevices = async (jids, useCache, ignoreZeroDevices) => {
|
|
129
|
+
var _a, _b;
|
|
130
|
+
const deviceResults = [];
|
|
131
|
+
if (!useCache) {
|
|
132
|
+
logger.debug('not using cache for devices');
|
|
133
|
+
}
|
|
134
|
+
const toFetch = [];
|
|
135
|
+
jids = Array.from(new Set(jids));
|
|
136
|
+
for (let jid of jids) {
|
|
137
|
+
const user = (_a = (0, WABinary_1.jidDecode)(jid)) === null || _a === void 0 ? void 0 : _a.user;
|
|
138
|
+
jid = (0, WABinary_1.jidNormalizedUser)(jid);
|
|
139
|
+
if (useCache) {
|
|
140
|
+
const devices = userDevicesCache.get(user);
|
|
141
|
+
if (devices) {
|
|
142
|
+
deviceResults.push(...devices.filter(item => !isOwnDeviceJid((0, WABinary_1.jidEncode)(item.user, 's.whatsapp.net', item.device))));
|
|
143
|
+
logger.trace({ user }, 'using cache for devices');
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
toFetch.push(jid);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
toFetch.push(jid);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (!toFetch.length) {
|
|
154
|
+
return deviceResults;
|
|
155
|
+
}
|
|
156
|
+
const query = new WAUSync_1.USyncQuery()
|
|
157
|
+
.withContext('message')
|
|
158
|
+
.withDeviceProtocol();
|
|
159
|
+
for (const jid of toFetch) {
|
|
160
|
+
query.withUser(new WAUSync_1.USyncUser().withId(jid));
|
|
161
|
+
}
|
|
162
|
+
const result = await sock.executeUSyncQuery(query);
|
|
163
|
+
if (result) {
|
|
164
|
+
const extracted = (0, Utils_1.extractDeviceJids)(result === null || result === void 0 ? void 0 : result.list, authState.creds.me.id, ignoreZeroDevices, (_b = authState.creds.me) === null || _b === void 0 ? void 0 : _b.lid);
|
|
165
|
+
const deviceMap = {};
|
|
166
|
+
for (const item of extracted) {
|
|
167
|
+
deviceMap[item.user] = deviceMap[item.user] || [];
|
|
168
|
+
deviceMap[item.user].push(item);
|
|
169
|
+
deviceResults.push(item);
|
|
170
|
+
}
|
|
171
|
+
for (const key in deviceMap) {
|
|
172
|
+
userDevicesCache.set(key, deviceMap[key]);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return deviceResults;
|
|
176
|
+
};
|
|
177
|
+
const assertSessions = async (jids, force) => {
|
|
178
|
+
let didFetchNewSession = false;
|
|
179
|
+
let jidsRequiringFetch = [];
|
|
180
|
+
const seenSignalIds = new Set();
|
|
181
|
+
const cleanJids = [...new Set(jids)].filter(j => {
|
|
182
|
+
if (!j || isOwnDeviceJid(j)) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
const signalId = signalRepository.jidToSignalProtocolAddress(j);
|
|
186
|
+
if (seenSignalIds.has(signalId)) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
seenSignalIds.add(signalId);
|
|
190
|
+
return true;
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
if (force) {
|
|
194
|
+
jidsRequiringFetch = cleanJids;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const addrs = cleanJids.map(jid => (signalRepository
|
|
198
|
+
.jidToSignalProtocolAddress(jid)));
|
|
199
|
+
const sessions = await authState.keys.get('session', addrs);
|
|
200
|
+
for (const jid of cleanJids) {
|
|
201
|
+
const signalId = signalRepository
|
|
202
|
+
.jidToSignalProtocolAddress(jid);
|
|
203
|
+
if (!sessions[signalId]) {
|
|
204
|
+
jidsRequiringFetch.push(jid);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (jidsRequiringFetch.length) {
|
|
209
|
+
logger.debug({ jidsRequiringFetch }, 'fetching sessions');
|
|
210
|
+
try {
|
|
211
|
+
const result = await query({
|
|
212
|
+
tag: 'iq',
|
|
213
|
+
attrs: {
|
|
214
|
+
xmlns: 'encrypt',
|
|
215
|
+
type: 'get',
|
|
216
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
217
|
+
},
|
|
218
|
+
content: [
|
|
219
|
+
{
|
|
220
|
+
tag: 'key',
|
|
221
|
+
attrs: {},
|
|
222
|
+
content: jidsRequiringFetch.map(jid => {
|
|
223
|
+
const attrs = { jid };
|
|
224
|
+
if (force) {
|
|
225
|
+
attrs.reason = 'identity';
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
tag: 'user',
|
|
229
|
+
attrs,
|
|
230
|
+
};
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
]
|
|
234
|
+
});
|
|
235
|
+
await (0, Utils_1.parseAndInjectE2ESessions)(result, signalRepository);
|
|
236
|
+
didFetchNewSession = true;
|
|
237
|
+
} catch (err) {
|
|
238
|
+
logger.warn({ err: err.message || err, jidsRequiringFetch }, 'failed to fetch sessions, continuing with local store');
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return didFetchNewSession;
|
|
242
|
+
};
|
|
243
|
+
const sendPeerDataOperationMessage = async (pdoMessage) => {
|
|
244
|
+
var _a;
|
|
245
|
+
//TODO: for later, abstract the logic to send a Peer Message instead of just PDO - useful for App State Key Resync with phone
|
|
246
|
+
if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
|
|
247
|
+
throw new boom_1.Boom('Not authenticated');
|
|
248
|
+
}
|
|
249
|
+
const protocolMessage = {
|
|
250
|
+
protocolMessage: {
|
|
251
|
+
peerDataOperationRequestMessage: pdoMessage,
|
|
252
|
+
type: WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_MESSAGE
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
const meJid = (0, WABinary_1.jidNormalizedUser)(authState.creds.me.id);
|
|
256
|
+
const msgId = await relayMessage(meJid, protocolMessage, {
|
|
257
|
+
additionalAttributes: {
|
|
258
|
+
category: 'peer',
|
|
259
|
+
// eslint-disable-next-line camelcase
|
|
260
|
+
push_priority: 'high_force',
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
return msgId;
|
|
264
|
+
};
|
|
265
|
+
const createParticipantNodes = async (jids, message, extraAttrs, { skipNoSession = false } = {}) => {
|
|
266
|
+
let patched = await patchMessageBeforeSending(message, jids);
|
|
267
|
+
if (!Array.isArray(patched)) {
|
|
268
|
+
patched = jids ? jids.map(jid => ({ recipientJid: jid, ...patched })) : [patched];
|
|
269
|
+
}
|
|
270
|
+
let shouldIncludeDeviceIdentity = false;
|
|
271
|
+
const nodes = await Promise.all(patched.map(async (patchedMessageWithJid) => {
|
|
272
|
+
const { recipientJid: jid, ...patchedMessage } = patchedMessageWithJid;
|
|
273
|
+
if (!jid || isOwnDeviceJid(jid)) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
const bytes = (0, Utils_1.encodeWAMessage)(patchedMessage);
|
|
277
|
+
let encrypted;
|
|
278
|
+
try {
|
|
279
|
+
encrypted = await signalRepository
|
|
280
|
+
.encryptMessage({ jid, data: bytes });
|
|
281
|
+
}
|
|
282
|
+
catch (err) {
|
|
283
|
+
const errMsg = String((err === null || err === void 0 ? void 0 : err.message) || err || '');
|
|
284
|
+
if (skipNoSession && ((err === null || err === void 0 ? void 0 : err.name) === 'SessionError' || /No (?:open )?sessions?/i.test(errMsg))) {
|
|
285
|
+
logger.warn({ jid, err: errMsg }, 'skipping participant device without signal session');
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
throw err;
|
|
289
|
+
}
|
|
290
|
+
const { type, ciphertext } = encrypted;
|
|
291
|
+
if (type === 'pkmsg') {
|
|
292
|
+
shouldIncludeDeviceIdentity = true;
|
|
293
|
+
}
|
|
294
|
+
const node = {
|
|
295
|
+
tag: 'to',
|
|
296
|
+
attrs: { jid },
|
|
297
|
+
content: [{
|
|
298
|
+
tag: 'enc',
|
|
299
|
+
attrs: {
|
|
300
|
+
v: '2',
|
|
301
|
+
type,
|
|
302
|
+
...extraAttrs || {}
|
|
303
|
+
},
|
|
304
|
+
content: ciphertext
|
|
305
|
+
}]
|
|
306
|
+
};
|
|
307
|
+
return node;
|
|
308
|
+
}));
|
|
309
|
+
return { nodes: nodes.filter(Boolean), shouldIncludeDeviceIdentity };
|
|
310
|
+
};
|
|
311
|
+
const relayMessage = async (
|
|
312
|
+
jid,
|
|
313
|
+
message,
|
|
314
|
+
{
|
|
315
|
+
messageId: msgId,
|
|
316
|
+
participant,
|
|
317
|
+
onTarget,
|
|
318
|
+
additionalAttributes,
|
|
319
|
+
additionalNodes,
|
|
320
|
+
useUserDevicesCache,
|
|
321
|
+
useCachedGroupMetadata,
|
|
322
|
+
statusJidList
|
|
323
|
+
} = {}
|
|
324
|
+
) => {
|
|
325
|
+
var _a;
|
|
326
|
+
const meId = authState.creds.me.id;
|
|
327
|
+
const meLid = authState.creds.me && authState.creds.me.lid;
|
|
328
|
+
const isRetryResend = Boolean(participant && participant.jid);
|
|
329
|
+
let shouldIncludeDeviceIdentity = isRetryResend;
|
|
330
|
+
const { user, server } = (0, WABinary_1.jidDecode)(jid);
|
|
331
|
+
const statusJid = 'status@broadcast';
|
|
332
|
+
const isGroup = server === 'g.us';
|
|
333
|
+
const isNewsletter = server === 'newsletter';
|
|
334
|
+
const isStatus = jid === statusJid;
|
|
335
|
+
const isLid = server === 'lid';
|
|
336
|
+
const isDirectRetryResend = isRetryResend && !isGroup && !isStatus && !isNewsletter;
|
|
337
|
+
msgId = msgId || (0, Utils_1.generateMessageIDV2)((_a = sock.user) === null || _a === void 0 ? void 0 : _a.id);
|
|
338
|
+
useUserDevicesCache = useUserDevicesCache !== false;
|
|
339
|
+
useCachedGroupMetadata = useCachedGroupMetadata !== false && !isStatus;
|
|
340
|
+
const participants = [];
|
|
341
|
+
const destinationJid = (!isStatus) ? (0, WABinary_1.jidEncode)(user, isLid ? 'lid' : isGroup ? 'g.us' : isNewsletter ? 'newsletter' : 's.whatsapp.net') : statusJid;
|
|
342
|
+
const binaryNodeContent = [];
|
|
343
|
+
const devices = [];
|
|
344
|
+
const meMsg = {
|
|
345
|
+
deviceSentMessage: {
|
|
346
|
+
destinationJid,
|
|
347
|
+
message
|
|
348
|
+
},
|
|
349
|
+
messageContextInfo: message.messageContextInfo
|
|
350
|
+
};
|
|
351
|
+
const extraAttrs = {};
|
|
352
|
+
if (participant) {
|
|
353
|
+
// when the retry request is not for a group
|
|
354
|
+
// only send to the specific device that asked for a retry
|
|
355
|
+
// otherwise the message is sent out to every device that should be a recipient
|
|
356
|
+
if (!isGroup && !isStatus) {
|
|
357
|
+
additionalAttributes = { ...additionalAttributes, 'device_fanout': 'false' };
|
|
358
|
+
}
|
|
359
|
+
const { user, device } = (0, WABinary_1.jidDecode)(participant.jid);
|
|
360
|
+
devices.push({ user, device, jid: participant.jid });
|
|
361
|
+
}
|
|
362
|
+
await authState.keys.transaction(async () => {
|
|
363
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
364
|
+
const mediaType = getMediaType(message);
|
|
365
|
+
if (mediaType) {
|
|
366
|
+
extraAttrs['mediatype'] = mediaType;
|
|
367
|
+
}
|
|
368
|
+
if ((_a = (0, Utils_1.normalizeMessageContent)(message)) === null || _a === void 0 ? void 0 : _a.pinInChatMessage) {
|
|
369
|
+
extraAttrs['decrypt-fail'] = 'hide';
|
|
370
|
+
}
|
|
371
|
+
if (isGroup || isStatus) {
|
|
372
|
+
const [groupData, senderKeyMap] = await Promise.all([
|
|
373
|
+
(async () => {
|
|
374
|
+
let groupData = useCachedGroupMetadata && cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined;
|
|
375
|
+
if (groupData && Array.isArray(groupData === null || groupData === void 0 ? void 0 : groupData.participants)) {
|
|
376
|
+
logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata');
|
|
377
|
+
}
|
|
378
|
+
else if (!isStatus) {
|
|
379
|
+
groupData = await groupMetadata(jid);
|
|
380
|
+
}
|
|
381
|
+
return groupData;
|
|
382
|
+
})(),
|
|
383
|
+
(async () => {
|
|
384
|
+
if (!participant && !isStatus) {
|
|
385
|
+
const result = await authState.keys.get('sender-key-memory', [jid]);
|
|
386
|
+
return result[jid] || {};
|
|
387
|
+
}
|
|
388
|
+
return {};
|
|
389
|
+
})()
|
|
390
|
+
]);
|
|
391
|
+
if (!participant) {
|
|
392
|
+
const participantsList = (groupData && !isStatus) ? groupData.participants.map(p => p.id) : [];
|
|
393
|
+
if (isStatus && statusJidList) {
|
|
394
|
+
participantsList.push(...statusJidList);
|
|
395
|
+
}
|
|
396
|
+
if (!isStatus) {
|
|
397
|
+
additionalAttributes = {
|
|
398
|
+
...additionalAttributes,
|
|
399
|
+
// eslint-disable-next-line camelcase
|
|
400
|
+
addressing_mode: (groupData === null || groupData === void 0 ? void 0 : groupData.addressingMode) || 'pn'
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
const additionalDevices = await getUSyncDevices(participantsList, !!useUserDevicesCache, false);
|
|
404
|
+
devices.push(...additionalDevices);
|
|
405
|
+
}
|
|
406
|
+
const patched = await patchMessageBeforeSending(message);
|
|
407
|
+
if (Array.isArray(patched)) {
|
|
408
|
+
throw new boom_1.Boom('Per-jid patching is not supported in groups');
|
|
409
|
+
}
|
|
410
|
+
const bytes = (0, Utils_1.encodeWAMessage)(patched);
|
|
411
|
+
const meLid = authState.creds.me && authState.creds.me.lid;
|
|
412
|
+
const senderKeyAuthor = ((groupData === null || groupData === void 0 ? void 0 : groupData.addressingMode) === 'lid' && meLid) ? meLid : meId;
|
|
413
|
+
const { ciphertext, senderKeyDistributionMessage } = await signalRepository.encryptGroupMessage({
|
|
414
|
+
group: destinationJid,
|
|
415
|
+
data: bytes,
|
|
416
|
+
meId: senderKeyAuthor,
|
|
417
|
+
});
|
|
418
|
+
const senderKeyJids = [];
|
|
419
|
+
// ensure a connection is established with every device
|
|
420
|
+
for (const dev of devices) {
|
|
421
|
+
const { user, device } = dev;
|
|
422
|
+
const jid = dev.jid || (0, WABinary_1.jidEncode)(user, (groupData === null || groupData === void 0 ? void 0 : groupData.addressingMode) === 'lid' ? 'lid' : 's.whatsapp.net', device);
|
|
423
|
+
if (isOwnDeviceJid(jid)) {
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if ((!senderKeyMap[jid] || !!participant) && !senderKeyJids.includes(jid)) {
|
|
427
|
+
senderKeyJids.push(jid);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
// if there are some participants with whom the session has not been established
|
|
431
|
+
// if there are, we re-send the senderkey
|
|
432
|
+
if (senderKeyJids.length) {
|
|
433
|
+
logger.debug({ senderKeyJids }, 'sending new sender key');
|
|
434
|
+
const senderKeyMsg = {
|
|
435
|
+
senderKeyDistributionMessage: {
|
|
436
|
+
axolotlSenderKeyDistributionMessage: senderKeyDistributionMessage,
|
|
437
|
+
groupId: destinationJid
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
await assertSessions(senderKeyJids, false);
|
|
441
|
+
const result = await createParticipantNodes(senderKeyJids, senderKeyMsg, extraAttrs, { skipNoSession: true });
|
|
442
|
+
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || result.shouldIncludeDeviceIdentity;
|
|
443
|
+
participants.push(...result.nodes);
|
|
444
|
+
for (const node of result.nodes) {
|
|
445
|
+
const sentJid = node && node.attrs && node.attrs.jid;
|
|
446
|
+
if (sentJid) {
|
|
447
|
+
senderKeyMap[sentJid] = true;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
const skmsgAttrs = { v: '2', type: 'skmsg' };
|
|
452
|
+
if (participant && participant.count !== undefined && participant.count !== null) {
|
|
453
|
+
skmsgAttrs.count = participant.count.toString();
|
|
454
|
+
}
|
|
455
|
+
binaryNodeContent.push({
|
|
456
|
+
tag: 'enc',
|
|
457
|
+
attrs: skmsgAttrs,
|
|
458
|
+
content: ciphertext
|
|
459
|
+
});
|
|
460
|
+
await authState.keys.set({ 'sender-key-memory': { [jid]: senderKeyMap } });
|
|
461
|
+
}
|
|
462
|
+
else if (isNewsletter) {
|
|
463
|
+
// Message edit
|
|
464
|
+
if ((_b = message.protocolMessage) === null || _b === void 0 ? void 0 : _b.editedMessage) {
|
|
465
|
+
msgId = (_c = message.protocolMessage.key) === null || _c === void 0 ? void 0 : _c.id;
|
|
466
|
+
message = message.protocolMessage.editedMessage;
|
|
467
|
+
}
|
|
468
|
+
// Message delete
|
|
469
|
+
if (((_d = message.protocolMessage) === null || _d === void 0 ? void 0 : _d.type) === WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE) {
|
|
470
|
+
msgId = (_e = message.protocolMessage.key) === null || _e === void 0 ? void 0 : _e.id;
|
|
471
|
+
message = {};
|
|
472
|
+
}
|
|
473
|
+
const patched = await patchMessageBeforeSending(message, []);
|
|
474
|
+
if (Array.isArray(patched)) {
|
|
475
|
+
throw new boom_1.Boom('Per-jid patching is not supported in channel');
|
|
476
|
+
}
|
|
477
|
+
const bytes = (0, Utils_1.encodeNewsletterMessage)(patched);
|
|
478
|
+
binaryNodeContent.push({
|
|
479
|
+
tag: 'plaintext',
|
|
480
|
+
attrs: mediaType ? { mediatype: mediaType } : {},
|
|
481
|
+
content: bytes
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
else if (isDirectRetryResend) {
|
|
485
|
+
const retryJid = participant.jid;
|
|
486
|
+
if (!isOwnDeviceJid(retryJid)) {
|
|
487
|
+
const isParticipantMe = (0, WABinary_1.areJidsSameUser)(retryJid, meId)
|
|
488
|
+
|| (!!meLid && (0, WABinary_1.areJidsSameUser)(retryJid, meLid));
|
|
489
|
+
const isSelfChat = (0, WABinary_1.areJidsSameUser)(destinationJid, meId)
|
|
490
|
+
|| (!!meLid && (0, WABinary_1.areJidsSameUser)(destinationJid, meLid));
|
|
491
|
+
const rawRetryPayload = (isParticipantMe && !isSelfChat) ? meMsg : message;
|
|
492
|
+
let patchedRetry = await patchMessageBeforeSending(rawRetryPayload, [retryJid]);
|
|
493
|
+
if (Array.isArray(patchedRetry)) {
|
|
494
|
+
const found = patchedRetry.find(item => item && item.recipientJid === retryJid) || patchedRetry[0];
|
|
495
|
+
if (found) {
|
|
496
|
+
const { recipientJid: _ignored, ...rest } = found;
|
|
497
|
+
patchedRetry = rest;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
await assertSessions([retryJid], false);
|
|
501
|
+
const encodedRetry = (0, Utils_1.encodeWAMessage)(patchedRetry);
|
|
502
|
+
const { type, ciphertext } = await signalRepository.encryptMessage({
|
|
503
|
+
jid: retryJid,
|
|
504
|
+
data: encodedRetry
|
|
505
|
+
});
|
|
506
|
+
if (type === 'pkmsg') {
|
|
507
|
+
shouldIncludeDeviceIdentity = true;
|
|
508
|
+
}
|
|
509
|
+
const retryEncAttrs = {
|
|
510
|
+
v: '2',
|
|
511
|
+
type,
|
|
512
|
+
...extraAttrs
|
|
513
|
+
};
|
|
514
|
+
if (participant.count !== undefined && participant.count !== null) {
|
|
515
|
+
retryEncAttrs.count = participant.count.toString();
|
|
516
|
+
}
|
|
517
|
+
binaryNodeContent.push({
|
|
518
|
+
tag: 'enc',
|
|
519
|
+
attrs: retryEncAttrs,
|
|
520
|
+
content: ciphertext
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
else {
|
|
525
|
+
const { user: meUser } = (0, WABinary_1.jidDecode)(meId);
|
|
526
|
+
const { user: meLidUser } = meLid
|
|
527
|
+
? (0, WABinary_1.jidDecode)(meLid)
|
|
528
|
+
: { user: null };
|
|
529
|
+
const ownUserForDevices = (isLid && meLidUser) ? meLidUser : meUser;
|
|
530
|
+
if (!participant) {
|
|
531
|
+
devices.push({ user });
|
|
532
|
+
if (user !== ownUserForDevices) {
|
|
533
|
+
devices.push({ user: ownUserForDevices });
|
|
534
|
+
}
|
|
535
|
+
if ((additionalAttributes === null || additionalAttributes === void 0 ? void 0 : additionalAttributes['category']) !== 'peer') {
|
|
536
|
+
const senderIdentityForUsync = (isLid && meLid) ? meLid : meId;
|
|
537
|
+
const additionalDevices = await getUSyncDevices([senderIdentityForUsync, jid], !!useUserDevicesCache, true);
|
|
538
|
+
devices.push(...additionalDevices);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const allJids = [];
|
|
543
|
+
const meJids = [];
|
|
544
|
+
const otherJids = [];
|
|
545
|
+
|
|
546
|
+
const mePnUser = meUser;
|
|
547
|
+
|
|
548
|
+
for (const { user, device } of devices) {
|
|
549
|
+
const isMe = user === mePnUser || (!!meLidUser && user === meLidUser);
|
|
550
|
+
const shouldUseLid = isLid;
|
|
551
|
+
const targetUser = (isLid && isMe && meLidUser) ? meLidUser : user;
|
|
552
|
+
|
|
553
|
+
const jid = (0, WABinary_1.jidEncode)(
|
|
554
|
+
targetUser,
|
|
555
|
+
shouldUseLid ? "lid" : "s.whatsapp.net",
|
|
556
|
+
device
|
|
557
|
+
);
|
|
558
|
+
|
|
559
|
+
const isExactSenderDevice = isOwnDeviceJid(jid);
|
|
560
|
+
|
|
561
|
+
let skip = false;
|
|
562
|
+
|
|
563
|
+
if (isExactSenderDevice) {
|
|
564
|
+
skip = true;
|
|
565
|
+
} else if (onTarget && !participant && !((0, WABinary_1.isJidGroup)(jid) || isStatus)) {
|
|
566
|
+
if (isMe) {
|
|
567
|
+
skip = true;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const isSelfChat = (0, WABinary_1.jidDecode)(destinationJid)?.user === mePnUser || (!!meLidUser && (0, WABinary_1.jidDecode)(destinationJid)?.user === meLidUser);
|
|
572
|
+
|
|
573
|
+
if (!skip) {
|
|
574
|
+
if (isSelfChat) {
|
|
575
|
+
if (!otherJids.includes(jid)) otherJids.push(jid);
|
|
576
|
+
} else if (isMe) {
|
|
577
|
+
if (!meJids.includes(jid)) meJids.push(jid);
|
|
578
|
+
} else {
|
|
579
|
+
if (!otherJids.includes(jid)) otherJids.push(jid);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (!allJids.includes(jid)) allJids.push(jid);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
await assertSessions(allJids, false);
|
|
587
|
+
const [{ nodes: meNodes, shouldIncludeDeviceIdentity: s1 }, { nodes: otherNodes, shouldIncludeDeviceIdentity: s2 }] = await Promise.all([
|
|
588
|
+
createParticipantNodes(meJids, meMsg, extraAttrs),
|
|
589
|
+
createParticipantNodes(otherJids, message, extraAttrs)
|
|
590
|
+
]);
|
|
591
|
+
participants.push(...meNodes);
|
|
592
|
+
participants.push(...otherNodes);
|
|
593
|
+
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || s1 || s2;
|
|
594
|
+
}
|
|
595
|
+
if (participants.length) {
|
|
596
|
+
if ((additionalAttributes === null || additionalAttributes === void 0 ? void 0 : additionalAttributes['category']) === 'peer') {
|
|
597
|
+
const peerNode = (_j = (_h = participants[0]) === null || _h === void 0 ? void 0 : _h.content) === null || _j === void 0 ? void 0 : _j[0];
|
|
598
|
+
if (peerNode) {
|
|
599
|
+
binaryNodeContent.push(peerNode); // push only enc
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
else {
|
|
603
|
+
binaryNodeContent.push({
|
|
604
|
+
tag: 'participants',
|
|
605
|
+
attrs: {},
|
|
606
|
+
content: participants
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
const stanza = {
|
|
611
|
+
tag: 'message',
|
|
612
|
+
attrs: {
|
|
613
|
+
id: msgId,
|
|
614
|
+
type: isNewsletter ? getTypeMessage(message) : 'text',
|
|
615
|
+
...(additionalAttributes || {})
|
|
616
|
+
},
|
|
617
|
+
content: binaryNodeContent
|
|
618
|
+
};
|
|
619
|
+
// if the participant to send to is explicitly specified (generally retry recp)
|
|
620
|
+
// ensure the message is only sent to that person
|
|
621
|
+
// if a retry receipt is sent to everyone -- it'll fail decryption for everyone else who received the msg
|
|
622
|
+
if (participant) {
|
|
623
|
+
const isParticipantMe = (0, WABinary_1.areJidsSameUser)(participant.jid, meId)
|
|
624
|
+
|| (!!meLid && (0, WABinary_1.areJidsSameUser)(participant.jid, meLid));
|
|
625
|
+
if ((0, WABinary_1.isJidGroup)(destinationJid)) {
|
|
626
|
+
stanza.attrs.to = destinationJid;
|
|
627
|
+
stanza.attrs.participant = participant.jid;
|
|
628
|
+
}
|
|
629
|
+
else if (isParticipantMe) {
|
|
630
|
+
stanza.attrs.to = participant.jid;
|
|
631
|
+
stanza.attrs.recipient = destinationJid;
|
|
632
|
+
}
|
|
633
|
+
else {
|
|
634
|
+
stanza.attrs.to = participant.jid;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
stanza.attrs.to = destinationJid;
|
|
639
|
+
}
|
|
640
|
+
if (shouldIncludeDeviceIdentity) {
|
|
641
|
+
stanza.content.push({
|
|
642
|
+
tag: 'device-identity',
|
|
643
|
+
attrs: {},
|
|
644
|
+
content: (0, Utils_1.encodeSignedDeviceIdentity)(authState.creds.account, true)
|
|
645
|
+
});
|
|
646
|
+
logger.debug({ jid }, 'adding device identity');
|
|
647
|
+
}
|
|
648
|
+
if (additionalNodes && additionalNodes.length > 0) {
|
|
649
|
+
stanza.content.push(...additionalNodes);
|
|
650
|
+
}
|
|
651
|
+
const content = (0, Utils_1.normalizeMessageContent)(message);
|
|
652
|
+
const contentType = (0, Utils_1.getContentType)(content);
|
|
653
|
+
if (((0, WABinary_1.isJidGroup)(jid) || (0, WABinary_1.isJidUser)(jid) || (0, WABinary_1.isLidUser)(jid)) && (contentType === 'interactiveMessage' ||
|
|
654
|
+
contentType === 'buttonsMessage' ||
|
|
655
|
+
contentType === 'listMessage')) {
|
|
656
|
+
const bizNode = { tag: 'biz', attrs: {} };
|
|
657
|
+
if ((((_l = (_k = message === null || message === void 0 ? void 0 : message.viewOnceMessage) === null || _k === void 0 ? void 0 : _k.message) === null || _l === void 0 ? void 0 : _l.interactiveMessage) || ((_o = (_m = message === null || message === void 0 ? void 0 : message.viewOnceMessageV2) === null || _m === void 0 ? void 0 : _m.message) === null || _o === void 0 ? void 0 : _o.interactiveMessage) || ((_q = (_p = message === null || message === void 0 ? void 0 : message.viewOnceMessageV2Extension) === null || _p === void 0 ? void 0 : _p.message) === null || _q === void 0 ? void 0 : _q.interactiveMessage) || (message === null || message === void 0 ? void 0 : message.interactiveMessage)) || (((_s = (_r = message === null || message === void 0 ? void 0 : message.viewOnceMessage) === null || _r === void 0 ? void 0 : _r.message) === null || _s === void 0 ? void 0 : _s.buttonsMessage) || ((_u = (_t = message === null || message === void 0 ? void 0 : message.viewOnceMessageV2) === null || _t === void 0 ? void 0 : _t.message) === null || _u === void 0 ? void 0 : _u.buttonsMessage) || ((_w = (_v = message === null || message === void 0 ? void 0 : message.viewOnceMessageV2Extension) === null || _v === void 0 ? void 0 : _v.message) === null || _w === void 0 ? void 0 : _w.buttonsMessage) || (message === null || message === void 0 ? void 0 : message.buttonsMessage))) {
|
|
658
|
+
bizNode.content = [{
|
|
659
|
+
tag: 'interactive',
|
|
660
|
+
attrs: {
|
|
661
|
+
type: 'native_flow',
|
|
662
|
+
v: '1'
|
|
663
|
+
},
|
|
664
|
+
content: [{
|
|
665
|
+
tag: 'native_flow',
|
|
666
|
+
attrs: { v: '9', name: 'mixed' }
|
|
667
|
+
}]
|
|
668
|
+
}];
|
|
669
|
+
}
|
|
670
|
+
else if (message === null || message === void 0 ? void 0 : message.listMessage) {
|
|
671
|
+
// list message only support in private chat
|
|
672
|
+
bizNode.content = [{
|
|
673
|
+
tag: 'list',
|
|
674
|
+
attrs: {
|
|
675
|
+
type: 'product_list',
|
|
676
|
+
v: '2'
|
|
677
|
+
}
|
|
678
|
+
}];
|
|
679
|
+
}
|
|
680
|
+
stanza.content.push(bizNode);
|
|
681
|
+
}
|
|
682
|
+
const recipientCount = isDirectRetryResend
|
|
683
|
+
? (binaryNodeContent.some(n => n && n.tag === 'enc') ? 1 : 0)
|
|
684
|
+
: participants.length;
|
|
685
|
+
logger.debug({ msgId, isRetryResend }, `sending message to ${recipientCount} devices`);
|
|
686
|
+
console.log(`[WA-SEND] Mengirim pesan (${msgId}) ke ${destinationJid}${isDirectRetryResend ? ` [RETRY ke ${participant.jid}]` : ''} [penerima: ${recipientCount} perangkat]`);
|
|
687
|
+
await sendNode(stanza);
|
|
688
|
+
});
|
|
689
|
+
return msgId;
|
|
690
|
+
};
|
|
691
|
+
const getTypeMessage = (msg) => {
|
|
692
|
+
if (msg.viewOnceMessage) {
|
|
693
|
+
return getTypeMessage(msg.viewOnceMessage.message);
|
|
694
|
+
}
|
|
695
|
+
else if (msg.viewOnceMessageV2) {
|
|
696
|
+
return getTypeMessage(msg.viewOnceMessageV2.message);
|
|
697
|
+
}
|
|
698
|
+
else if (msg.viewOnceMessageV2Extension) {
|
|
699
|
+
return getTypeMessage(msg.viewOnceMessageV2Extension.message);
|
|
700
|
+
}
|
|
701
|
+
else if (msg.ephemeralMessage) {
|
|
702
|
+
return getTypeMessage(msg.ephemeralMessage.message);
|
|
703
|
+
}
|
|
704
|
+
else if (msg.documentWithCaptionMessage) {
|
|
705
|
+
return getTypeMessage(msg.documentWithCaptionMessage.message);
|
|
706
|
+
}
|
|
707
|
+
else if (msg.reactionMessage) {
|
|
708
|
+
return 'reaction';
|
|
709
|
+
}
|
|
710
|
+
else if (msg.pollCreationMessage || msg.pollCreationMessageV2 || msg.pollCreationMessageV3 || msg.pollUpdateMessage) {
|
|
711
|
+
return 'poll';
|
|
712
|
+
}
|
|
713
|
+
else if (getMediaType(msg)) {
|
|
714
|
+
return 'media';
|
|
715
|
+
}
|
|
716
|
+
else {
|
|
717
|
+
return 'text';
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
const getMediaType = (message) => {
|
|
721
|
+
if (message.imageMessage) {
|
|
722
|
+
return 'image';
|
|
723
|
+
}
|
|
724
|
+
else if (message.videoMessage) {
|
|
725
|
+
return message.videoMessage.gifPlayback ? 'gif' : 'video';
|
|
726
|
+
}
|
|
727
|
+
else if (message.audioMessage) {
|
|
728
|
+
return message.audioMessage.ptt ? 'ptt' : 'audio';
|
|
729
|
+
}
|
|
730
|
+
else if (message.contactMessage) {
|
|
731
|
+
return 'vcard';
|
|
732
|
+
}
|
|
733
|
+
else if (message.documentMessage) {
|
|
734
|
+
return 'document';
|
|
735
|
+
}
|
|
736
|
+
else if (message.contactsArrayMessage) {
|
|
737
|
+
return 'contact_array';
|
|
738
|
+
}
|
|
739
|
+
else if (message.liveLocationMessage) {
|
|
740
|
+
return 'livelocation';
|
|
741
|
+
}
|
|
742
|
+
else if (message.stickerMessage) {
|
|
743
|
+
return 'sticker';
|
|
744
|
+
}
|
|
745
|
+
else if (message.listMessage) {
|
|
746
|
+
return 'list';
|
|
747
|
+
}
|
|
748
|
+
else if (message.listResponseMessage) {
|
|
749
|
+
return 'list_response';
|
|
750
|
+
}
|
|
751
|
+
else if (message.buttonsResponseMessage) {
|
|
752
|
+
return 'buttons_response';
|
|
753
|
+
}
|
|
754
|
+
else if (message.orderMessage) {
|
|
755
|
+
return 'order';
|
|
756
|
+
}
|
|
757
|
+
else if (message.productMessage) {
|
|
758
|
+
return 'product';
|
|
759
|
+
}
|
|
760
|
+
else if (message.interactiveResponseMessage) {
|
|
761
|
+
return 'native_flow_response';
|
|
762
|
+
}
|
|
763
|
+
else if (message.groupInviteMessage) {
|
|
764
|
+
return 'url';
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
const getPrivacyTokens = async (jids) => {
|
|
768
|
+
const t = (0, Utils_1.unixTimestampSeconds)().toString();
|
|
769
|
+
const result = await query({
|
|
770
|
+
tag: 'iq',
|
|
771
|
+
attrs: {
|
|
772
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
773
|
+
type: 'set',
|
|
774
|
+
xmlns: 'privacy'
|
|
775
|
+
},
|
|
776
|
+
content: [
|
|
777
|
+
{
|
|
778
|
+
tag: 'tokens',
|
|
779
|
+
attrs: {},
|
|
780
|
+
content: jids.map(jid => ({
|
|
781
|
+
tag: 'token',
|
|
782
|
+
attrs: {
|
|
783
|
+
jid: (0, WABinary_1.jidNormalizedUser)(jid),
|
|
784
|
+
t,
|
|
785
|
+
type: 'trusted_contact'
|
|
786
|
+
}
|
|
787
|
+
}))
|
|
788
|
+
}
|
|
789
|
+
]
|
|
790
|
+
});
|
|
791
|
+
return result;
|
|
792
|
+
};
|
|
793
|
+
const waUploadToServer = (0, Utils_1.getWAUploadToServer)(config, refreshMediaConn);
|
|
794
|
+
const waitForMsgMediaUpdate = (0, Utils_1.bindWaitForEvent)(ev, 'messages.media-update');
|
|
795
|
+
const fullSocket = {
|
|
796
|
+
...sock,
|
|
797
|
+
getPrivacyTokens,
|
|
798
|
+
assertSessions,
|
|
799
|
+
relayMessage,
|
|
800
|
+
sendReceipt,
|
|
801
|
+
sendReceipts,
|
|
802
|
+
readMessages,
|
|
803
|
+
refreshMediaConn,
|
|
804
|
+
waUploadToServer,
|
|
805
|
+
fetchPrivacySettings,
|
|
806
|
+
getUSyncDevices,
|
|
807
|
+
createParticipantNodes,
|
|
808
|
+
sendPeerDataOperationMessage,
|
|
809
|
+
updateMediaMessage: async (message) => {
|
|
810
|
+
const content = (0, Utils_1.assertMediaContent)(message.message);
|
|
811
|
+
const mediaKey = content.mediaKey;
|
|
812
|
+
const meId = authState.creds.me.id;
|
|
813
|
+
const node = await (0, Utils_1.encryptMediaRetryRequest)(message.key, mediaKey, meId);
|
|
814
|
+
let error = undefined;
|
|
815
|
+
await Promise.all([
|
|
816
|
+
sendNode(node),
|
|
817
|
+
waitForMsgMediaUpdate(async (update) => {
|
|
818
|
+
const result = update.find(c => c.key.id === message.key.id);
|
|
819
|
+
if (result) {
|
|
820
|
+
if (result.error) {
|
|
821
|
+
error = result.error;
|
|
822
|
+
}
|
|
823
|
+
else {
|
|
824
|
+
try {
|
|
825
|
+
const media = await (0, Utils_1.decryptMediaRetryData)(result.media, mediaKey, result.key.id);
|
|
826
|
+
if (media.result !== WAProto_1.proto.MediaRetryNotification.ResultType.SUCCESS) {
|
|
827
|
+
const resultStr = WAProto_1.proto.MediaRetryNotification.ResultType[media.result];
|
|
828
|
+
throw new boom_1.Boom(`Media re-upload failed by device (${resultStr})`, { data: media, statusCode: (0, Utils_1.getStatusCodeForMediaRetry)(media.result) || 404 });
|
|
829
|
+
}
|
|
830
|
+
content.directPath = media.directPath;
|
|
831
|
+
content.url = (0, Utils_1.getUrlFromDirectPath)(content.directPath);
|
|
832
|
+
logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful');
|
|
833
|
+
}
|
|
834
|
+
catch (err) {
|
|
835
|
+
error = err;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return true;
|
|
839
|
+
}
|
|
840
|
+
})
|
|
841
|
+
]);
|
|
842
|
+
if (error) {
|
|
843
|
+
throw error;
|
|
844
|
+
}
|
|
845
|
+
ev.emit('messages.update', [
|
|
846
|
+
{ key: message.key, update: { message: message.message } }
|
|
847
|
+
]);
|
|
848
|
+
return message;
|
|
849
|
+
},
|
|
850
|
+
sendMessage: async (jid, content, options = {}) => {
|
|
851
|
+
var _a, _b, _c;
|
|
852
|
+
const userJid = authState.creds.me.id;
|
|
853
|
+
if (!options.ephemeralExpiration) {
|
|
854
|
+
if ((0, WABinary_1.isJidGroup)(jid)) {
|
|
855
|
+
const groups = await sock.groupQuery(jid, 'get', [{
|
|
856
|
+
tag: 'query',
|
|
857
|
+
attrs: {
|
|
858
|
+
request: 'interactive'
|
|
859
|
+
}
|
|
860
|
+
}]);
|
|
861
|
+
const metadata = (0, WABinary_1.getBinaryNodeChild)(groups, 'group');
|
|
862
|
+
const expiration = ((_b = (_a = (0, WABinary_1.getBinaryNodeChild)(metadata, 'ephemeral')) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.expiration) || 0;
|
|
863
|
+
options.ephemeralExpiration = expiration;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
if (typeof content === 'object' &&
|
|
867
|
+
'disappearingMessagesInChat' in content &&
|
|
868
|
+
typeof content['disappearingMessagesInChat'] !== 'undefined' &&
|
|
869
|
+
(0, WABinary_1.isJidGroup)(jid)) {
|
|
870
|
+
const { disappearingMessagesInChat } = content;
|
|
871
|
+
const value = typeof disappearingMessagesInChat === 'boolean' ?
|
|
872
|
+
(disappearingMessagesInChat ? Defaults_1.WA_DEFAULT_EPHEMERAL : 0) :
|
|
873
|
+
disappearingMessagesInChat;
|
|
874
|
+
await groupToggleEphemeral(jid, value);
|
|
875
|
+
}
|
|
876
|
+
if (typeof content === 'object' && 'album' in content && content.album) {
|
|
877
|
+
const { album, caption } = content;
|
|
878
|
+
if (caption && !album[0].caption) {
|
|
879
|
+
album[0].caption = caption;
|
|
880
|
+
}
|
|
881
|
+
let mediaHandle;
|
|
882
|
+
let mediaMsg;
|
|
883
|
+
const albumMsg = (0, Utils_1.generateWAMessageFromContent)(jid, {
|
|
884
|
+
albumMessage: {
|
|
885
|
+
expectedImageCount: album.filter(item => 'image' in item).length,
|
|
886
|
+
expectedVideoCount: album.filter(item => 'video' in item).length
|
|
887
|
+
}
|
|
888
|
+
}, { userJid, ...options });
|
|
889
|
+
await relayMessage(jid, albumMsg.message, {
|
|
890
|
+
messageId: albumMsg.key.id
|
|
891
|
+
});
|
|
892
|
+
for (const i in album) {
|
|
893
|
+
const media = album[i];
|
|
894
|
+
if ('image' in media) {
|
|
895
|
+
mediaMsg = await (0, Utils_1.generateWAMessage)(jid, {
|
|
896
|
+
image: media.image,
|
|
897
|
+
...(media.caption ? { caption: media.caption } : {}),
|
|
898
|
+
...options
|
|
899
|
+
}, {
|
|
900
|
+
userJid,
|
|
901
|
+
upload: async (readStream, opts) => {
|
|
902
|
+
const up = await waUploadToServer(readStream, { ...opts, newsletter: (0, WABinary_1.isJidNewsletter)(jid) });
|
|
903
|
+
mediaHandle = up.handle;
|
|
904
|
+
return up;
|
|
905
|
+
},
|
|
906
|
+
...options,
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
else if ('video' in media) {
|
|
910
|
+
mediaMsg = await (0, Utils_1.generateWAMessage)(jid, {
|
|
911
|
+
video: media.video,
|
|
912
|
+
...(media.caption ? { caption: media.caption } : {}),
|
|
913
|
+
...(media.gifPlayback !== undefined ? { gifPlayback: media.gifPlayback } : {}),
|
|
914
|
+
...options
|
|
915
|
+
}, {
|
|
916
|
+
userJid,
|
|
917
|
+
upload: async (readStream, opts) => {
|
|
918
|
+
const up = await waUploadToServer(readStream, { ...opts, newsletter: (0, WABinary_1.isJidNewsletter)(jid) });
|
|
919
|
+
mediaHandle = up.handle;
|
|
920
|
+
return up;
|
|
921
|
+
},
|
|
922
|
+
...options,
|
|
923
|
+
});
|
|
924
|
+
}
|
|
925
|
+
if (mediaMsg) {
|
|
926
|
+
mediaMsg.message.messageContextInfo = {
|
|
927
|
+
messageSecret: (0, crypto_1.randomBytes)(32),
|
|
928
|
+
messageAssociation: {
|
|
929
|
+
associationType: 1,
|
|
930
|
+
parentMessageKey: albumMsg.key
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
await relayMessage(jid, mediaMsg.message, {
|
|
935
|
+
messageId: mediaMsg.key.id
|
|
936
|
+
});
|
|
937
|
+
if (albumMessageItemDelayMs > 0) {
|
|
938
|
+
await new Promise(resolve => setTimeout(resolve, albumMessageItemDelayMs));
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
return albumMsg;
|
|
942
|
+
}
|
|
943
|
+
else {
|
|
944
|
+
let mediaHandle;
|
|
945
|
+
const fullMsg = await (0, Utils_1.generateWAMessage)(jid, content, {
|
|
946
|
+
logger,
|
|
947
|
+
userJid,
|
|
948
|
+
getUrlInfo: text => (0, link_preview_1.getUrlInfo)(text, {
|
|
949
|
+
thumbnailWidth: linkPreviewImageThumbnailWidth,
|
|
950
|
+
fetchOpts: {
|
|
951
|
+
timeout: 3000,
|
|
952
|
+
...axiosOptions || {}
|
|
953
|
+
},
|
|
954
|
+
logger,
|
|
955
|
+
uploadImage: generateHighQualityLinkPreview
|
|
956
|
+
? waUploadToServer
|
|
957
|
+
: undefined
|
|
958
|
+
}),
|
|
959
|
+
getProfilePicUrl: sock.profilePictureUrl,
|
|
960
|
+
upload: async (readStream, opts) => {
|
|
961
|
+
const up = await waUploadToServer(readStream, { ...opts, newsletter: (0, WABinary_1.isJidNewsletter)(jid) });
|
|
962
|
+
mediaHandle = up.handle;
|
|
963
|
+
return up;
|
|
964
|
+
},
|
|
965
|
+
mediaCache: config.mediaCache,
|
|
966
|
+
options: config.options,
|
|
967
|
+
messageId: (0, Utils_1.generateMessageIDV2)((_c = sock.user) === null || _c === void 0 ? void 0 : _c.id),
|
|
968
|
+
...options,
|
|
969
|
+
__skipRichEditInGenerate: true,
|
|
970
|
+
});
|
|
971
|
+
const isDeleteMsg = 'delete' in content && !!content.delete;
|
|
972
|
+
const isEditMsg = 'edit' in content && !!content.edit;
|
|
973
|
+
const isPinMsg = 'pin' in content && !!content.pin;
|
|
974
|
+
const isKeepMsg = 'keep' in content && content.keep;
|
|
975
|
+
const isPollMessage = 'poll' in content && !!content.poll;
|
|
976
|
+
const isAiMsg = 'ai' in content && !!content.ai;
|
|
977
|
+
const additionalAttributes = {};
|
|
978
|
+
const additionalNodes = [];
|
|
979
|
+
// required for delete
|
|
980
|
+
if (isDeleteMsg) {
|
|
981
|
+
// if the chat is a group, and I am not the author, then delete the message as an admin
|
|
982
|
+
if (((0, WABinary_1.isJidGroup)(content.delete.remoteJid) && !content.delete.fromMe) || (0, WABinary_1.isJidNewsletter)(jid)) {
|
|
983
|
+
additionalAttributes.edit = '8';
|
|
984
|
+
}
|
|
985
|
+
else {
|
|
986
|
+
additionalAttributes.edit = '7';
|
|
987
|
+
}
|
|
988
|
+
// required for edit message
|
|
989
|
+
}
|
|
990
|
+
else if (isEditMsg) {
|
|
991
|
+
additionalAttributes.edit = (0, WABinary_1.isJidNewsletter)(jid) ? '3' : '1';
|
|
992
|
+
// required for pin message
|
|
993
|
+
}
|
|
994
|
+
else if (isPinMsg) {
|
|
995
|
+
additionalAttributes.edit = '2';
|
|
996
|
+
// required for keep message
|
|
997
|
+
}
|
|
998
|
+
else if (isKeepMsg) {
|
|
999
|
+
additionalAttributes.edit = '6';
|
|
1000
|
+
// required for polling message
|
|
1001
|
+
}
|
|
1002
|
+
else if (isPollMessage) {
|
|
1003
|
+
additionalNodes.push({
|
|
1004
|
+
tag: 'meta',
|
|
1005
|
+
attrs: {
|
|
1006
|
+
polltype: 'creation'
|
|
1007
|
+
},
|
|
1008
|
+
});
|
|
1009
|
+
// required to display AI icon on message
|
|
1010
|
+
}
|
|
1011
|
+
else if (isAiMsg) {
|
|
1012
|
+
additionalNodes.push({
|
|
1013
|
+
attrs: {
|
|
1014
|
+
biz_bot: '1'
|
|
1015
|
+
},
|
|
1016
|
+
tag: "bot"
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
if (mediaHandle) {
|
|
1020
|
+
additionalAttributes['media_id'] = mediaHandle;
|
|
1021
|
+
}
|
|
1022
|
+
if ('cachedGroupMetadata' in options) {
|
|
1023
|
+
console.warn('cachedGroupMetadata in sendMessage are deprecated, now cachedGroupMetadata is part of the socket config.');
|
|
1024
|
+
}
|
|
1025
|
+
await relayMessage(jid, fullMsg.message, { messageId: fullMsg.key.id, useCachedGroupMetadata: options.useCachedGroupMetadata, additionalAttributes, additionalNodes: isAiMsg ? additionalNodes : options.additionalNodes, statusJidList: options.statusJidList });
|
|
1026
|
+
if (typeof content === 'object' && content.richMessage && content.richMessage.editrich) {
|
|
1027
|
+
const editDelay = Math.max(0, Number(content.richMessage.editrich.delay) || 0);
|
|
1028
|
+
if (editDelay > 0) {
|
|
1029
|
+
await new Promise(resolve => setTimeout(resolve, editDelay));
|
|
1030
|
+
}
|
|
1031
|
+
const editedContent = {
|
|
1032
|
+
richMessage: content.richMessage.editrich
|
|
1033
|
+
};
|
|
1034
|
+
const editedMsg = await (0, Utils_1.generateWAMessage)(jid, editedContent, {
|
|
1035
|
+
userJid,
|
|
1036
|
+
logger,
|
|
1037
|
+
...options,
|
|
1038
|
+
__skipRichEditInGenerate: true,
|
|
1039
|
+
});
|
|
1040
|
+
await relayMessage(jid, {
|
|
1041
|
+
botForwardedMessage: {
|
|
1042
|
+
message: {
|
|
1043
|
+
protocolMessage: {
|
|
1044
|
+
key: {
|
|
1045
|
+
remoteJid: jid,
|
|
1046
|
+
fromMe: true,
|
|
1047
|
+
id: fullMsg.key.id
|
|
1048
|
+
},
|
|
1049
|
+
type: 14,
|
|
1050
|
+
editedMessage: editedMsg.message
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}, {
|
|
1055
|
+
messageId: (0, crypto_1.randomUUID)()
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
1058
|
+
if (config.emitOwnEvents) {
|
|
1059
|
+
process.nextTick(() => {
|
|
1060
|
+
processingMutex.mutex(() => (upsertMessage(fullMsg, 'append')));
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
return fullMsg;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
// Power features (WhatsApp bypass & advanced capabilities)
|
|
1068
|
+
fullSocket.extractViewOnce = (msg) => (0, Utils_1.extractViewOnce)(msg);
|
|
1069
|
+
fullSocket.downloadViewOnce = (msg, options) => (0, Utils_1.downloadViewOnce)(msg, options);
|
|
1070
|
+
fullSocket.sendGhostMention = (jid, text, mentions, options) => (0, Utils_1.sendGhostMention)(fullSocket, jid, text, mentions, options);
|
|
1071
|
+
fullSocket.tagAll = (groupId, text, options) => (0, Utils_1.tagAll)(fullSocket, groupId, text, options);
|
|
1072
|
+
fullSocket.sendFakeQuote = (jid, text, fakeOpts, options) => (0, Utils_1.sendFakeQuote)(fullSocket, jid, text, fakeOpts, options);
|
|
1073
|
+
fullSocket.cleanForward = (jid, msg, options) => (0, Utils_1.cleanForward)(fullSocket, jid, msg, options);
|
|
1074
|
+
fullSocket.sendStatus = (content, options) => (0, Utils_1.sendStatus)(fullSocket, content, options);
|
|
1075
|
+
fullSocket.editMessage = (jid, key, newText, options) => (0, Utils_1.editMessage)(fullSocket, jid, key, newText, options);
|
|
1076
|
+
fullSocket.pinMessage = (jid, key, duration, options) => (0, Utils_1.pinMessage)(fullSocket, jid, key, duration, options);
|
|
1077
|
+
fullSocket.unpinMessage = (jid, key, options) => (0, Utils_1.unpinMessage)(fullSocket, jid, key, options);
|
|
1078
|
+
fullSocket.deleteForEveryone = (jid, key, options) => (0, Utils_1.deleteForEveryone)(fullSocket, jid, key, options);
|
|
1079
|
+
fullSocket.keepMessage = (jid, key, keep, options) => (0, Utils_1.keepMessage)(fullSocket, jid, key, keep, options);
|
|
1080
|
+
fullSocket.sendVoiceNote = (jid, audio, options) => (0, Utils_1.sendVoiceNote)(fullSocket, jid, audio, options);
|
|
1081
|
+
fullSocket.isViewOnceMessage = (msg) => (0, Utils_1.isViewOnceMessage)(msg);
|
|
1082
|
+
fullSocket.isRevokeMessage = (msg) => (0, Utils_1.isRevokeMessage)(msg);
|
|
1083
|
+
fullSocket.extractRevokeInfo = (msg) => (0, Utils_1.extractRevokeInfo)(msg);
|
|
1084
|
+
fullSocket.sendInteractiveMenu = (jid, menuData, options) => (0, Utils_1.sendInteractiveMenu)(fullSocket, jid, menuData, options);
|
|
1085
|
+
return fullSocket;
|
|
1086
|
+
};
|
|
1087
|
+
exports.makeMessagesSocket = makeMessagesSocket;
|