@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
@@ -1,239 +0,0 @@
1
- "use strict";
2
-
3
- const utils = require('../utils');
4
-
5
-
6
- /**
7
- * Formats a single attachment object from a GraphQL response.
8
- * @param {Object} attachment The raw attachment object.
9
- * @returns {Object} A formatted attachment object.
10
- */
11
- function formatAttachmentsGraphQLResponse(attachment) {
12
- switch (attachment.__typename) {
13
- case "MessageImage":
14
- return {
15
- type: "photo",
16
- ID: attachment.legacy_attachment_id,
17
- filename: attachment.filename,
18
- thumbnailUrl: attachment.thumbnail.uri,
19
- previewUrl: attachment.preview.uri,
20
- previewWidth: attachment.preview.width,
21
- previewHeight: attachment.preview.height,
22
- largePreviewUrl: attachment.large_preview.uri,
23
- largePreviewHeight: attachment.large_preview.height,
24
- largePreviewWidth: attachment.large_preview.width,
25
- url: attachment.large_preview.uri,
26
- width: attachment.original_dimensions.x,
27
- height: attachment.original_dimensions.y,
28
- name: attachment.filename
29
- };
30
- case "MessageAnimatedImage":
31
- return {
32
- type: "animated_image",
33
- ID: attachment.legacy_attachment_id,
34
- filename: attachment.filename,
35
- previewUrl: attachment.preview_image.uri,
36
- previewWidth: attachment.preview_image.width,
37
- previewHeight: attachment.preview_image.height,
38
- url: attachment.animated_image.uri,
39
- width: attachment.animated_image.width,
40
- height: attachment.animated_image.height,
41
- name: attachment.filename,
42
- facebookUrl: attachment.animated_image.uri,
43
- };
44
- case "MessageVideo":
45
- return {
46
- type: "video",
47
- ID: attachment.legacy_attachment_id,
48
- filename: attachment.filename,
49
- duration: attachment.playable_duration_in_ms,
50
- thumbnailUrl: attachment.large_image.uri,
51
- previewUrl: attachment.large_image.uri,
52
- previewWidth: attachment.large_image.width,
53
- previewHeight: attachment.large_image.height,
54
- url: attachment.playable_url,
55
- width: attachment.original_dimensions.x,
56
- height: attachment.original_dimensions.y,
57
- videoType: attachment.video_type.toLowerCase(),
58
- };
59
- case "MessageFile":
60
- return {
61
- type: "file",
62
- ID: attachment.message_file_fbid,
63
- filename: attachment.filename,
64
- url: attachment.url,
65
- isMalicious: attachment.is_malicious,
66
- contentType: attachment.content_type,
67
- name: attachment.filename,
68
- };
69
- case "MessageAudio":
70
- return {
71
- type: "audio",
72
- ID: attachment.url_shimhash,
73
- filename: attachment.filename,
74
- duration: attachment.playable_duration_in_ms,
75
- audioType: attachment.audio_type,
76
- url: attachment.playable_url,
77
- isVoiceMail: attachment.is_voicemail,
78
- };
79
- default:
80
- return {
81
- type: "unknown",
82
- error: "Don't know about attachment type " + attachment.__typename,
83
- };
84
- }
85
- }
86
-
87
- /**
88
- * Formats a share (extensible) attachment from a GraphQL response.
89
- * @param {Object} attachment The raw extensible attachment object.
90
- * @returns {Object} A formatted share attachment object.
91
- */
92
- function formatExtensibleAttachment(attachment) {
93
- if (attachment.story_attachment) {
94
- return {
95
- type: "share",
96
- ID: attachment.legacy_attachment_id,
97
- url: attachment.story_attachment.url,
98
- title: attachment.story_attachment.title_with_entities.text,
99
- description: attachment.story_attachment.description && attachment.story_attachment.description.text,
100
- source: attachment.story_attachment.source == null ? null : attachment.story_attachment.source.text,
101
- image: attachment.story_attachment.media?.image?.uri,
102
- width: attachment.story_attachment.media?.image?.width,
103
- height: attachment.story_attachment.media?.image?.height,
104
- playable: attachment.story_attachment.media?.is_playable,
105
- duration: attachment.story_attachment.media?.playable_duration_in_ms,
106
- playableUrl: attachment.story_attachment.media?.playable_url,
107
- subattachments: attachment.story_attachment.subattachments,
108
- properties: attachment.story_attachment.properties.reduce((obj, cur) => {
109
- obj[cur.key] = cur.value.text;
110
- return obj;
111
- }, {}),
112
- };
113
- }
114
- return { type: "unknown", error: "Don't know what to do with extensible_attachment." };
115
- }
116
-
117
- /**
118
- * Formats the response from a GraphQL message history query.
119
- * @param {Object} data The raw GraphQL response data.
120
- * @returns {Array<Object>} An array of formatted message objects.
121
- */
122
- function formatMessagesGraphQLResponse(data) {
123
- const messageThread = data.o0.data.message_thread;
124
- if (!messageThread) return [];
125
-
126
- const threadID = messageThread.thread_key.thread_fbid ? messageThread.thread_key.thread_fbid : messageThread.thread_key.other_user_id;
127
-
128
- return messageThread.messages.nodes.map(d => {
129
- switch (d.__typename) {
130
- case "UserMessage":
131
- const mentions = {};
132
- if (d.message?.ranges) {
133
- d.message.ranges.forEach(e => {
134
- mentions[e.entity.id] = d.message.text.substring(e.offset, e.offset + e.length);
135
- });
136
- }
137
- return {
138
- type: "message",
139
- attachments: d.sticker ? [{
140
- type: "sticker",
141
- ID: d.sticker.id,
142
- url: d.sticker.url,
143
- packID: d.sticker.pack?.id,
144
- frameCount: d.sticker.frame_count,
145
- frameRate: d.sticker.frame_rate,
146
- framesPerRow: d.sticker.frames_per_row,
147
- framesPerCol: d.sticker.frames_per_col,
148
- stickerID: d.sticker.id,
149
- }] : (d.blob_attachments || []).map(formatAttachmentsGraphQLResponse).concat(d.extensible_attachment ? [formatExtensibleAttachment(d.extensible_attachment)] : []),
150
- body: d.message?.text || "",
151
- isGroup: messageThread.thread_type === "GROUP",
152
- messageID: d.message_id,
153
- senderID: d.message_sender.id,
154
- threadID: threadID,
155
- timestamp: d.timestamp_precise,
156
- mentions: mentions,
157
- isUnread: d.unread,
158
- messageReactions: d.message_reactions?.map(r => ({ reaction: r.reaction, userID: r.user.id })) || [],
159
- };
160
- case "ThreadNameMessage":
161
- case "ThreadImageMessage":
162
- case "ParticipantLeftMessage":
163
- case "ParticipantsAddedMessage":
164
- case "GenericAdminTextMessage": {
165
-
166
-
167
- return {
168
- type: "event",
169
- messageID: d.message_id,
170
- threadID: threadID,
171
- isGroup: messageThread.thread_type === "GROUP",
172
- senderID: d.message_sender.id,
173
- author: d.message_sender.id,
174
- timestamp: d.timestamp_precise,
175
- snippet: d.snippet,
176
- logMessageType: utils.getAdminTextMessageType(d.extensible_message_admin_text_type || d.__typename),
177
- logMessageData: d.extensible_message_admin_text || d,
178
- };
179
- }
180
- default:
181
- return { type: "unknown", error: "Unknown message type " + d.__typename, raw: d };
182
- }
183
- });
184
- }
185
-
186
- /**
187
- * @param {Object} defaultFuncs
188
- * @param {Object} api
189
- * @param {Object} ctx
190
- * @returns {function(threadID: string, amount: number, timestamp: number | null): Promise<Array<Object>>}
191
- */
192
- module.exports = function (defaultFuncs, api, ctx) {
193
- /**
194
- * Retrieves the message history for a specific thread.
195
- * @param {string} threadID The ID of the thread to fetch history from.
196
- * @param {number} amount The number of messages to retrieve.
197
- * @param {number | null} timestamp The timestamp to start fetching messages before.
198
- * @returns {Promise<Array<Object>>} A promise that resolves with an array of formatted message objects.
199
- */
200
- return async function getThreadHistory(threadID, amount, timestamp) {
201
- if (!threadID || !amount) {
202
- throw new Error("getThreadHistory: threadID and amount are required.");
203
- }
204
-
205
- const form = {
206
- av: ctx.globalOptions.pageID,
207
- queries: JSON.stringify({
208
- o0: {
209
- doc_id: "1498317363570230",
210
- query_params: {
211
- id: threadID,
212
- message_limit: amount,
213
- load_messages: 1,
214
- load_read_receipts: false,
215
- before: timestamp || null,
216
- },
217
- },
218
- }),
219
- };
220
-
221
- try {
222
- const resData = await defaultFuncs.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form);
223
- const parsedData = await utils.parseAndCheckLogin(ctx, defaultFuncs)(resData);
224
-
225
- if (parsedData.error || (Array.isArray(parsedData) && parsedData[parsedData.length - 1].error_results !== 0)) {
226
- throw parsedData;
227
- }
228
-
229
- if (!Array.isArray(parsedData) || !parsedData[0] || !parsedData[0].o0 || !parsedData[0].o0.data) {
230
- throw { error: "getThreadHistory: Malformed response from GraphQL.", res: parsedData };
231
- }
232
-
233
- return formatMessagesGraphQLResponse(parsedData[0]);
234
- } catch (err) {
235
- utils.error("getThreadHistory", err);
236
- throw err;
237
- }
238
- };
239
- };
@@ -1,267 +0,0 @@
1
-
2
- "use strict";
3
-
4
- const utils = require('../utils');
5
-
6
- /**
7
- * Formats an event reminder object from a GraphQL response.
8
- * @param {Object} reminder The raw event reminder object.
9
- * @returns {Object} A formatted event reminder object.
10
- */
11
- function formatEventReminders(reminder) {
12
- return {
13
- reminderID: reminder.id,
14
- eventCreatorID: reminder.lightweight_event_creator.id,
15
- time: reminder.time,
16
- eventType: reminder.lightweight_event_type.toLowerCase(),
17
- locationName: reminder.location_name,
18
- locationCoordinates: reminder.location_coordinates,
19
- locationPage: reminder.location_page,
20
- eventStatus: reminder.lightweight_event_status.toLowerCase(),
21
- note: reminder.note,
22
- repeatMode: reminder.repeat_mode.toLowerCase(),
23
- eventTitle: reminder.event_title,
24
- triggerMessage: reminder.trigger_message,
25
- secondsToNotifyBefore: reminder.seconds_to_notify_before,
26
- allowsRsvp: reminder.allows_rsvp,
27
- relatedEvent: reminder.related_event,
28
- members: reminder.event_reminder_members.edges.map(function (member) {
29
- return {
30
- memberID: member.node.id,
31
- state: member.guest_list_state.toLowerCase(),
32
- };
33
- }),
34
- };
35
- }
36
-
37
- /**
38
- * Formats a thread object from a GraphQL response.
39
- * @param {Object} data The raw GraphQL data for a thread.
40
- * @returns {Object | null} A formatted thread object or null if data is invalid.
41
- * @throws {Error} If Facebook returns a GraphQL error
42
- */
43
- function formatThreadGraphQLResponse(data) {
44
- // Check for GraphQL errors and throw with details instead of silently returning null
45
- if (data.errors) {
46
- const details = data.errors.map(e => e.message || e).join(', ');
47
- const error = new Error(`GraphQL error in getThreadInfo: ${details}`);
48
- Object.assign(error, {
49
- details: details,
50
- fullErrors: data.errors
51
- });
52
- utils.error("formatThreadGraphQLResponse", error);
53
- throw error;
54
- }
55
-
56
- const messageThread = data.message_thread;
57
- if (!messageThread) {
58
- const error = new Error("No message_thread in GraphQL response - thread may not exist or access may be restricted");
59
- Object.assign(error, {
60
- details: "The GraphQL query returned successfully but contained no message_thread data"
61
- });
62
- utils.error("formatThreadGraphQLResponse", error);
63
- throw error;
64
- }
65
-
66
- const threadID = messageThread.thread_key.thread_fbid
67
- ? messageThread.thread_key.thread_fbid
68
- : messageThread.thread_key.other_user_id;
69
-
70
- const lastM = messageThread.last_message;
71
- const snippetID =
72
- lastM?.nodes?.[0]?.message_sender?.messaging_actor?.id || null;
73
- const snippetText = lastM?.nodes?.[0]?.snippet || null;
74
- const lastR = messageThread.last_read_receipt;
75
- const lastReadTimestamp = lastR?.nodes?.[0]?.timestamp_precise || null;
76
-
77
- return {
78
- threadID: threadID,
79
- threadName: messageThread.name,
80
- participantIDs: messageThread.all_participants.edges.map(
81
- (d) => d.node.messaging_actor.id,
82
- ),
83
- userInfo: messageThread.all_participants.edges.map((d) => ({
84
- id: d.node.messaging_actor.id,
85
- name: d.node.messaging_actor.name,
86
- firstName: d.node.messaging_actor.short_name,
87
- vanity: d.node.messaging_actor.username,
88
- url: d.node.messaging_actor.url,
89
- thumbSrc: d.node.messaging_actor.big_image_src.uri,
90
- profileUrl: d.node.messaging_actor.big_image_src.uri,
91
- gender: d.node.messaging_actor.gender,
92
- type: d.node.messaging_actor.__typename,
93
- isFriend: d.node.messaging_actor.is_viewer_friend,
94
- isBirthday: !!d.node.messaging_actor.is_birthday,
95
- })),
96
- unreadCount: messageThread.unread_count,
97
- messageCount: messageThread.messages_count,
98
- timestamp: messageThread.updated_time_precise,
99
- muteUntil: messageThread.mute_until,
100
- isGroup: messageThread.thread_type == "GROUP",
101
- isSubscribed: messageThread.is_viewer_subscribed,
102
- isArchived: messageThread.has_viewer_archived,
103
- folder: messageThread.folder,
104
- cannotReplyReason: messageThread.cannot_reply_reason,
105
- eventReminders: messageThread.event_reminders
106
- ? messageThread.event_reminders.nodes.map(formatEventReminders)
107
- : null,
108
- emoji: messageThread.customization_info
109
- ? messageThread.customization_info.emoji
110
- : null,
111
- color:
112
- messageThread.customization_info &&
113
- messageThread.customization_info.outgoing_bubble_color
114
- ? messageThread.customization_info.outgoing_bubble_color.slice(2)
115
- : null,
116
- threadTheme: messageThread.thread_theme,
117
- nicknames:
118
- messageThread.customization_info &&
119
- messageThread.customization_info.participant_customizations
120
- ? messageThread.customization_info.participant_customizations.reduce(
121
- function (res, val) {
122
- if (val.nickname) res[val.participant_id] = val.nickname;
123
- return res;
124
- },
125
- {},
126
- )
127
- : {},
128
- adminIDs: messageThread.thread_admins,
129
- approvalMode: Boolean(messageThread.approval_mode),
130
- approvalQueue: messageThread.group_approval_queue.nodes.map((a) => ({
131
- inviterID: a.inviter.id,
132
- requesterID: a.requester.id,
133
- timestamp: a.request_timestamp,
134
- request_source: a.request_source,
135
- })),
136
- reactionsMuteMode: messageThread.reactions_mute_mode.toLowerCase(),
137
- mentionsMuteMode: messageThread.mentions_mute_mode.toLowerCase(),
138
- isPinProtected: messageThread.is_pin_protected,
139
- relatedPageThread: messageThread.related_page_thread,
140
- name: messageThread.name,
141
- snippet: snippetText,
142
- snippetSender: snippetID,
143
- snippetAttachments: [],
144
- serverTimestamp: messageThread.updated_time_precise,
145
- imageSrc: messageThread.image ? messageThread.image.uri : null,
146
- isCanonicalUser: messageThread.is_canonical_neo_user,
147
- isCanonical: messageThread.thread_type != "GROUP",
148
- recipientsLoadable: true,
149
- hasEmailParticipant: false,
150
- readOnly: false,
151
- canReply: messageThread.cannot_reply_reason == null,
152
- lastMessageTimestamp: messageThread.last_message
153
- ? messageThread.last_message.timestamp_precise
154
- : null,
155
- lastMessageType: "message",
156
- lastReadTimestamp: lastReadTimestamp,
157
- threadType: messageThread.thread_type == "GROUP" ? 2 : 1,
158
- inviteLink: {
159
- enable: messageThread.joinable_mode
160
- ? messageThread.joinable_mode.mode == 1
161
- : false,
162
- link: messageThread.joinable_mode
163
- ? messageThread.joinable_mode.link
164
- : null,
165
- },
166
- };
167
- }
168
-
169
- /**
170
- * @param {Object} defaultFuncs
171
- * @param {Object} api
172
- * @param {Object} ctx
173
- * @returns {function(threadID: string | string[]): Promise<Object>}
174
- */
175
- module.exports = function (defaultFuncs, api, ctx) {
176
- /**
177
- * Retrieves information about one or more threads.
178
- * @param {string|string[]} threadID A single thread ID or an array of thread IDs.
179
- * @returns {Promise<Object>} A promise that resolves with an object of thread info, or a single thread object if one ID was passed.
180
- */
181
- return async function getThreadInfo(threadID) {
182
- const threadIDs = Array.isArray(threadID) ? threadID : [threadID];
183
-
184
- // Validate thread IDs
185
- if (!ctx.validator.validateIDArray(threadIDs, ctx.validator.isValidThreadID)) {
186
- throw new Error("Invalid thread ID(s)");
187
- }
188
-
189
- let form = {};
190
- threadIDs.forEach((t, i) => {
191
- form["o" + i] = {
192
- doc_id: "3449967031715030",
193
- query_params: {
194
- id: t,
195
- message_limit: 0,
196
- load_messages: false,
197
- load_read_receipts: false,
198
- before: null,
199
- },
200
- };
201
- });
202
-
203
- form = {
204
- queries: JSON.stringify(form),
205
- batch_name: "MessengerGraphQLThreadFetcher",
206
- };
207
-
208
- try {
209
- const resData = await defaultFuncs
210
- .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
211
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs));
212
-
213
- if (resData.error) {
214
- throw resData;
215
- }
216
-
217
- const threadInfos = {};
218
- for (let i = resData.length - 2; i >= 0; i--) {
219
- const res = resData[i];
220
-
221
- // Check for error_results and throw instead of silently continuing
222
- if (res.error_results) {
223
- const error = new Error(`Facebook returned error_results for thread query: ${res.error_results} errors`);
224
- Object.assign(error, {
225
- error_count: res.error_results,
226
- thread_index: i
227
- });
228
- utils.error("getThreadInfo", error);
229
- throw error;
230
- }
231
-
232
- const oKey = Object.keys(res)[0];
233
- const responseData = res[oKey];
234
-
235
- // Check for errors in the response object
236
- if (responseData.errors || responseData.error_results) {
237
- const details = responseData.errors
238
- ? JSON.stringify(responseData.errors)
239
- : `error_results: ${responseData.error_results}`;
240
- const error = new Error(`GraphQL error in thread response: ${details}`);
241
- Object.assign(error, {
242
- details: details,
243
- thread_index: i,
244
- fullErrors: responseData.errors
245
- });
246
- utils.error("getThreadInfo", error);
247
- throw error;
248
- }
249
-
250
- const threadInfo = formatThreadGraphQLResponse(responseData.data);
251
- if (threadInfo) {
252
- threadInfos[threadInfo.threadID || threadID[threadID.length - 1 - i]] = threadInfo;
253
- }
254
- }
255
-
256
- // Cache the thread infos
257
- for (const id in threadInfos) {
258
- ctx.cache.set(`thread_${id}`, threadInfos[id]);
259
- }
260
-
261
- return Array.isArray(threadID) ? threadInfos : Object.values(threadInfos)[0] || null;
262
- } catch (err) {
263
- utils.error("getThreadInfo", err);
264
- throw err;
265
- }
266
- };
267
- };