@cexy/hoonfca 1.0.1 → 1.0.3

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