alicezetion 1.0.9 → 1.1.1

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