fca-neokex-fix 1.0.1

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 (114) hide show
  1. package/CHANGELOG.md +220 -0
  2. package/LICENSE +26 -0
  3. package/README.md +346 -0
  4. package/THEME_FEATURES.md +137 -0
  5. package/examples/README.md +131 -0
  6. package/examples/apply-ai-theme.js +127 -0
  7. package/examples/check-current-theme.js +74 -0
  8. package/examples/simple-bot.js +114 -0
  9. package/examples/test-bot.js +752 -0
  10. package/examples/test-logging.js +85 -0
  11. package/examples/theme-usage-example.js +53 -0
  12. package/index.js +2 -0
  13. package/package.json +105 -0
  14. package/src/apis/addExternalModule.js +24 -0
  15. package/src/apis/addUserToGroup.js +108 -0
  16. package/src/apis/changeAdminStatus.js +148 -0
  17. package/src/apis/changeArchivedStatus.js +61 -0
  18. package/src/apis/changeAvatar.js +103 -0
  19. package/src/apis/changeBio.js +69 -0
  20. package/src/apis/changeBlockedStatus.js +54 -0
  21. package/src/apis/changeGroupImage.js +136 -0
  22. package/src/apis/changeThreadColor.js +116 -0
  23. package/src/apis/comment.js +207 -0
  24. package/src/apis/createAITheme.js +129 -0
  25. package/src/apis/createNewGroup.js +79 -0
  26. package/src/apis/createPoll.js +73 -0
  27. package/src/apis/deleteMessage.js +44 -0
  28. package/src/apis/deleteThread.js +52 -0
  29. package/src/apis/editMessage.js +70 -0
  30. package/src/apis/emoji.js +124 -0
  31. package/src/apis/fetchThemeData.js +65 -0
  32. package/src/apis/follow.js +81 -0
  33. package/src/apis/forwardMessage.js +52 -0
  34. package/src/apis/friend.js +243 -0
  35. package/src/apis/gcmember.js +122 -0
  36. package/src/apis/gcname.js +123 -0
  37. package/src/apis/gcrule.js +119 -0
  38. package/src/apis/getAccess.js +111 -0
  39. package/src/apis/getBotInfo.js +88 -0
  40. package/src/apis/getBotInitialData.js +43 -0
  41. package/src/apis/getFriendsList.js +79 -0
  42. package/src/apis/getMessage.js +423 -0
  43. package/src/apis/getTheme.js +104 -0
  44. package/src/apis/getThemeInfo.js +96 -0
  45. package/src/apis/getThreadHistory.js +239 -0
  46. package/src/apis/getThreadInfo.js +257 -0
  47. package/src/apis/getThreadList.js +222 -0
  48. package/src/apis/getThreadPictures.js +58 -0
  49. package/src/apis/getUserID.js +83 -0
  50. package/src/apis/getUserInfo.js +495 -0
  51. package/src/apis/getUserInfoV2.js +146 -0
  52. package/src/apis/handleMessageRequest.js +50 -0
  53. package/src/apis/httpGet.js +63 -0
  54. package/src/apis/httpPost.js +89 -0
  55. package/src/apis/httpPostFormData.js +69 -0
  56. package/src/apis/listenMqtt.js +796 -0
  57. package/src/apis/listenSpeed.js +170 -0
  58. package/src/apis/logout.js +63 -0
  59. package/src/apis/markAsDelivered.js +47 -0
  60. package/src/apis/markAsRead.js +95 -0
  61. package/src/apis/markAsReadAll.js +41 -0
  62. package/src/apis/markAsSeen.js +70 -0
  63. package/src/apis/mqttDeltaValue.js +330 -0
  64. package/src/apis/muteThread.js +45 -0
  65. package/src/apis/nickname.js +132 -0
  66. package/src/apis/notes.js +163 -0
  67. package/src/apis/pinMessage.js +141 -0
  68. package/src/apis/produceMetaTheme.js +180 -0
  69. package/src/apis/realtime.js +161 -0
  70. package/src/apis/removeUserFromGroup.js +117 -0
  71. package/src/apis/resolvePhotoUrl.js +58 -0
  72. package/src/apis/searchForThread.js +154 -0
  73. package/src/apis/sendMessage.js +281 -0
  74. package/src/apis/sendMessageMqtt.js +188 -0
  75. package/src/apis/sendTypingIndicator.js +41 -0
  76. package/src/apis/setMessageReaction.js +27 -0
  77. package/src/apis/setMessageReactionMqtt.js +61 -0
  78. package/src/apis/setThreadTheme.js +260 -0
  79. package/src/apis/setThreadThemeMqtt.js +94 -0
  80. package/src/apis/share.js +107 -0
  81. package/src/apis/shareContact.js +66 -0
  82. package/src/apis/stickers.js +257 -0
  83. package/src/apis/story.js +181 -0
  84. package/src/apis/theme.js +233 -0
  85. package/src/apis/unfriend.js +47 -0
  86. package/src/apis/unsendMessage.js +17 -0
  87. package/src/database/appStateBackup.js +189 -0
  88. package/src/database/models/index.js +56 -0
  89. package/src/database/models/thread.js +31 -0
  90. package/src/database/models/user.js +32 -0
  91. package/src/database/threadData.js +101 -0
  92. package/src/database/userData.js +90 -0
  93. package/src/engine/client.js +91 -0
  94. package/src/engine/models/buildAPI.js +109 -0
  95. package/src/engine/models/loginHelper.js +326 -0
  96. package/src/engine/models/setOptions.js +53 -0
  97. package/src/utils/auth-helpers.js +149 -0
  98. package/src/utils/autoReLogin.js +169 -0
  99. package/src/utils/axios.js +290 -0
  100. package/src/utils/clients.js +270 -0
  101. package/src/utils/constants.js +396 -0
  102. package/src/utils/formatters/data/formatAttachment.js +370 -0
  103. package/src/utils/formatters/data/formatDelta.js +153 -0
  104. package/src/utils/formatters/index.js +159 -0
  105. package/src/utils/formatters/value/formatCookie.js +91 -0
  106. package/src/utils/formatters/value/formatDate.js +36 -0
  107. package/src/utils/formatters/value/formatID.js +16 -0
  108. package/src/utils/formatters.js +1067 -0
  109. package/src/utils/headers.js +199 -0
  110. package/src/utils/index.js +151 -0
  111. package/src/utils/monitoring.js +358 -0
  112. package/src/utils/rateLimiter.js +380 -0
  113. package/src/utils/tokenRefresh.js +311 -0
  114. package/src/utils/user-agents.js +238 -0
@@ -0,0 +1,257 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Sticker API Module
5
+ * Provides access to Facebook's GraphQL-based sticker endpoints.
6
+ * Made by @ChoruOfficial
7
+ */
8
+
9
+ const utils = require('../utils');
10
+ /**
11
+ * Format the sticker pack list (store or tray)
12
+ * @param {object} data - Raw GraphQL response
13
+ * @returns {{ packs: Array<{id: string, name: string, thumbnail: string}>, page_info: object, store_id?: string }}
14
+ */
15
+ function formatPackList(data) {
16
+ const trayPacks = data?.data?.picker_plugins?.sticker_picker?.sticker_store?.tray_packs?.edges;
17
+ const storePacks = data?.data?.viewer?.sticker_store?.available_packs?.edges;
18
+
19
+ const packData = storePacks || trayPacks;
20
+ if (!packData || !packData.edges) return { packs: [], page_info: { has_next_page: false } };
21
+
22
+ const formattedPacks = packData.edges.map(edge => edge.node ? ({
23
+ id: edge.node.id,
24
+ name: edge.node.name,
25
+ thumbnail: edge.node.thumbnail_image?.uri
26
+ }) : null).filter(Boolean);
27
+
28
+ return {
29
+ packs: formattedPacks,
30
+ page_info: packData.page_info,
31
+ store_id: data?.data?.viewer?.sticker_store?.id
32
+ };
33
+ }
34
+
35
+ /**
36
+ * Format search result stickers
37
+ * @param {object} data - Raw GraphQL response
38
+ * @returns {Array<Object>}
39
+ */
40
+ function formatStickerSearchResults(data) {
41
+ const stickers = data?.data?.sticker_search?.sticker_results?.edges;
42
+ if (!stickers) return [];
43
+ return stickers.map(edge => edge.node ? ({
44
+ type: "sticker",
45
+ ID: edge.node.id,
46
+ url: edge.node.image?.uri,
47
+ animatedUrl: edge.node.animated_image?.uri,
48
+ packID: edge.node.pack?.id,
49
+ label: edge.node.label || edge.node.accessibility_label,
50
+ stickerID: edge.node.id
51
+ }) : null).filter(Boolean);
52
+ }
53
+
54
+ /**
55
+ * Format sticker pack content
56
+ * @param {object} data - Raw GraphQL response
57
+ * @returns {Array<Object>}
58
+ */
59
+ function formatStickerPackResults(data) {
60
+ const stickers = data?.data?.sticker_pack?.stickers?.edges;
61
+ if (!stickers) return [];
62
+ return stickers.map(edge => edge.node ? ({
63
+ type: "sticker",
64
+ ID: edge.node.id,
65
+ url: edge.node.image?.uri,
66
+ animatedUrl: edge.node.animated_image?.uri,
67
+ packID: edge.node.pack?.id,
68
+ label: edge.node.label || edge.node.accessibility_label,
69
+ stickerID: edge.node.id
70
+ }) : null).filter(Boolean);
71
+ }
72
+
73
+ /**
74
+ * Format AI-generated stickers
75
+ * @param {object} data - Raw GraphQL response
76
+ * @returns {Array<Object>}
77
+ */
78
+ function formatAiStickers(data) {
79
+ const stickers = data?.data?.xfb_trending_generated_ai_stickers?.nodes;
80
+ if (!stickers) return [];
81
+ return stickers.map(node => ({
82
+ type: "sticker",
83
+ ID: node.id,
84
+ url: node.url,
85
+ label: node.label,
86
+ stickerID: node.id
87
+ })).filter(Boolean);
88
+ }
89
+
90
+ module.exports = function(defaultFuncs, api, ctx) {
91
+ /**
92
+ * Make a GraphQL request and handle login and error checking
93
+ * @param {object} form - Form data for the request
94
+ * @returns {Promise<object>}
95
+ */
96
+ async function makeRequest(form) {
97
+ const resData = await defaultFuncs.post("https://www.facebook.com/api/graphql/", ctx.jar, form)
98
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs));
99
+ if (!resData) throw new Error("GraphQL request returned no data.");
100
+ if (resData.errors) {
101
+ utils.error("StickerAPI GraphQL Error", resData.errors[0].message);
102
+ throw resData.errors[0];
103
+ }
104
+ return resData;
105
+ }
106
+
107
+ return {
108
+ /**
109
+ * Search for stickers by keyword
110
+ * @param {string} query - Search term
111
+ * @returns {Promise<Array<Object>>}
112
+ */
113
+ search: async function(query) {
114
+ const form = {
115
+ fb_api_caller_class: 'RelayModern',
116
+ fb_api_req_friendly_name: 'CometStickerPickerSearchResultsRootQuery',
117
+ variables: JSON.stringify({
118
+ scale: 3,
119
+ search_query: query,
120
+ sticker_height: 128,
121
+ sticker_width: 128,
122
+ stickerInterface: "MESSAGES"
123
+ }),
124
+ doc_id: '24004987559125954'
125
+ };
126
+ const res = await makeRequest(form);
127
+ return formatStickerSearchResults(res);
128
+ },
129
+
130
+ /**
131
+ * List user's sticker packs
132
+ * @returns {Promise<Array<Object>>}
133
+ */
134
+ listPacks: async function() {
135
+ const form = {
136
+ fb_api_caller_class: 'RelayModern',
137
+ fb_api_req_friendly_name: 'CometStickerPickerCardQuery',
138
+ variables: JSON.stringify({ scale: 3, stickerInterface: "MESSAGES" }),
139
+ doc_id: '10095807770482952'
140
+ };
141
+ const res = await makeRequest(form);
142
+ return formatPackList(res).packs;
143
+ },
144
+
145
+ /**
146
+ * Get all available sticker packs from the store (with pagination)
147
+ * @returns {Promise<Array<Object>>}
148
+ */
149
+ getStorePacks: async function() {
150
+ utils.log("Starting to fetch all sticker packs from store...");
151
+ let allPacks = [];
152
+
153
+ const initialForm = {
154
+ fb_api_caller_class: 'RelayModern',
155
+ fb_api_req_friendly_name: 'CometStickersStoreDialogQuery',
156
+ variables: JSON.stringify({}),
157
+ doc_id: '29237828849196584'
158
+ };
159
+ let res = await makeRequest(initialForm);
160
+ let { packs, page_info, store_id } = formatPackList(res);
161
+ allPacks.push(...packs);
162
+ utils.log(`Fetched first page with ${packs.length} packs.`);
163
+
164
+ while (page_info && page_info.has_next_page) {
165
+ utils.log("Fetching next page with cursor:", page_info.end_cursor);
166
+
167
+ const paginatedForm = {
168
+ fb_api_caller_class: 'RelayModern',
169
+ fb_api_req_friendly_name: 'CometStickersStorePackListPaginationQuery',
170
+ variables: JSON.stringify({
171
+ count: 20,
172
+ cursor: page_info.end_cursor,
173
+ id: store_id
174
+ }),
175
+ doc_id: '9898634630218439'
176
+ };
177
+ res = await makeRequest(paginatedForm);
178
+ let paginatedResult = formatPackList(res);
179
+ allPacks.push(...paginatedResult.packs);
180
+ page_info = paginatedResult.page_info;
181
+ utils.log(`Fetched ${paginatedResult.packs.length} more packs. Total now: ${allPacks.length}`);
182
+ }
183
+
184
+ utils.log(`Finished fetching. Total unique packs found: ${allPacks.length}`);
185
+ return allPacks;
186
+ },
187
+
188
+ /**
189
+ * Merge user's and store sticker packs into one list
190
+ * @returns {Promise<Array<Object>>}
191
+ */
192
+ listAllPacks: async function() {
193
+ const [myPacks, storePacks] = await Promise.all([
194
+ this.listPacks(),
195
+ this.getStorePacks()
196
+ ]);
197
+ const allPacksMap = new Map();
198
+ myPacks.forEach(pack => allPacksMap.set(pack.id, pack));
199
+ storePacks.forEach(pack => allPacksMap.set(pack.id, pack));
200
+ return Array.from(allPacksMap.values());
201
+ },
202
+
203
+ /**
204
+ * Add a sticker pack by ID
205
+ * @param {string} packID - The ID of the sticker pack
206
+ * @returns {Promise<Object>}
207
+ */
208
+ addPack: async function(packID) {
209
+ const form = {
210
+ fb_api_caller_class: 'RelayModern',
211
+ fb_api_req_friendly_name: 'CometStickersStorePackMutationAddMutation',
212
+ variables: JSON.stringify({
213
+ input: {
214
+ pack_id: packID,
215
+ actor_id: ctx.userID,
216
+ client_mutation_id: Math.round(Math.random() * 10).toString()
217
+ }
218
+ }),
219
+ doc_id: '9877489362345320'
220
+ };
221
+ const res = await makeRequest(form);
222
+ return res.data.sticker_pack_add.sticker_pack;
223
+ },
224
+
225
+ /**
226
+ * Get all stickers in a pack
227
+ * @param {string} packID - Sticker pack ID
228
+ * @returns {Promise<Array<Object>>}
229
+ */
230
+ getStickersInPack: async function(packID) {
231
+ const form = {
232
+ fb_api_caller_class: 'RelayModern',
233
+ fb_api_req_friendly_name: 'CometStickerPickerPackContentRootQuery',
234
+ variables: JSON.stringify({ packID, stickerWidth: 128, stickerHeight: 128, scale: 3 }),
235
+ doc_id: '23982341384707469'
236
+ };
237
+ const res = await makeRequest(form);
238
+ return formatStickerPackResults(res);
239
+ },
240
+
241
+ /**
242
+ * Get trending AI-generated stickers
243
+ * @param {{ limit?: number }} options - Options object
244
+ * @returns {Promise<Array<Object>>}
245
+ */
246
+ getAiStickers: async function({ limit = 10 } = {}) {
247
+ const form = {
248
+ fb_api_caller_class: 'RelayModern',
249
+ fb_api_req_friendly_name: 'CometStickerPickerStickerGeneratedCardQuery',
250
+ variables: JSON.stringify({ limit }),
251
+ doc_id: '24151467751156443'
252
+ };
253
+ const res = await makeRequest(form);
254
+ return formatAiStickers(res);
255
+ }
256
+ };
257
+ };
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+ const { URL } = require('url');
5
+
6
+ /**
7
+ * @namespace api.story
8
+ * @description A collection of functions for interacting with Facebook Stories.
9
+ * @license Ex-it
10
+ * @author Jonell Magallanes, ChoruOfficial
11
+ */
12
+ module.exports = function (defaultFuncs, api, ctx) {
13
+
14
+ /**
15
+ * (Internal) Extracts the Story ID from a Facebook story URL.
16
+ * @param {string} url The Facebook story URL.
17
+ * @returns {string|null} The extracted Story ID or null if not found.
18
+ */
19
+ function getStoryIDFromURL(url) {
20
+ try {
21
+ const urlObject = new URL(url);
22
+ const pathParts = urlObject.pathname.split('/');
23
+ const storiesIndex = pathParts.indexOf('stories');
24
+ if (storiesIndex !== -1 && pathParts.length > storiesIndex + 2) {
25
+ return pathParts[storiesIndex + 2];
26
+ }
27
+ return null;
28
+ } catch (e) {
29
+ return null;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * (Internal) The core function to send a reply or reaction to a story.
35
+ * @param {string} storyIdOrUrl The ID or URL of the story.
36
+ * @param {string} message The text message or emoji reaction.
37
+ * @param {boolean} isReaction True if the reply is a lightweight reaction.
38
+ * @returns {Promise<object>} The server's response.
39
+ */
40
+ async function sendStoryReply(storyIdOrUrl, message, isReaction) {
41
+ try {
42
+ const allowedReactions = ["❤️", "👍", "🤗", "😆", "😡", "😢", "😮"];
43
+
44
+ if (!storyIdOrUrl) throw new Error("Story ID or URL is required.");
45
+ if (!message) throw new Error("A message or reaction is required.");
46
+
47
+ let storyID = getStoryIDFromURL(storyIdOrUrl);
48
+ if (!storyID) storyID = storyIdOrUrl;
49
+
50
+ const variables = {
51
+ input: {
52
+ attribution_id_v2: "StoriesCometSuspenseRoot.react,comet.stories.viewer,via_cold_start",
53
+ message: message,
54
+ story_id: storyID,
55
+ story_reply_type: isReaction ? "LIGHT_WEIGHT" : "TEXT",
56
+ actor_id: ctx.userID,
57
+ client_mutation_id: Math.floor(Math.random() * 10 + 1).toString()
58
+ }
59
+ };
60
+
61
+ if (isReaction) {
62
+ if (!allowedReactions.includes(message)) {
63
+ throw new Error(`Invalid reaction. Please use one of: ${allowedReactions.join(" ")}`);
64
+ }
65
+ variables.input.lightweight_reaction_actions = {
66
+ offsets: [0],
67
+ reaction: message,
68
+ };
69
+ }
70
+
71
+ const form = {
72
+ av: ctx.userID,
73
+ __user: ctx.userID,
74
+ __a: "1",
75
+ fb_dtsg: ctx.fb_dtsg,
76
+ jazoest: ctx.jazoest,
77
+ fb_api_caller_class: "RelayModern",
78
+ fb_api_req_friendly_name: "useStoriesSendReplyMutation",
79
+ variables: JSON.stringify(variables),
80
+ doc_id: "9697491553691692"
81
+ };
82
+
83
+ const res = await defaultFuncs.post("https://www.facebook.com/api/graphql/", ctx.jar, form, {});
84
+ if (res.data.errors) throw new Error(JSON.stringify(res.data.errors));
85
+
86
+ const storyReplyData = res.data?.data?.direct_message_reply;
87
+ if (!storyReplyData) throw new Error("Could not find 'direct_message_reply' in the response data.");
88
+
89
+ return { success: true, result: storyReplyData };
90
+ } catch (err) {
91
+ console.error("Error in story reply API:", err);
92
+ throw err;
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Creates a new text-based story.
98
+ * @param {string} message The text content of the story.
99
+ * @param {string} [fontName="classic"] The name of the font to use. Options: `headline`, `classic`, `casual`, `fancy`.
100
+ * @param {string} [backgroundName="blue"] The name of the background to use. Options: `orange`, `blue`, `green`, `modern`.
101
+ * @returns {Promise<{success: boolean, storyID: string}>} A promise that resolves with the new story's ID.
102
+ */
103
+ async function create(message, fontName = "classic", backgroundName = "blue") {
104
+ const fontMap = {
105
+ headline: "1919119914775364",
106
+ classic: "516266749248495",
107
+ casual: "516266749248495",
108
+ fancy: "1790435664339626"
109
+ };
110
+ const bgMap = {
111
+ orange: "2163607613910521",
112
+ blue: "401372137331149",
113
+ green: "367314917184744",
114
+ modern: "554617635055752"
115
+ };
116
+
117
+ const fontId = fontMap[fontName.toLowerCase()] || fontMap.classic;
118
+ const bgId = bgMap[backgroundName.toLowerCase()] || bgMap.blue;
119
+
120
+ const variables = {
121
+ input: {
122
+ audiences: [{ stories: { self: { target_id: ctx.userID } } }],
123
+ audiences_is_complete: true,
124
+ logging: { composer_session_id: "createStoriesText-" + Date.now() },
125
+ navigation_data: { attribution_id_v2: "StoriesCreateRoot.react,comet.stories.create" },
126
+ source: "WWW",
127
+ message: { ranges: [], text: message },
128
+ text_format_metadata: { inspirations_custom_font_id: fontId },
129
+ text_format_preset_id: bgId,
130
+ tracking: [null],
131
+ actor_id: ctx.userID,
132
+ client_mutation_id: "2"
133
+ }
134
+ };
135
+
136
+ const form = {
137
+ __a: "1",
138
+ fb_api_caller_class: "RelayModern",
139
+ fb_api_req_friendly_name: "StoriesCreateMutation",
140
+ variables: JSON.stringify(variables),
141
+ doc_id: "24226878183562473"
142
+ };
143
+
144
+ try {
145
+ const res = await defaultFuncs.post("https://www.facebook.com/api/graphql/", ctx.jar, form, {});
146
+ if (res.data.errors) throw new Error(JSON.stringify(res.data.errors));
147
+
148
+ const storyNode = res.data?.data?.story_create?.viewer?.actor?.story_bucket?.nodes[0]?.first_story_to_show;
149
+ if (!storyNode || !storyNode.id) throw new Error("Could not find the storyCardID in the response.");
150
+
151
+ return { success: true, storyID: storyNode.id };
152
+ } catch (error) {
153
+ throw error;
154
+ }
155
+ }
156
+
157
+ return {
158
+ /**
159
+ * Creates a new text-based story.
160
+ * @param {string} message The text content of the story.
161
+ * @param {string} [fontName="classic"] The font to use (`headline`, `classic`, `fancy`).
162
+ * @param {string} [backgroundName="blue"] The background to use (`orange`, `blue`, `green`, `modern`).
163
+ * @returns {Promise<{success: boolean, storyID: string}>}
164
+ */
165
+ create,
166
+ /**
167
+ * Reacts to a story with a specific emoji.
168
+ * @param {string} storyIdOrUrl The ID or full URL of the story to react to.
169
+ * @param {string} reaction The emoji to react with. Must be one of: ❤️, 👍, 🤗, 😆, 😡, 😢, 😮.
170
+ * @returns {Promise<{success: boolean, result: object}>}
171
+ */
172
+ react: (storyIdOrUrl, reaction) => sendStoryReply(storyIdOrUrl, reaction, true),
173
+ /**
174
+ * Sends a text message reply to a story.
175
+ * @param {string} storyIdOrUrl The ID or full URL of the story to reply to.
176
+ * @param {string} message The text message to send.
177
+ * @returns {Promise<{success: boolean, result: object}>}
178
+ */
179
+ msg: (storyIdOrUrl, message) => sendStoryReply(storyIdOrUrl, message, false)
180
+ };
181
+ };
@@ -0,0 +1,233 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+ module.exports = function (defaultFuncs, api, ctx) {
6
+ /**
7
+ * Made by Choru Official
8
+ * Mqtt & Graph
9
+ * Manages or sets the custom theme for a Facebook thread.
10
+ * If only a theme name/keyword is provided, it attempts to find and set the matching theme.
11
+ * If "list" is provided as the themeName, it lists available themes.
12
+ *
13
+ * @param {string} themeName The name or partial name of the theme (case-insensitive), or "list" to list themes.
14
+ * @param {string} threadID The ID of the thread.
15
+ * @param {Function} [callback] Optional callback function.
16
+ * @param {string} [initiatorID] The ID of the user who initiated the theme change (e.g., from event.senderID).
17
+ * @returns {Promise<void|Array<object>|object>} A promise that resolves on success (for setting theme, with a detailed event object), or with an array of themes (for listing), or rejects on error.
18
+ */
19
+ return async function theme(themeName, threadID, callback, initiatorID) {
20
+ let _callback;
21
+ let _initiatorID;
22
+
23
+ let _resolveFunc;
24
+ let _rejectFunc;
25
+ const finalReturnPromise = new Promise((resolve, reject) => {
26
+ _resolveFunc = resolve;
27
+ _rejectFunc = reject;
28
+ });
29
+
30
+ if (utils.getType(callback) === "Function" || utils.getType(callback) === "AsyncFunction") {
31
+ _callback = callback;
32
+ _initiatorID = initiatorID;
33
+ } else if (utils.getType(threadID) === "Function" || utils.getType(threadID) === "AsyncFunction") {
34
+ _callback = threadID;
35
+ threadID = null;
36
+ _initiatorID = callback;
37
+ } else if (utils.getType(callback) === "string") {
38
+ _initiatorID = callback;
39
+ _callback = undefined;
40
+ } else {
41
+ _callback = undefined;
42
+ _initiatorID = undefined;
43
+ }
44
+
45
+ if (!_callback) {
46
+ _callback = function (_err, _data) {
47
+ if (_err) _rejectFunc(_err);
48
+ else _resolveFunc(_data);
49
+ };
50
+ }
51
+
52
+ _initiatorID = _initiatorID || ctx.userID;
53
+
54
+ threadID = threadID || ctx.threadID;
55
+
56
+ if (!threadID) {
57
+ return _callback(new Error("threadID is required to manage themes."));
58
+ }
59
+ if (!themeName) {
60
+ return _callback(new Error("themeName (or 'list') is required."));
61
+ }
62
+
63
+ if (!ctx.mqttClient) {
64
+ return _callback(new Error("Not connected to MQTT"));
65
+ }
66
+
67
+ const fetchThemes = async () => {
68
+ const form = {
69
+ fb_api_caller_class: 'RelayModern',
70
+ fb_api_req_friendly_name: 'MWPThreadThemeQuery_AllThemesQuery',
71
+ variables: JSON.stringify({ version: "default" }),
72
+ server_timestamps: true,
73
+ doc_id: '24474714052117636',
74
+ };
75
+
76
+ try {
77
+ const resData = await defaultFuncs
78
+ .post("https://www.facebook.com/api/graphql/", ctx.jar, form, null, {
79
+ "x-fb-friendly-name": "MWPThreadThemeQuery_AllThemesQuery",
80
+ "x-fb-lsd": ctx.lsd,
81
+ "referer": `https://www.facebook.com/messages/t/${threadID}`
82
+ })
83
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs));
84
+
85
+ if (resData.errors) {
86
+ throw new Error(JSON.stringify(resData.errors));
87
+ }
88
+ if (!resData.data || !resData.data.messenger_thread_themes) {
89
+ throw new Error("Could not retrieve thread themes from response.");
90
+ }
91
+ return resData.data.messenger_thread_themes.map(themeData => {
92
+ if (!themeData || !themeData.id) return null;
93
+
94
+ return {
95
+ id: themeData.id,
96
+ name: themeData.accessibility_label,
97
+ description: themeData.description,
98
+ appColorMode: themeData.app_color_mode,
99
+ composerBackgroundColor: themeData.composer_background_color,
100
+ backgroundGradientColors: themeData.background_gradient_colors,
101
+ titleBarButtonTintColor: themeData.title_bar_button_tint_color,
102
+ inboundMessageGradientColors: themeData.inbound_message_gradient_colors,
103
+ titleBarTextColor: themeData.title_bar_text_color,
104
+ composerTintColor: themeData.composer_tint_color,
105
+ titleBarAttributionColor: themeData.title_bar_attribution_color,
106
+ composerInputBackgroundColor: themeData.composer_input_background_color,
107
+ hotLikeColor: themeData.hot_like_color,
108
+ backgroundImage: themeData.background_asset?.image?.uri,
109
+ messageTextColor: themeData.message_text_color,
110
+ inboundMessageTextColor: themeData.inbound_message_text_color,
111
+ primaryButtonBackgroundColor: themeData.primary_button_background_color,
112
+ titleBarBackgroundColor: themeData.title_bar_background_color,
113
+ tertiaryTextColor: themeData.tertiary_text_color,
114
+ reactionPillBackgroundColor: themeData.reaction_pill_background_color,
115
+ secondaryTextColor: themeData.secondary_text_color,
116
+ fallbackColor: themeData.fallback_color,
117
+ gradientColors: themeData.gradient_colors,
118
+ normalThemeId: themeData.normal_theme_id,
119
+ iconAsset: themeData.icon_asset?.image?.uri,
120
+ };
121
+ }).filter(Boolean);
122
+ } catch (fetchErr) {
123
+ throw new Error(`Failed to fetch theme list: ${fetchErr.message || fetchErr}`);
124
+ }
125
+ };
126
+
127
+ const setThreadTheme = async (themeIDToSet, actualThemeName, initiatorID) => {
128
+ let currentEpochId = parseInt(utils.generateOfflineThreadingID());
129
+
130
+ const createAndPublish = (label, queueName, payload) => {
131
+ currentEpochId = parseInt(utils.generateOfflineThreadingID());
132
+ ctx.wsReqNumber += 1;
133
+ ctx.wsTaskNumber += 1;
134
+
135
+ const request_id = ctx.wsReqNumber;
136
+
137
+ const queryPayload = {
138
+ thread_key: threadID.toString(),
139
+ theme_fbid: themeIDToSet.toString(),
140
+ sync_group: 1,
141
+ ...payload
142
+ };
143
+
144
+ const query = {
145
+ failure_count: null,
146
+ label: label,
147
+ payload: JSON.stringify(queryPayload),
148
+ queue_name: queueName,
149
+ task_id: ctx.wsTaskNumber,
150
+ };
151
+
152
+ const context = {
153
+ app_id: ctx.appID,
154
+ payload: {
155
+ epoch_id: currentEpochId,
156
+ tasks: [query],
157
+ version_id: '24631415369801570',
158
+ },
159
+ request_id: request_id,
160
+ type: 3,
161
+ };
162
+ context.payload = JSON.stringify(context.payload);
163
+
164
+ return new Promise((res, rej) => {
165
+ ctx.mqttClient.publish('/ls_req', JSON.stringify(context), { qos: 1, retain: false }, err => {
166
+ if (err) {
167
+ return rej(new Error(`MQTT publish failed for request ${request_id}: ${err.message}`));
168
+ }
169
+ res();
170
+ });
171
+ });
172
+ };
173
+
174
+ try {
175
+ await Promise.all([
176
+ createAndPublish('1013', `ai_generated_theme`, {}),
177
+ createAndPublish('1037', `msgr_custom_thread_theme`, {}),
178
+ createAndPublish('1028', `thread_theme_writer`, {}),
179
+ createAndPublish('43', `thread_theme`, { source: null, payload: null })
180
+ ]);
181
+
182
+ return {
183
+ type: "thread_theme_update",
184
+ threadID: threadID,
185
+ themeID: themeIDToSet,
186
+ themeName: actualThemeName,
187
+ senderID: initiatorID,
188
+ BotID: ctx.userID,
189
+ timestamp: Date.now(),
190
+ };
191
+
192
+ } catch (publishErr) {
193
+ throw new Error(`Failed to publish theme change MQTT messages: ${publishErr.message || publishErr}`);
194
+ }
195
+ };
196
+
197
+ try {
198
+ if (themeName.toLowerCase() === "list") {
199
+ const themes = await fetchThemes();
200
+ _callback(null, themes);
201
+ } else {
202
+ const themes = await fetchThemes();
203
+ const normalizedThemeName = themeName.toLowerCase();
204
+
205
+ let matchedTheme = null;
206
+
207
+ if (!isNaN(normalizedThemeName)) {
208
+ matchedTheme = themes.find(t => t.id === normalizedThemeName);
209
+ }
210
+ if (!matchedTheme) {
211
+ matchedTheme = themes.find(t => t.name.toLowerCase() === normalizedThemeName);
212
+ }
213
+
214
+ if (!matchedTheme) {
215
+ matchedTheme = themes.find(t => t.name.toLowerCase().includes(normalizedThemeName));
216
+ }
217
+
218
+ if (!matchedTheme) {
219
+ throw new Error(`Theme "${themeName}" not found. Try '/theme list' for available themes.`);
220
+ }
221
+
222
+ const themeEventObject = await setThreadTheme(matchedTheme.id, matchedTheme.name, _initiatorID);
223
+
224
+ _callback(null, themeEventObject);
225
+ }
226
+ } catch (err) {
227
+ const finalError = err instanceof Error ? err : new Error(err.message || err.error || 'An unknown error occurred during theme operation.');
228
+ _callback(finalError);
229
+ }
230
+
231
+ return finalReturnPromise;
232
+ };
233
+ };