stfca 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/LICENSE-MIT +4 -0
  2. package/README.md +325 -0
  3. package/index.d.ts +615 -0
  4. package/index.js +1 -0
  5. package/module/config.js +33 -0
  6. package/module/login.js +48 -0
  7. package/module/loginHelper.js +722 -0
  8. package/module/options.js +44 -0
  9. package/package.json +69 -0
  10. package/src/api/action/addExternalModule.js +25 -0
  11. package/src/api/action/changeAvatar.js +137 -0
  12. package/src/api/action/changeBio.js +75 -0
  13. package/src/api/action/getCurrentUserID.js +7 -0
  14. package/src/api/action/handleFriendRequest.js +57 -0
  15. package/src/api/action/logout.js +76 -0
  16. package/src/api/action/refreshFb_dtsg.js +71 -0
  17. package/src/api/action/setPostReaction.js +106 -0
  18. package/src/api/action/unfriend.js +54 -0
  19. package/src/api/http/httpGet.js +46 -0
  20. package/src/api/http/httpPost.js +52 -0
  21. package/src/api/http/postFormData.js +47 -0
  22. package/src/api/messaging/addUserToGroup.js +68 -0
  23. package/src/api/messaging/changeAdminStatus.js +122 -0
  24. package/src/api/messaging/changeArchivedStatus.js +55 -0
  25. package/src/api/messaging/changeBlockedStatus.js +48 -0
  26. package/src/api/messaging/changeGroupImage.js +90 -0
  27. package/src/api/messaging/changeNickname.js +70 -0
  28. package/src/api/messaging/changeThreadColor.js +79 -0
  29. package/src/api/messaging/changeThreadEmoji.js +106 -0
  30. package/src/api/messaging/createNewGroup.js +88 -0
  31. package/src/api/messaging/createPoll.js +43 -0
  32. package/src/api/messaging/deleteMessage.js +56 -0
  33. package/src/api/messaging/deleteThread.js +56 -0
  34. package/src/api/messaging/editMessage.js +68 -0
  35. package/src/api/messaging/forwardAttachment.js +51 -0
  36. package/src/api/messaging/getEmojiUrl.js +29 -0
  37. package/src/api/messaging/getFriendsList.js +82 -0
  38. package/src/api/messaging/getMessage.js +829 -0
  39. package/src/api/messaging/handleMessageRequest.js +65 -0
  40. package/src/api/messaging/markAsDelivered.js +57 -0
  41. package/src/api/messaging/markAsRead.js +88 -0
  42. package/src/api/messaging/markAsReadAll.js +49 -0
  43. package/src/api/messaging/markAsSeen.js +61 -0
  44. package/src/api/messaging/muteThread.js +50 -0
  45. package/src/api/messaging/removeUserFromGroup.js +105 -0
  46. package/src/api/messaging/resolvePhotoUrl.js +43 -0
  47. package/src/api/messaging/searchForThread.js +52 -0
  48. package/src/api/messaging/sendMessage.js +379 -0
  49. package/src/api/messaging/sendMessageMqtt.js +323 -0
  50. package/src/api/messaging/sendTypingIndicator.js +67 -0
  51. package/src/api/messaging/setMessageReaction.js +75 -0
  52. package/src/api/messaging/setTitle.js +119 -0
  53. package/src/api/messaging/shareContact.js +49 -0
  54. package/src/api/messaging/threadColors.js +128 -0
  55. package/src/api/messaging/unsendMessage.js +81 -0
  56. package/src/api/messaging/uploadAttachment.js +95 -0
  57. package/src/api/socket/core/connectMqtt.js +179 -0
  58. package/src/api/socket/core/getSeqID.js +25 -0
  59. package/src/api/socket/core/getTaskResponseData.js +22 -0
  60. package/src/api/socket/core/markDelivery.js +12 -0
  61. package/src/api/socket/core/parseDelta.js +351 -0
  62. package/src/api/socket/detail/buildStream.js +208 -0
  63. package/src/api/socket/detail/constants.js +24 -0
  64. package/src/api/socket/listenMqtt.js +133 -0
  65. package/src/api/threads/getThreadHistory.js +664 -0
  66. package/src/api/threads/getThreadInfo.js +358 -0
  67. package/src/api/threads/getThreadList.js +248 -0
  68. package/src/api/threads/getThreadPictures.js +78 -0
  69. package/src/api/users/getUserID.js +65 -0
  70. package/src/api/users/getUserInfo.js +319 -0
  71. package/src/api/users/getUserInfoV2.js +133 -0
  72. package/src/core/sendReqMqtt.js +63 -0
  73. package/src/database/models/index.js +49 -0
  74. package/src/database/models/thread.js +31 -0
  75. package/src/database/models/user.js +32 -0
  76. package/src/database/threadData.js +98 -0
  77. package/src/database/userData.js +89 -0
  78. package/src/utils/client.js +214 -0
  79. package/src/utils/constants.js +23 -0
  80. package/src/utils/format.js +1111 -0
  81. package/src/utils/headers.js +41 -0
  82. package/src/utils/request.js +215 -0
@@ -0,0 +1,1111 @@
1
+ "use strict";
2
+ var url = require("url");
3
+ const querystring = require("querystring");
4
+
5
+ function getType(obj) {
6
+ return Object.prototype.toString.call(obj).slice(8, -1);
7
+ }
8
+
9
+ function generateOfflineThreadingID() {
10
+ var ret = Date.now();
11
+ var value = Math.floor(Math.random() * 4294967295);
12
+ var str = ("0000000000000000000000" + value.toString(2)).slice(-22);
13
+ var msgs = ret.toString(2) + str;
14
+ return binaryToDecimal(msgs);
15
+ }
16
+ var h;
17
+ var i = {};
18
+ var j = {
19
+ _: "%",
20
+ A: "%2",
21
+ B: "000",
22
+ C: "%7d",
23
+ D: "%7b%22",
24
+ E: "%2c%22",
25
+ F: "%22%3a",
26
+ G: "%2c%22ut%22%3a1",
27
+ H: "%2c%22bls%22%3a",
28
+ I: "%2c%22n%22%3a%22%",
29
+ J: "%22%3a%7b%22i%22%3a0%7d",
30
+ K: "%2c%22pt%22%3a0%2c%22vis%22%3a",
31
+ L: "%2c%22ch%22%3a%7b%22h%22%3a%22",
32
+ M: "%7b%22v%22%3a2%2c%22time%22%3a1",
33
+ N: ".channel%22%2c%22sub%22%3a%5b",
34
+ O: "%2c%22sb%22%3a1%2c%22t%22%3a%5b",
35
+ P: "%2c%22ud%22%3a100%2c%22lc%22%3a0",
36
+ Q: "%5d%2c%22f%22%3anull%2c%22uct%22%3a",
37
+ R: ".channel%22%2c%22sub%22%3a%5b1%5d",
38
+ S: "%22%2c%22m%22%3a0%7d%2c%7b%22i%22%3a",
39
+ T: "%2c%22blc%22%3a1%2c%22snd%22%3a1%2c%22ct%22%3a",
40
+ U: "%2c%22blc%22%3a0%2c%22snd%22%3a1%2c%22ct%22%3a",
41
+ V: "%2c%22blc%22%3a0%2c%22snd%22%3a0%2c%22ct%22%3a",
42
+ W: "%2c%22s%22%3a0%2c%22blo%22%3a0%7d%2c%22bl%22%3a%7b%22ac%22%3a",
43
+ X: "%2c%22ri%22%3a0%7d%2c%22state%22%3a%7b%22p%22%3a0%2c%22ut%22%3a1",
44
+ Y:
45
+ "%2c%22pt%22%3a0%2c%22vis%22%3a1%2c%22bls%22%3a0%2c%22blc%22%3a0%2c%22snd%22%3a1%2c%22ct%22%3a",
46
+ Z:
47
+ "%2c%22sb%22%3a1%2c%22t%22%3a%5b%5d%2c%22f%22%3anull%2c%22uct%22%3a0%2c%22s%22%3a0%2c%22blo%22%3a0%7d%2c%22bl%22%3a%7b%22ac%22%3a"
48
+ };
49
+ (function () {
50
+ var l = [];
51
+ for (var m in j) {
52
+ i[j[m]] = m;
53
+ l.push(j[m]);
54
+ }
55
+ l.reverse();
56
+ h = new RegExp(l.join("|"), "g");
57
+ })();
58
+
59
+ function getAdminTextMessageType(m) {
60
+ switch (m.type) {
61
+ case "joinable_group_link_mode_change":
62
+ return "log:link-status";
63
+ case "magic_words":
64
+ return "log:magic-words";
65
+ case "change_thread_theme":
66
+ return "log:thread-color";
67
+ case "change_thread_icon":
68
+ case "change_thread_quick_reaction":
69
+ return "log:thread-icon";
70
+ case "change_thread_nickname":
71
+ return "log:user-nickname";
72
+ case "change_thread_admins":
73
+ return "log:thread-admins";
74
+ case "group_poll":
75
+ return "log:thread-poll";
76
+ case "change_thread_approval_mode":
77
+ return "log:thread-approval-mode";
78
+ case "messenger_call_log":
79
+ case "participant_joined_group_call":
80
+ return "log:thread-call";
81
+ case "pin_messages_v2":
82
+ return "log:thread-pinned";
83
+ case "unpin_messages_v2":
84
+ return "log:unpin-message";
85
+ default:
86
+ return m.type;
87
+ }
88
+ }
89
+
90
+ function formatDeltaEvent(m) {
91
+ var logMessageType;
92
+ var logMessageData;
93
+ switch (m.class) {
94
+ case "AdminTextMessage":
95
+ logMessageType = getAdminTextMessageType(m);
96
+ logMessageData = m.untypedData;
97
+ break;
98
+ case "ThreadName":
99
+ logMessageType = "log:thread-name";
100
+ logMessageData = { name: m.name };
101
+ break;
102
+ case "ParticipantsAddedToGroupThread":
103
+ logMessageType = "log:subscribe";
104
+ logMessageData = { addedParticipants: m.addedParticipants };
105
+ break;
106
+ case "ParticipantLeftGroupThread":
107
+ logMessageType = "log:unsubscribe";
108
+ logMessageData = { leftParticipantFbId: m.leftParticipantFbId };
109
+ break;
110
+ case "UserLocation": {
111
+ logMessageType = "log:user-location";
112
+ logMessageData = {
113
+ Image:
114
+ m.attachments[0].mercury.extensible_attachment.story_attachment.media
115
+ .image,
116
+ Location:
117
+ m.attachments[0].mercury.extensible_attachment.story_attachment.target
118
+ .location_title,
119
+ coordinates:
120
+ m.attachments[0].mercury.extensible_attachment.story_attachment.target
121
+ .coordinate,
122
+ url: m.attachments[0].mercury.extensible_attachment.story_attachment.url
123
+ };
124
+ }
125
+ case "ApprovalQueue":
126
+ logMessageType = "log:approval-queue";
127
+ logMessageData = {
128
+ approvalQueue: {
129
+ action: m.action,
130
+ recipientFbId: m.recipientFbId,
131
+ requestSource: m.requestSource,
132
+ ...m.messageMetadata
133
+ }
134
+ };
135
+ }
136
+ return {
137
+ type: "event",
138
+ threadID: formatID(
139
+ (
140
+ m.messageMetadata.threadKey.threadFbId ||
141
+ m.messageMetadata.threadKey.otherUserFbId
142
+ ).toString()
143
+ ),
144
+ logMessageType: logMessageType,
145
+ logMessageData: logMessageData,
146
+ logMessageBody: m.messageMetadata.adminText,
147
+ author: m.messageMetadata.actorFbId,
148
+ participantIDs: (m?.participants || []).map(e => e.toString())
149
+ };
150
+ }
151
+
152
+
153
+ function _formatAttachment(attachment1, attachment2) {
154
+ attachment2 = attachment2 || { id: "", image_data: {} };
155
+ attachment1 = attachment1.mercury ? attachment1.mercury : attachment1;
156
+ var blob = attachment1.blob_attachment;
157
+ var type =
158
+ blob && blob.__typename ? blob.__typename : attachment1.attach_type;
159
+ if (!type && attachment1.sticker_attachment) {
160
+ type = "StickerAttachment";
161
+ blob = attachment1.sticker_attachment;
162
+ } else if (!type && attachment1.extensible_attachment) {
163
+ if (
164
+ attachment1.extensible_attachment.story_attachment &&
165
+ attachment1.extensible_attachment.story_attachment.target &&
166
+ attachment1.extensible_attachment.story_attachment.target.__typename &&
167
+ attachment1.extensible_attachment.story_attachment.target.__typename ===
168
+ "MessageLocation"
169
+ )
170
+ type = "MessageLocation";
171
+ else type = "ExtensibleAttachment";
172
+
173
+ blob = attachment1.extensible_attachment;
174
+ }
175
+ switch (type) {
176
+ case "sticker":
177
+ return {
178
+ type: "sticker",
179
+ ID: attachment1.metadata.stickerID.toString(),
180
+ url: attachment1.url,
181
+
182
+ packID: attachment1.metadata.packID.toString(),
183
+ spriteUrl: attachment1.metadata.spriteURI,
184
+ spriteUrl2x: attachment1.metadata.spriteURI2x,
185
+ width: attachment1.metadata.width,
186
+ height: attachment1.metadata.height,
187
+
188
+ caption: attachment2.caption,
189
+ description: attachment2.description,
190
+
191
+ frameCount: attachment1.metadata.frameCount,
192
+ frameRate: attachment1.metadata.frameRate,
193
+ framesPerRow: attachment1.metadata.framesPerRow,
194
+ framesPerCol: attachment1.metadata.framesPerCol,
195
+
196
+ stickerID: attachment1.metadata.stickerID.toString(), // @Legacy
197
+ spriteURI: attachment1.metadata.spriteURI, // @Legacy
198
+ spriteURI2x: attachment1.metadata.spriteURI2x // @Legacy
199
+ };
200
+ case "file":
201
+ return {
202
+ type: "file",
203
+ filename: attachment1.name,
204
+ ID: attachment2.id.toString(),
205
+ url: attachment1.url,
206
+
207
+ isMalicious: attachment2.is_malicious,
208
+ contentType: attachment2.mime_type,
209
+
210
+ name: attachment1.name, // @Legacy
211
+ mimeType: attachment2.mime_type, // @Legacy
212
+ fileSize: attachment2.file_size // @Legacy
213
+ };
214
+ case "photo":
215
+ return {
216
+ type: "photo",
217
+ ID: attachment1.metadata.fbid.toString(),
218
+ filename: attachment1.fileName,
219
+ thumbnailUrl: attachment1.thumbnail_url,
220
+
221
+ previewUrl: attachment1.preview_url,
222
+ previewWidth: attachment1.preview_width,
223
+ previewHeight: attachment1.preview_height,
224
+
225
+ largePreviewUrl: attachment1.large_preview_url,
226
+ largePreviewWidth: attachment1.large_preview_width,
227
+ largePreviewHeight: attachment1.large_preview_height,
228
+
229
+ url: attachment1.metadata.url, // @Legacy
230
+ width: attachment1.metadata.dimensions.split(",")[0], // @Legacy
231
+ height: attachment1.metadata.dimensions.split(",")[1], // @Legacy
232
+ name: attachment1.fileName // @Legacy
233
+ };
234
+ case "animated_image":
235
+ return {
236
+ type: "animated_image",
237
+ ID: attachment2.id.toString(),
238
+ filename: attachment2.filename,
239
+
240
+ previewUrl: attachment1.preview_url,
241
+ previewWidth: attachment1.preview_width,
242
+ previewHeight: attachment1.preview_height,
243
+
244
+ url: attachment2.image_data.url,
245
+ width: attachment2.image_data.width,
246
+ height: attachment2.image_data.height,
247
+
248
+ name: attachment1.name, // @Legacy
249
+ facebookUrl: attachment1.url, // @Legacy
250
+ thumbnailUrl: attachment1.thumbnail_url, // @Legacy
251
+ mimeType: attachment2.mime_type, // @Legacy
252
+ rawGifImage: attachment2.image_data.raw_gif_image, // @Legacy
253
+ rawWebpImage: attachment2.image_data.raw_webp_image, // @Legacy
254
+ animatedGifUrl: attachment2.image_data.animated_gif_url, // @Legacy
255
+ animatedGifPreviewUrl: attachment2.image_data.animated_gif_preview_url, // @Legacy
256
+ animatedWebpUrl: attachment2.image_data.animated_webp_url, // @Legacy
257
+ animatedWebpPreviewUrl: attachment2.image_data.animated_webp_preview_url // @Legacy
258
+ };
259
+ case "share":
260
+ return {
261
+ type: "share",
262
+ ID: attachment1.share.share_id.toString(),
263
+ url: attachment2.href,
264
+
265
+ title: attachment1.share.title,
266
+ description: attachment1.share.description,
267
+ source: attachment1.share.source,
268
+
269
+ image: attachment1.share.media.image,
270
+ width: attachment1.share.media.image_size.width,
271
+ height: attachment1.share.media.image_size.height,
272
+ playable: attachment1.share.media.playable,
273
+ duration: attachment1.share.media.duration,
274
+
275
+ subattachments: attachment1.share.subattachments,
276
+ properties: {},
277
+
278
+ animatedImageSize: attachment1.share.media.animated_image_size, // @Legacy
279
+ facebookUrl: attachment1.share.uri, // @Legacy
280
+ target: attachment1.share.target, // @Legacy
281
+ styleList: attachment1.share.style_list // @Legacy
282
+ };
283
+ case "video":
284
+ return {
285
+ type: "video",
286
+ ID: attachment1.metadata.fbid.toString(),
287
+ filename: attachment1.name,
288
+
289
+ previewUrl: attachment1.preview_url,
290
+ previewWidth: attachment1.preview_width,
291
+ previewHeight: attachment1.preview_height,
292
+
293
+ url: attachment1.url,
294
+ width: attachment1.metadata.dimensions.width,
295
+ height: attachment1.metadata.dimensions.height,
296
+
297
+ duration: attachment1.metadata.duration,
298
+ videoType: "unknown",
299
+
300
+ thumbnailUrl: attachment1.thumbnail_url // @Legacy
301
+ };
302
+ case "error":
303
+ return {
304
+ type: "error",
305
+ attachment1: attachment1,
306
+ attachment2: attachment2
307
+ };
308
+ case "MessageImage":
309
+ return {
310
+ type: "photo",
311
+ ID: blob.legacy_attachment_id,
312
+ filename: blob.filename,
313
+ thumbnailUrl: blob.thumbnail.uri,
314
+
315
+ previewUrl: blob.preview.uri,
316
+ previewWidth: blob.preview.width,
317
+ previewHeight: blob.preview.height,
318
+
319
+ largePreviewUrl: blob.large_preview.uri,
320
+ largePreviewWidth: blob.large_preview.width,
321
+ largePreviewHeight: blob.large_preview.height,
322
+
323
+ url: blob.large_preview.uri, // @Legacy
324
+ width: blob.original_dimensions.x, // @Legacy
325
+ height: blob.original_dimensions.y, // @Legacy
326
+ name: blob.filename // @Legacy
327
+ };
328
+ case "MessageAnimatedImage":
329
+ return {
330
+ type: "animated_image",
331
+ ID: blob.legacy_attachment_id,
332
+ filename: blob.filename,
333
+
334
+ previewUrl: blob.preview_image.uri,
335
+ previewWidth: blob.preview_image.width,
336
+ previewHeight: blob.preview_image.height,
337
+
338
+ url: blob.animated_image.uri,
339
+ width: blob.animated_image.width,
340
+ height: blob.animated_image.height,
341
+
342
+ thumbnailUrl: blob.preview_image.uri, // @Legacy
343
+ name: blob.filename, // @Legacy
344
+ facebookUrl: blob.animated_image.uri, // @Legacy
345
+ rawGifImage: blob.animated_image.uri, // @Legacy
346
+ animatedGifUrl: blob.animated_image.uri, // @Legacy
347
+ animatedGifPreviewUrl: blob.preview_image.uri, // @Legacy
348
+ animatedWebpUrl: blob.animated_image.uri, // @Legacy
349
+ animatedWebpPreviewUrl: blob.preview_image.uri // @Legacy
350
+ };
351
+ case "MessageVideo":
352
+ return {
353
+ type: "video",
354
+ filename: blob.filename,
355
+ ID: blob.legacy_attachment_id,
356
+
357
+ previewUrl: blob.large_image.uri,
358
+ previewWidth: blob.large_image.width,
359
+ previewHeight: blob.large_image.height,
360
+
361
+ url: blob.playable_url,
362
+ width: blob.original_dimensions.x,
363
+ height: blob.original_dimensions.y,
364
+
365
+ duration: blob.playable_duration_in_ms,
366
+ videoType: blob.video_type.toLowerCase(),
367
+
368
+ thumbnailUrl: blob.large_image.uri // @Legacy
369
+ };
370
+ case "MessageAudio":
371
+ return {
372
+ type: "audio",
373
+ filename: blob.filename,
374
+ ID: blob.url_shimhash,
375
+
376
+ audioType: blob.audio_type,
377
+ duration: blob.playable_duration_in_ms,
378
+ url: blob.playable_url,
379
+
380
+ isVoiceMail: blob.is_voicemail
381
+ };
382
+ case "StickerAttachment":
383
+ return {
384
+ type: "sticker",
385
+ ID: blob.id,
386
+ url: blob.url,
387
+
388
+ packID: blob.pack ? blob.pack.id : null,
389
+ spriteUrl: blob.sprite_image,
390
+ spriteUrl2x: blob.sprite_image_2x,
391
+ width: blob.width,
392
+ height: blob.height,
393
+
394
+ caption: blob.label,
395
+ description: blob.label,
396
+
397
+ frameCount: blob.frame_count,
398
+ frameRate: blob.frame_rate,
399
+ framesPerRow: blob.frames_per_row,
400
+ framesPerCol: blob.frames_per_column,
401
+
402
+ stickerID: blob.id, // @Legacy
403
+ spriteURI: blob.sprite_image, // @Legacy
404
+ spriteURI2x: blob.sprite_image_2x // @Legacy
405
+ };
406
+ case "MessageLocation":
407
+ var urlAttach = blob.story_attachment.url;
408
+ var mediaAttach = blob.story_attachment.media;
409
+
410
+ var u = querystring.parse(url.parse(urlAttach).query).u;
411
+ var where1 = querystring.parse(url.parse(u).query).where1;
412
+ var address = where1.split(", ");
413
+
414
+ var latitude;
415
+ var longitude;
416
+
417
+ try {
418
+ latitude = Number.parseFloat(address[0]);
419
+ longitude = Number.parseFloat(address[1]);
420
+ } catch (err) {
421
+ /* empty */
422
+ }
423
+
424
+ var imageUrl;
425
+ var width;
426
+ var height;
427
+
428
+ if (mediaAttach && mediaAttach.image) {
429
+ imageUrl = mediaAttach.image.uri;
430
+ width = mediaAttach.image.width;
431
+ height = mediaAttach.image.height;
432
+ }
433
+
434
+ return {
435
+ type: "location",
436
+ ID: blob.legacy_attachment_id,
437
+ latitude: latitude,
438
+ longitude: longitude,
439
+ image: imageUrl,
440
+ width: width,
441
+ height: height,
442
+ url: u || urlAttach,
443
+ address: where1,
444
+
445
+ facebookUrl: blob.story_attachment.url, // @Legacy
446
+ target: blob.story_attachment.target, // @Legacy
447
+ styleList: blob.story_attachment.style_list // @Legacy
448
+ };
449
+ case "ExtensibleAttachment":
450
+ return {
451
+ type: "share",
452
+ ID: blob.legacy_attachment_id,
453
+ url: blob.story_attachment.url,
454
+
455
+ title: blob.story_attachment.title_with_entities.text,
456
+ description:
457
+ blob.story_attachment.description &&
458
+ blob.story_attachment.description.text,
459
+ source: blob.story_attachment.source
460
+ ? blob.story_attachment.source.text
461
+ : null,
462
+
463
+ image:
464
+ blob.story_attachment.media &&
465
+ blob.story_attachment.media.image &&
466
+ blob.story_attachment.media.image.uri,
467
+ width:
468
+ blob.story_attachment.media &&
469
+ blob.story_attachment.media.image &&
470
+ blob.story_attachment.media.image.width,
471
+ height:
472
+ blob.story_attachment.media &&
473
+ blob.story_attachment.media.image &&
474
+ blob.story_attachment.media.image.height,
475
+ playable:
476
+ blob.story_attachment.media &&
477
+ blob.story_attachment.media.is_playable,
478
+ duration:
479
+ blob.story_attachment.media &&
480
+ blob.story_attachment.media.playable_duration_in_ms,
481
+ playableUrl:
482
+ blob.story_attachment.media == null
483
+ ? null
484
+ : blob.story_attachment.media.playable_url,
485
+
486
+ subattachments: blob.story_attachment.subattachments,
487
+ properties: blob.story_attachment.properties.reduce(function (
488
+ /** @type {{ [x: string]: any; }} */ obj,
489
+ /** @type {{ key: string | number; value: { text: any; }; }} */ cur
490
+ ) {
491
+ obj[cur.key] = cur.value.text;
492
+ return obj;
493
+ },
494
+ {}),
495
+
496
+ facebookUrl: blob.story_attachment.url, // @Legacy
497
+ target: blob.story_attachment.target, // @Legacy
498
+ styleList: blob.story_attachment.style_list // @Legacy
499
+ };
500
+ case "MessageFile":
501
+ return {
502
+ type: "file",
503
+ filename: blob.filename,
504
+ ID: blob.message_file_fbid,
505
+
506
+ url: blob.url,
507
+ isMalicious: blob.is_malicious,
508
+ contentType: blob.content_type,
509
+
510
+ name: blob.filename,
511
+ mimeType: "",
512
+ fileSize: -1
513
+ };
514
+ default:
515
+ throw new Error(
516
+ "unrecognized attach_file of type " +
517
+ type +
518
+ "`" +
519
+ JSON.stringify(attachment1, null, 4) +
520
+ " attachment2: " +
521
+ JSON.stringify(attachment2, null, 4) +
522
+ "`"
523
+ );
524
+ }
525
+ }
526
+
527
+ function formatAttachment(attachments, attachmentIds, attachmentMap, shareMap) {
528
+ attachmentMap = shareMap || attachmentMap;
529
+ return attachments
530
+ ? attachments.map(function (i) {
531
+ if (
532
+ !attachmentMap ||
533
+ !attachmentIds ||
534
+ !attachmentMap[attachmentIds[i]]
535
+ ) {
536
+ return _formatAttachment(val);
537
+ }
538
+ return _formatAttachment(val, attachmentMap[attachmentIds[i]]);
539
+ })
540
+ : [];
541
+ }
542
+
543
+ function formatDeltaMessage(m) {
544
+ var md = m.messageMetadata;
545
+ var mdata =
546
+ m.data === undefined
547
+ ? []
548
+ : m.data.prng === undefined
549
+ ? []
550
+ : JSON.parse(m.data.prng);
551
+ var m_id = mdata.map((/** @type {{ i: any; }} */ u) => u.i);
552
+ var m_offset = mdata.map((/** @type {{ o: any; }} */ u) => u.o);
553
+ var m_length = mdata.map((/** @type {{ l: any; }} */ u) => u.l);
554
+ var mentions = {};
555
+ var body = m.body || "";
556
+ var args = body == "" ? [] : body.trim().split(/\s+/);
557
+ for (var i = 0; i < m_id.length; i++)
558
+ mentions[m_id[i]] = m.body.substring(
559
+ m_offset[i],
560
+ m_offset[i] + m_length[i]
561
+ );
562
+ return {
563
+ type: "message",
564
+ senderID: formatID(md.actorFbId.toString()),
565
+ threadID: formatID(
566
+ (md.threadKey.threadFbId || md.threadKey.otherUserFbId).toString()
567
+ ),
568
+ messageID: md.messageId,
569
+ args: args,
570
+ body: body,
571
+ attachments: (m.attachments || []).map((/** @type {any} */ v) =>
572
+ _formatAttachment(v)
573
+ ),
574
+ mentions: mentions,
575
+ timestamp: md.timestamp,
576
+ isGroup: !!md.threadKey.threadFbId,
577
+ participantIDs: m.participants || []
578
+ };
579
+ }
580
+
581
+ function formatID(id) {
582
+ if (id != undefined && id != null) return id.replace(/(fb)?id[:.]/, "");
583
+ else return id;
584
+ }
585
+
586
+ function formatMessage(m) {
587
+ var originalMessage = m.message ? m.message : m;
588
+ var obj = {
589
+ type: "message",
590
+ senderName: originalMessage.sender_name,
591
+ senderID: formatID(originalMessage.sender_fbid.toString()),
592
+ participantNames: originalMessage.group_thread_info
593
+ ? originalMessage.group_thread_info.participant_names
594
+ : [originalMessage.sender_name.split(" ")[0]],
595
+ participantIDs: originalMessage.group_thread_info
596
+ ? originalMessage.group_thread_info.participant_ids.map(function (v) {
597
+ return formatID(v.toString());
598
+ })
599
+ : [formatID(originalMessage.sender_fbid)],
600
+ body: originalMessage.body || "",
601
+ threadID: formatID(
602
+ (
603
+ originalMessage.thread_fbid || originalMessage.other_user_fbid
604
+ ).toString()
605
+ ),
606
+ threadName: originalMessage.group_thread_info
607
+ ? originalMessage.group_thread_info.name
608
+ : originalMessage.sender_name,
609
+ location: originalMessage.coordinates ? originalMessage.coordinates : null,
610
+ messageID: originalMessage.mid
611
+ ? originalMessage.mid.toString()
612
+ : originalMessage.message_id,
613
+ attachments: formatAttachment(
614
+ originalMessage.attachments,
615
+ originalMessage.attachmentIds,
616
+ originalMessage.attachment_map,
617
+ originalMessage.share_map
618
+ ),
619
+ timestamp: originalMessage.timestamp,
620
+ timestampAbsolute: originalMessage.timestamp_absolute,
621
+ timestampRelative: originalMessage.timestamp_relative,
622
+ timestampDatetime: originalMessage.timestamp_datetime,
623
+ tags: originalMessage.tags,
624
+ reactions: originalMessage.reactions ? originalMessage.reactions : [],
625
+ isUnread: originalMessage.is_unread
626
+ };
627
+ if (m.type === "pages_messaging")
628
+ obj.pageID = m.realtime_viewer_fbid.toString();
629
+ obj.isGroup = obj.participantIDs.length > 2;
630
+ return obj;
631
+ }
632
+
633
+ function formatEvent(m) {
634
+ var originalMessage = m.message ? m.message : m;
635
+ var logMessageType = originalMessage.log_message_type;
636
+ var logMessageData;
637
+ if (logMessageType === "log:generic-admin-text") {
638
+ logMessageData = originalMessage.log_message_data.untypedData;
639
+ logMessageType = getAdminTextMessageType(
640
+ originalMessage.log_message_data.message_type
641
+ );
642
+ } else logMessageData = originalMessage.log_message_data;
643
+ return Object.assign(formatMessage(originalMessage), {
644
+ type: "event",
645
+ logMessageType: logMessageType,
646
+ logMessageData: logMessageData,
647
+ logMessageBody: originalMessage.log_message_body
648
+ });
649
+ }
650
+
651
+ function formatHistoryMessage(m) {
652
+ switch (m.action_type) {
653
+ case "ma-type:log-message":
654
+ return formatEvent(m);
655
+ default:
656
+ return formatMessage(m);
657
+ }
658
+ }
659
+
660
+ function formatDeltaReadReceipt(delta) {
661
+ return {
662
+ reader: (delta.threadKey.otherUserFbId || delta.actorFbId).toString(),
663
+ time: delta.actionTimestampMs,
664
+ threadID: formatID(
665
+ (delta.threadKey.otherUserFbId || delta.threadKey.threadFbId).toString()
666
+ ),
667
+ type: "read_receipt"
668
+ };
669
+ }
670
+
671
+ function decodeClientPayload(payload) {
672
+ function Utf8ArrayToStr(array) {
673
+ var out, i, len, c;
674
+ var char2, char3;
675
+ out = "";
676
+ len = array.length;
677
+ i = 0;
678
+ while (i < len) {
679
+ c = array[i++];
680
+ switch (c >> 4) {
681
+ case 0:
682
+ case 1:
683
+ case 2:
684
+ case 3:
685
+ case 4:
686
+ case 5:
687
+ case 6:
688
+ case 7:
689
+ out += String.fromCharCode(c);
690
+ break;
691
+ case 12:
692
+ case 13:
693
+ char2 = array[i++];
694
+ out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
695
+ break;
696
+ case 14:
697
+ char2 = array[i++];
698
+ char3 = array[i++];
699
+ out += String.fromCharCode(
700
+ ((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
701
+ );
702
+ break;
703
+ }
704
+ }
705
+ return out;
706
+ }
707
+ return JSON.parse(Utf8ArrayToStr(payload));
708
+ }
709
+
710
+ function formatTyp(event) {
711
+ return {
712
+ isTyping: !!event.st,
713
+ from: event.from.toString(),
714
+ threadID: formatID(
715
+ (event.to || event.thread_fbid || event.from).toString()
716
+ ),
717
+ fromMobile: event.hasOwnProperty("from_mobile") ? event.from_mobile : true,
718
+ userID: (event.realtime_viewer_fbid || event.from).toString(),
719
+ type: "typ"
720
+ };
721
+ }
722
+
723
+
724
+ function formatThread(data) {
725
+ return {
726
+ threadID: formatID(data.thread_fbid.toString()),
727
+ participants: data.participants.map(formatID),
728
+ participantIDs: data.participants.map(formatID),
729
+ name: data.name,
730
+ nicknames: data.custom_nickname,
731
+ snippet: data.snippet,
732
+ snippetAttachments: data.snippet_attachments,
733
+ snippetSender: formatID((data.snippet_sender || "").toString()),
734
+ unreadCount: data.unread_count,
735
+ messageCount: data.message_count,
736
+ imageSrc: data.image_src,
737
+ timestamp: data.timestamp,
738
+ muteUntil: data.mute_until,
739
+ isCanonicalUser: data.is_canonical_user,
740
+ isCanonical: data.is_canonical,
741
+ isSubscribed: data.is_subscribed,
742
+ folder: data.folder,
743
+ isArchived: data.is_archived,
744
+ recipientsLoadable: data.recipients_loadable,
745
+ hasEmailParticipant: data.has_email_participant,
746
+ readOnly: data.read_only,
747
+ canReply: data.can_reply,
748
+ cannotReplyReason: data.cannot_reply_reason,
749
+ lastMessageTimestamp: data.last_message_timestamp,
750
+ lastReadTimestamp: data.last_read_timestamp,
751
+ lastMessageType: data.last_message_type,
752
+ emoji: data.custom_like_icon,
753
+ color: data.custom_color,
754
+ adminIDs: data.admin_ids,
755
+ threadType: data.thread_type
756
+ };
757
+ }
758
+
759
+ function generateThreadingID(clientID) {
760
+ var k = Date.now();
761
+ var l = Math.floor(Math.random() * 4294967295);
762
+ var m = clientID;
763
+ return "<" + k + ":" + l + "-" + m + "@mail.projektitan.com>";
764
+ }
765
+
766
+ function padZeros(val, len) {
767
+ val = String(val);
768
+ len = len || 2;
769
+ while (val.length < len) val = "0" + val;
770
+ return val;
771
+ }
772
+
773
+ function binaryToDecimal(data) {
774
+ var ret = "";
775
+ while (data !== "0") {
776
+ var end = 0;
777
+ var fullName = "";
778
+ var i = 0;
779
+ for (; i < data.length; i++) {
780
+ end = 2 * end + parseInt(data[i], 10);
781
+ if (end >= 10) {
782
+ fullName += "1";
783
+ end -= 10;
784
+ } else fullName += "0";
785
+ }
786
+ ret = end.toString() + ret;
787
+ data = fullName.slice(fullName.indexOf("1"));
788
+ }
789
+ return ret;
790
+ }
791
+
792
+ function presenceEncode(str) {
793
+ return encodeURIComponent(str)
794
+ .replace(/([_A-Z])|%../g, function (m, n) {
795
+ return n ? "%" + n.charCodeAt(0).toString(16) : m;
796
+ })
797
+ .toLowerCase()
798
+ .replace(h, function (m) {
799
+ return i[m];
800
+ });
801
+ }
802
+
803
+ function presenceDecode(str) {
804
+ return decodeURIComponent(
805
+ str.replace(/[_A-Z]/g, function (/** @type {string | number} */ m) {
806
+ return j[m];
807
+ })
808
+ );
809
+ }
810
+
811
+ function generatePresence(userID) {
812
+ var time = Date.now();
813
+ return (
814
+ "E" +
815
+ presenceEncode(
816
+ JSON.stringify({
817
+ v: 3,
818
+ time: parseInt(time / 1000, 10),
819
+ user: userID,
820
+ state: {
821
+ ut: 0,
822
+ t2: [],
823
+ lm2: null,
824
+ uct2: time,
825
+ tr: null,
826
+ tw: Math.floor(Math.random() * 4294967295) + 1,
827
+ at: time
828
+ },
829
+ ch: {
830
+ ["p_" + userID]: 0
831
+ }
832
+ })
833
+ )
834
+ );
835
+ }
836
+
837
+ function generateAccessiblityCookie() {
838
+ var time = Date.now();
839
+ return encodeURIComponent(
840
+ JSON.stringify({
841
+ sr: 0,
842
+ "sr-ts": time,
843
+ jk: 0,
844
+ "jk-ts": time,
845
+ kb: 0,
846
+ "kb-ts": time,
847
+ hcm: 0,
848
+ "hcm-ts": time
849
+ })
850
+ );
851
+ }
852
+
853
+ function getGUID() {
854
+ var sectionLength = Date.now();
855
+ var id = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
856
+ var r = Math.floor((sectionLength + Math.random() * 16) % 16);
857
+ sectionLength = Math.floor(sectionLength / 16);
858
+ var _guid = (c == "x" ? r : (r & 7) | 8).toString(16);
859
+ return _guid;
860
+ });
861
+ return id;
862
+ }
863
+
864
+ function formatDeltaReadReceipt(delta) {
865
+ return {
866
+ reader: (delta.threadKey.otherUserFbId || delta.actorFbId).toString(),
867
+ time: delta.actionTimestampMs,
868
+ threadID: formatID(
869
+ (delta.threadKey.otherUserFbId || delta.threadKey.threadFbId).toString()
870
+ ),
871
+ type: "read_receipt"
872
+ };
873
+ }
874
+
875
+ function formatReadReceipt(event) {
876
+ return {
877
+ reader: event.reader.toString(),
878
+ time: event.time,
879
+ threadID: formatID((event.thread_fbid || event.reader).toString()),
880
+ type: "read_receipt"
881
+ };
882
+ }
883
+
884
+ function formatRead(event) {
885
+ return {
886
+ threadID: formatID(
887
+ (
888
+ (event.chat_ids && event.chat_ids[0]) ||
889
+ (event.thread_fbids && event.thread_fbids[0])
890
+ ).toString()
891
+ ),
892
+ time: event.timestamp,
893
+ type: "read"
894
+ };
895
+ }
896
+
897
+ function arrToForm(form) {
898
+ return arrayToObject(
899
+ form,
900
+ function (v) {
901
+ return v.name;
902
+ },
903
+ function (v) {
904
+ return v.val;
905
+ }
906
+ );
907
+ }
908
+
909
+ function arrayToObject(arr, getKey, getValue) {
910
+ return arr.reduce(function (acc, val) {
911
+ acc[getKey(val)] = getValue(val);
912
+ return acc;
913
+ }, {});
914
+ }
915
+
916
+ function getSignatureID() {
917
+ return Math.floor(Math.random() * 2147483648).toString(16);
918
+ }
919
+
920
+ function generateTimestampRelative() {
921
+ var d = new Date();
922
+ return d.getHours() + ":" + padZeros(d.getMinutes());
923
+ }
924
+
925
+ var NUM_TO_MONTH = [
926
+ "Jan",
927
+ "Feb",
928
+ "Mar",
929
+ "Apr",
930
+ "May",
931
+ "Jun",
932
+ "Jul",
933
+ "Aug",
934
+ "Sep",
935
+ "Oct",
936
+ "Nov",
937
+ "Dec"
938
+ ];
939
+ var NUM_TO_DAY = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
940
+
941
+ function formatDate(date) {
942
+ var d = date.getUTCDate();
943
+ d = d >= 10 ? d : "0" + d;
944
+ var h = date.getUTCHours();
945
+ h = h >= 10 ? h : "0" + h;
946
+ var m = date.getUTCMinutes();
947
+ m = m >= 10 ? m : "0" + m;
948
+ var s = date.getUTCSeconds();
949
+ s = s >= 10 ? s : "0" + s;
950
+ return (
951
+ NUM_TO_DAY[date.getUTCDay()] +
952
+ ", " +
953
+ d +
954
+ " " +
955
+ NUM_TO_MONTH[date.getUTCMonth()] +
956
+ " " +
957
+ date.getUTCFullYear() +
958
+ " " +
959
+ h +
960
+ ":" +
961
+ m +
962
+ ":" +
963
+ s +
964
+ " GMT"
965
+ );
966
+ }
967
+
968
+ function formatCookie(arr, url) {
969
+ return (
970
+ arr[0] + "=" + arr[1] + "; Path=" + arr[3] + "; Domain=" + url + ".com"
971
+ );
972
+ }
973
+
974
+ function formatProxyPresence(presence, userID) {
975
+ if (presence.lat === undefined || presence.p === undefined) return null;
976
+ return {
977
+ type: "presence",
978
+ timestamp: presence.lat * 1000,
979
+ userID: userID || "",
980
+ statuses: presence.p
981
+ };
982
+ }
983
+
984
+ function formatPresence(presence, userID) {
985
+ return {
986
+ type: "presence",
987
+ timestamp: presence.la * 1000,
988
+ userID: userID || "",
989
+ statuses: presence.a
990
+ };
991
+ }
992
+
993
+ function getData_Path(Obj, Arr, Stt) {
994
+ if (Arr.length === 0 && Obj != undefined) {
995
+ return Obj;
996
+ } else if (Obj == undefined) {
997
+ return Stt;
998
+ }
999
+ const head = Arr[0];
1000
+ if (head == undefined) {
1001
+ return Stt;
1002
+ }
1003
+ const tail = Arr.slice(1);
1004
+ return getData_Path(Obj[head], tail, Stt++);
1005
+ }
1006
+
1007
+ function setData_Path(obj, path, value) {
1008
+ if (!path.length) {
1009
+ return obj;
1010
+ }
1011
+ const currentKey = path[0];
1012
+ let currentObj = obj[currentKey];
1013
+
1014
+ if (!currentObj) {
1015
+ obj[currentKey] = value;
1016
+ currentObj = obj[currentKey];
1017
+ }
1018
+ path.shift();
1019
+ if (!path.length) {
1020
+ currentObj = value;
1021
+ } else {
1022
+ currentObj = setData_Path(currentObj, path, value);
1023
+ }
1024
+
1025
+ return obj;
1026
+ }
1027
+
1028
+ function getPaths(obj, parentPath = []) {
1029
+ let paths = [];
1030
+ for (let prop in obj) {
1031
+ if (typeof obj[prop] === "object") {
1032
+ paths = paths.concat(getPaths(obj[prop], [...parentPath, prop]));
1033
+ } else {
1034
+ paths.push([...parentPath, prop]);
1035
+ }
1036
+ }
1037
+ return paths;
1038
+ }
1039
+
1040
+ function cleanHTML(text) {
1041
+ text = text.replace(
1042
+ /(<br>)|(<\/?i>)|(<\/?em>)|(<\/?b>)|(!?~)|(&amp;)|(&#039;)|(&lt;)|(&gt;)|(&quot;)/g,
1043
+ match => {
1044
+ switch (match) {
1045
+ case "<br>":
1046
+ return "\n";
1047
+ case "<i>":
1048
+ case "<em>":
1049
+ case "</i>":
1050
+ case "</em>":
1051
+ return "*";
1052
+ case "<b>":
1053
+ case "</b>":
1054
+ return "**";
1055
+ case "~!":
1056
+ case "!~":
1057
+ return "||";
1058
+ case "&amp;":
1059
+ return "&";
1060
+ case "&#039;":
1061
+ return "'";
1062
+ case "&lt;":
1063
+ return "<";
1064
+ case "&gt;":
1065
+ return ">";
1066
+ case "&quot;":
1067
+ return '"';
1068
+ }
1069
+ }
1070
+ );
1071
+ return text;
1072
+ }
1073
+
1074
+ function getCurrentTimestamp() {
1075
+ const date = new Date();
1076
+ const unixTime = date.getTime();
1077
+ return unixTime;
1078
+ }
1079
+
1080
+ module.exports = {
1081
+ getType,
1082
+ generateOfflineThreadingID,
1083
+ getAdminTextMessageType,
1084
+ formatDeltaEvent,
1085
+ formatHistoryMessage,
1086
+ formatMessage,
1087
+ _formatAttachment,
1088
+ formatDeltaEvent,
1089
+ formatDeltaMessage,
1090
+ formatDeltaReadReceipt,
1091
+ getType,
1092
+ formatID,
1093
+ decodeClientPayload,
1094
+ formatTyp,
1095
+ formatThread,
1096
+ generateThreadingID,
1097
+ formatRead,
1098
+ formatTyp,
1099
+ getGUID,
1100
+ getPaths,
1101
+ cleanHTML,
1102
+ generateTimestampRelative,
1103
+ formatDate,
1104
+ formatCookie,
1105
+ formatProxyPresence,
1106
+ formatPresence,
1107
+ getData_Path,
1108
+ setData_Path,
1109
+ getSignatureID,
1110
+ getCurrentTimestamp
1111
+ };