ws-rapido 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 (84) hide show
  1. package/index.js +477 -0
  2. package/package.json +46 -0
  3. package/src/addExternalModule.js +25 -0
  4. package/src/addUserToGroup.js +115 -0
  5. package/src/changeAdminStatus.js +103 -0
  6. package/src/changeArchivedStatus.js +55 -0
  7. package/src/changeAvatar.js +136 -0
  8. package/src/changeAvatarV2.js +86 -0
  9. package/src/changeBio.js +76 -0
  10. package/src/changeBlockedStatus.js +49 -0
  11. package/src/changeBlockedStatusMqtt.js +80 -0
  12. package/src/changeCover.js +72 -0
  13. package/src/changeGroupImage.js +135 -0
  14. package/src/changeName.js +78 -0
  15. package/src/changeNickname.js +59 -0
  16. package/src/changeThreadColor.js +65 -0
  17. package/src/changeThreadEmoji.js +55 -0
  18. package/src/changeUsername.js +58 -0
  19. package/src/createCommentPost.js +229 -0
  20. package/src/createNewGroup.js +88 -0
  21. package/src/createPoll.js +71 -0
  22. package/src/createPost.js +275 -0
  23. package/src/data/getThreadInfo.json +1 -0
  24. package/src/deleteMessage.js +56 -0
  25. package/src/deleteThread.js +56 -0
  26. package/src/editMessage.js +76 -0
  27. package/src/follow.js +74 -0
  28. package/src/forwardAttachment.js +60 -0
  29. package/src/getAccess.js +111 -0
  30. package/src/getAvatarUser.js +78 -0
  31. package/src/getBotInitialData.js +43 -0
  32. package/src/getCtx.js +5 -0
  33. package/src/getCurrentUserID.js +7 -0
  34. package/src/getEmojiUrl.js +29 -0
  35. package/src/getFriendsList.js +83 -0
  36. package/src/getMessage.js +835 -0
  37. package/src/getOptions.js +5 -0
  38. package/src/getRegion.js +7 -0
  39. package/src/getThreadHistory.js +680 -0
  40. package/src/getThreadHistoryDeprecated.js +93 -0
  41. package/src/getThreadInfo.js +227 -0
  42. package/src/getThreadInfoDeprecated.js +80 -0
  43. package/src/getThreadList.js +269 -0
  44. package/src/getThreadListDeprecated.js +75 -0
  45. package/src/getThreadPictures.js +79 -0
  46. package/src/getUID.js +122 -0
  47. package/src/getUserID.js +66 -0
  48. package/src/getUserInfo.js +82 -0
  49. package/src/handleFriendRequest.js +57 -0
  50. package/src/handleMessageRequest.js +65 -0
  51. package/src/httpGet.js +64 -0
  52. package/src/httpPost.js +64 -0
  53. package/src/httpPostFormData.js +70 -0
  54. package/src/listenMqtt.js +674 -0
  55. package/src/listenNotification.js +85 -0
  56. package/src/logout.js +75 -0
  57. package/src/markAsDelivered.js +55 -0
  58. package/src/markAsRead.js +85 -0
  59. package/src/markAsReadAll.js +50 -0
  60. package/src/markAsSeen.js +61 -0
  61. package/src/muteThread.js +52 -0
  62. package/src/pinMessage.js +59 -0
  63. package/src/refreshFb_dtsg.js +89 -0
  64. package/src/removeUserFromGroup.js +79 -0
  65. package/src/resolvePhotoUrl.js +45 -0
  66. package/src/searchForThread.js +53 -0
  67. package/src/searchStickers.js +53 -0
  68. package/src/sendMessage.js +442 -0
  69. package/src/sendMessageMqtt.js +316 -0
  70. package/src/sendTypingIndicator.js +28 -0
  71. package/src/setMessageReaction.js +122 -0
  72. package/src/setMessageReactionMqtt.js +62 -0
  73. package/src/setPostReaction.js +108 -0
  74. package/src/setProfileGuard.js +44 -0
  75. package/src/setStoryReaction.js +64 -0
  76. package/src/setTitle.js +90 -0
  77. package/src/shareContact.js +110 -0
  78. package/src/shareLink.js +59 -0
  79. package/src/stopListenMqtt.js +23 -0
  80. package/src/threadColors.js +131 -0
  81. package/src/unfriend.js +52 -0
  82. package/src/unsendMessage.js +45 -0
  83. package/src/uploadAttachment.js +94 -0
  84. package/utils.js +1441 -0
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ module.exports = function (defaultFuncs, api, ctx) {
4
+ return (str) => ctx.globalOptions[str];
5
+ };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+
3
+ module.exports = function (defaultFuncs, api, ctx) {
4
+ return function getRegion() {
5
+ return ctx?.region;
6
+ };
7
+ };
@@ -0,0 +1,680 @@
1
+ "use strict";
2
+
3
+ const utils = require("../utils");
4
+ // @NethWs3Dev
5
+
6
+ function getExtension(original_extension, filename = "") {
7
+ if (original_extension) {
8
+ return original_extension;
9
+ } else {
10
+ const extension = filename.split(".").pop();
11
+ if (extension === filename) {
12
+ return "";
13
+ } else {
14
+ return extension;
15
+ }
16
+ }
17
+ }
18
+
19
+ function formatAttachmentsGraphQLResponse(attachment) {
20
+ switch (attachment.__typename) {
21
+ case "MessageImage":
22
+ return {
23
+ type: "photo",
24
+ ID: attachment.legacy_attachment_id,
25
+ filename: attachment.filename,
26
+ original_extension: getExtension(
27
+ attachment.original_extension,
28
+ attachment.filename,
29
+ ),
30
+ thumbnailUrl: attachment.thumbnail.uri,
31
+
32
+ previewUrl: attachment.preview.uri,
33
+ previewWidth: attachment.preview.width,
34
+ previewHeight: attachment.preview.height,
35
+
36
+ largePreviewUrl: attachment.large_preview.uri,
37
+ largePreviewHeight: attachment.large_preview.height,
38
+ largePreviewWidth: attachment.large_preview.width,
39
+
40
+ // You have to query for the real image. See below.
41
+ url: attachment.large_preview.uri, // @Legacy
42
+ width: attachment.large_preview.width, // @Legacy
43
+ height: attachment.large_preview.height, // @Legacy
44
+ name: attachment.filename, // @Legacy
45
+
46
+ // @Undocumented
47
+ attributionApp: attachment.attribution_app
48
+ ? {
49
+ attributionAppID: attachment.attribution_app.id,
50
+ name: attachment.attribution_app.name,
51
+ logo: attachment.attribution_app.square_logo,
52
+ }
53
+ : null,
54
+
55
+ // @TODO No idea what this is, should we expose it?
56
+ // Ben - July 15th 2017
57
+ // renderAsSticker: attachment.render_as_sticker,
58
+
59
+ // This is _not_ the real URI, this is still just a large preview.
60
+ // To get the URL we'll need to support a POST query to
61
+ //
62
+ // https://www.facebook.com/webgraphql/query/
63
+ //
64
+ // With the following query params:
65
+ //
66
+ // query_id:728987990612546
67
+ // variables:{"id":"100009069356507","photoID":"10213724771692996"}
68
+ // dpr:1
69
+ //
70
+ // No special form though.
71
+ };
72
+ case "MessageAnimatedImage":
73
+ return {
74
+ type: "animated_image",
75
+ ID: attachment.legacy_attachment_id,
76
+ filename: attachment.filename,
77
+ original_extension: getExtension(
78
+ attachment.original_extension,
79
+ attachment.filename,
80
+ ),
81
+
82
+ previewUrl: attachment.preview_image.uri,
83
+ previewWidth: attachment.preview_image.width,
84
+ previewHeight: attachment.preview_image.height,
85
+
86
+ url: attachment.animated_image.uri,
87
+ width: attachment.animated_image.width,
88
+ height: attachment.animated_image.height,
89
+
90
+ thumbnailUrl: attachment.preview_image.uri, // @Legacy
91
+ name: attachment.filename, // @Legacy
92
+ facebookUrl: attachment.animated_image.uri, // @Legacy
93
+ rawGifImage: attachment.animated_image.uri, // @Legacy
94
+ animatedGifUrl: attachment.animated_image.uri, // @Legacy
95
+ animatedGifPreviewUrl: attachment.preview_image.uri, // @Legacy
96
+ animatedWebpUrl: attachment.animated_image.uri, // @Legacy
97
+ animatedWebpPreviewUrl: attachment.preview_image.uri, // @Legacy
98
+
99
+ // @Undocumented
100
+ attributionApp: attachment.attribution_app
101
+ ? {
102
+ attributionAppID: attachment.attribution_app.id,
103
+ name: attachment.attribution_app.name,
104
+ logo: attachment.attribution_app.square_logo,
105
+ }
106
+ : null,
107
+ };
108
+ case "MessageVideo":
109
+ return {
110
+ type: "video",
111
+ ID: attachment.legacy_attachment_id,
112
+ filename: attachment.filename,
113
+ original_extension: getExtension(
114
+ attachment.original_extension,
115
+ attachment.filename,
116
+ ),
117
+ duration: attachment.playable_duration_in_ms,
118
+
119
+ thumbnailUrl: attachment.large_image.uri, // @Legacy
120
+
121
+ previewUrl: attachment.large_image.uri,
122
+ previewWidth: attachment.large_image.width,
123
+ previewHeight: attachment.large_image.height,
124
+
125
+ url: attachment.playable_url,
126
+ width: attachment.original_dimensions.x,
127
+ height: attachment.original_dimensions.y,
128
+
129
+ videoType: attachment.video_type.toLowerCase(),
130
+ };
131
+ case "MessageFile":
132
+ return {
133
+ type: "file",
134
+ ID: attachment.message_file_fbid,
135
+ filename: attachment.filename,
136
+ original_extension: getExtension(
137
+ attachment.original_extension,
138
+ attachment.filename,
139
+ ),
140
+
141
+ url: attachment.url,
142
+ isMalicious: attachment.is_malicious,
143
+ contentType: attachment.content_type,
144
+
145
+ name: attachment.filename, // @Legacy
146
+ mimeType: "", // @Legacy
147
+ fileSize: -1, // @Legacy
148
+ };
149
+ case "MessageAudio":
150
+ return {
151
+ type: "audio",
152
+ ID: attachment.url_shimhash, // Not fowardable
153
+ filename: attachment.filename,
154
+ original_extension: getExtension(
155
+ attachment.original_extension,
156
+ attachment.filename,
157
+ ),
158
+
159
+ duration: attachment.playable_duration_in_ms,
160
+ audioType: attachment.audio_type,
161
+ url: attachment.playable_url,
162
+
163
+ isVoiceMail: attachment.is_voicemail,
164
+ };
165
+ default:
166
+ return {
167
+ error: "Don't know about attachment type " + attachment.__typename,
168
+ };
169
+ }
170
+ }
171
+
172
+ function formatExtensibleAttachment(attachment) {
173
+ if (attachment.story_attachment) {
174
+ return {
175
+ type: "share",
176
+ ID: attachment.legacy_attachment_id,
177
+ url: attachment.story_attachment.url,
178
+
179
+ title: attachment.story_attachment.title_with_entities.text,
180
+ description:
181
+ attachment.story_attachment.description &&
182
+ attachment.story_attachment.description.text,
183
+ source:
184
+ attachment.story_attachment.source == null
185
+ ? null
186
+ : attachment.story_attachment.source.text,
187
+
188
+ image:
189
+ attachment.story_attachment.media == null
190
+ ? null
191
+ : attachment.story_attachment.media.animated_image == null &&
192
+ attachment.story_attachment.media.image == null
193
+ ? null
194
+ : (
195
+ attachment.story_attachment.media.animated_image ||
196
+ attachment.story_attachment.media.image
197
+ ).uri,
198
+ width:
199
+ attachment.story_attachment.media == null
200
+ ? null
201
+ : attachment.story_attachment.media.animated_image == null &&
202
+ attachment.story_attachment.media.image == null
203
+ ? null
204
+ : (
205
+ attachment.story_attachment.media.animated_image ||
206
+ attachment.story_attachment.media.image
207
+ ).width,
208
+ height:
209
+ attachment.story_attachment.media == null
210
+ ? null
211
+ : attachment.story_attachment.media.animated_image == null &&
212
+ attachment.story_attachment.media.image == null
213
+ ? null
214
+ : (
215
+ attachment.story_attachment.media.animated_image ||
216
+ attachment.story_attachment.media.image
217
+ ).height,
218
+ playable:
219
+ attachment.story_attachment.media == null
220
+ ? null
221
+ : attachment.story_attachment.media.is_playable,
222
+ duration:
223
+ attachment.story_attachment.media == null
224
+ ? null
225
+ : attachment.story_attachment.media.playable_duration_in_ms,
226
+ playableUrl:
227
+ attachment.story_attachment.media == null
228
+ ? null
229
+ : attachment.story_attachment.media.playable_url,
230
+
231
+ subattachments: attachment.story_attachment.subattachments,
232
+
233
+ // Format example:
234
+ //
235
+ // [{
236
+ // key: "width",
237
+ // value: { text: "1280" }
238
+ // }]
239
+ //
240
+ // That we turn into:
241
+ //
242
+ // {
243
+ // width: "1280"
244
+ // }
245
+ //
246
+ properties: attachment.story_attachment.properties.reduce(function (
247
+ obj,
248
+ cur,
249
+ ) {
250
+ obj[cur.key] = cur.value.text;
251
+ return obj;
252
+ }, {}),
253
+
254
+ // Deprecated fields
255
+ animatedImageSize: "", // @Legacy
256
+ facebookUrl: "", // @Legacy
257
+ styleList: "", // @Legacy
258
+ target: "", // @Legacy
259
+ thumbnailUrl:
260
+ attachment.story_attachment.media == null
261
+ ? null
262
+ : attachment.story_attachment.media.animated_image == null &&
263
+ attachment.story_attachment.media.image == null
264
+ ? null
265
+ : (
266
+ attachment.story_attachment.media.animated_image ||
267
+ attachment.story_attachment.media.image
268
+ ).uri, // @Legacy
269
+ thumbnailWidth:
270
+ attachment.story_attachment.media == null
271
+ ? null
272
+ : attachment.story_attachment.media.animated_image == null &&
273
+ attachment.story_attachment.media.image == null
274
+ ? null
275
+ : (
276
+ attachment.story_attachment.media.animated_image ||
277
+ attachment.story_attachment.media.image
278
+ ).width, // @Legacy
279
+ thumbnailHeight:
280
+ attachment.story_attachment.media == null
281
+ ? null
282
+ : attachment.story_attachment.media.animated_image == null &&
283
+ attachment.story_attachment.media.image == null
284
+ ? null
285
+ : (
286
+ attachment.story_attachment.media.animated_image ||
287
+ attachment.story_attachment.media.image
288
+ ).height, // @Legacy
289
+ };
290
+ } else {
291
+ return { error: "Don't know what to do with extensible_attachment." };
292
+ }
293
+ }
294
+
295
+ function formatReactionsGraphQL(reaction) {
296
+ return {
297
+ reaction: reaction.reaction,
298
+ userID: reaction.user.id,
299
+ };
300
+ }
301
+
302
+ function formatEventData(event) {
303
+ if (event == null) {
304
+ return {};
305
+ }
306
+
307
+ switch (event.__typename) {
308
+ case "ThemeColorExtensibleMessageAdminText":
309
+ return {
310
+ color: event.theme_color,
311
+ };
312
+ case "ThreadNicknameExtensibleMessageAdminText":
313
+ return {
314
+ nickname: event.nickname,
315
+ participantID: event.participant_id,
316
+ };
317
+ case "ThreadIconExtensibleMessageAdminText":
318
+ return {
319
+ threadIcon: event.thread_icon,
320
+ };
321
+ case "InstantGameUpdateExtensibleMessageAdminText":
322
+ return {
323
+ gameID: event.game == null ? null : event.game.id,
324
+ update_type: event.update_type,
325
+ collapsed_text: event.collapsed_text,
326
+ expanded_text: event.expanded_text,
327
+ instant_game_update_data: event.instant_game_update_data,
328
+ };
329
+ case "GameScoreExtensibleMessageAdminText":
330
+ return {
331
+ game_type: event.game_type,
332
+ };
333
+ case "RtcCallLogExtensibleMessageAdminText":
334
+ return {
335
+ event: event.event,
336
+ is_video_call: event.is_video_call,
337
+ server_info_data: event.server_info_data,
338
+ };
339
+ case "GroupPollExtensibleMessageAdminText":
340
+ return {
341
+ event_type: event.event_type,
342
+ total_count: event.total_count,
343
+ question: event.question,
344
+ };
345
+ case "AcceptPendingThreadExtensibleMessageAdminText":
346
+ return {
347
+ accepter_id: event.accepter_id,
348
+ requester_id: event.requester_id,
349
+ };
350
+ case "ConfirmFriendRequestExtensibleMessageAdminText":
351
+ return {
352
+ friend_request_recipient: event.friend_request_recipient,
353
+ friend_request_sender: event.friend_request_sender,
354
+ };
355
+ case "AddContactExtensibleMessageAdminText":
356
+ return {
357
+ contact_added_id: event.contact_added_id,
358
+ contact_adder_id: event.contact_adder_id,
359
+ };
360
+ case "AdExtensibleMessageAdminText":
361
+ return {
362
+ ad_client_token: event.ad_client_token,
363
+ ad_id: event.ad_id,
364
+ ad_preferences_link: event.ad_preferences_link,
365
+ ad_properties: event.ad_properties,
366
+ };
367
+ // never data
368
+ case "ParticipantJoinedGroupCallExtensibleMessageAdminText":
369
+ case "ThreadEphemeralTtlModeExtensibleMessageAdminText":
370
+ case "StartedSharingVideoExtensibleMessageAdminText":
371
+ case "LightweightEventCreateExtensibleMessageAdminText":
372
+ case "LightweightEventNotifyExtensibleMessageAdminText":
373
+ case "LightweightEventNotifyBeforeEventExtensibleMessageAdminText":
374
+ case "LightweightEventUpdateTitleExtensibleMessageAdminText":
375
+ case "LightweightEventUpdateTimeExtensibleMessageAdminText":
376
+ case "LightweightEventUpdateLocationExtensibleMessageAdminText":
377
+ case "LightweightEventDeleteExtensibleMessageAdminText":
378
+ return {};
379
+ default:
380
+ return {
381
+ error: "Don't know what to with event data type " + event.__typename,
382
+ };
383
+ }
384
+ }
385
+
386
+ function formatMessagesGraphQLResponse(data) {
387
+ const messageThread = data.o0.data.message_thread;
388
+ const threadID = messageThread.thread_key.thread_fbid
389
+ ? messageThread.thread_key.thread_fbid
390
+ : messageThread.thread_key.other_user_id;
391
+
392
+ const messages = messageThread.messages.nodes.map(function (d) {
393
+ switch (d.__typename) {
394
+ case "UserMessage":
395
+ // Give priority to stickers. They're seen as normal messages but we've
396
+ // been considering them as attachments.
397
+ var maybeStickerAttachment;
398
+ if (d.sticker) {
399
+ maybeStickerAttachment = [
400
+ {
401
+ type: "sticker",
402
+ ID: d.sticker.id,
403
+ url: d.sticker.url,
404
+
405
+ packID: d.sticker.pack ? d.sticker.pack.id : null,
406
+ spriteUrl: d.sticker.sprite_image,
407
+ spriteUrl2x: d.sticker.sprite_image_2x,
408
+ width: d.sticker.width,
409
+ height: d.sticker.height,
410
+
411
+ caption: d.snippet, // Not sure what the heck caption was.
412
+ description: d.sticker.label, // Not sure about this one either.
413
+
414
+ frameCount: d.sticker.frame_count,
415
+ frameRate: d.sticker.frame_rate,
416
+ framesPerRow: d.sticker.frames_per_row,
417
+ framesPerCol: d.sticker.frames_per_col,
418
+
419
+ stickerID: d.sticker.id, // @Legacy
420
+ spriteURI: d.sticker.sprite_image, // @Legacy
421
+ spriteURI2x: d.sticker.sprite_image_2x, // @Legacy
422
+ },
423
+ ];
424
+ }
425
+
426
+ var mentionsObj = {};
427
+ if (d.message !== null) {
428
+ d.message.ranges.forEach((e) => {
429
+ mentionsObj[e.entity.id] = d.message.text.substr(
430
+ e.offset,
431
+ e.length,
432
+ );
433
+ });
434
+ }
435
+
436
+ return {
437
+ type: "message",
438
+ attachments: maybeStickerAttachment
439
+ ? maybeStickerAttachment
440
+ : d.blob_attachments && d.blob_attachments.length > 0
441
+ ? d.blob_attachments.map(formatAttachmentsGraphQLResponse)
442
+ : d.extensible_attachment
443
+ ? [formatExtensibleAttachment(d.extensible_attachment)]
444
+ : [],
445
+ body: d.message !== null ? d.message.text : "",
446
+ isGroup: messageThread.thread_type === "GROUP",
447
+ messageID: d.message_id,
448
+ senderID: d.message_sender.id,
449
+ threadID: threadID,
450
+ timestamp: d.timestamp_precise,
451
+
452
+ mentions: mentionsObj,
453
+ isUnread: d.unread,
454
+
455
+ // New
456
+ messageReactions: d.message_reactions
457
+ ? d.message_reactions.map(formatReactionsGraphQL)
458
+ : null,
459
+ isSponsored: d.is_sponsored,
460
+ snippet: d.snippet,
461
+ };
462
+ case "ThreadNameMessage":
463
+ return {
464
+ type: "event",
465
+ messageID: d.message_id,
466
+ threadID: threadID,
467
+ isGroup: messageThread.thread_type === "GROUP",
468
+ senderID: d.message_sender.id,
469
+ timestamp: d.timestamp_precise,
470
+ eventType: "change_thread_name",
471
+ snippet: d.snippet,
472
+ eventData: {
473
+ threadName: d.thread_name,
474
+ },
475
+
476
+ // @Legacy
477
+ author: d.message_sender.id,
478
+ logMessageType: "log:thread-name",
479
+ logMessageData: { name: d.thread_name },
480
+ };
481
+ case "ThreadImageMessage":
482
+ return {
483
+ type: "event",
484
+ messageID: d.message_id,
485
+ threadID: threadID,
486
+ isGroup: messageThread.thread_type === "GROUP",
487
+ senderID: d.message_sender.id,
488
+ timestamp: d.timestamp_precise,
489
+ eventType: "change_thread_image",
490
+ snippet: d.snippet,
491
+ eventData:
492
+ d.image_with_metadata == null
493
+ ? {} /* removed image */
494
+ : {
495
+ /* image added */
496
+ threadImage: {
497
+ attachmentID: d.image_with_metadata.legacy_attachment_id,
498
+ width: d.image_with_metadata.original_dimensions.x,
499
+ height: d.image_with_metadata.original_dimensions.y,
500
+ url: d.image_with_metadata.preview.uri,
501
+ },
502
+ },
503
+
504
+ // @Legacy
505
+ logMessageType: "log:thread-icon",
506
+ logMessageData: {
507
+ thread_icon: d.image_with_metadata
508
+ ? d.image_with_metadata.preview.uri
509
+ : null,
510
+ },
511
+ };
512
+ case "ParticipantLeftMessage":
513
+ return {
514
+ type: "event",
515
+ messageID: d.message_id,
516
+ threadID: threadID,
517
+ isGroup: messageThread.thread_type === "GROUP",
518
+ senderID: d.message_sender.id,
519
+ timestamp: d.timestamp_precise,
520
+ eventType: "remove_participants",
521
+ snippet: d.snippet,
522
+ eventData: {
523
+ // Array of IDs.
524
+ participantsRemoved: d.participants_removed.map(function (p) {
525
+ return p.id;
526
+ }),
527
+ },
528
+
529
+ // @Legacy
530
+ logMessageType: "log:unsubscribe",
531
+ logMessageData: {
532
+ leftParticipantFbId: d.participants_removed.map(function (p) {
533
+ return p.id;
534
+ }),
535
+ },
536
+ };
537
+ case "ParticipantsAddedMessage":
538
+ return {
539
+ type: "event",
540
+ messageID: d.message_id,
541
+ threadID: threadID,
542
+ isGroup: messageThread.thread_type === "GROUP",
543
+ senderID: d.message_sender.id,
544
+ timestamp: d.timestamp_precise,
545
+ eventType: "add_participants",
546
+ snippet: d.snippet,
547
+ eventData: {
548
+ // Array of IDs.
549
+ participantsAdded: d.participants_added.map(function (p) {
550
+ return p.id;
551
+ }),
552
+ },
553
+
554
+ // @Legacy
555
+ logMessageType: "log:subscribe",
556
+ logMessageData: {
557
+ addedParticipants: d.participants_added.map(function (p) {
558
+ return p.id;
559
+ }),
560
+ },
561
+ };
562
+ case "VideoCallMessage":
563
+ return {
564
+ type: "event",
565
+ messageID: d.message_id,
566
+ threadID: threadID,
567
+ isGroup: messageThread.thread_type === "GROUP",
568
+ senderID: d.message_sender.id,
569
+ timestamp: d.timestamp_precise,
570
+ eventType: "video_call",
571
+ snippet: d.snippet,
572
+
573
+ // @Legacy
574
+ logMessageType: "other",
575
+ };
576
+ case "VoiceCallMessage":
577
+ return {
578
+ type: "event",
579
+ messageID: d.message_id,
580
+ threadID: threadID,
581
+ isGroup: messageThread.thread_type === "GROUP",
582
+ senderID: d.message_sender.id,
583
+ timestamp: d.timestamp_precise,
584
+ eventType: "voice_call",
585
+ snippet: d.snippet,
586
+
587
+ // @Legacy
588
+ logMessageType: "other",
589
+ };
590
+ case "GenericAdminTextMessage":
591
+ return {
592
+ type: "event",
593
+ messageID: d.message_id,
594
+ threadID: threadID,
595
+ isGroup: messageThread.thread_type === "GROUP",
596
+ senderID: d.message_sender.id,
597
+ timestamp: d.timestamp_precise,
598
+ snippet: d.snippet,
599
+ eventType: d.extensible_message_admin_text_type.toLowerCase(),
600
+ eventData: formatEventData(d.extensible_message_admin_text),
601
+
602
+ // @Legacy
603
+ logMessageType: utils.getAdminTextMessageType(
604
+ d.extensible_message_admin_text_type,
605
+ ),
606
+ logMessageData: d.extensible_message_admin_text, // Maybe different?
607
+ };
608
+ default:
609
+ return { error: "Don't know about message type " + d.__typename };
610
+ }
611
+ });
612
+ return messages;
613
+ }
614
+
615
+ module.exports = function (defaultFuncs, api, ctx) {
616
+ return function getThreadHistoryGraphQL(
617
+ threadID,
618
+ amount,
619
+ timestamp,
620
+ callback,
621
+ ) {
622
+ let resolveFunc = function () {};
623
+ let rejectFunc = function () {};
624
+ const returnPromise = new Promise(function (resolve, reject) {
625
+ resolveFunc = resolve;
626
+ rejectFunc = reject;
627
+ });
628
+
629
+ if (!callback) {
630
+ callback = function (err, data) {
631
+ if (err) {
632
+ return rejectFunc(err);
633
+ }
634
+ resolveFunc(data);
635
+ };
636
+ }
637
+
638
+ // `queries` has to be a string. I couldn't tell from the dev utils. This
639
+ // took me a really long time to figure out. I deserve a cookie for this.
640
+ const form = {
641
+ av: ctx.globalOptions.pageID,
642
+ queries: JSON.stringify({
643
+ o0: {
644
+ // This doc_id was valid on February 2nd 2017.
645
+ doc_id: "1498317363570230",
646
+ query_params: {
647
+ id: threadID,
648
+ message_limit: amount,
649
+ load_messages: 1,
650
+ load_read_receipts: false,
651
+ before: timestamp,
652
+ },
653
+ },
654
+ }),
655
+ };
656
+
657
+ defaultFuncs
658
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
659
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
660
+ .then(function (resData) {
661
+ if (resData.error) {
662
+ throw resData;
663
+ }
664
+ // This returns us an array of things. The last one is the success /
665
+ // failure one.
666
+ // @TODO What do we do in this case?
667
+ if (resData[resData.length - 1].error_results !== 0) {
668
+ throw new Error("There was an error_result.");
669
+ }
670
+
671
+ callback(null, formatMessagesGraphQLResponse(resData[0]));
672
+ })
673
+ .catch(function (err) {
674
+ utils.error("getThreadHistoryGraphQL", err);
675
+ return callback(err);
676
+ });
677
+
678
+ return returnPromise;
679
+ };
680
+ };