@lazyneoaz/nkxchat 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.
Files changed (123) hide show
  1. package/LICENSE +3 -0
  2. package/README.md +199 -0
  3. package/examples/login-with-cookies.js +102 -0
  4. package/examples/verify.js +70 -0
  5. package/index.js +2 -0
  6. package/package.json +84 -0
  7. package/src/apis/addExternalModule.js +24 -0
  8. package/src/apis/addUserToGroup.js +108 -0
  9. package/src/apis/changeAdminStatus.js +148 -0
  10. package/src/apis/changeArchivedStatus.js +61 -0
  11. package/src/apis/changeAvatar.js +103 -0
  12. package/src/apis/changeBio.js +69 -0
  13. package/src/apis/changeBlockedStatus.js +54 -0
  14. package/src/apis/changeGroupImage.js +136 -0
  15. package/src/apis/changeThreadColor.js +116 -0
  16. package/src/apis/changeThreadEmoji.js +53 -0
  17. package/src/apis/comment.js +207 -0
  18. package/src/apis/createAITheme.js +129 -0
  19. package/src/apis/createNewGroup.js +79 -0
  20. package/src/apis/createPoll.js +73 -0
  21. package/src/apis/deleteMessage.js +44 -0
  22. package/src/apis/deleteThread.js +52 -0
  23. package/src/apis/editMessage.js +70 -0
  24. package/src/apis/emoji.js +124 -0
  25. package/src/apis/enableAutoSaveAppState.js +69 -0
  26. package/src/apis/fetchThemeData.js +113 -0
  27. package/src/apis/follow.js +81 -0
  28. package/src/apis/forwardAttachment.js +178 -0
  29. package/src/apis/forwardMessage.js +52 -0
  30. package/src/apis/friend.js +243 -0
  31. package/src/apis/gcmember.js +122 -0
  32. package/src/apis/gcname.js +123 -0
  33. package/src/apis/gcrule.js +119 -0
  34. package/src/apis/getAccess.js +111 -0
  35. package/src/apis/getBotInfo.js +88 -0
  36. package/src/apis/getBotInitialData.js +43 -0
  37. package/src/apis/getEmojiUrl.js +40 -0
  38. package/src/apis/getFriendsList.js +79 -0
  39. package/src/apis/getMessage.js +423 -0
  40. package/src/apis/getTheme.js +123 -0
  41. package/src/apis/getThemeInfo.js +116 -0
  42. package/src/apis/getThemePictures.js +87 -0
  43. package/src/apis/getThreadColors.js +119 -0
  44. package/src/apis/getThreadHistory.js +239 -0
  45. package/src/apis/getThreadInfo.js +267 -0
  46. package/src/apis/getThreadList.js +232 -0
  47. package/src/apis/getThreadPictures.js +58 -0
  48. package/src/apis/getUserID.js +117 -0
  49. package/src/apis/getUserInfo.js +513 -0
  50. package/src/apis/getUserInfoV2.js +146 -0
  51. package/src/apis/handleFriendRequest.js +66 -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 +924 -0
  57. package/src/apis/listenSpeed.js +178 -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 +40 -0
  62. package/src/apis/markAsSeen.js +70 -0
  63. package/src/apis/mqttDeltaValue.js +252 -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 +150 -0
  68. package/src/apis/produceMetaTheme.js +160 -0
  69. package/src/apis/realtime.js +182 -0
  70. package/src/apis/refreshFb_dtsg.js +94 -0
  71. package/src/apis/removeUserFromGroup.js +117 -0
  72. package/src/apis/resolvePhotoUrl.js +58 -0
  73. package/src/apis/searchForThread.js +154 -0
  74. package/src/apis/sendEffect.js +306 -0
  75. package/src/apis/sendMessage.js +353 -0
  76. package/src/apis/sendMessageMqtt.js +255 -0
  77. package/src/apis/sendTypingIndicator.js +40 -0
  78. package/src/apis/setMessageReaction.js +27 -0
  79. package/src/apis/setMessageReactionMqtt.js +61 -0
  80. package/src/apis/setPostReaction.js +118 -0
  81. package/src/apis/setThreadTheme.js +210 -0
  82. package/src/apis/setThreadThemeMqtt.js +94 -0
  83. package/src/apis/setTitle.js +26 -0
  84. package/src/apis/share.js +106 -0
  85. package/src/apis/shareContact.js +66 -0
  86. package/src/apis/stickers.js +257 -0
  87. package/src/apis/story.js +181 -0
  88. package/src/apis/theme.js +233 -0
  89. package/src/apis/unfriend.js +47 -0
  90. package/src/apis/unsendMessage.js +17 -0
  91. package/src/apis/uploadAttachment.js +87 -0
  92. package/src/database/appStateBackup.js +189 -0
  93. package/src/database/models/index.js +56 -0
  94. package/src/database/models/thread.js +31 -0
  95. package/src/database/models/user.js +32 -0
  96. package/src/database/threadData.js +101 -0
  97. package/src/database/userData.js +90 -0
  98. package/src/engine/client.js +92 -0
  99. package/src/engine/models/buildAPI.js +118 -0
  100. package/src/engine/models/loginHelper.js +492 -0
  101. package/src/engine/models/setOptions.js +88 -0
  102. package/src/types/index.d.ts +498 -0
  103. package/src/utils/antiSuspension.js +516 -0
  104. package/src/utils/auth-helpers.js +149 -0
  105. package/src/utils/autoReLogin.js +237 -0
  106. package/src/utils/axios.js +368 -0
  107. package/src/utils/cache.js +54 -0
  108. package/src/utils/clients.js +279 -0
  109. package/src/utils/constants.js +525 -0
  110. package/src/utils/formatters/data/formatAttachment.js +370 -0
  111. package/src/utils/formatters/data/formatDelta.js +109 -0
  112. package/src/utils/formatters/index.js +159 -0
  113. package/src/utils/formatters/value/formatCookie.js +91 -0
  114. package/src/utils/formatters/value/formatDate.js +36 -0
  115. package/src/utils/formatters/value/formatID.js +16 -0
  116. package/src/utils/formatters.js +1369 -0
  117. package/src/utils/headers.js +235 -0
  118. package/src/utils/index.js +152 -0
  119. package/src/utils/monitoring.js +333 -0
  120. package/src/utils/rateLimiter.js +251 -0
  121. package/src/utils/tokenRefresh.js +285 -0
  122. package/src/utils/user-agents.js +238 -0
  123. package/src/utils/validation.js +157 -0
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+ module.exports = (defaultFuncs, api, ctx) => {
6
+ return async function unfriend(userID, callback) {
7
+ let resolveFunc = () => {};
8
+ let rejectFunc = () => {};
9
+ const returnPromise = new Promise((resolve, reject) => {
10
+ resolveFunc = resolve;
11
+ rejectFunc = reject;
12
+ });
13
+
14
+ if (!callback) {
15
+ callback = (err, result) => {
16
+ if (err) return rejectFunc(err);
17
+ resolveFunc(result);
18
+ };
19
+ }
20
+
21
+ try {
22
+ const form = {
23
+ uid: userID,
24
+ unref: "bd_friends_tab",
25
+ floc: "friends_tab",
26
+ "nctr[_mod]": "pagelet_timeline_app_collection_" + ctx.userID + ":2356318349:2"
27
+ };
28
+
29
+ const res = await defaultFuncs.post(
30
+ "https://www.facebook.com/ajax/profile/removefriendconfirm.php",
31
+ ctx.jar,
32
+ form
33
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
34
+
35
+ if (res.error) {
36
+ throw res;
37
+ }
38
+
39
+ callback(null, true);
40
+ } catch (err) {
41
+ utils.error("unfriend", err);
42
+ callback(err);
43
+ }
44
+
45
+ return returnPromise;
46
+ };
47
+ };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+
6
+ module.exports = function (defaultFuncs, api, ctx) {
7
+ return async (messageID) => {
8
+ const defData = await defaultFuncs.post("https://www.facebook.com/messaging/unsend_message/", ctx.jar, {
9
+ message_id: messageID
10
+ })
11
+ const resData = await utils.parseAndCheckLogin(ctx, defaultFuncs)(defData);
12
+ if (resData.error) {
13
+ throw new Error(resData);
14
+ }
15
+ return resData;
16
+ };
17
+ };
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+
3
+ const utils = require("../utils");
4
+
5
+ /**
6
+ * Uploads one or more file attachments to Facebook's servers and returns
7
+ * an array of attachment IDs that can be used in sendMessage({ attachment: id }).
8
+ *
9
+ * @example
10
+ * const fs = require("fs");
11
+ * const ids = await api.uploadAttachment(fs.createReadStream("./photo.jpg"));
12
+ * await api.sendMessage({ attachment: ids }, threadID);
13
+ *
14
+ * @param {ReadableStream|ReadableStream[]} attachments A single stream or array of streams.
15
+ * @param {Function} [callback] callback(err, ids[])
16
+ * @returns {Promise<string[]>} Array of attachment ID strings.
17
+ */
18
+ module.exports = function (defaultFuncs, api, ctx) {
19
+ function detectType(stream) {
20
+ const p = (stream.path || stream._path || "").toString().toLowerCase();
21
+ const ext = p.split(".").pop();
22
+ if (["mp3", "wav", "aac", "m4a", "ogg", "opus", "flac"].includes(ext)) return { voice_clip: "true" };
23
+ if (["mp4", "mov", "avi", "mkv", "webm", "wmv", "flv"].includes(ext)) return { video: "true" };
24
+ if (["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg"].includes(ext)) return { image: "true" };
25
+ return { file: "true" };
26
+ }
27
+
28
+ async function uploadOne(stream, threadID) {
29
+ if (!utils.isReadableStream(stream)) {
30
+ throw new Error("Each attachment must be a readable stream, not " + utils.getType(stream));
31
+ }
32
+ const form = {
33
+ upload_1024: stream,
34
+ ...detectType(stream),
35
+ };
36
+ const res = await defaultFuncs
37
+ .postFormData(
38
+ "https://upload.facebook.com/ajax/mercury/upload.php",
39
+ ctx.jar,
40
+ form,
41
+ {},
42
+ { ...ctx, requestThreadID: String(threadID || "") }
43
+ )
44
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs));
45
+
46
+ if (res.error) throw new Error(JSON.stringify(res));
47
+
48
+ const meta = res.payload && res.payload.metadata && res.payload.metadata[0];
49
+ if (!meta) throw new Error("Upload succeeded but no metadata returned");
50
+
51
+ // Return the first ID value in the metadata object
52
+ const idKey = Object.keys(meta)[0];
53
+ return meta[idKey];
54
+ }
55
+
56
+ return function uploadAttachment(attachments, callback) {
57
+ let resolveFunc, rejectFunc;
58
+ const promise = new Promise((resolve, reject) => {
59
+ resolveFunc = resolve;
60
+ rejectFunc = reject;
61
+ });
62
+ if (!callback) {
63
+ callback = (err, data) => { if (err) return rejectFunc(err); resolveFunc(data); };
64
+ }
65
+
66
+ const list = Array.isArray(attachments) ? attachments : [attachments];
67
+ if (!list.length) {
68
+ return callback(new Error("Please pass an attachment or an array of attachments."));
69
+ }
70
+
71
+ (async () => {
72
+ try {
73
+ const ids = [];
74
+ for (const stream of list) {
75
+ const id = await uploadOne(stream);
76
+ ids.push(id);
77
+ }
78
+ callback(null, ids);
79
+ } catch (err) {
80
+ utils.error("uploadAttachment", err.message || err);
81
+ callback(err instanceof Error ? err : new Error(String(err.message || err)));
82
+ }
83
+ })();
84
+
85
+ return promise;
86
+ };
87
+ };
@@ -0,0 +1,189 @@
1
+ const models = require("./models");
2
+ const utils = require("../utils");
3
+
4
+ let uniqueIndexEnsured = false;
5
+
6
+ function getBackupModel() {
7
+ if (!models || !models.sequelize || !models.Sequelize) return null;
8
+ const sequelize = models.sequelize;
9
+ const { DataTypes } = models.Sequelize;
10
+
11
+ if (sequelize.models && sequelize.models.AppStateBackup) {
12
+ return sequelize.models.AppStateBackup;
13
+ }
14
+
15
+ const dialect =
16
+ typeof sequelize.getDialect === "function"
17
+ ? sequelize.getDialect()
18
+ : "sqlite";
19
+ const LongText =
20
+ dialect === "mysql" || dialect === "mariadb"
21
+ ? DataTypes.TEXT("long")
22
+ : DataTypes.TEXT;
23
+
24
+ const AppStateBackup = sequelize.define(
25
+ "AppStateBackup",
26
+ {
27
+ id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
28
+ userID: { type: DataTypes.STRING, allowNull: false },
29
+ type: { type: DataTypes.STRING, allowNull: false },
30
+ data: { type: LongText }
31
+ },
32
+ {
33
+ tableName: "app_state_backups",
34
+ timestamps: true,
35
+ indexes: [{ unique: true, fields: ["userID", "type"] }]
36
+ }
37
+ );
38
+ return AppStateBackup;
39
+ }
40
+
41
+ async function ensureUniqueIndex(sequelize) {
42
+ if (uniqueIndexEnsured) return;
43
+ try {
44
+ await sequelize
45
+ .getQueryInterface()
46
+ .addIndex("app_state_backups", ["userID", "type"], {
47
+ unique: true,
48
+ name: "app_state_user_type_unique"
49
+ });
50
+ } catch {}
51
+ uniqueIndexEnsured = true;
52
+ }
53
+
54
+ async function upsertBackup(Model, userID, type, data) {
55
+ const where = { userID: String(userID || ""), type };
56
+ const row = await Model.findOne({ where });
57
+ if (row) {
58
+ await row.update({ data });
59
+ utils.log(`Overwrote existing ${type} backup for user ${where.userID}`);
60
+ return;
61
+ }
62
+ await Model.create({ ...where, data });
63
+ utils.log(`Created new ${type} backup for user ${where.userID}`);
64
+ }
65
+
66
+ async function backupAppStateSQL(jar, userID) {
67
+ try {
68
+ const Model = getBackupModel();
69
+ if (!Model) return;
70
+ await Model.sync();
71
+ await ensureUniqueIndex(models.sequelize);
72
+
73
+ const appState = utils.getAppState(jar);
74
+ const cookieStr = cookieHeaderFromJar(jar);
75
+
76
+ await upsertBackup(Model, userID, "appstate", JSON.stringify(appState));
77
+ await upsertBackup(Model, userID, "cookie", cookieStr);
78
+
79
+ utils.log("AppState backup stored successfully");
80
+ } catch (e) {
81
+ utils.warn(
82
+ `Failed to save AppState backup: ${
83
+ e && e.message ? e.message : String(e)
84
+ }`
85
+ );
86
+ }
87
+ }
88
+
89
+ async function getLatestBackup(userID, type) {
90
+ try {
91
+ const Model = getBackupModel();
92
+ if (!Model) return null;
93
+ const row = await Model.findOne({
94
+ where: { userID: String(userID || ""), type }
95
+ });
96
+ return row ? row.data : null;
97
+ } catch {
98
+ return null;
99
+ }
100
+ }
101
+
102
+ async function getLatestBackupAny(type) {
103
+ try {
104
+ const Model = getBackupModel();
105
+ if (!Model) return null;
106
+ const row = await Model.findOne({
107
+ where: { type },
108
+ order: [["updatedAt", "DESC"]]
109
+ });
110
+ return row ? row.data : null;
111
+ } catch {
112
+ return null;
113
+ }
114
+ }
115
+
116
+ function cookieHeaderFromJar(jar) {
117
+ const urls = ["https://www.facebook.com", "https://www.messenger.com"];
118
+ const seen = new Set();
119
+ const parts = [];
120
+ for (const url of urls) {
121
+ let cookieString = "";
122
+ try {
123
+ if (typeof jar.getCookieStringSync === "function") {
124
+ cookieString = jar.getCookieStringSync(url);
125
+ }
126
+ } catch {}
127
+ if (!cookieString) continue;
128
+ for (const kv of cookieString.split(";")) {
129
+ const trimmed = kv.trim();
130
+ const name = trimmed.split("=")[0];
131
+ if (!name || seen.has(name)) continue;
132
+ seen.add(name);
133
+ parts.push(trimmed);
134
+ }
135
+ }
136
+ return parts.join("; ");
137
+ }
138
+
139
+ async function hydrateJarFromDB(jar, userID) {
140
+ try {
141
+ const { normalizeCookieHeaderString, setJarFromPairs } = require("../utils/formatters/value/formatCookie");
142
+
143
+ let cookieHeader = null;
144
+ let appStateJson = null;
145
+
146
+ if (userID) {
147
+ cookieHeader = await getLatestBackup(userID, "cookie");
148
+ appStateJson = await getLatestBackup(userID, "appstate");
149
+ } else {
150
+ cookieHeader = await getLatestBackupAny("cookie");
151
+ appStateJson = await getLatestBackupAny("appstate");
152
+ }
153
+
154
+ if (cookieHeader) {
155
+ const pairs = normalizeCookieHeaderString(cookieHeader);
156
+ if (pairs.length) {
157
+ setJarFromPairs(jar, pairs);
158
+ return true;
159
+ }
160
+ }
161
+
162
+ if (appStateJson) {
163
+ let parsed = null;
164
+ try {
165
+ parsed = JSON.parse(appStateJson);
166
+ } catch {}
167
+ if (Array.isArray(parsed)) {
168
+ const pairs = parsed.map(c => [c.name || c.key, c.value].join("="));
169
+ setJarFromPairs(jar, pairs);
170
+ return true;
171
+ }
172
+ }
173
+
174
+ return false;
175
+ } catch {
176
+ return false;
177
+ }
178
+ }
179
+
180
+ module.exports = {
181
+ getBackupModel,
182
+ ensureUniqueIndex,
183
+ upsertBackup,
184
+ backupAppStateSQL,
185
+ getLatestBackup,
186
+ getLatestBackupAny,
187
+ hydrateJarFromDB,
188
+ cookieHeaderFromJar
189
+ };
@@ -0,0 +1,56 @@
1
+ const { Sequelize } = require("sequelize");
2
+ const fs = require("fs");
3
+ const path = require("path");
4
+
5
+ const databasePath = path.join(process.cwd(), "nkxchat_database");
6
+ if (!fs.existsSync(databasePath)) {
7
+ fs.mkdirSync(databasePath, { recursive: true });
8
+ }
9
+
10
+ const sequelize = new Sequelize({
11
+ dialect: "sqlite",
12
+ storage: path.join(databasePath, "nkxchat.sqlite"),
13
+ logging: false,
14
+ pool: {
15
+ max: 5,
16
+ min: 0,
17
+ acquire: 30000,
18
+ idle: 10000
19
+ },
20
+ retry: {
21
+ max: 3
22
+ },
23
+ dialectOptions: {
24
+ timeout: 5000
25
+ },
26
+ isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED
27
+ });
28
+
29
+ const models = {};
30
+
31
+ fs.readdirSync(__dirname)
32
+ .filter(file => file.endsWith(".js") && file !== "index.js")
33
+ .forEach(file => {
34
+ const model = require(path.join(__dirname, file))(sequelize);
35
+ models[model.name] = model;
36
+ });
37
+
38
+ Object.keys(models).forEach(modelName => {
39
+ if (models[modelName].associate) {
40
+ models[modelName].associate(models);
41
+ }
42
+ });
43
+
44
+ models.sequelize = sequelize;
45
+ models.Sequelize = Sequelize;
46
+
47
+ models.syncAll = async () => {
48
+ try {
49
+ await sequelize.sync({ force: false });
50
+ } catch (error) {
51
+ console.error("Failed to synchronize nkxchat database models:", error);
52
+ throw error;
53
+ }
54
+ };
55
+
56
+ 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.JSON,
21
+ allowNull: true
22
+ }
23
+ },
24
+ {
25
+ sequelize,
26
+ modelName: "FcaNeoProThread",
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.JSON,
21
+ allowNull: true
22
+ }
23
+ },
24
+ {
25
+ sequelize,
26
+ modelName: "FcaNeoProUser",
27
+ timestamps: true
28
+ }
29
+ );
30
+
31
+ return User;
32
+ };
@@ -0,0 +1,101 @@
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
+
10
+ const validateData = data => {
11
+ if (!data || typeof data !== "object" || Array.isArray(data)) {
12
+ throw new Error("Invalid data: must be a non-empty object.");
13
+ }
14
+ };
15
+
16
+ module.exports = function(api) {
17
+ return {
18
+ async create(threadID, data) {
19
+ try {
20
+ let thread = await Thread.findOne({ where: { threadID } });
21
+ if (thread) {
22
+ return { thread: thread.get(), created: false };
23
+ }
24
+ thread = await Thread.create({ threadID, ...data });
25
+ return { thread: thread.get(), created: true };
26
+ } catch (error) {
27
+ throw new Error(`Failed to create thread: ${error.message}`);
28
+ }
29
+ },
30
+
31
+ async get(threadID) {
32
+ try {
33
+ threadID = validateThreadID(threadID);
34
+ const thread = await Thread.findOne({ where: { threadID } });
35
+ return thread ? thread.get() : null;
36
+ } catch (error) {
37
+ throw new Error(`Failed to get thread: ${error.message}`);
38
+ }
39
+ },
40
+
41
+ async update(threadID, data) {
42
+ try {
43
+ threadID = validateThreadID(threadID);
44
+ validateData(data);
45
+ const thread = await Thread.findOne({ where: { threadID } });
46
+
47
+ if (thread) {
48
+ await thread.update(data);
49
+ return { thread: thread.get(), created: false };
50
+ } else {
51
+ const newThread = await Thread.create({ ...data, threadID });
52
+ return { thread: newThread.get(), created: true };
53
+ }
54
+ } catch (error) {
55
+ throw new Error(`Failed to update thread: ${error.message}`);
56
+ }
57
+ },
58
+
59
+ async del(threadID) {
60
+ try {
61
+ if (!threadID) {
62
+ throw new Error("threadID is required and cannot be undefined");
63
+ }
64
+ threadID = validateThreadID(threadID);
65
+ if (!threadID) {
66
+ throw new Error("Invalid threadID");
67
+ }
68
+ const result = await Thread.destroy({ where: { threadID } });
69
+ if (result === 0) {
70
+ throw new Error("No thread found with the specified threadID");
71
+ }
72
+ return result;
73
+ } catch (error) {
74
+ throw new Error(`Failed to delete thread: ${error.message}`);
75
+ }
76
+ },
77
+
78
+ async delAll() {
79
+ try {
80
+ return await Thread.destroy({ where: {} });
81
+ } catch (error) {
82
+ throw new Error(`Failed to delete all threads: ${error.message}`);
83
+ }
84
+ },
85
+
86
+ async getAll(keys = null) {
87
+ try {
88
+ const attributes =
89
+ typeof keys === "string"
90
+ ? [keys]
91
+ : Array.isArray(keys)
92
+ ? keys
93
+ : undefined;
94
+ const threads = await Thread.findAll({ attributes });
95
+ return threads.map(thread => thread.get());
96
+ } catch (error) {
97
+ throw new Error(`Failed to get all threads: ${error.message}`);
98
+ }
99
+ }
100
+ };
101
+ };
@@ -0,0 +1,90 @@
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
+
10
+ const validateData = data => {
11
+ if (!data || typeof data !== "object" || Array.isArray(data)) {
12
+ throw new Error("Invalid data: must be a non-empty object.");
13
+ }
14
+ };
15
+
16
+ module.exports = function (api) {
17
+ return {
18
+ async create(userID, data) {
19
+ try {
20
+ userID = validateUserID(userID);
21
+ validateData(data);
22
+ let user = await User.findOne({ where: { userID } });
23
+ if (user) return { user: user.get(), created: false };
24
+ const payload = Object.prototype.hasOwnProperty.call(data, "data") ? data : { data };
25
+ user = await User.create({ userID, ...payload });
26
+ return { user: user.get(), created: true };
27
+ } catch (error) {
28
+ throw new Error(`Failed to create user: ${error.message}`);
29
+ }
30
+ },
31
+
32
+ async get(userID) {
33
+ try {
34
+ userID = validateUserID(userID);
35
+ const user = await User.findOne({ where: { userID } });
36
+ return user ? user.get() : null;
37
+ } catch (error) {
38
+ throw new Error(`Failed to get user: ${error.message}`);
39
+ }
40
+ },
41
+
42
+ async update(userID, data) {
43
+ try {
44
+ userID = validateUserID(userID);
45
+ validateData(data);
46
+ const payload = Object.prototype.hasOwnProperty.call(data, "data") ? data : { data };
47
+ const user = await User.findOne({ where: { userID } });
48
+ if (user) {
49
+ await user.update(payload);
50
+ return { user: user.get(), created: false };
51
+ } else {
52
+ const newUser = await User.create({ userID, ...payload });
53
+ return { user: newUser.get(), created: true };
54
+ }
55
+ } catch (error) {
56
+ throw new Error(`Failed to update user: ${error.message}`);
57
+ }
58
+ },
59
+
60
+ async del(userID) {
61
+ try {
62
+ if (!userID) throw new Error("userID is required and cannot be undefined");
63
+ userID = validateUserID(userID);
64
+ const result = await User.destroy({ where: { userID } });
65
+ if (result === 0) throw new Error("No user found with the specified userID");
66
+ return result;
67
+ } catch (error) {
68
+ throw new Error(`Failed to delete user: ${error.message}`);
69
+ }
70
+ },
71
+
72
+ async delAll() {
73
+ try {
74
+ return await User.destroy({ where: {} });
75
+ } catch (error) {
76
+ throw new Error(`Failed to delete all users: ${error.message}`);
77
+ }
78
+ },
79
+
80
+ async getAll(keys = null) {
81
+ try {
82
+ const attributes = typeof keys === "string" ? [keys] : Array.isArray(keys) ? keys : undefined;
83
+ const users = await User.findAll({ attributes });
84
+ return users.map(u => u.get());
85
+ } catch (error) {
86
+ throw new Error(`Failed to get all users: ${error.message}`);
87
+ }
88
+ }
89
+ };
90
+ };