fca-rqzax-remake 0.0.1-security → 2.6.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of fca-rqzax-remake might be problematic. Click here for more details.

Files changed (70) hide show
  1. package/.cache/replit/__replit_disk_meta.json +1 -0
  2. package/.cache/replit/modules.stamp +0 -0
  3. package/.cache/replit/nix/env.json +1 -0
  4. package/.gitattributes +2 -0
  5. package/.travis.yml +6 -0
  6. package/CHANGELOG.md +2 -0
  7. package/DOCS.md +1738 -0
  8. package/LICENSE-MIT +21 -0
  9. package/README.md +213 -3
  10. package/index.html +1200 -0
  11. package/index.js +542 -0
  12. package/package.json +65 -3
  13. package/replit.nix +9 -0
  14. package/src/addExternalModule.js +16 -0
  15. package/src/addUserToGroup.js +78 -0
  16. package/src/changeAdminStatus.js +79 -0
  17. package/src/changeArchivedStatus.js +41 -0
  18. package/src/changeBio.js +65 -0
  19. package/src/changeBlockedStatus.js +36 -0
  20. package/src/changeGroupImage.js +106 -0
  21. package/src/changeNickname.js +45 -0
  22. package/src/changeThreadColor.js +62 -0
  23. package/src/changeThreadEmoji.js +42 -0
  24. package/src/createNewGroup.js +70 -0
  25. package/src/createPoll.js +60 -0
  26. package/src/deleteMessage.js +45 -0
  27. package/src/deleteThread.js +43 -0
  28. package/src/forwardAttachment.js +48 -0
  29. package/src/getCurrentUserID.js +7 -0
  30. package/src/getEmojiUrl.js +27 -0
  31. package/src/getFriendsList.js +73 -0
  32. package/src/getThreadHistory.js +537 -0
  33. package/src/getThreadHistoryDeprecated.js +71 -0
  34. package/src/getThreadInfo.js +171 -0
  35. package/src/getThreadInfoDeprecated.js +56 -0
  36. package/src/getThreadList.js +213 -0
  37. package/src/getThreadListDeprecated.js +46 -0
  38. package/src/getThreadPictures.js +59 -0
  39. package/src/getUserID.js +62 -0
  40. package/src/getUserInfo.js +66 -0
  41. package/src/handleFriendRequest.js +46 -0
  42. package/src/handleMessageRequest.js +49 -0
  43. package/src/httpGet.js +49 -0
  44. package/src/httpPost.js +48 -0
  45. package/src/listenMqtt.js +663 -0
  46. package/src/logout.js +68 -0
  47. package/src/markAsDelivered.js +48 -0
  48. package/src/markAsRead.js +70 -0
  49. package/src/markAsReadAll.js +43 -0
  50. package/src/markAsSeen.js +51 -0
  51. package/src/muteThread.js +47 -0
  52. package/src/removeUserFromGroup.js +49 -0
  53. package/src/resolvePhotoUrl.js +37 -0
  54. package/src/searchForThread.js +43 -0
  55. package/src/sendMessage.js +321 -0
  56. package/src/sendTypingIndicator.js +80 -0
  57. package/src/setMessageReaction.js +109 -0
  58. package/src/setPostReaction.js +64 -0
  59. package/src/setTitle.js +74 -0
  60. package/src/threadColors.js +41 -0
  61. package/src/unfriend.js +43 -0
  62. package/src/unsendMessage.js +40 -0
  63. package/test/data/shareAttach.js +146 -0
  64. package/test/data/something.mov +0 -0
  65. package/test/data/test.png +0 -0
  66. package/test/data/test.txt +7 -0
  67. package/test/example-config.json +18 -0
  68. package/test/test-page.js +140 -0
  69. package/test/test.js +385 -0
  70. package/utils.js +1234 -0
@@ -0,0 +1,663 @@
1
+ /* eslint-disable no-redeclare */
2
+ "use strict";
3
+ var utils = require("../utils");
4
+ var log = require("npmlog");
5
+ var mqtt = require('mqtt');
6
+ var websocket = require('websocket-stream');
7
+ var HttpsProxyAgent = require('https-proxy-agent');
8
+ const EventEmitter = require('events');
9
+
10
+
11
+ var identity = function () { };
12
+ var form = {};
13
+ var getSeqID = function () { };
14
+
15
+ var topics = ["/legacy_web","/webrtc","/rtc_multi","/onevc","/br_sr","/sr_res","/t_ms","/thread_typing","/orca_typing_notifications","/notify_disconnect","/orca_presence","/inbox","/mercury", "/messaging_events", "/orca_message_notifications", "/pp","/webrtc_response"];
16
+
17
+ /* [ Noti ? ]
18
+ ! "/br_sr", //Notification
19
+ * => Need to publish /br_sr right after this
20
+
21
+ ! "/notify_disconnect",
22
+ * => Need to publish /messenger_sync_create_queue right after this
23
+
24
+ ! "/orca_presence",
25
+ * => Will receive /sr_res right here.
26
+ */
27
+
28
+ async function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
29
+ //Don't really know what this does but I think it's for the active state?
30
+ //TODO: Move to ctx when implemented
31
+ var chatOn = ctx.globalOptions.online;
32
+ var foreground = false;
33
+
34
+ var sessionID = Math.floor(Math.random() * 9007199254740991) + 1;
35
+ var username = {u: ctx.userID,s: sessionID,chat_on: chatOn,fg: foreground,d: utils.getGUID(),ct: "websocket",aid: "219994525426954", mqtt_sid: "",cp: 3,ecp: 10,st: [],pm: [],dc: "",no_auto_fg: true,gas: null,pack: []};
36
+ var cookies = ctx.jar.getCookies("https://www.facebook.com").join("; ");
37
+
38
+ var host;
39
+ if (ctx.mqttEndpoint) host = `${ctx.mqttEndpoint}&sid=${sessionID}`;
40
+ else if (ctx.region) host = `wss://edge-chat.facebook.com/chat?region=${ctx.region.toLocaleLowerCase()}&sid=${sessionID}`;
41
+ else host = `wss://edge-chat.facebook.com/chat?sid=${sessionID}`;
42
+
43
+ var options = {
44
+ clientId: "mqttwsclient",
45
+ protocolId: 'MQIsdp',
46
+ protocolVersion: 3,
47
+ username: JSON.stringify(username),
48
+ clean: true,
49
+ wsOptions: {
50
+ headers: {
51
+ 'Cookie': cookies,
52
+ 'Origin': 'https://www.facebook.com',
53
+ 'User-Agent': ctx.globalOptions.userAgent,
54
+ 'Referer': 'https://www.facebook.com/',
55
+ 'Host': new URL(host).hostname //'edge-chat.facebook.com'
56
+ },
57
+ origin: 'https://www.facebook.com',
58
+ protocolVersion: 13
59
+ },
60
+ keepalive: 13,
61
+ reschedulePings: true
62
+ };
63
+
64
+ if (typeof ctx.globalOptions.proxy != "undefined") {
65
+ var agent = new HttpsProxyAgent(ctx.globalOptions.proxy);
66
+ options.wsOptions.agent = agent;
67
+ }
68
+
69
+ ctx.mqttClient = new mqtt.Client(_ => websocket(host, options.wsOptions), options);
70
+
71
+ var mqttClient = ctx.mqttClient;
72
+
73
+ mqttClient.on('error', function (err) {
74
+ log.error("listenMqtt", err);
75
+ mqttClient.end();
76
+ if (ctx.globalOptions.autoReconnect) getSeqID();
77
+ else globalCallback({ type: "stop_listen", error: "Connection refused: Server unavailable" }, null);
78
+ });
79
+
80
+ mqttClient.on('connect', function () {
81
+ /*
82
+ const http = require("http");
83
+ const dashboard = http.createServer(function (request, res) {
84
+ res.writeHead(200, "OK", { "Content-Type": "text/plain" });
85
+ res.write("Vô Nghĩa");
86
+ res.end();
87
+ });
88
+
89
+ const port = process.env.PORT || 8080;
90
+ const express = require('express');
91
+ const app = express();
92
+ app.get('/', function(req, res) {
93
+ res.sendFile(path.join(__dirname, '/index.html'));
94
+ });
95
+ app.listen(port);
96
+ console.log('Máy chủ bắt đầu tại lúc http://localhost:' + port);
97
+ /*
98
+ ! Chỉ Dành Cho Mấy Pro Chạy Laptop Hoặc PC Thôi =))
99
+ */
100
+ // dashboard.listen(1000);
101
+
102
+ topics.forEach(topicsub => mqttClient.subscribe(topicsub));
103
+
104
+ var topic;
105
+ var queue = {
106
+ sync_api_version: 11,
107
+ max_deltas_able_to_process: 100,
108
+ delta_batch_size: 500,
109
+ encoding: "JSON",
110
+ entity_fbid: ctx.userID,
111
+ };
112
+
113
+ if (ctx.syncToken) {
114
+ topic = "/messenger_sync_get_diffs";
115
+ queue.last_seq_id = ctx.lastSeqId;
116
+ queue.sync_token = ctx.syncToken;
117
+ } else {
118
+ topic = "/messenger_sync_create_queue";
119
+ queue.initial_titan_sequence_id = ctx.lastSeqId;
120
+ queue.device_params = null;
121
+ }
122
+
123
+ mqttClient.publish(topic, JSON.stringify(queue), { qos: 1, retain: false });
124
+
125
+ var rTimeout = setTimeout(function () {
126
+ mqttClient.end();
127
+ getSeqID();
128
+ }, 3000);
129
+
130
+ ctx.tmsWait = function () {
131
+ clearTimeout(rTimeout);
132
+ ctx.globalOptions.emitReady ? globalCallback({type: "ready",error: null}) : '';
133
+ delete ctx.tmsWait;
134
+ };
135
+ });
136
+
137
+ mqttClient.on('message', function (topic, message, _packet) {
138
+ let jsonMessage = Buffer.from(message).toString('utf-8');
139
+ try { jsonMessage = JSON.parse(jsonMessage); }
140
+ catch (e) { jsonMessage = {}; }
141
+ if (topic === "/t_ms") {
142
+ if (ctx.tmsWait && typeof ctx.tmsWait == "function") ctx.tmsWait();
143
+
144
+ if (jsonMessage.firstDeltaSeqId && jsonMessage.syncToken) {
145
+ ctx.lastSeqId = jsonMessage.firstDeltaSeqId;
146
+ ctx.syncToken = jsonMessage.syncToken;
147
+ }
148
+
149
+ if (jsonMessage.lastIssuedSeqId) ctx.lastSeqId = parseInt(jsonMessage.lastIssuedSeqId);
150
+
151
+ //If it contains more than 1 delta
152
+ for (var i in jsonMessage.deltas) {
153
+ var delta = jsonMessage.deltas[i];
154
+ parseDelta(defaultFuncs, api, ctx, globalCallback, { "delta": delta });
155
+ }
156
+ } else if (topic === "/thread_typing" || topic === "/orca_typing_notifications") {
157
+ var typ = {
158
+ type: "typ",
159
+ isTyping: !!jsonMessage.state,
160
+ from: jsonMessage.sender_fbid.toString(),
161
+ threadID: utils.formatID((jsonMessage.thread || jsonMessage.sender_fbid).toString())
162
+ };
163
+ (function () { globalCallback(null, typ); })();
164
+ } else if (topic === "/orca_presence") {
165
+ if (!ctx.globalOptions.updatePresence) {
166
+ for (var i in jsonMessage.list) {
167
+ var data = jsonMessage.list[i];
168
+ var userID = data["u"];
169
+
170
+ var presence = {
171
+ type: "presence",
172
+ userID: userID.toString(),
173
+ //Convert to ms
174
+ timestamp: data["l"] * 1000,
175
+ statuses: data["p"]
176
+ };
177
+ (function () { globalCallback(null, presence); })();
178
+ }
179
+ }
180
+ }
181
+
182
+ });
183
+
184
+ mqttClient.on('close', function () {
185
+ //(function () { globalCallback("Connection closed."); })();
186
+ });
187
+ }
188
+
189
+ function parseDelta(defaultFuncs, api, ctx, globalCallback, v) {
190
+ if (v.delta.class == "NewMessage") {
191
+ //Not tested for pages
192
+ if (ctx.globalOptions.pageID && ctx.globalOptions.pageID != v.queue) return;
193
+
194
+ (function resolveAttachmentUrl(i) {
195
+ if (v.delta.attachments && (i == v.delta.attachments.length)) {
196
+ var fmtMsg;
197
+ try {
198
+ fmtMsg = utils.formatDeltaMessage(v);
199
+ } catch (err) {
200
+ return globalCallback({
201
+ error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
202
+ detail: err,
203
+ res: v,
204
+ type: "parse_error"
205
+ });
206
+ }
207
+ if (fmtMsg)
208
+ if (ctx.globalOptions.autoMarkDelivery) markDelivery(ctx, api, fmtMsg.threadID, fmtMsg.messageID);
209
+
210
+ return !ctx.globalOptions.selfListen && fmtMsg.senderID === ctx.userID ? undefined : (function () { globalCallback(null, fmtMsg); })();
211
+ } else {
212
+ if (v.delta.attachments && (v.delta.attachments[i].mercury.attach_type == "photo")) {
213
+ api.resolvePhotoUrl(v.delta.attachments[i].fbid, (err, url) => {
214
+ if (!err) v.delta.attachments[i].mercury.metadata.url = url;
215
+ return resolveAttachmentUrl(i + 1);
216
+ });
217
+ } else return resolveAttachmentUrl(i + 1);
218
+ }
219
+ })(0);
220
+ }
221
+
222
+ if (v.delta.class == "ClientPayload") {
223
+ var clientPayload = utils.decodeClientPayload(v.delta.payload);
224
+ if (clientPayload && clientPayload.deltas) {
225
+ for (var i in clientPayload.deltas) {
226
+ var delta = clientPayload.deltas[i];
227
+ if (delta.deltaMessageReaction && !!ctx.globalOptions.listenEvents) {
228
+ (function () {
229
+ globalCallback(null, {
230
+ type: "message_reaction",
231
+ threadID: (delta.deltaMessageReaction.threadKey.threadFbId ? delta.deltaMessageReaction.threadKey.threadFbId : delta.deltaMessageReaction.threadKey.otherUserFbId).toString(),
232
+ messageID: delta.deltaMessageReaction.messageId,
233
+ reaction: delta.deltaMessageReaction.reaction,
234
+ senderID: delta.deltaMessageReaction.senderId.toString(),
235
+ userID: delta.deltaMessageReaction.userId.toString()
236
+ });
237
+ })();
238
+ } else if (delta.deltaRecallMessageData && !!ctx.globalOptions.listenEvents) {
239
+ (function () {
240
+ globalCallback(null, {
241
+ type: "message_unsend",
242
+ threadID: (delta.deltaRecallMessageData.threadKey.threadFbId ? delta.deltaRecallMessageData.threadKey.threadFbId : delta.deltaRecallMessageData.threadKey.otherUserFbId).toString(),
243
+ messageID: delta.deltaRecallMessageData.messageID,
244
+ senderID: delta.deltaRecallMessageData.senderID.toString(),
245
+ deletionTimestamp: delta.deltaRecallMessageData.deletionTimestamp,
246
+ timestamp: delta.deltaRecallMessageData.timestamp
247
+ });
248
+ })();
249
+ } else if (delta.deltaMessageReply) {
250
+ //Mention block - #1
251
+ var mdata =
252
+ delta.deltaMessageReply.message === undefined ? [] :
253
+ delta.deltaMessageReply.message.data === undefined ? [] :
254
+ delta.deltaMessageReply.message.data.prng === undefined ? [] :
255
+ JSON.parse(delta.deltaMessageReply.message.data.prng);
256
+ var m_id = mdata.map(u => u.i);
257
+ var m_offset = mdata.map(u => u.o);
258
+ var m_length = mdata.map(u => u.l);
259
+
260
+ var mentions = {};
261
+
262
+ for (var i = 0; i < m_id.length; i++) mentions[m_id[i]] = (delta.deltaMessageReply.message.body || "").substring(m_offset[i], m_offset[i] + m_length[i]);
263
+ //Mention block - 1#
264
+ var callbackToReturn = {
265
+ type: "message_reply",
266
+ threadID: (delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.message.messageMetadata.threadKey.otherUserFbId).toString(),
267
+ messageID: delta.deltaMessageReply.message.messageMetadata.messageId,
268
+ senderID: delta.deltaMessageReply.message.messageMetadata.actorFbId.toString(),
269
+ attachments: delta.deltaMessageReply.message.attachments.map(function (att) {
270
+ var mercury = JSON.parse(att.mercuryJSON);
271
+ Object.assign(att, mercury);
272
+ return att;
273
+ }).map(att => {
274
+ var x;
275
+ try {
276
+ x = utils._formatAttachment(att);
277
+ } catch (ex) {
278
+ x = att;
279
+ x.error = ex;
280
+ x.type = "unknown";
281
+ }
282
+ return x;
283
+ }),
284
+ args: (delta.deltaMessageReply.message.body || "").trim().split(/\s+/),
285
+ body: (delta.deltaMessageReply.message.body || ""),
286
+ isGroup: !!delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId,
287
+ mentions: mentions,
288
+ timestamp: delta.deltaMessageReply.message.messageMetadata.timestamp,
289
+ participantIDs: (delta.deltaMessageReply.message.participants || []).map(e => e.toString())
290
+ };
291
+
292
+ if (delta.deltaMessageReply.repliedToMessage) {
293
+ //Mention block - #2
294
+ mdata =
295
+ delta.deltaMessageReply.repliedToMessage === undefined ? [] :
296
+ delta.deltaMessageReply.repliedToMessage.data === undefined ? [] :
297
+ delta.deltaMessageReply.repliedToMessage.data.prng === undefined ? [] :
298
+ JSON.parse(delta.deltaMessageReply.repliedToMessage.data.prng);
299
+ m_id = mdata.map(u => u.i);
300
+ m_offset = mdata.map(u => u.o);
301
+ m_length = mdata.map(u => u.l);
302
+
303
+ var rmentions = {};
304
+
305
+ for (var i = 0; i < m_id.length; i++) rmentions[m_id[i]] = (delta.deltaMessageReply.repliedToMessage.body || "").substring(m_offset[i], m_offset[i] + m_length[i]);
306
+ //Mention block - 2#
307
+ callbackToReturn.messageReply = {
308
+ threadID: (delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.otherUserFbId).toString(),
309
+ messageID: delta.deltaMessageReply.repliedToMessage.messageMetadata.messageId,
310
+ senderID: delta.deltaMessageReply.repliedToMessage.messageMetadata.actorFbId.toString(),
311
+ attachments: delta.deltaMessageReply.repliedToMessage.attachments.map(function (att) {
312
+ var mercury = JSON.parse(att.mercuryJSON);
313
+ Object.assign(att, mercury);
314
+ return att;
315
+ }).map(att => {
316
+ var x;
317
+ try {
318
+ x = utils._formatAttachment(att);
319
+ } catch (ex) {
320
+ x = att;
321
+ x.error = ex;
322
+ x.type = "unknown";
323
+ }
324
+ return x;
325
+ }),
326
+ args: (delta.deltaMessageReply.repliedToMessage.body || "").trim().split(/\s+/),
327
+ body: delta.deltaMessageReply.repliedToMessage.body || "",
328
+ isGroup: !!delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId,
329
+ mentions: rmentions,
330
+ timestamp: delta.deltaMessageReply.repliedToMessage.messageMetadata.timestamp,
331
+ participantIDs: (delta.deltaMessageReply.repliedToMessage.participants || []).map(e => e.toString())
332
+ };
333
+ } else if (delta.deltaMessageReply.replyToMessageId) {
334
+ return defaultFuncs
335
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, {
336
+ "av": ctx.globalOptions.pageID,
337
+ "queries": JSON.stringify({
338
+ "o0": {
339
+ //Using the same doc_id as forcedFetch
340
+ "doc_id": "2848441488556444",
341
+ "query_params": {
342
+ "thread_and_message_id": {
343
+ "thread_id": callbackToReturn.threadID,
344
+ "message_id": delta.deltaMessageReply.replyToMessageId.id,
345
+ }
346
+ }
347
+ }
348
+ })
349
+ })
350
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
351
+ .then((resData) => {
352
+ if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
353
+ if (resData[resData.length - 1].successful_results === 0) throw { error: "forcedFetch: there was no successful_results", res: resData };
354
+ var fetchData = resData[0].o0.data.message;
355
+ var mobj = {};
356
+ for (var n in fetchData.message.ranges) mobj[fetchData.message.ranges[n].entity.id] = (fetchData.message.text || "").substr(fetchData.message.ranges[n].offset, fetchData.message.ranges[n].length);
357
+
358
+ callbackToReturn.messageReply = {
359
+ threadID: callbackToReturn.threadID,
360
+ messageID: fetchData.message_id,
361
+ senderID: fetchData.message_sender.id.toString(),
362
+ attachments: fetchData.message.blob_attachment.map(att => {
363
+ var x;
364
+ try {
365
+ x = utils._formatAttachment({ blob_attachment: att });
366
+ } catch (ex) {
367
+ x = att;
368
+ x.error = ex;
369
+ x.type = "unknown";
370
+ }
371
+ return x;
372
+ }),
373
+ args: (fetchData.message.text || "").trim().split(/\s+/) || [],
374
+ body: fetchData.message.text || "",
375
+ isGroup: callbackToReturn.isGroup,
376
+ mentions: mobj,
377
+ timestamp: parseInt(fetchData.timestamp_precise)
378
+ };
379
+ })
380
+ .catch(err => log.error("forcedFetch", err))
381
+ .finally(function () {
382
+ if (ctx.globalOptions.autoMarkDelivery) markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
383
+ !ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID ? undefined : (function () { globalCallback(null, callbackToReturn); })();
384
+ });
385
+ } else callbackToReturn.delta = delta;
386
+
387
+ if (ctx.globalOptions.autoMarkDelivery) markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
388
+
389
+ return !ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID ? undefined : (function () { globalCallback(null, callbackToReturn); })();
390
+ }
391
+ }
392
+ return;
393
+ }
394
+ }
395
+
396
+ if (v.delta.class !== "NewMessage" && !ctx.globalOptions.listenEvents) return;
397
+ switch (v.delta.class) {
398
+ case "ReadReceipt":
399
+ var fmtMsg;
400
+ try {
401
+ fmtMsg = utils.formatDeltaReadReceipt(v.delta);
402
+ } catch (err) {
403
+ return globalCallback({
404
+ error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
405
+ detail: err,
406
+ res: v.delta,
407
+ type: "parse_error"
408
+ });
409
+ }
410
+ return (function () { globalCallback(null, fmtMsg); })();
411
+ case "AdminTextMessage":
412
+ switch (v.delta.type) {
413
+ case "change_thread_theme":
414
+ case "change_thread_icon":
415
+ case "change_thread_nickname":
416
+ case "change_thread_admins":
417
+ case "change_thread_approval_mode":
418
+ case "group_poll":
419
+ case "messenger_call_log":
420
+ case "participant_joined_group_call":
421
+ var fmtMsg;
422
+ try {
423
+ fmtMsg = utils.formatDeltaEvent(v.delta);
424
+ } catch (err) {
425
+ return globalCallback({
426
+ error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
427
+ detail: err,
428
+ res: v.delta,
429
+ type: "parse_error"
430
+ });
431
+ }
432
+ return (function () { globalCallback(null, fmtMsg); })();
433
+ default:
434
+ return;
435
+ }
436
+ break;
437
+ //For group images
438
+ case "ForcedFetch":
439
+ if (!v.delta.threadKey) return;
440
+ var mid = v.delta.messageId;
441
+ var tid = v.delta.threadKey.threadFbId;
442
+ if (mid && tid) {
443
+ const form = {
444
+ "av": ctx.globalOptions.pageID,
445
+ "queries": JSON.stringify({
446
+ "o0": {
447
+ //This doc_id is valid as of March 25, 2020
448
+ "doc_id": "2848441488556444",
449
+ "query_params": {
450
+ "thread_and_message_id": {
451
+ "thread_id": tid.toString(),
452
+ "message_id": mid,
453
+ }
454
+ }
455
+ }
456
+ })
457
+ };
458
+
459
+ defaultFuncs
460
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
461
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
462
+ .then((resData) => {
463
+ if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
464
+
465
+ if (resData[resData.length - 1].successful_results === 0) throw { error: "forcedFetch: there was no successful_results", res: resData };
466
+
467
+ var fetchData = resData[0].o0.data.message;
468
+
469
+ if (utils.getType(fetchData) == "Object") {
470
+ log.info("forcedFetch", fetchData);
471
+ switch (fetchData.__typename) {
472
+ case "ThreadImageMessage":
473
+ (!ctx.globalOptions.selfListen &&
474
+ fetchData.message_sender.id.toString() === ctx.userID) ||
475
+ !ctx.loggedIn ?
476
+ undefined :
477
+ (function () {
478
+ globalCallback(null, {
479
+ type: "change_thread_image",
480
+ threadID: utils.formatID(tid.toString()),
481
+ snippet: fetchData.snippet,
482
+ timestamp: fetchData.timestamp_precise,
483
+ author: fetchData.message_sender.id,
484
+ image: {
485
+ attachmentID: fetchData.image_with_metadata && fetchData.image_with_metadata.legacy_attachment_id,
486
+ width: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.x,
487
+ height: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.y,
488
+ url: fetchData.image_with_metadata && fetchData.image_with_metadata.preview.uri
489
+ }
490
+ });
491
+ })();
492
+ break;
493
+ case "UserMessage":
494
+ log.info("ff-Return", {
495
+ type: "message",
496
+ senderID: utils.formatID(fetchData.message_sender.id),
497
+ body: fetchData.message.text || "",
498
+ threadID: utils.formatID(tid.toString()),
499
+ messageID: fetchData.message_id,
500
+ attachments: [{
501
+ type: "share",
502
+ ID: fetchData.extensible_attachment.legacy_attachment_id,
503
+ url: fetchData.extensible_attachment.story_attachment.url,
504
+
505
+ title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
506
+ description: fetchData.extensible_attachment.story_attachment.description.text,
507
+ source: fetchData.extensible_attachment.story_attachment.source,
508
+
509
+ image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
510
+ width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
511
+ height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
512
+ playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
513
+ duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
514
+
515
+ subattachments: fetchData.extensible_attachment.subattachments,
516
+ properties: fetchData.extensible_attachment.story_attachment.properties,
517
+ }],
518
+ mentions: {},
519
+ timestamp: parseInt(fetchData.timestamp_precise),
520
+ isGroup: (fetchData.message_sender.id != tid.toString())
521
+ });
522
+ globalCallback(null, {
523
+ type: "message",
524
+ senderID: utils.formatID(fetchData.message_sender.id),
525
+ body: fetchData.message.text || "",
526
+ threadID: utils.formatID(tid.toString()),
527
+ messageID: fetchData.message_id,
528
+ attachments: [{
529
+ type: "share",
530
+ ID: fetchData.extensible_attachment.legacy_attachment_id,
531
+ url: fetchData.extensible_attachment.story_attachment.url,
532
+
533
+ title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
534
+ description: fetchData.extensible_attachment.story_attachment.description.text,
535
+ source: fetchData.extensible_attachment.story_attachment.source,
536
+
537
+ image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
538
+ width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
539
+ height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
540
+ playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
541
+ duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
542
+
543
+ subattachments: fetchData.extensible_attachment.subattachments,
544
+ properties: fetchData.extensible_attachment.story_attachment.properties,
545
+ }],
546
+ mentions: {},
547
+ timestamp: parseInt(fetchData.timestamp_precise),
548
+ isGroup: (fetchData.message_sender.id != tid.toString())
549
+ });
550
+ }
551
+ } else log.error("forcedFetch", fetchData);
552
+ })
553
+ .catch((err) => log.error("forcedFetch", err));
554
+ }
555
+ break;
556
+ case "ThreadName":
557
+ case "ParticipantsAddedToGroupThread":
558
+ case "ParticipantLeftGroupThread":
559
+ var formattedEvent;
560
+ try {
561
+ formattedEvent = utils.formatDeltaEvent(v.delta);
562
+ } catch (err) {
563
+ return globalCallback({
564
+ error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
565
+ detail: err,
566
+ res: v.delta,
567
+ type: "parse_error"
568
+ });
569
+ }
570
+ return (!ctx.globalOptions.selfListen && formattedEvent.author.toString() === ctx.userID) || !ctx.loggedIn ? undefined : (function () { globalCallback(null, formattedEvent); })();
571
+ }
572
+ }
573
+
574
+ function markDelivery(ctx, api, threadID, messageID) {
575
+ if (threadID && messageID) {
576
+ api.markAsDelivered(threadID, messageID, (err) => {
577
+ if (err) log.error("markAsDelivered", err);
578
+ else {
579
+ if (ctx.globalOptions.autoMarkRead) {
580
+ api.markAsRead(threadID, (err) => {
581
+ if (err) log.error("markAsDelivered", err);
582
+ });
583
+ }
584
+ }
585
+ });
586
+ }
587
+ }
588
+
589
+ module.exports = function (defaultFuncs, api, ctx) {
590
+ var globalCallback = identity;
591
+ getSeqID = function getSeqID() {
592
+ ctx.t_mqttCalled = false;
593
+ defaultFuncs
594
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
595
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
596
+ .then((resData) => {
597
+ if (utils.getType(resData) != "Array") throw { error: "Chưa Đăng Nhập Được - Appstate Đã Bị Lỗi", res: resData };
598
+ if (resData && resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
599
+ if (resData[resData.length - 1].successful_results === 0) throw { error: "getSeqId: there was no successful_results", res: resData };
600
+ if (resData[0].o0.data.viewer.message_threads.sync_sequence_id) {
601
+ ctx.lastSeqId = resData[0].o0.data.viewer.message_threads.sync_sequence_id;
602
+ listenMqtt(defaultFuncs, api, ctx, globalCallback);
603
+ } else throw { error: "getSeqId: no sync_sequence_id found.", res: resData };
604
+ })
605
+ .catch((err) => {
606
+ log.error("getSeqId", err);
607
+ if (utils.getType(err) == "Object" && err.error === "Chưa Đăng Nhập Được - Appstate Đã Bị Lỗi") ctx.loggedIn = false;
608
+ return globalCallback(err);
609
+ });
610
+ };
611
+
612
+ return async function (callback) {
613
+ class MessageEmitter extends EventEmitter {
614
+ stopListening(callback) {
615
+ callback = callback || (() => { });
616
+ globalCallback = identity;
617
+ if (ctx.mqttClient) {
618
+ ctx.mqttClient.unsubscribe("/webrtc");
619
+ ctx.mqttClient.unsubscribe("/rtc_multi");
620
+ ctx.mqttClient.unsubscribe("/onevc");
621
+ ctx.mqttClient.publish("/browser_close", "{}");
622
+ ctx.mqttClient.end(false, function (...data) {
623
+ callback(data);
624
+ ctx.mqttClient = undefined;
625
+ });
626
+ }
627
+ }
628
+ }
629
+
630
+ var msgEmitter = new MessageEmitter();
631
+ globalCallback = (callback || function (error, message) {
632
+ if (error) return msgEmitter.emit("error", error);
633
+ msgEmitter.emit("message", message);
634
+ });
635
+
636
+ //Reset some stuff
637
+ if (!ctx.firstListen) ctx.lastSeqId = null;
638
+ ctx.syncToken = undefined;
639
+ ctx.t_mqttCalled = false;
640
+
641
+ //Same request as getThreadList
642
+ form = {
643
+ "av": ctx.globalOptions.pageID,
644
+ "queries": JSON.stringify({
645
+ "o0": {
646
+ "doc_id": "3336396659757871",
647
+ "query_params": {
648
+ "limit": 1,
649
+ "before": null,
650
+ "tags": ["INBOX"],
651
+ "includeDeliveryReceipts": false,
652
+ "includeSeqID": true
653
+ }
654
+ }
655
+ })
656
+ };
657
+
658
+ if (!ctx.firstListen || !ctx.lastSeqId) getSeqID();
659
+ else listenMqtt(defaultFuncs, api, ctx, globalCallback);
660
+ ctx.firstListen = false;
661
+ return msgEmitter;
662
+ };
663
+ };