sweetheart-koja 1.3.8

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

Potentially problematic release.


This version of sweetheart-koja might be problematic. Click here for more details.

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