fb-nextgen 1.0.0

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

Potentially problematic release.


This version of fb-nextgen might be problematic. Click here for more details.

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