@cexy/hoonfca 1.0.4 → 1.0.5

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 (135) hide show
  1. package/CHANGELOG.md +181 -0
  2. package/DOCS.md +2636 -0
  3. package/LICENSE-MIT +21 -21
  4. package/README.md +632 -0
  5. package/func/checkUpdate.js +222 -0
  6. package/func/logger.js +48 -0
  7. package/index.d.ts +731 -605
  8. package/index.js +6 -774
  9. package/module/config.js +34 -0
  10. package/module/login.js +136 -0
  11. package/module/loginHelper.js +1034 -0
  12. package/module/options.js +45 -0
  13. package/package.json +54 -19
  14. package/src/api/action/addExternalModule.js +25 -0
  15. package/src/api/action/changeAvatar.js +137 -0
  16. package/src/{changeBio.js → api/action/changeBio.js} +75 -76
  17. package/src/api/action/enableAutoSaveAppState.js +73 -0
  18. package/src/{getCurrentUserID.js → api/action/getCurrentUserID.js} +7 -7
  19. package/src/{handleFriendRequest.js → api/action/handleFriendRequest.js} +57 -57
  20. package/src/api/action/logout.js +76 -0
  21. package/src/api/action/refreshFb_dtsg.js +48 -0
  22. package/src/api/action/setPostReaction.js +106 -0
  23. package/src/api/action/unfriend.js +54 -0
  24. package/src/api/http/httpGet.js +46 -0
  25. package/src/api/http/httpPost.js +52 -0
  26. package/src/api/http/postFormData.js +47 -0
  27. package/src/api/messaging/addUserToGroup.js +68 -0
  28. package/src/api/messaging/changeAdminStatus.js +122 -0
  29. package/src/api/messaging/changeArchivedStatus.js +55 -0
  30. package/src/api/messaging/changeBlockedStatus.js +48 -0
  31. package/src/api/messaging/changeGroupImage.js +90 -0
  32. package/src/api/messaging/changeNickname.js +70 -0
  33. package/src/api/messaging/changeThreadColor.js +79 -0
  34. package/src/api/messaging/changeThreadEmoji.js +106 -0
  35. package/src/{createNewGroup.js → api/messaging/createNewGroup.js} +88 -88
  36. package/src/api/messaging/createPoll.js +43 -0
  37. package/src/api/messaging/createThemeAI.js +98 -0
  38. package/src/{deleteMessage.js → api/messaging/deleteMessage.js} +56 -56
  39. package/src/{deleteThread.js → api/messaging/deleteThread.js} +56 -56
  40. package/src/api/messaging/editMessage.js +68 -0
  41. package/src/api/messaging/forwardAttachment.js +51 -0
  42. package/src/{getEmojiUrl.js → api/messaging/getEmojiUrl.js} +8 -8
  43. package/src/{getFriendsList.js → api/messaging/getFriendsList.js} +82 -84
  44. package/src/api/messaging/getMessage.js +829 -0
  45. package/src/api/messaging/getThemePictures.js +62 -0
  46. package/src/api/messaging/handleMessageRequest.js +65 -0
  47. package/src/{markAsDelivered.js → api/messaging/markAsDelivered.js} +57 -58
  48. package/src/{markAsRead.js → api/messaging/markAsRead.js} +88 -88
  49. package/src/{markAsReadAll.js → api/messaging/markAsReadAll.js} +49 -50
  50. package/src/api/messaging/markAsSeen.js +61 -0
  51. package/src/{muteThread.js → api/messaging/muteThread.js} +50 -52
  52. package/src/api/messaging/removeUserFromGroup.js +106 -0
  53. package/src/{resolvePhotoUrl.js → api/messaging/resolvePhotoUrl.js} +43 -45
  54. package/src/api/messaging/scheduler.js +264 -0
  55. package/src/{searchForThread.js → api/messaging/searchForThread.js} +52 -53
  56. package/src/api/messaging/sendMessage.js +272 -0
  57. package/src/api/messaging/sendTypingIndicator.js +67 -0
  58. package/src/api/messaging/setMessageReaction.js +76 -0
  59. package/src/api/messaging/setTitle.js +119 -0
  60. package/src/{shareContact.js → api/messaging/shareContact.js} +49 -53
  61. package/src/api/messaging/threadColors.js +128 -0
  62. package/src/api/messaging/unsendMessage.js +81 -0
  63. package/src/api/messaging/uploadAttachment.js +94 -0
  64. package/src/api/socket/core/connectMqtt.js +255 -0
  65. package/src/api/socket/core/emitAuth.js +106 -0
  66. package/src/api/socket/core/getSeqID.js +40 -0
  67. package/src/api/socket/core/getTaskResponseData.js +22 -0
  68. package/src/api/socket/core/markDelivery.js +12 -0
  69. package/src/api/socket/core/parseDelta.js +391 -0
  70. package/src/api/socket/detail/buildStream.js +208 -0
  71. package/src/api/socket/detail/constants.js +24 -0
  72. package/src/api/socket/listenMqtt.js +364 -0
  73. package/src/api/socket/middleware/index.js +216 -0
  74. package/src/{getThreadHistory.js → api/threads/getThreadHistory.js} +664 -647
  75. package/src/api/threads/getThreadInfo.js +438 -0
  76. package/src/{getThreadList.js → api/threads/getThreadList.js} +293 -292
  77. package/src/{getThreadPictures.js → api/threads/getThreadPictures.js} +78 -79
  78. package/src/{getUserID.js → api/users/getUserID.js} +65 -66
  79. package/src/api/users/getUserInfo.js +327 -0
  80. package/src/api/users/getUserInfoV2.js +134 -0
  81. package/src/core/sendReqMqtt.js +96 -0
  82. package/src/database/models/index.js +87 -0
  83. package/src/database/models/thread.js +45 -0
  84. package/src/database/models/user.js +46 -0
  85. package/src/database/threadData.js +98 -0
  86. package/src/database/userData.js +89 -0
  87. package/src/utils/client.js +320 -0
  88. package/src/utils/constants.js +23 -0
  89. package/{utils.js → src/utils/format.js} +1115 -1447
  90. package/src/utils/headers.js +115 -0
  91. package/src/utils/request.js +305 -0
  92. package/.travis.yml +0 -6
  93. package/src/addExternalModule.js +0 -23
  94. package/src/addUserToGroup.js +0 -113
  95. package/src/changeAdminStatus.js +0 -95
  96. package/src/changeApprovalMode.js +0 -79
  97. package/src/changeArchivedStatus.js +0 -55
  98. package/src/changeBlockedStatus.js +0 -47
  99. package/src/changeBlockedStatusMqtt.js +0 -86
  100. package/src/changeGroupImage.js +0 -133
  101. package/src/changeNickname.js +0 -59
  102. package/src/changeThreadColor.js +0 -71
  103. package/src/changeThreadEmoji.js +0 -55
  104. package/src/createPoll.js +0 -130
  105. package/src/editMessage.js +0 -63
  106. package/src/forwardAttachment.js +0 -60
  107. package/src/forwardMessage.js +0 -63
  108. package/src/getGroupsList.js +0 -83
  109. package/src/getMarketplace.js +0 -84
  110. package/src/getNotifications.js +0 -86
  111. package/src/getPagesList.js +0 -83
  112. package/src/getStories.js +0 -88
  113. package/src/getThreadInfo.js +0 -222
  114. package/src/getUserInfo.js +0 -128
  115. package/src/handleMessageRequest.js +0 -65
  116. package/src/httpGet.js +0 -58
  117. package/src/httpPost.js +0 -58
  118. package/src/listenMqtt.js +0 -1018
  119. package/src/logout.js +0 -75
  120. package/src/markAsSeen.js +0 -61
  121. package/src/pinMessage.js +0 -61
  122. package/src/removeUserFromGroup.js +0 -79
  123. package/src/searchUsers.js +0 -89
  124. package/src/sendComment.js +0 -161
  125. package/src/sendMessage.js +0 -457
  126. package/src/sendTypingIndicator.js +0 -179
  127. package/src/setBio.js +0 -86
  128. package/src/setMessageReaction.js +0 -186
  129. package/src/setPostReaction.js +0 -71
  130. package/src/setTheme.js +0 -313
  131. package/src/setTitle.js +0 -90
  132. package/src/shareLink.js +0 -62
  133. package/src/threadColors.js +0 -57
  134. package/src/unfriend.js +0 -53
  135. package/src/unsendMessage.js +0 -104
@@ -1,292 +1,293 @@
1
- "use strict";
2
-
3
- const utils = require("../utils");
4
- const log = require("npmlog");
5
-
6
- function createProfileUrl(url, username, id) {
7
- if (url) return url;
8
- return (
9
- "https://www.facebook.com/" + (username || utils.formatID(id.toString()))
10
- );
11
- }
12
-
13
- function formatParticipants(participants) {
14
- return participants.edges.map((p) => {
15
- p = p.node.messaging_actor;
16
- switch (p["__typename"]) {
17
- case "User":
18
- return {
19
- accountType: p["__typename"],
20
- userID: utils.formatID(p.id.toString()), // do we need .toString()? when it is not a string?
21
- name: p.name,
22
- shortName: p.short_name,
23
- gender: p.gender,
24
- url: p.url, // how about making it profileURL
25
- profilePicture: p.big_image_src.uri,
26
- username: p.username || null,
27
- // TODO: maybe better names for these?
28
- isViewerFriend: p.is_viewer_friend, // true/false
29
- isMessengerUser: p.is_messenger_user, // true/false
30
- isVerified: p.is_verified, // true/false
31
- isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
32
- isViewerCoworker: p.is_viewer_coworker, // true/false
33
- isEmployee: p.is_employee, // null? when it is something other? can someone check?
34
- };
35
- case "Page":
36
- return {
37
- accountType: p["__typename"],
38
- userID: utils.formatID(p.id.toString()), // or maybe... pageID?
39
- name: p.name,
40
- url: p.url,
41
- profilePicture: p.big_image_src.uri,
42
- username: p.username || null,
43
- // uhm... better names maybe?
44
- acceptsMessengerUserFeedback: p.accepts_messenger_user_feedback, // true/false
45
- isMessengerUser: p.is_messenger_user, // true/false
46
- isVerified: p.is_verified, // true/false
47
- isMessengerPlatformBot: p.is_messenger_platform_bot, // true/false
48
- isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
49
- };
50
- case "ReducedMessagingActor":
51
- return {
52
- accountType: p["__typename"],
53
- userID: utils.formatID(p.id.toString()),
54
- name: p.name,
55
- url: createProfileUrl(p.url, p.username, p.id), // in this case p.url is null all the time
56
- profilePicture: p.big_image_src.uri, // in this case it is default facebook photo, we could determine gender using it
57
- username: p.username || null, // maybe we could use it to generate profile URL?
58
- isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
59
- };
60
- case "UnavailableMessagingActor":
61
- return {
62
- accountType: p["__typename"],
63
- userID: utils.formatID(p.id.toString()),
64
- name: p.name, // "Facebook User" in user's language
65
- url: createProfileUrl(p.url, p.username, p.id), // in this case p.url is null all the time
66
- profilePicture: p.big_image_src.uri, // default male facebook photo
67
- username: p.username || null, // maybe we could use it to generate profile URL?
68
- isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
69
- };
70
- default:
71
- log.warn(
72
- "getThreadList",
73
- "Found participant with unsupported typename. Please open an issue at https://github.com/Schmavery/fca-unofficial/issues\n" +
74
- JSON.stringify(p, null, 2),
75
- );
76
- return {
77
- accountType: p["__typename"],
78
- userID: utils.formatID(p.id.toString()),
79
- name: p.name || `[unknown ${p["__typename"]}]`, // probably it will always be something... but fallback to [unknown], just in case
80
- };
81
- }
82
- });
83
- }
84
-
85
- // "FF8C0077" -> "8C0077"
86
- function formatColor(color) {
87
- if (color && color.match(/^(?:[0-9a-fA-F]{8})$/g)) {
88
- return color.slice(2);
89
- }
90
- return color;
91
- }
92
-
93
- function getThreadName(t) {
94
- if (t.name || t.thread_key.thread_fbid) return t.name;
95
-
96
- for (let po of t.all_participants.edges) {
97
- let p = po.node;
98
- if (p.messaging_actor.id === t.thread_key.other_user_id)
99
- return p.messaging_actor.name;
100
- }
101
- }
102
-
103
- function mapNicknames(customizationInfo) {
104
- return customizationInfo && customizationInfo.participant_customizations
105
- ? customizationInfo.participant_customizations.map((u) => {
106
- return {
107
- userID: u.participant_id,
108
- nickname: u.nickname,
109
- };
110
- })
111
- : [];
112
- }
113
-
114
- function formatThreadList(data) {
115
- return data.map((t) => {
116
- let lastMessageNode =
117
- t.last_message && t.last_message.nodes && t.last_message.nodes.length > 0
118
- ? t.last_message.nodes[0]
119
- : null;
120
- return {
121
- threadID: t.thread_key
122
- ? utils.formatID(t.thread_key.thread_fbid || t.thread_key.other_user_id)
123
- : null, // shall never be null
124
- name: getThreadName(t),
125
- unreadCount: t.unread_count,
126
- messageCount: t.messages_count,
127
- imageSrc: t.image ? t.image.uri : null,
128
- emoji: t.customization_info ? t.customization_info.emoji : null,
129
- color: formatColor(
130
- t.customization_info
131
- ? t.customization_info.outgoing_bubble_color
132
- : null,
133
- ),
134
- nicknames: mapNicknames(t.customization_info),
135
- muteUntil: t.mute_until,
136
- participants: formatParticipants(t.all_participants),
137
- adminIDs: t.thread_admins.map((a) => a.id),
138
- folder: t.folder,
139
- isGroup: t.thread_type === "GROUP",
140
- // rtc_call_data: t.rtc_call_data, // TODO: format and document this
141
- // isPinProtected: t.is_pin_protected, // feature from future? always false (2018-04-04)
142
- customizationEnabled: t.customization_enabled, // false for ONE_TO_ONE with Page or ReducedMessagingActor
143
- participantAddMode: t.participant_add_mode_as_string, // "ADD" if "GROUP" and null if "ONE_TO_ONE"
144
- montageThread: t.montage_thread
145
- ? Buffer.from(t.montage_thread.id, "base64").toString()
146
- : null, // base64 encoded string "message_thread:0000000000000000"
147
- // it is not userID nor any other ID known to me...
148
- // can somebody inspect it? where is it used?
149
- // probably Messenger Day uses it
150
- reactionsMuteMode: t.reactions_mute_mode,
151
- mentionsMuteMode: t.mentions_mute_mode,
152
- isArchived: t.has_viewer_archived,
153
- isSubscribed: t.is_viewer_subscribed,
154
- timestamp: t.updated_time_precise, // in miliseconds
155
- // isCanonicalUser: t.is_canonical_neo_user, // is it always false?
156
- // TODO: how about putting snippet in another object? current implementation does not handle every possibile message type etc.
157
- snippet: lastMessageNode ? lastMessageNode.snippet : null,
158
- snippetAttachments: lastMessageNode
159
- ? lastMessageNode.extensible_attachment
160
- : null, // TODO: not sure if it works
161
- snippetSender: lastMessageNode
162
- ? utils.formatID(
163
- (
164
- lastMessageNode.message_sender.messaging_actor.id || ""
165
- ).toString(),
166
- )
167
- : null,
168
- lastMessageTimestamp: lastMessageNode
169
- ? lastMessageNode.timestamp_precise
170
- : null, // timestamp in miliseconds
171
- lastReadTimestamp:
172
- t.last_read_receipt && t.last_read_receipt.nodes.length > 0
173
- ? t.last_read_receipt.nodes[0]
174
- ? t.last_read_receipt.nodes[0].timestamp_precise
175
- : null
176
- : null, // timestamp in miliseconds
177
- cannotReplyReason: t.cannot_reply_reason, // TODO: inspect possible values
178
- approvalMode: Boolean(t.approval_mode),
179
-
180
- // @Legacy
181
- participantIDs: formatParticipants(t.all_participants).map(
182
- (participant) => participant.userID,
183
- ),
184
- threadType: t.thread_type === "GROUP" ? 2 : 1, // "GROUP" or "ONE_TO_ONE"
185
- };
186
- });
187
- }
188
-
189
- module.exports = function (defaultFuncs, api, ctx) {
190
- return function getThreadList(limit, timestamp, tags, callback) {
191
- if (
192
- !callback &&
193
- (utils.getType(tags) === "Function" ||
194
- utils.getType(tags) === "AsyncFunction")
195
- ) {
196
- callback = tags;
197
- tags = [""];
198
- }
199
- if (
200
- utils.getType(limit) !== "Number" ||
201
- !Number.isInteger(limit) ||
202
- limit <= 0
203
- ) {
204
- throw { error: "getThreadList: limit must be a positive integer" };
205
- }
206
- if (
207
- utils.getType(timestamp) !== "Null" &&
208
- (utils.getType(timestamp) !== "Number" || !Number.isInteger(timestamp))
209
- ) {
210
- throw { error: "getThreadList: timestamp must be an integer or null" };
211
- }
212
- if (utils.getType(tags) === "String") {
213
- tags = [tags];
214
- }
215
- if (utils.getType(tags) !== "Array") {
216
- throw { error: "getThreadList: tags must be an array" };
217
- }
218
-
219
- var resolveFunc = function () {};
220
- var rejectFunc = function () {};
221
- var returnPromise = new Promise(function (resolve, reject) {
222
- resolveFunc = resolve;
223
- rejectFunc = reject;
224
- });
225
-
226
- if (
227
- utils.getType(callback) !== "Function" &&
228
- utils.getType(callback) !== "AsyncFunction"
229
- ) {
230
- callback = function (err, data) {
231
- if (err) {
232
- return rejectFunc(err);
233
- }
234
- resolveFunc(data);
235
- };
236
- }
237
-
238
- const form = {
239
- av: ctx.globalOptions.pageID,
240
- queries: JSON.stringify({
241
- o0: {
242
- // This doc_id was valid on 2020-07-20
243
- doc_id: "3336396659757871",
244
- query_params: {
245
- limit: limit + (timestamp ? 1 : 0),
246
- before: timestamp,
247
- tags: tags,
248
- includeDeliveryReceipts: true,
249
- includeSeqID: false,
250
- },
251
- },
252
- }),
253
- batch_name: "MessengerGraphQLThreadlistFetcher",
254
- };
255
-
256
- defaultFuncs
257
- .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
258
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
259
- .then((resData) => {
260
- if (resData[resData.length - 1].error_results > 0) {
261
- throw resData[0].o0.errors;
262
- }
263
-
264
- if (resData[resData.length - 1].successful_results === 0) {
265
- throw {
266
- error: "getThreadList: there was no successful_results",
267
- res: resData,
268
- };
269
- }
270
-
271
- // When we ask for threads using timestamp from the previous request,
272
- // we are getting the last thread repeated as the first thread in this response.
273
- // .shift() gets rid of it
274
- // It is also the reason for increasing limit by 1 when timestamp is set
275
- // this way user asks for 10 threads, we are asking for 11,
276
- // but after removing the duplicated one, it is again 10
277
- if (timestamp) {
278
- resData[0].o0.data.viewer.message_threads.nodes.shift();
279
- }
280
- callback(
281
- null,
282
- formatThreadList(resData[0].o0.data.viewer.message_threads.nodes),
283
- );
284
- })
285
- .catch((err) => {
286
- log.error("getThreadList", err);
287
- return callback(err);
288
- });
289
-
290
- return returnPromise;
291
- };
292
- };
1
+ "use strict";
2
+
3
+ const log = require("npmlog");
4
+ const { parseAndCheckLogin } = require("../../utils/client");
5
+ const { formatID, getType } = require("../../utils/format");
6
+ function createProfileUrl(url, username, id) {
7
+ if (url) return url;
8
+ return (
9
+ "https://www.facebook.com/" + (username || formatID(id.toString()))
10
+ );
11
+ }
12
+
13
+ function formatParticipants(participants) {
14
+ return participants.edges.map(p => {
15
+ p = p.node.messaging_actor;
16
+ switch (p["__typename"]) {
17
+ case "User":
18
+ return {
19
+ accountType: p["__typename"],
20
+ userID: formatID(p.id.toString()), // do we need .toString()? when it is not a string?
21
+ name: p.name,
22
+ shortName: p.short_name,
23
+ gender: p.gender,
24
+ url: p.url, // how about making it profileURL
25
+ profilePicture: p.big_image_src.uri,
26
+ username: p.username || null,
27
+ // TODO: maybe better names for these?
28
+ isViewerFriend: p.is_viewer_friend, // true/false
29
+ isMessengerUser: p.is_messenger_user, // true/false
30
+ isVerified: p.is_verified, // true/false
31
+ isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
32
+ isViewerCoworker: p.is_viewer_coworker, // true/false
33
+ isEmployee: p.is_employee // null? when it is something other? can someone check?
34
+ };
35
+ case "Page":
36
+ return {
37
+ accountType: p["__typename"],
38
+ userID: formatID(p.id.toString()), // or maybe... pageID?
39
+ name: p.name,
40
+ url: p.url,
41
+ profilePicture: p.big_image_src.uri,
42
+ username: p.username || null,
43
+ // uhm... better names maybe?
44
+ acceptsMessengerUserFeedback: p.accepts_messenger_user_feedback, // true/false
45
+ isMessengerUser: p.is_messenger_user, // true/false
46
+ isVerified: p.is_verified, // true/false
47
+ isMessengerPlatformBot: p.is_messenger_platform_bot, // true/false
48
+ isMessageBlockedByViewer: p.is_message_blocked_by_viewer // true/false
49
+ };
50
+ case "ReducedMessagingActor":
51
+ case "UnavailableMessagingActor":
52
+ return {
53
+ accountType: p["__typename"],
54
+ userID: formatID(p.id.toString()),
55
+ name: p.name,
56
+ url: createProfileUrl(p.url, p.username, p.id), // in this case p.url is null all the time
57
+ profilePicture: p.big_image_src.uri, // in this case it is default facebook photo, we could determine gender using it
58
+ username: p.username || null, // maybe we could use it to generate profile URL?
59
+ isMessageBlockedByViewer: p.is_message_blocked_by_viewer // true/false
60
+ };
61
+ default:
62
+ log.warn(
63
+ "getThreadList",
64
+ "Found participant with unsupported typename. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues\n" +
65
+ JSON.stringify(p, null, 2)
66
+ );
67
+ return {
68
+ accountType: p["__typename"],
69
+ userID: formatID(p.id.toString()),
70
+ name: p.name || `[unknown ${p["__typename"]}]` // probably it will always be something... but fallback to [unknown], just in case
71
+ };
72
+ }
73
+ });
74
+ }
75
+
76
+ // "FF8C0077" -> "8C0077"
77
+ function formatColor(color) {
78
+ if (color && color.match(/^(?:[0-9a-fA-F]{8})$/g)) return color.slice(2);
79
+ return color;
80
+ }
81
+
82
+ function getThreadName(t) {
83
+ if (t.name || t.thread_key.thread_fbid) return t.name;
84
+
85
+ for (let po of t.all_participants.edges) {
86
+ let p = po.node;
87
+ if (p.messaging_actor.id === t.thread_key.other_user_id)
88
+ return p.messaging_actor.name;
89
+ }
90
+ }
91
+
92
+ function mapNicknames(customizationInfo) {
93
+ return customizationInfo && customizationInfo.participant_customizations
94
+ ? customizationInfo.participant_customizations.map(u => {
95
+ return {
96
+ userID: u.participant_id,
97
+ nickname: u.nickname
98
+ };
99
+ })
100
+ : [];
101
+ }
102
+
103
+ function formatThreadList(data) {
104
+ return data.map(t => {
105
+ let lastMessageNode =
106
+ t.last_message && t.last_message.nodes && t.last_message.nodes.length > 0
107
+ ? t.last_message.nodes[0]
108
+ : null;
109
+ return {
110
+ threadID: t.thread_key
111
+ ? formatID(t.thread_key.thread_fbid || t.thread_key.other_user_id)
112
+ : null, // shall never be null
113
+ name: getThreadName(t),
114
+ unreadCount: t.unread_count,
115
+ messageCount: t.messages_count,
116
+ imageSrc: t.image ? t.image.uri : null,
117
+ emoji: t.customization_info ? t.customization_info.emoji : null,
118
+ color: formatColor(
119
+ t.customization_info ? t.customization_info.outgoing_bubble_color : null
120
+ ),
121
+ threadTheme: t.thread_theme,
122
+ nicknames: mapNicknames(t.customization_info),
123
+ muteUntil: t.mute_until,
124
+ participants: formatParticipants(t.all_participants),
125
+ adminIDs: t.thread_admins.map(a => a.id),
126
+ folder: t.folder,
127
+ isGroup: t.thread_type === "GROUP",
128
+ customizationEnabled: t.customization_enabled, // false for ONE_TO_ONE with Page or ReducedMessagingActor
129
+ participantAddMode: t.participant_add_mode_as_string, // "ADD" if "GROUP" and null if "ONE_TO_ONE"
130
+ montageThread: t.montage_thread
131
+ ? Buffer.from(t.montage_thread.id, "base64").toString()
132
+ : null, // base64 encoded string "message_thread:0000000000000000"
133
+ reactionsMuteMode: t.reactions_mute_mode,
134
+ mentionsMuteMode: t.mentions_mute_mode,
135
+ isArchived: t.has_viewer_archived,
136
+ isSubscribed: t.is_viewer_subscribed,
137
+ timestamp: t.updated_time_precise, // in miliseconds
138
+ snippet: lastMessageNode ? lastMessageNode.snippet : null,
139
+ snippetAttachments: lastMessageNode
140
+ ? lastMessageNode.extensible_attachment
141
+ : null, // TODO: not sure if it works
142
+ snippetSender: lastMessageNode
143
+ ? formatID(
144
+ (lastMessageNode.message_sender.messaging_actor.id || "").toString()
145
+ )
146
+ : null,
147
+ lastMessageTimestamp: lastMessageNode
148
+ ? lastMessageNode.timestamp_precise
149
+ : null, // timestamp in miliseconds
150
+ lastReadTimestamp:
151
+ t.last_read_receipt && t.last_read_receipt.nodes.length > 0
152
+ ? t.last_read_receipt.nodes[0]
153
+ ? t.last_read_receipt.nodes[0].timestamp_precise
154
+ : null
155
+ : null,
156
+ cannotReplyReason: t.cannot_reply_reason,
157
+ approvalMode: Boolean(t.approval_mode),
158
+ participantIDs: formatParticipants(t.all_participants).map(
159
+ participant => participant.userID
160
+ ),
161
+ threadType: t.thread_type === "GROUP" ? 2 : 1, // "GROUP" or "ONE_TO_ONE"
162
+ inviteLink: {
163
+ enable: t.joinable_mode ? t.joinable_mode.mode == 1 : false,
164
+ link: t.joinable_mode ? t.joinable_mode.link : null
165
+ }
166
+ };
167
+ });
168
+ }
169
+
170
+ module.exports = function(defaultFuncs, api, ctx) {
171
+ return function getThreadList(limit, timestamp, tags, callback) {
172
+ if (
173
+ !callback &&
174
+ (getType(tags) === "Function" ||
175
+ getType(tags) === "AsyncFunction")
176
+ ) {
177
+ callback = tags;
178
+ tags = [""];
179
+ }
180
+ if (
181
+ getType(limit) !== "Number" ||
182
+ !Number.isInteger(limit) ||
183
+ limit <= 0
184
+ )
185
+ throw { error: "getThreadList: limit must be a positive integer" };
186
+ if (
187
+ getType(timestamp) !== "Null" &&
188
+ (getType(timestamp) !== "Number" || !Number.isInteger(timestamp))
189
+ )
190
+ throw { error: "getThreadList: timestamp must be an integer or null" };
191
+ if (getType(tags) === "String") tags = [tags];
192
+ if (getType(tags) !== "Array")
193
+ throw { error: "getThreadList: tags must be an array" };
194
+ var resolveFunc = function() {};
195
+ var rejectFunc = function() {};
196
+ var returnPromise = new Promise(function(resolve, reject) {
197
+ resolveFunc = resolve;
198
+ rejectFunc = reject;
199
+ });
200
+ if (
201
+ getType(callback) !== "Function" &&
202
+ getType(callback) !== "AsyncFunction"
203
+ ) {
204
+ callback = function(err, data) {
205
+ if (err) return rejectFunc(err);
206
+ resolveFunc(data);
207
+ };
208
+ }
209
+ const form = {
210
+ av: ctx.userID,
211
+ queries: JSON.stringify({
212
+ o0: {
213
+ doc_id: "3336396659757871",
214
+ query_params: {
215
+ limit: limit + (timestamp ? 1 : 0),
216
+ before: timestamp,
217
+ tags: tags,
218
+ includeDeliveryReceipts: true,
219
+ includeSeqID: false
220
+ }
221
+ }
222
+ }),
223
+ batch_name: "MessengerGraphQLThreadlistFetcher"
224
+ };
225
+ defaultFuncs
226
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
227
+ .then(parseAndCheckLogin(ctx, defaultFuncs))
228
+ .then(resData => {
229
+ // Validate resData is an array and has elements
230
+ if (!resData || !Array.isArray(resData) || resData.length === 0) {
231
+ throw {
232
+ error: "getThreadList: Invalid response data - resData is not a valid array",
233
+ res: resData
234
+ };
235
+ }
236
+
237
+ // Validate last element exists and has required properties
238
+ const lastElement = resData[resData.length - 1];
239
+ if (!lastElement || typeof lastElement !== "object") {
240
+ throw {
241
+ error: "getThreadList: Invalid response data - last element is missing or invalid",
242
+ res: resData
243
+ };
244
+ }
245
+
246
+ if (lastElement.error_results > 0) {
247
+ // Check if first element and o0 exist before accessing errors
248
+ if (resData[0] && resData[0].o0 && resData[0].o0.errors) {
249
+ throw resData[0].o0.errors;
250
+ } else {
251
+ throw {
252
+ error: "getThreadList: Error results > 0 but error details not available",
253
+ res: resData
254
+ };
255
+ }
256
+ }
257
+
258
+ if (lastElement.successful_results === 0) {
259
+ throw {
260
+ error: "getThreadList: there was no successful_results",
261
+ res: resData
262
+ };
263
+ }
264
+
265
+ // Validate first element and nested data structure
266
+ if (!resData[0] || !resData[0].o0 || !resData[0].o0.data ||
267
+ !resData[0].o0.data.viewer || !resData[0].o0.data.viewer.message_threads ||
268
+ !Array.isArray(resData[0].o0.data.viewer.message_threads.nodes)) {
269
+ throw {
270
+ error: "getThreadList: Invalid response data structure - missing required fields",
271
+ res: resData
272
+ };
273
+ }
274
+
275
+ if (timestamp) {
276
+ const nodes = resData[0].o0.data.viewer.message_threads.nodes;
277
+ if (Array.isArray(nodes) && nodes.length > 0) {
278
+ nodes.shift();
279
+ }
280
+ }
281
+
282
+ callback(
283
+ null,
284
+ formatThreadList(resData[0].o0.data.viewer.message_threads.nodes)
285
+ );
286
+ })
287
+ .catch(err => {
288
+ log.error("getThreadList", err);
289
+ return callback(err);
290
+ });
291
+ return returnPromise;
292
+ };
293
+ };