@genuxofficial/baileys 1.0.0
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 +21 -0
- package/README.md +1 -0
- package/WAProto/GenerateStatics.sh +4 -0
- package/WAProto/WAProto.proto +3344 -0
- package/WAProto/index.d.ts +37016 -0
- package/WAProto/index.js +101044 -0
- package/WASignalGroup/GroupProtocol.js +1697 -0
- package/WASignalGroup/ciphertext_message.js +16 -0
- package/WASignalGroup/group_cipher.js +120 -0
- package/WASignalGroup/group_session_builder.js +46 -0
- package/WASignalGroup/index.js +5 -0
- package/WASignalGroup/keyhelper.js +21 -0
- package/WASignalGroup/protobufs.js +3 -0
- package/WASignalGroup/queue_job.js +69 -0
- package/WASignalGroup/sender_chain_key.js +50 -0
- package/WASignalGroup/sender_key_distribution_message.js +78 -0
- package/WASignalGroup/sender_key_message.js +92 -0
- package/WASignalGroup/sender_key_name.js +70 -0
- package/WASignalGroup/sender_key_record.js +56 -0
- package/WASignalGroup/sender_key_state.js +129 -0
- package/WASignalGroup/sender_message_key.js +39 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.d.ts +53 -0
- package/lib/Defaults/index.js +106 -0
- package/lib/Signal/libsignal.d.ts +3 -0
- package/lib/Signal/libsignal.js +152 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +18 -0
- package/lib/Socket/Client/types.d.ts +17 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +62 -0
- package/lib/Socket/business.d.ts +149 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.d.ts +85 -0
- package/lib/Socket/chats.js +850 -0
- package/lib/Socket/groups.d.ts +124 -0
- package/lib/Socket/groups.js +314 -0
- package/lib/Socket/index.d.ts +149 -0
- package/lib/Socket/index.js +10 -0
- package/lib/Socket/messages-recv.d.ts +136 -0
- package/lib/Socket/messages-recv.js +912 -0
- package/lib/Socket/messages-send.d.ts +130 -0
- package/lib/Socket/messages-send.js +673 -0
- package/lib/Socket/socket.d.ts +45 -0
- package/lib/Socket/socket.js +622 -0
- package/lib/Socket/usync.d.ts +38 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.d.ts +14 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.d.ts +118 -0
- package/lib/Store/make-in-memory-store.js +420 -0
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.d.ts +105 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.d.ts +13 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.d.ts +108 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.d.ts +19 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.d.ts +172 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.d.ts +56 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.d.ts +46 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.d.ts +29 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.d.ts +271 -0
- package/lib/Types/Message.js +9 -0
- package/lib/Types/Product.d.ts +78 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.d.ts +57 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.d.ts +118 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.d.ts +27 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.d.ts +25 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.d.ts +63 -0
- package/lib/Types/index.js +41 -0
- package/lib/Utils/auth-utils.d.ts +18 -0
- package/lib/Utils/auth-utils.js +200 -0
- package/lib/Utils/baileys-event-stream.d.ts +16 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/business.d.ts +22 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.d.ts +72 -0
- package/lib/Utils/chat-utils.js +745 -0
- package/lib/Utils/crypto.d.ts +42 -0
- package/lib/Utils/crypto.js +181 -0
- package/lib/Utils/decode-wa-message.d.ts +36 -0
- package/lib/Utils/decode-wa-message.js +205 -0
- package/lib/Utils/event-buffer.d.ts +35 -0
- package/lib/Utils/event-buffer.js +520 -0
- package/lib/Utils/generics.d.ts +94 -0
- package/lib/Utils/generics.js +433 -0
- package/lib/Utils/history.d.ts +19 -0
- package/lib/Utils/history.js +94 -0
- package/lib/Utils/index.d.ts +17 -0
- package/lib/Utils/index.js +33 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +116 -0
- package/lib/Utils/logger.d.ts +11 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/make-mutex.js +44 -0
- package/lib/Utils/messages-media.d.ts +108 -0
- package/lib/Utils/messages-media.js +667 -0
- package/lib/Utils/messages.d.ts +78 -0
- package/lib/Utils/messages.js +760 -0
- package/lib/Utils/noise-handler.d.ts +21 -0
- package/lib/Utils/noise-handler.js +150 -0
- package/lib/Utils/process-message.d.ts +42 -0
- package/lib/Utils/process-message.js +373 -0
- package/lib/Utils/signal.d.ts +33 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +12 -0
- package/lib/Utils/use-multi-file-auth-state.js +94 -0
- package/lib/Utils/validate-connection.d.ts +10 -0
- package/lib/Utils/validate-connection.js +175 -0
- package/lib/WABinary/constants.d.ts +27 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.d.ts +8 -0
- package/lib/WABinary/decode.js +254 -0
- package/lib/WABinary/encode.d.ts +4 -0
- package/lib/WABinary/encode.js +234 -0
- package/lib/WABinary/generic-utils.d.ts +16 -0
- package/lib/WABinary/generic-utils.js +110 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.d.ts +31 -0
- package/lib/WABinary/jid-utils.js +62 -0
- package/lib/WABinary/types.d.ts +18 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.d.ts +18 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.d.ts +39 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.d.ts +4 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/index.d.ts +4 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.d.ts +26 -0
- package/lib/WAUSync/USyncQuery.js +79 -0
- package/lib/WAUSync/USyncUser.d.ts +10 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +31 -0
- package/package.json +104 -0
@@ -0,0 +1,673 @@
|
|
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 node_cache_1 = __importDefault(require("@cacheable/node-cache"));
|
8
|
+
const boom_1 = require("@hapi/boom");
|
9
|
+
const WAProto_1 = require("../../WAProto");
|
10
|
+
const Defaults_1 = require("../Defaults");
|
11
|
+
const Utils_1 = require("../Utils");
|
12
|
+
const link_preview_1 = require("../Utils/link-preview");
|
13
|
+
const WABinary_1 = require("../WABinary");
|
14
|
+
const WAUSync_1 = require("../WAUSync");
|
15
|
+
const groups_1 = require("./groups");
|
16
|
+
const makeMessagesSocket = (config) => {
|
17
|
+
const { logger, linkPreviewImageThumbnailWidth, generateHighQualityLinkPreview, options: axiosOptions, patchMessageBeforeSending, cachedGroupMetadata, } = config;
|
18
|
+
const sock = (0, groups_1.makeGroupsSocket)(config);
|
19
|
+
const { ev, authState, processingMutex, signalRepository, upsertMessage, query, fetchPrivacySettings, sendNode, groupMetadata, groupToggleEphemeral, } = sock;
|
20
|
+
const userDevicesCache = config.userDevicesCache || new node_cache_1.default({
|
21
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.USER_DEVICES,
|
22
|
+
useClones: false
|
23
|
+
});
|
24
|
+
let mediaConn;
|
25
|
+
const refreshMediaConn = async (forceGet = false) => {
|
26
|
+
const media = await mediaConn;
|
27
|
+
if (!media || forceGet || (new Date().getTime() - media.fetchDate.getTime()) > media.ttl * 1000) {
|
28
|
+
mediaConn = (async () => {
|
29
|
+
const result = await query({
|
30
|
+
tag: 'iq',
|
31
|
+
attrs: {
|
32
|
+
type: 'set',
|
33
|
+
xmlns: 'w:m',
|
34
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
35
|
+
},
|
36
|
+
content: [{ tag: 'media_conn', attrs: {} }]
|
37
|
+
});
|
38
|
+
const mediaConnNode = (0, WABinary_1.getBinaryNodeChild)(result, 'media_conn');
|
39
|
+
const node = {
|
40
|
+
hosts: (0, WABinary_1.getBinaryNodeChildren)(mediaConnNode, 'host').map(({ attrs }) => ({
|
41
|
+
hostname: attrs.hostname,
|
42
|
+
maxContentLengthBytes: +attrs.maxContentLengthBytes,
|
43
|
+
})),
|
44
|
+
auth: mediaConnNode.attrs.auth,
|
45
|
+
ttl: +mediaConnNode.attrs.ttl,
|
46
|
+
fetchDate: new Date()
|
47
|
+
};
|
48
|
+
logger.debug('fetched media conn');
|
49
|
+
return node;
|
50
|
+
})();
|
51
|
+
}
|
52
|
+
return mediaConn;
|
53
|
+
};
|
54
|
+
/**
|
55
|
+
* generic send receipt function
|
56
|
+
* used for receipts of phone call, read, delivery etc.
|
57
|
+
* */
|
58
|
+
const sendReceipt = async (jid, participant, messageIds, type) => {
|
59
|
+
const node = {
|
60
|
+
tag: 'receipt',
|
61
|
+
attrs: {
|
62
|
+
id: messageIds[0],
|
63
|
+
},
|
64
|
+
};
|
65
|
+
const isReadReceipt = type === 'read' || type === 'read-self';
|
66
|
+
if (isReadReceipt) {
|
67
|
+
node.attrs.t = (0, Utils_1.unixTimestampSeconds)().toString();
|
68
|
+
}
|
69
|
+
if (type === 'sender' && (0, WABinary_1.isJidUser)(jid)) {
|
70
|
+
node.attrs.recipient = jid;
|
71
|
+
node.attrs.to = participant;
|
72
|
+
}
|
73
|
+
else {
|
74
|
+
node.attrs.to = jid;
|
75
|
+
if (participant) {
|
76
|
+
node.attrs.participant = participant;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
if (type) {
|
80
|
+
node.attrs.type = type;
|
81
|
+
}
|
82
|
+
const remainingMessageIds = messageIds.slice(1);
|
83
|
+
if (remainingMessageIds.length) {
|
84
|
+
node.content = [
|
85
|
+
{
|
86
|
+
tag: 'list',
|
87
|
+
attrs: {},
|
88
|
+
content: remainingMessageIds.map(id => ({
|
89
|
+
tag: 'item',
|
90
|
+
attrs: { id }
|
91
|
+
}))
|
92
|
+
}
|
93
|
+
];
|
94
|
+
}
|
95
|
+
logger.debug({ attrs: node.attrs, messageIds }, 'sending receipt for messages');
|
96
|
+
await sendNode(node);
|
97
|
+
};
|
98
|
+
/** Correctly bulk send receipts to multiple chats, participants */
|
99
|
+
const sendReceipts = async (keys, type) => {
|
100
|
+
const recps = (0, Utils_1.aggregateMessageKeysNotFromMe)(keys);
|
101
|
+
for (const { jid, participant, messageIds } of recps) {
|
102
|
+
await sendReceipt(jid, participant, messageIds, type);
|
103
|
+
}
|
104
|
+
};
|
105
|
+
/** Bulk read messages. Keys can be from different chats & participants */
|
106
|
+
const readMessages = async (keys) => {
|
107
|
+
const privacySettings = await fetchPrivacySettings();
|
108
|
+
// based on privacy settings, we have to change the read type
|
109
|
+
const readType = privacySettings.readreceipts === 'all' ? 'read' : 'read-self';
|
110
|
+
await sendReceipts(keys, readType);
|
111
|
+
};
|
112
|
+
/** Fetch all the devices we've to send a message to */
|
113
|
+
const getUSyncDevices = async (jids, useCache, ignoreZeroDevices) => {
|
114
|
+
var _a;
|
115
|
+
const deviceResults = [];
|
116
|
+
if (!useCache) {
|
117
|
+
logger.debug('not using cache for devices');
|
118
|
+
}
|
119
|
+
const toFetch = [];
|
120
|
+
jids = Array.from(new Set(jids));
|
121
|
+
for (let jid of jids) {
|
122
|
+
const user = (_a = (0, WABinary_1.jidDecode)(jid)) === null || _a === void 0 ? void 0 : _a.user;
|
123
|
+
jid = (0, WABinary_1.jidNormalizedUser)(jid);
|
124
|
+
if (useCache) {
|
125
|
+
const devices = userDevicesCache.get(user);
|
126
|
+
if (devices) {
|
127
|
+
deviceResults.push(...devices);
|
128
|
+
logger.trace({ user }, 'using cache for devices');
|
129
|
+
}
|
130
|
+
else {
|
131
|
+
toFetch.push(jid);
|
132
|
+
}
|
133
|
+
}
|
134
|
+
else {
|
135
|
+
toFetch.push(jid);
|
136
|
+
}
|
137
|
+
}
|
138
|
+
if (!toFetch.length) {
|
139
|
+
return deviceResults;
|
140
|
+
}
|
141
|
+
const query = new WAUSync_1.USyncQuery()
|
142
|
+
.withContext('message')
|
143
|
+
.withDeviceProtocol();
|
144
|
+
for (const jid of toFetch) {
|
145
|
+
query.withUser(new WAUSync_1.USyncUser().withId(jid));
|
146
|
+
}
|
147
|
+
const result = await sock.executeUSyncQuery(query);
|
148
|
+
if (result) {
|
149
|
+
const extracted = (0, Utils_1.extractDeviceJids)(result === null || result === void 0 ? void 0 : result.list, authState.creds.me.id, ignoreZeroDevices);
|
150
|
+
const deviceMap = {};
|
151
|
+
for (const item of extracted) {
|
152
|
+
deviceMap[item.user] = deviceMap[item.user] || [];
|
153
|
+
deviceMap[item.user].push(item);
|
154
|
+
deviceResults.push(item);
|
155
|
+
}
|
156
|
+
for (const key in deviceMap) {
|
157
|
+
userDevicesCache.set(key, deviceMap[key]);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
return deviceResults;
|
161
|
+
};
|
162
|
+
const assertSessions = async (jids, force) => {
|
163
|
+
let didFetchNewSession = false;
|
164
|
+
let jidsRequiringFetch = [];
|
165
|
+
if (force) {
|
166
|
+
jidsRequiringFetch = jids;
|
167
|
+
}
|
168
|
+
else {
|
169
|
+
const addrs = jids.map(jid => (signalRepository
|
170
|
+
.jidToSignalProtocolAddress(jid)));
|
171
|
+
const sessions = await authState.keys.get('session', addrs);
|
172
|
+
for (const jid of jids) {
|
173
|
+
const signalId = signalRepository
|
174
|
+
.jidToSignalProtocolAddress(jid);
|
175
|
+
if (!sessions[signalId]) {
|
176
|
+
jidsRequiringFetch.push(jid);
|
177
|
+
}
|
178
|
+
}
|
179
|
+
}
|
180
|
+
if (jidsRequiringFetch.length) {
|
181
|
+
logger.debug({ jidsRequiringFetch }, 'fetching sessions');
|
182
|
+
const result = await query({
|
183
|
+
tag: 'iq',
|
184
|
+
attrs: {
|
185
|
+
xmlns: 'encrypt',
|
186
|
+
type: 'get',
|
187
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
188
|
+
},
|
189
|
+
content: [
|
190
|
+
{
|
191
|
+
tag: 'key',
|
192
|
+
attrs: {},
|
193
|
+
content: jidsRequiringFetch.map(jid => ({
|
194
|
+
tag: 'user',
|
195
|
+
attrs: { jid },
|
196
|
+
}))
|
197
|
+
}
|
198
|
+
]
|
199
|
+
});
|
200
|
+
await (0, Utils_1.parseAndInjectE2ESessions)(result, signalRepository);
|
201
|
+
didFetchNewSession = true;
|
202
|
+
}
|
203
|
+
return didFetchNewSession;
|
204
|
+
};
|
205
|
+
const sendPeerDataOperationMessage = async (pdoMessage) => {
|
206
|
+
var _a;
|
207
|
+
//TODO: for later, abstract the logic to send a Peer Message instead of just PDO - useful for App State Key Resync with phone
|
208
|
+
if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
|
209
|
+
throw new boom_1.Boom('Not authenticated');
|
210
|
+
}
|
211
|
+
const protocolMessage = {
|
212
|
+
protocolMessage: {
|
213
|
+
peerDataOperationRequestMessage: pdoMessage,
|
214
|
+
type: WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_MESSAGE
|
215
|
+
}
|
216
|
+
};
|
217
|
+
const meJid = (0, WABinary_1.jidNormalizedUser)(authState.creds.me.id);
|
218
|
+
const msgId = await relayMessage(meJid, protocolMessage, {
|
219
|
+
additionalAttributes: {
|
220
|
+
category: 'peer',
|
221
|
+
// eslint-disable-next-line camelcase
|
222
|
+
push_priority: 'high_force',
|
223
|
+
},
|
224
|
+
});
|
225
|
+
return msgId;
|
226
|
+
};
|
227
|
+
const createParticipantNodes = async (jids, message, extraAttrs) => {
|
228
|
+
const patched = await patchMessageBeforeSending(message, jids);
|
229
|
+
const bytes = (0, Utils_1.encodeWAMessage)(patched);
|
230
|
+
let shouldIncludeDeviceIdentity = false;
|
231
|
+
const nodes = await Promise.all(jids.map(async (jid) => {
|
232
|
+
const { type, ciphertext } = await signalRepository
|
233
|
+
.encryptMessage({ jid, data: bytes });
|
234
|
+
if (type === 'pkmsg') {
|
235
|
+
shouldIncludeDeviceIdentity = true;
|
236
|
+
}
|
237
|
+
const node = {
|
238
|
+
tag: 'to',
|
239
|
+
attrs: { jid },
|
240
|
+
content: [{
|
241
|
+
tag: 'enc',
|
242
|
+
attrs: {
|
243
|
+
v: '2',
|
244
|
+
type,
|
245
|
+
...extraAttrs || {}
|
246
|
+
},
|
247
|
+
content: ciphertext
|
248
|
+
}]
|
249
|
+
};
|
250
|
+
return node;
|
251
|
+
}));
|
252
|
+
return { nodes, shouldIncludeDeviceIdentity };
|
253
|
+
};
|
254
|
+
const relayMessage = async (jid, message, { messageId: msgId, participant, additionalAttributes, additionalNodes, useUserDevicesCache, useCachedGroupMetadata, statusJidList }) => {
|
255
|
+
var _a;
|
256
|
+
const meId = authState.creds.me.id;
|
257
|
+
let shouldIncludeDeviceIdentity = false;
|
258
|
+
const { user, server } = (0, WABinary_1.jidDecode)(jid);
|
259
|
+
const statusJid = 'status@broadcast';
|
260
|
+
const isGroup = server === 'g.us';
|
261
|
+
const isStatus = jid === statusJid;
|
262
|
+
const isLid = server === 'lid';
|
263
|
+
msgId = msgId || (0, Utils_1.generateMessageIDV2)((_a = sock.user) === null || _a === void 0 ? void 0 : _a.id);
|
264
|
+
useUserDevicesCache = useUserDevicesCache !== false;
|
265
|
+
useCachedGroupMetadata = useCachedGroupMetadata !== false && !isStatus;
|
266
|
+
const participants = [];
|
267
|
+
const destinationJid = (!isStatus) ? (0, WABinary_1.jidEncode)(user, isLid ? 'lid' : isGroup ? 'g.us' : 's.whatsapp.net') : statusJid;
|
268
|
+
const binaryNodeContent = [];
|
269
|
+
const devices = [];
|
270
|
+
const meMsg = {
|
271
|
+
deviceSentMessage: {
|
272
|
+
destinationJid,
|
273
|
+
message
|
274
|
+
}
|
275
|
+
};
|
276
|
+
const extraAttrs = {};
|
277
|
+
if (participant) {
|
278
|
+
// when the retry request is not for a group
|
279
|
+
// only send to the specific device that asked for a retry
|
280
|
+
// otherwise the message is sent out to every device that should be a recipient
|
281
|
+
if (!isGroup && !isStatus) {
|
282
|
+
additionalAttributes = { ...additionalAttributes, 'device_fanout': 'false' };
|
283
|
+
}
|
284
|
+
const { user, device } = (0, WABinary_1.jidDecode)(participant.jid);
|
285
|
+
devices.push({ user, device });
|
286
|
+
}
|
287
|
+
await authState.keys.transaction(async () => {
|
288
|
+
var _a, _b, _c, _d, _e;
|
289
|
+
const mediaType = getMediaType(message);
|
290
|
+
if (mediaType) {
|
291
|
+
extraAttrs['mediatype'] = mediaType;
|
292
|
+
}
|
293
|
+
if ((_a = (0, Utils_1.normalizeMessageContent)(message)) === null || _a === void 0 ? void 0 : _a.pinInChatMessage) {
|
294
|
+
extraAttrs['decrypt-fail'] = 'hide';
|
295
|
+
}
|
296
|
+
if (isGroup || isStatus) {
|
297
|
+
const [groupData, senderKeyMap] = await Promise.all([
|
298
|
+
(async () => {
|
299
|
+
let groupData = useCachedGroupMetadata && cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined;
|
300
|
+
if (groupData && Array.isArray(groupData === null || groupData === void 0 ? void 0 : groupData.participants)) {
|
301
|
+
logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata');
|
302
|
+
}
|
303
|
+
else if (!isStatus) {
|
304
|
+
groupData = await groupMetadata(jid);
|
305
|
+
}
|
306
|
+
return groupData;
|
307
|
+
})(),
|
308
|
+
(async () => {
|
309
|
+
if (!participant && !isStatus) {
|
310
|
+
const result = await authState.keys.get('sender-key-memory', [jid]);
|
311
|
+
return result[jid] || {};
|
312
|
+
}
|
313
|
+
return {};
|
314
|
+
})()
|
315
|
+
]);
|
316
|
+
if (!participant) {
|
317
|
+
const participantsList = (groupData && !isStatus) ? groupData.participants.map(p => p.id) : [];
|
318
|
+
if (isStatus && statusJidList) {
|
319
|
+
participantsList.push(...statusJidList);
|
320
|
+
}
|
321
|
+
const additionalDevices = await getUSyncDevices(participantsList, !!useUserDevicesCache, false);
|
322
|
+
devices.push(...additionalDevices);
|
323
|
+
}
|
324
|
+
const patched = await patchMessageBeforeSending(message, devices.map(d => (0, WABinary_1.jidEncode)(d.user, isLid ? 'lid' : 's.whatsapp.net', d.device)));
|
325
|
+
const bytes = (0, Utils_1.encodeWAMessage)(patched);
|
326
|
+
const { ciphertext, senderKeyDistributionMessage } = await signalRepository.encryptGroupMessage({
|
327
|
+
group: destinationJid,
|
328
|
+
data: bytes,
|
329
|
+
meId,
|
330
|
+
});
|
331
|
+
const senderKeyJids = [];
|
332
|
+
// ensure a connection is established with every device
|
333
|
+
for (const { user, device } of devices) {
|
334
|
+
const jid = (0, WABinary_1.jidEncode)(user, isLid ? 'lid' : 's.whatsapp.net', device);
|
335
|
+
if (!senderKeyMap[jid] || !!participant) {
|
336
|
+
senderKeyJids.push(jid);
|
337
|
+
// store that this person has had the sender keys sent to them
|
338
|
+
senderKeyMap[jid] = true;
|
339
|
+
}
|
340
|
+
}
|
341
|
+
// if there are some participants with whom the session has not been established
|
342
|
+
// if there are, we re-send the senderkey
|
343
|
+
if (senderKeyJids.length) {
|
344
|
+
logger.debug({ senderKeyJids }, 'sending new sender key');
|
345
|
+
const senderKeyMsg = {
|
346
|
+
senderKeyDistributionMessage: {
|
347
|
+
axolotlSenderKeyDistributionMessage: senderKeyDistributionMessage,
|
348
|
+
groupId: destinationJid
|
349
|
+
}
|
350
|
+
};
|
351
|
+
await assertSessions(senderKeyJids, false);
|
352
|
+
const result = await createParticipantNodes(senderKeyJids, senderKeyMsg, extraAttrs);
|
353
|
+
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || result.shouldIncludeDeviceIdentity;
|
354
|
+
participants.push(...result.nodes);
|
355
|
+
}
|
356
|
+
binaryNodeContent.push({
|
357
|
+
tag: 'enc',
|
358
|
+
attrs: { v: '2', type: 'skmsg' },
|
359
|
+
content: ciphertext
|
360
|
+
});
|
361
|
+
await authState.keys.set({ 'sender-key-memory': { [jid]: senderKeyMap } });
|
362
|
+
}
|
363
|
+
else {
|
364
|
+
const { user: meUser } = (0, WABinary_1.jidDecode)(meId);
|
365
|
+
if (!participant) {
|
366
|
+
devices.push({ user });
|
367
|
+
if (user !== meUser) {
|
368
|
+
devices.push({ user: meUser });
|
369
|
+
}
|
370
|
+
if ((additionalAttributes === null || additionalAttributes === void 0 ? void 0 : additionalAttributes['category']) !== 'peer') {
|
371
|
+
const additionalDevices = await getUSyncDevices([meId, jid], !!useUserDevicesCache, true);
|
372
|
+
devices.push(...additionalDevices);
|
373
|
+
}
|
374
|
+
}
|
375
|
+
const allJids = [];
|
376
|
+
const meJids = [];
|
377
|
+
const otherJids = [];
|
378
|
+
for (const { user, device } of devices) {
|
379
|
+
const isMe = user === meUser;
|
380
|
+
const jid = (0, WABinary_1.jidEncode)(isMe && isLid ? ((_c = (_b = authState.creds) === null || _b === void 0 ? void 0 : _b.me) === null || _c === void 0 ? void 0 : _c.lid.split(':')[0]) || user : user, isLid ? 'lid' : 's.whatsapp.net', device);
|
381
|
+
if (isMe) {
|
382
|
+
meJids.push(jid);
|
383
|
+
}
|
384
|
+
else {
|
385
|
+
otherJids.push(jid);
|
386
|
+
}
|
387
|
+
allJids.push(jid);
|
388
|
+
}
|
389
|
+
await assertSessions(allJids, false);
|
390
|
+
const [{ nodes: meNodes, shouldIncludeDeviceIdentity: s1 }, { nodes: otherNodes, shouldIncludeDeviceIdentity: s2 }] = await Promise.all([
|
391
|
+
createParticipantNodes(meJids, meMsg, extraAttrs),
|
392
|
+
createParticipantNodes(otherJids, message, extraAttrs)
|
393
|
+
]);
|
394
|
+
participants.push(...meNodes);
|
395
|
+
participants.push(...otherNodes);
|
396
|
+
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || s1 || s2;
|
397
|
+
}
|
398
|
+
if (participants.length) {
|
399
|
+
if ((additionalAttributes === null || additionalAttributes === void 0 ? void 0 : additionalAttributes['category']) === 'peer') {
|
400
|
+
const peerNode = (_e = (_d = participants[0]) === null || _d === void 0 ? void 0 : _d.content) === null || _e === void 0 ? void 0 : _e[0];
|
401
|
+
if (peerNode) {
|
402
|
+
binaryNodeContent.push(peerNode); // push only enc
|
403
|
+
}
|
404
|
+
}
|
405
|
+
else {
|
406
|
+
binaryNodeContent.push({
|
407
|
+
tag: 'participants',
|
408
|
+
attrs: {},
|
409
|
+
content: participants
|
410
|
+
});
|
411
|
+
}
|
412
|
+
}
|
413
|
+
const stanza = {
|
414
|
+
tag: 'message',
|
415
|
+
attrs: {
|
416
|
+
id: msgId,
|
417
|
+
type: getMessageType(message),
|
418
|
+
...(additionalAttributes || {})
|
419
|
+
},
|
420
|
+
content: binaryNodeContent
|
421
|
+
};
|
422
|
+
// if the participant to send to is explicitly specified (generally retry recp)
|
423
|
+
// ensure the message is only sent to that person
|
424
|
+
// if a retry receipt is sent to everyone -- it'll fail decryption for everyone else who received the msg
|
425
|
+
if (participant) {
|
426
|
+
if ((0, WABinary_1.isJidGroup)(destinationJid)) {
|
427
|
+
stanza.attrs.to = destinationJid;
|
428
|
+
stanza.attrs.participant = participant.jid;
|
429
|
+
}
|
430
|
+
else if ((0, WABinary_1.areJidsSameUser)(participant.jid, meId)) {
|
431
|
+
stanza.attrs.to = participant.jid;
|
432
|
+
stanza.attrs.recipient = destinationJid;
|
433
|
+
}
|
434
|
+
else {
|
435
|
+
stanza.attrs.to = participant.jid;
|
436
|
+
}
|
437
|
+
}
|
438
|
+
else {
|
439
|
+
stanza.attrs.to = destinationJid;
|
440
|
+
}
|
441
|
+
if (shouldIncludeDeviceIdentity) {
|
442
|
+
stanza.content.push({
|
443
|
+
tag: 'device-identity',
|
444
|
+
attrs: {},
|
445
|
+
content: (0, Utils_1.encodeSignedDeviceIdentity)(authState.creds.account, true)
|
446
|
+
});
|
447
|
+
logger.debug({ jid }, 'adding device identity');
|
448
|
+
}
|
449
|
+
if (additionalNodes && additionalNodes.length > 0) {
|
450
|
+
stanza.content.push(...additionalNodes);
|
451
|
+
}
|
452
|
+
logger.debug({ msgId }, `sending message to ${participants.length} devices`);
|
453
|
+
await sendNode(stanza);
|
454
|
+
});
|
455
|
+
return msgId;
|
456
|
+
};
|
457
|
+
const getMessageType = (message) => {
|
458
|
+
if (message.pollCreationMessage || message.pollCreationMessageV2 || message.pollCreationMessageV3) {
|
459
|
+
return 'poll';
|
460
|
+
}
|
461
|
+
return 'text';
|
462
|
+
};
|
463
|
+
const getMediaType = (message) => {
|
464
|
+
if (message.imageMessage) {
|
465
|
+
return 'image';
|
466
|
+
}
|
467
|
+
else if (message.videoMessage) {
|
468
|
+
return message.videoMessage.gifPlayback ? 'gif' : 'video';
|
469
|
+
}
|
470
|
+
else if (message.audioMessage) {
|
471
|
+
return message.audioMessage.ptt ? 'ptt' : 'audio';
|
472
|
+
}
|
473
|
+
else if (message.contactMessage) {
|
474
|
+
return 'vcard';
|
475
|
+
}
|
476
|
+
else if (message.documentMessage) {
|
477
|
+
return 'document';
|
478
|
+
}
|
479
|
+
else if (message.contactsArrayMessage) {
|
480
|
+
return 'contact_array';
|
481
|
+
}
|
482
|
+
else if (message.liveLocationMessage) {
|
483
|
+
return 'livelocation';
|
484
|
+
}
|
485
|
+
else if (message.stickerMessage) {
|
486
|
+
return 'sticker';
|
487
|
+
}
|
488
|
+
else if (message.listMessage) {
|
489
|
+
return 'list';
|
490
|
+
}
|
491
|
+
else if (message.listResponseMessage) {
|
492
|
+
return 'list_response';
|
493
|
+
}
|
494
|
+
else if (message.buttonsResponseMessage) {
|
495
|
+
return 'buttons_response';
|
496
|
+
}
|
497
|
+
else if (message.orderMessage) {
|
498
|
+
return 'order';
|
499
|
+
}
|
500
|
+
else if (message.productMessage) {
|
501
|
+
return 'product';
|
502
|
+
}
|
503
|
+
else if (message.interactiveResponseMessage) {
|
504
|
+
return 'native_flow_response';
|
505
|
+
}
|
506
|
+
else if (message.groupInviteMessage) {
|
507
|
+
return 'url';
|
508
|
+
}
|
509
|
+
};
|
510
|
+
const getPrivacyTokens = async (jids) => {
|
511
|
+
const t = (0, Utils_1.unixTimestampSeconds)().toString();
|
512
|
+
const result = await query({
|
513
|
+
tag: 'iq',
|
514
|
+
attrs: {
|
515
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
516
|
+
type: 'set',
|
517
|
+
xmlns: 'privacy'
|
518
|
+
},
|
519
|
+
content: [
|
520
|
+
{
|
521
|
+
tag: 'tokens',
|
522
|
+
attrs: {},
|
523
|
+
content: jids.map(jid => ({
|
524
|
+
tag: 'token',
|
525
|
+
attrs: {
|
526
|
+
jid: (0, WABinary_1.jidNormalizedUser)(jid),
|
527
|
+
t,
|
528
|
+
type: 'trusted_contact'
|
529
|
+
}
|
530
|
+
}))
|
531
|
+
}
|
532
|
+
]
|
533
|
+
});
|
534
|
+
return result;
|
535
|
+
};
|
536
|
+
const waUploadToServer = (0, Utils_1.getWAUploadToServer)(config, refreshMediaConn);
|
537
|
+
const waitForMsgMediaUpdate = (0, Utils_1.bindWaitForEvent)(ev, 'messages.media-update');
|
538
|
+
return {
|
539
|
+
...sock,
|
540
|
+
getPrivacyTokens,
|
541
|
+
assertSessions,
|
542
|
+
relayMessage,
|
543
|
+
sendReceipt,
|
544
|
+
sendReceipts,
|
545
|
+
readMessages,
|
546
|
+
refreshMediaConn,
|
547
|
+
waUploadToServer,
|
548
|
+
fetchPrivacySettings,
|
549
|
+
sendPeerDataOperationMessage,
|
550
|
+
createParticipantNodes,
|
551
|
+
getUSyncDevices,
|
552
|
+
updateMediaMessage: async (message) => {
|
553
|
+
const content = (0, Utils_1.assertMediaContent)(message.message);
|
554
|
+
const mediaKey = content.mediaKey;
|
555
|
+
const meId = authState.creds.me.id;
|
556
|
+
const node = await (0, Utils_1.encryptMediaRetryRequest)(message.key, mediaKey, meId);
|
557
|
+
let error = undefined;
|
558
|
+
await Promise.all([
|
559
|
+
sendNode(node),
|
560
|
+
waitForMsgMediaUpdate(async (update) => {
|
561
|
+
const result = update.find(c => c.key.id === message.key.id);
|
562
|
+
if (result) {
|
563
|
+
if (result.error) {
|
564
|
+
error = result.error;
|
565
|
+
}
|
566
|
+
else {
|
567
|
+
try {
|
568
|
+
const media = await (0, Utils_1.decryptMediaRetryData)(result.media, mediaKey, result.key.id);
|
569
|
+
if (media.result !== WAProto_1.proto.MediaRetryNotification.ResultType.SUCCESS) {
|
570
|
+
const resultStr = WAProto_1.proto.MediaRetryNotification.ResultType[media.result];
|
571
|
+
throw new boom_1.Boom(`Media re-upload failed by device (${resultStr})`, { data: media, statusCode: (0, Utils_1.getStatusCodeForMediaRetry)(media.result) || 404 });
|
572
|
+
}
|
573
|
+
content.directPath = media.directPath;
|
574
|
+
content.url = (0, Utils_1.getUrlFromDirectPath)(content.directPath);
|
575
|
+
logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful');
|
576
|
+
}
|
577
|
+
catch (err) {
|
578
|
+
error = err;
|
579
|
+
}
|
580
|
+
}
|
581
|
+
return true;
|
582
|
+
}
|
583
|
+
})
|
584
|
+
]);
|
585
|
+
if (error) {
|
586
|
+
throw error;
|
587
|
+
}
|
588
|
+
ev.emit('messages.update', [
|
589
|
+
{ key: message.key, update: { message: message.message } }
|
590
|
+
]);
|
591
|
+
return message;
|
592
|
+
},
|
593
|
+
sendMessage: async (jid, content, options = {}) => {
|
594
|
+
var _a, _b, _c;
|
595
|
+
const userJid = authState.creds.me.id;
|
596
|
+
if (typeof content === 'object' &&
|
597
|
+
'disappearingMessagesInChat' in content &&
|
598
|
+
typeof content['disappearingMessagesInChat'] !== 'undefined' &&
|
599
|
+
(0, WABinary_1.isJidGroup)(jid)) {
|
600
|
+
const { disappearingMessagesInChat } = content;
|
601
|
+
const value = typeof disappearingMessagesInChat === 'boolean' ?
|
602
|
+
(disappearingMessagesInChat ? Defaults_1.WA_DEFAULT_EPHEMERAL : 0) :
|
603
|
+
disappearingMessagesInChat;
|
604
|
+
await groupToggleEphemeral(jid, value);
|
605
|
+
}
|
606
|
+
else {
|
607
|
+
const fullMsg = await (0, Utils_1.generateWAMessage)(jid, content, {
|
608
|
+
logger,
|
609
|
+
userJid,
|
610
|
+
getUrlInfo: text => (0, link_preview_1.getUrlInfo)(text, {
|
611
|
+
thumbnailWidth: linkPreviewImageThumbnailWidth,
|
612
|
+
fetchOpts: {
|
613
|
+
timeout: 3000,
|
614
|
+
...axiosOptions || {}
|
615
|
+
},
|
616
|
+
logger,
|
617
|
+
uploadImage: generateHighQualityLinkPreview
|
618
|
+
? waUploadToServer
|
619
|
+
: undefined
|
620
|
+
}),
|
621
|
+
//TODO: CACHE
|
622
|
+
getProfilePicUrl: sock.profilePictureUrl,
|
623
|
+
upload: waUploadToServer,
|
624
|
+
mediaCache: config.mediaCache,
|
625
|
+
options: config.options,
|
626
|
+
messageId: (0, Utils_1.generateMessageIDV2)((_a = sock.user) === null || _a === void 0 ? void 0 : _a.id),
|
627
|
+
...options,
|
628
|
+
});
|
629
|
+
const isDeleteMsg = 'delete' in content && !!content.delete;
|
630
|
+
const isEditMsg = 'edit' in content && !!content.edit;
|
631
|
+
const isPinMsg = 'pin' in content && !!content.pin;
|
632
|
+
const isPollMessage = 'poll' in content && !!content.poll;
|
633
|
+
const additionalAttributes = {};
|
634
|
+
const additionalNodes = [];
|
635
|
+
// required for delete
|
636
|
+
if (isDeleteMsg) {
|
637
|
+
// if the chat is a group, and I am not the author, then delete the message as an admin
|
638
|
+
if ((0, WABinary_1.isJidGroup)((_b = content.delete) === null || _b === void 0 ? void 0 : _b.remoteJid) && !((_c = content.delete) === null || _c === void 0 ? void 0 : _c.fromMe)) {
|
639
|
+
additionalAttributes.edit = '8';
|
640
|
+
}
|
641
|
+
else {
|
642
|
+
additionalAttributes.edit = '7';
|
643
|
+
}
|
644
|
+
}
|
645
|
+
else if (isEditMsg) {
|
646
|
+
additionalAttributes.edit = '1';
|
647
|
+
}
|
648
|
+
else if (isPinMsg) {
|
649
|
+
additionalAttributes.edit = '2';
|
650
|
+
}
|
651
|
+
else if (isPollMessage) {
|
652
|
+
additionalNodes.push({
|
653
|
+
tag: 'meta',
|
654
|
+
attrs: {
|
655
|
+
polltype: 'creation'
|
656
|
+
},
|
657
|
+
});
|
658
|
+
}
|
659
|
+
if ('cachedGroupMetadata' in options) {
|
660
|
+
console.warn('cachedGroupMetadata in sendMessage are deprecated, now cachedGroupMetadata is part of the socket config.');
|
661
|
+
}
|
662
|
+
await relayMessage(jid, fullMsg.message, { messageId: fullMsg.key.id, useCachedGroupMetadata: options.useCachedGroupMetadata, additionalAttributes, statusJidList: options.statusJidList, additionalNodes });
|
663
|
+
if (config.emitOwnEvents) {
|
664
|
+
process.nextTick(() => {
|
665
|
+
processingMutex.mutex(() => (upsertMessage(fullMsg, 'append')));
|
666
|
+
});
|
667
|
+
}
|
668
|
+
return fullMsg;
|
669
|
+
}
|
670
|
+
}
|
671
|
+
};
|
672
|
+
};
|
673
|
+
exports.makeMessagesSocket = makeMessagesSocket;
|