surya-sahil-fca 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +175 -0
- package/DOCS.md +2636 -0
- package/LICENSE-MIT +21 -0
- package/README.md +107 -0
- package/func/checkUpdate.js +222 -0
- package/func/logger.js +48 -0
- package/index.d.ts +746 -0
- package/index.js +8 -0
- package/module/config.js +34 -0
- package/module/login.js +126 -0
- package/module/loginHelper.js +747 -0
- package/module/options.js +45 -0
- package/package.json +82 -0
- package/src/api/messaging/changeGroupImage.js +90 -0
- package/src/api/messaging/changeNickname.js +70 -0
- package/src/api/messaging/changeThreadName.js +123 -0
- package/src/api/messaging/createCommentPost.js +207 -0
- package/src/api/messaging/sendMessage.js +272 -0
- package/src/api/messaging/sendTypingIndicator.js +67 -0
- package/src/api/messaging/setMessageReaction.js +76 -0
- package/src/api/messaging/setTitle.js +119 -0
- package/src/api/messaging/stickers.js +257 -0
- package/src/core/sendReqMqtt.js +96 -0
- package/src/database/models/index.js +49 -0
- package/src/database/models/thread.js +31 -0
- package/src/database/models/user.js +32 -0
- package/src/database/threadData.js +98 -0
- package/src/database/userData.js +89 -0
- package/src/utils/client.js +320 -0
- package/src/utils/constants.js +23 -0
- package/src/utils/format.js +1115 -0
- package/src/utils/headers.js +115 -0
- package/src/utils/request.js +305 -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,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function sendReqMqtt(ctx, payload, options, callback) {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
const cb = typeof options === "function" ? options : callback;
|
|
6
|
+
const opts = typeof options === "object" && options ? options : {};
|
|
7
|
+
if (!ctx || !ctx.mqttClient) {
|
|
8
|
+
const err = new Error("Not connected to MQTT");
|
|
9
|
+
if (cb) cb(err);
|
|
10
|
+
return reject(err);
|
|
11
|
+
}
|
|
12
|
+
if (typeof ctx.wsReqNumber !== "number") ctx.wsReqNumber = 0;
|
|
13
|
+
const reqID = typeof opts.request_id === "number" ? opts.request_id : ++ctx.wsReqNumber;
|
|
14
|
+
const timeoutMs = typeof opts.timeout === "number" ? opts.timeout : 20000;
|
|
15
|
+
const qos = typeof opts.qos === "number" ? opts.qos : 1;
|
|
16
|
+
const retain = !!opts.retain;
|
|
17
|
+
const reqTopic = "/ls_req";
|
|
18
|
+
const respTopic = opts.respTopic || "/ls_resp";
|
|
19
|
+
const form = JSON.stringify({
|
|
20
|
+
app_id: opts.app_id || "",
|
|
21
|
+
payload: typeof payload === "string" ? payload : JSON.stringify(payload),
|
|
22
|
+
request_id: reqID,
|
|
23
|
+
type: opts.type == null ? 3 : opts.type
|
|
24
|
+
});
|
|
25
|
+
let timer = null;
|
|
26
|
+
let cleaned = false;
|
|
27
|
+
|
|
28
|
+
// Cleanup function to ensure listeners and timers are always removed
|
|
29
|
+
const cleanup = () => {
|
|
30
|
+
if (cleaned) return;
|
|
31
|
+
cleaned = true;
|
|
32
|
+
try {
|
|
33
|
+
if (timer) {
|
|
34
|
+
clearTimeout(timer);
|
|
35
|
+
timer = null;
|
|
36
|
+
}
|
|
37
|
+
if (ctx.mqttClient && typeof ctx.mqttClient.removeListener === "function") {
|
|
38
|
+
ctx.mqttClient.removeListener("message", handleRes);
|
|
39
|
+
}
|
|
40
|
+
} catch (err) {
|
|
41
|
+
// Ignore cleanup errors
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const handleRes = (topic, message) => {
|
|
46
|
+
if (topic !== respTopic) return;
|
|
47
|
+
let msg;
|
|
48
|
+
try {
|
|
49
|
+
msg = JSON.parse(message.toString());
|
|
50
|
+
} catch {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (msg.request_id !== reqID) return;
|
|
54
|
+
if (typeof opts.filter === "function" && !opts.filter(msg)) return;
|
|
55
|
+
cleanup();
|
|
56
|
+
try {
|
|
57
|
+
msg.payload = typeof msg.payload === "string" ? JSON.parse(msg.payload) : msg.payload;
|
|
58
|
+
} catch { }
|
|
59
|
+
const out = { success: true, response: msg.payload, raw: msg };
|
|
60
|
+
if (cb) cb(null, out);
|
|
61
|
+
resolve(out);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
ctx.mqttClient.on("message", handleRes);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
cleanup();
|
|
68
|
+
const error = new Error("Failed to attach message listener");
|
|
69
|
+
if (cb) cb(error);
|
|
70
|
+
return reject(error);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
timer = setTimeout(() => {
|
|
74
|
+
cleanup();
|
|
75
|
+
const err = new Error("MQTT response timeout");
|
|
76
|
+
if (cb) cb(err);
|
|
77
|
+
reject(err);
|
|
78
|
+
}, timeoutMs);
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
ctx.mqttClient.publish(reqTopic, form, { qos, retain }, (err) => {
|
|
82
|
+
if (err) {
|
|
83
|
+
cleanup();
|
|
84
|
+
if (cb) cb(err);
|
|
85
|
+
reject(err);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
} catch (err) {
|
|
89
|
+
cleanup();
|
|
90
|
+
if (cb) cb(err);
|
|
91
|
+
reject(err);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports = sendReqMqtt;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const { Sequelize } = require("sequelize");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const databasePath = path.join(process.cwd(), "Fca_Database");
|
|
5
|
+
if (!fs.existsSync(databasePath)) {
|
|
6
|
+
fs.mkdirSync(databasePath, { recursive: true });
|
|
7
|
+
}
|
|
8
|
+
const sequelize = new Sequelize({
|
|
9
|
+
dialect: "sqlite",
|
|
10
|
+
storage: path.join(databasePath, "database.sqlite"),
|
|
11
|
+
logging: false,
|
|
12
|
+
pool: {
|
|
13
|
+
max: 5,
|
|
14
|
+
min: 0,
|
|
15
|
+
acquire: 30000,
|
|
16
|
+
idle: 10000
|
|
17
|
+
},
|
|
18
|
+
retry: {
|
|
19
|
+
max: 3
|
|
20
|
+
},
|
|
21
|
+
dialectOptions: {
|
|
22
|
+
timeout: 5000
|
|
23
|
+
},
|
|
24
|
+
isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED
|
|
25
|
+
});
|
|
26
|
+
const models = {};
|
|
27
|
+
fs.readdirSync(__dirname)
|
|
28
|
+
.filter(file => file.endsWith(".js") && file !== "index.js")
|
|
29
|
+
.forEach(file => {
|
|
30
|
+
const model = require(path.join(__dirname, file))(sequelize);
|
|
31
|
+
models[model.name] = model;
|
|
32
|
+
});
|
|
33
|
+
Object.keys(models).forEach(modelName => {
|
|
34
|
+
if (models[modelName].associate) {
|
|
35
|
+
models[modelName].associate(models);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
models.sequelize = sequelize;
|
|
39
|
+
models.Sequelize = Sequelize;
|
|
40
|
+
models.syncAll = async () => {
|
|
41
|
+
try {
|
|
42
|
+
await sequelize.sync({ force: false });
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error("Failed to synchronize models:", error);
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
module.exports = models;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module.exports = function(sequelize) {
|
|
2
|
+
const { Model, DataTypes } = require("sequelize");
|
|
3
|
+
|
|
4
|
+
class Thread extends Model {}
|
|
5
|
+
|
|
6
|
+
Thread.init(
|
|
7
|
+
{
|
|
8
|
+
num: {
|
|
9
|
+
type: DataTypes.INTEGER,
|
|
10
|
+
allowNull: false,
|
|
11
|
+
autoIncrement: true,
|
|
12
|
+
primaryKey: true
|
|
13
|
+
},
|
|
14
|
+
threadID: {
|
|
15
|
+
type: DataTypes.STRING,
|
|
16
|
+
allowNull: false,
|
|
17
|
+
unique: true
|
|
18
|
+
},
|
|
19
|
+
data: {
|
|
20
|
+
type: DataTypes.JSONB,
|
|
21
|
+
allowNull: true
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
sequelize,
|
|
26
|
+
modelName: "Thread",
|
|
27
|
+
timestamps: true
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
return Thread;
|
|
31
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module.exports = function (sequelize) {
|
|
2
|
+
const { Model, DataTypes } = require("sequelize");
|
|
3
|
+
|
|
4
|
+
class User extends Model { }
|
|
5
|
+
|
|
6
|
+
User.init(
|
|
7
|
+
{
|
|
8
|
+
num: {
|
|
9
|
+
type: DataTypes.INTEGER,
|
|
10
|
+
allowNull: false,
|
|
11
|
+
autoIncrement: true,
|
|
12
|
+
primaryKey: true
|
|
13
|
+
},
|
|
14
|
+
userID: {
|
|
15
|
+
type: DataTypes.STRING,
|
|
16
|
+
allowNull: false,
|
|
17
|
+
unique: true
|
|
18
|
+
},
|
|
19
|
+
data: {
|
|
20
|
+
type: DataTypes.JSONB,
|
|
21
|
+
allowNull: true
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
sequelize,
|
|
26
|
+
modelName: "User",
|
|
27
|
+
timestamps: true
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return User;
|
|
32
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const { Thread } = require("./models");
|
|
2
|
+
|
|
3
|
+
const validateThreadID = threadID => {
|
|
4
|
+
if (typeof threadID !== "string" && typeof threadID !== "number") {
|
|
5
|
+
throw new Error("Invalid threadID: must be a string or number.");
|
|
6
|
+
}
|
|
7
|
+
return String(threadID);
|
|
8
|
+
};
|
|
9
|
+
const validateData = data => {
|
|
10
|
+
if (!data || typeof data !== "object" || Array.isArray(data)) {
|
|
11
|
+
throw new Error("Invalid data: must be a non-empty object.");
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports = function(bot) {
|
|
16
|
+
return {
|
|
17
|
+
async create(threadID, data) {
|
|
18
|
+
try {
|
|
19
|
+
let thread = await Thread.findOne({ where: { threadID } });
|
|
20
|
+
if (thread) {
|
|
21
|
+
return { thread: thread.get(), created: false };
|
|
22
|
+
}
|
|
23
|
+
thread = await Thread.create({ threadID, ...data });
|
|
24
|
+
return { thread: thread.get(), created: true };
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw new Error(`Failed to create thread: ${error.message}`);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
async get(threadID) {
|
|
31
|
+
try {
|
|
32
|
+
threadID = validateThreadID(threadID);
|
|
33
|
+
const thread = await Thread.findOne({ where: { threadID } });
|
|
34
|
+
return thread ? thread.get() : null;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
throw new Error(`Failed to get thread: ${error.message}`);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
async update(threadID, data) {
|
|
41
|
+
try {
|
|
42
|
+
threadID = validateThreadID(threadID);
|
|
43
|
+
validateData(data);
|
|
44
|
+
const thread = await Thread.findOne({ where: { threadID } });
|
|
45
|
+
|
|
46
|
+
if (thread) {
|
|
47
|
+
await thread.update(data);
|
|
48
|
+
return { thread: thread.get(), created: false };
|
|
49
|
+
} else {
|
|
50
|
+
const newThread = await Thread.create({ ...data, threadID });
|
|
51
|
+
return { thread: newThread.get(), created: true };
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw new Error(`Failed to update thread: ${error.message}`);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
async del(threadID) {
|
|
59
|
+
try {
|
|
60
|
+
if (!threadID) {
|
|
61
|
+
throw new Error("threadID is required and cannot be undefined");
|
|
62
|
+
}
|
|
63
|
+
threadID = validateThreadID(threadID);
|
|
64
|
+
if (!threadID) {
|
|
65
|
+
throw new Error("Invalid threadID");
|
|
66
|
+
}
|
|
67
|
+
const result = await Thread.destroy({ where: { threadID } });
|
|
68
|
+
if (result === 0) {
|
|
69
|
+
throw new Error("No thread found with the specified threadID");
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
throw new Error(`Failed to delete thread: ${error.message}`);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
async delAll() {
|
|
77
|
+
try {
|
|
78
|
+
return await Thread.destroy({ where: {} });
|
|
79
|
+
} catch (error) {
|
|
80
|
+
throw new Error(`Failed to delete all threads: ${error.message}`);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
async getAll(keys = null) {
|
|
84
|
+
try {
|
|
85
|
+
const attributes =
|
|
86
|
+
typeof keys === "string"
|
|
87
|
+
? [keys]
|
|
88
|
+
: Array.isArray(keys)
|
|
89
|
+
? keys
|
|
90
|
+
: undefined;
|
|
91
|
+
const threads = await Thread.findAll({ attributes });
|
|
92
|
+
return threads.map(thread => thread.get());
|
|
93
|
+
} catch (error) {
|
|
94
|
+
throw new Error(`Failed to get all threads: ${error.message}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const { User } = require("./models");
|
|
2
|
+
|
|
3
|
+
const validateUserID = userID => {
|
|
4
|
+
if (typeof userID !== "string" && typeof userID !== "number") {
|
|
5
|
+
throw new Error("Invalid userID: must be a string or number.");
|
|
6
|
+
}
|
|
7
|
+
return String(userID);
|
|
8
|
+
};
|
|
9
|
+
const validateData = data => {
|
|
10
|
+
if (!data || typeof data !== "object" || Array.isArray(data)) {
|
|
11
|
+
throw new Error("Invalid data: must be a non-empty object.");
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports = function (bot) {
|
|
16
|
+
return {
|
|
17
|
+
async create(userID, data) {
|
|
18
|
+
try {
|
|
19
|
+
userID = validateUserID(userID);
|
|
20
|
+
validateData(data);
|
|
21
|
+
let user = await User.findOne({ where: { userID } });
|
|
22
|
+
if (user) return { user: user.get(), created: false };
|
|
23
|
+
const payload = Object.prototype.hasOwnProperty.call(data, "data") ? data : { data };
|
|
24
|
+
user = await User.create({ userID, ...payload });
|
|
25
|
+
return { user: user.get(), created: true };
|
|
26
|
+
} catch (error) {
|
|
27
|
+
throw new Error(`Failed to create user: ${error.message}`);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
async get(userID) {
|
|
32
|
+
try {
|
|
33
|
+
userID = validateUserID(userID);
|
|
34
|
+
const user = await User.findOne({ where: { userID } });
|
|
35
|
+
return user ? user.get() : null;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
throw new Error(`Failed to get user: ${error.message}`);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
async update(userID, data) {
|
|
42
|
+
try {
|
|
43
|
+
userID = validateUserID(userID);
|
|
44
|
+
validateData(data);
|
|
45
|
+
const payload = Object.prototype.hasOwnProperty.call(data, "data") ? data : { data };
|
|
46
|
+
const user = await User.findOne({ where: { userID } });
|
|
47
|
+
if (user) {
|
|
48
|
+
await user.update(payload);
|
|
49
|
+
return { user: user.get(), created: false };
|
|
50
|
+
} else {
|
|
51
|
+
const newUser = await User.create({ userID, ...payload });
|
|
52
|
+
return { user: newUser.get(), created: true };
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
throw new Error(`Failed to update user: ${error.message}`);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
async del(userID) {
|
|
60
|
+
try {
|
|
61
|
+
if (!userID) throw new Error("userID is required and cannot be undefined");
|
|
62
|
+
userID = validateUserID(userID);
|
|
63
|
+
const result = await User.destroy({ where: { userID } });
|
|
64
|
+
if (result === 0) throw new Error("No user found with the specified userID");
|
|
65
|
+
return result;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
throw new Error(`Failed to delete user: ${error.message}`);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
async delAll() {
|
|
72
|
+
try {
|
|
73
|
+
return await User.destroy({ where: {} });
|
|
74
|
+
} catch (error) {
|
|
75
|
+
throw new Error(`Failed to delete all users: ${error.message}`);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
async getAll(keys = null) {
|
|
80
|
+
try {
|
|
81
|
+
const attributes = typeof keys === "string" ? [keys] : Array.isArray(keys) ? keys : undefined;
|
|
82
|
+
const users = await User.findAll({ attributes });
|
|
83
|
+
return users.map(u => u.get());
|
|
84
|
+
} catch (error) {
|
|
85
|
+
throw new Error(`Failed to get all users: ${error.message}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
};
|