notherbase-fs 4.1.0 → 4.1.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.
- package/controllers/spirit-world.js +55 -60
- package/controllers/user.js +13 -11
- package/package.json +1 -1
|
@@ -19,10 +19,20 @@ export default class SpiritWorld {
|
|
|
19
19
|
|
|
20
20
|
this.user = new User();
|
|
21
21
|
this.router = express.Router();
|
|
22
|
-
this.router.post("/loadAll", this.loadAll);
|
|
23
|
-
this.router.post("/load", this.load);
|
|
24
|
-
this.router.post("/serve", this.serve);
|
|
25
|
-
this.router.use("/user", this.user.router);
|
|
22
|
+
this.router.post("/loadAll", this.catchErrors, this.loadAll);
|
|
23
|
+
this.router.post("/load", this.catchErrors, this.load);
|
|
24
|
+
this.router.post("/serve", this.catchErrors, this.serve);
|
|
25
|
+
this.router.use("/user", this.catchErrors, this.user.router);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// abstract out the try catch pattern
|
|
29
|
+
catchErrors = async (req, res, next) => {
|
|
30
|
+
try {
|
|
31
|
+
await next();
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.log(error);
|
|
34
|
+
fail(res, "Server error");
|
|
35
|
+
}
|
|
26
36
|
}
|
|
27
37
|
|
|
28
38
|
/**
|
|
@@ -91,29 +101,24 @@ export default class SpiritWorld {
|
|
|
91
101
|
* @returns {Object} The requested spirits.
|
|
92
102
|
*/
|
|
93
103
|
loadAll = async (req, res) => {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
104
|
+
let parent = null;
|
|
105
|
+
let data = req.body.data ? req.body.data : {};
|
|
106
|
+
let id = req.body.id ? req.body.id : null;
|
|
107
|
+
|
|
108
|
+
// if the scope is local, the parent is the user's id
|
|
109
|
+
if (req.body.scope === "local") {
|
|
110
|
+
let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
|
|
111
|
+
if (user?.memory?._id) parent = user.memory._id;
|
|
112
|
+
else {
|
|
113
|
+
fail(res, "User had no id on load()");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
108
117
|
|
|
109
|
-
|
|
110
|
-
|
|
118
|
+
// recall all spirits with the given service name and parent
|
|
119
|
+
let spirits = await req.db.Spirit.recallAll(req.body.service, parent, data, id);
|
|
111
120
|
|
|
112
|
-
|
|
113
|
-
} catch (error) {
|
|
114
|
-
console.log(error);
|
|
115
|
-
fail(res, "Server error");
|
|
116
|
-
}
|
|
121
|
+
res.send(spirits);
|
|
117
122
|
}
|
|
118
123
|
|
|
119
124
|
/**
|
|
@@ -123,29 +128,24 @@ export default class SpiritWorld {
|
|
|
123
128
|
* @returns {Object} The requested spirit.
|
|
124
129
|
*/
|
|
125
130
|
load = async (req, res) => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
131
|
+
let parent = null;
|
|
132
|
+
let data = req.body.data ? req.body.data : {};
|
|
133
|
+
let id = req.body.id ? req.body.id : null;
|
|
134
|
+
|
|
135
|
+
// if the scope is local, the parent is the user's id
|
|
136
|
+
if (req.body.scope === "local") {
|
|
137
|
+
let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
|
|
138
|
+
if (user?.memory?._id) parent = user.memory._id;
|
|
139
|
+
else {
|
|
140
|
+
fail(res, "User had no id on load()");
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
140
144
|
|
|
141
|
-
|
|
142
|
-
|
|
145
|
+
// recall all spirits with the given service name and parent
|
|
146
|
+
let spirit = await req.db.Spirit.recallOne(req.body.service, parent, data, id);
|
|
143
147
|
|
|
144
|
-
|
|
145
|
-
} catch (error) {
|
|
146
|
-
console.log(error);
|
|
147
|
-
fail(res, "Server error");
|
|
148
|
-
}
|
|
148
|
+
res.send(spirit);
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
/**
|
|
@@ -154,22 +154,17 @@ export default class SpiritWorld {
|
|
|
154
154
|
* @param {Object} res
|
|
155
155
|
*/
|
|
156
156
|
serve = async (req, res) => {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
let script, result = null;
|
|
157
|
+
let scriptPath = `${req.contentPath}${req.body.route}/${req.body.script}.js`;
|
|
158
|
+
|
|
159
|
+
let script, result = null;
|
|
161
160
|
|
|
162
|
-
|
|
163
|
-
|
|
161
|
+
if (fs.existsSync(scriptPath)) {
|
|
162
|
+
let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
|
|
164
163
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
else fail(res, `Script not found: ${req.body.script} at ${scriptPath}`);
|
|
170
|
-
} catch (error) {
|
|
171
|
-
console.log(error);
|
|
172
|
-
fail(res, "Server error");
|
|
164
|
+
script = await import(process.env.WINDOWS == "true" ? `file://${scriptPath}` : scriptPath);
|
|
165
|
+
result = await script.default(req, user, this.io);
|
|
166
|
+
success(res, "Served.", result);
|
|
173
167
|
}
|
|
168
|
+
else fail(res, `Script not found: ${req.body.script} at ${scriptPath}`);
|
|
174
169
|
}
|
|
175
170
|
}
|
package/controllers/user.js
CHANGED
|
@@ -221,19 +221,21 @@ export default class User {
|
|
|
221
221
|
let user = await req.db.Spirit.recallOne("user", null, { username: req.session.currentUser });
|
|
222
222
|
|
|
223
223
|
if (check(res, user, "Account not found!")) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
224
|
+
if (check(res, req.body.password, "Password error.")) {
|
|
225
|
+
let passResult = await bcrypt.compare(req.body.password, user.memory.data.password);
|
|
226
|
+
|
|
227
|
+
if (check(res, passResult, "Password doesn't match the username.")) {
|
|
228
|
+
let data = JSON.parse(req.body.data);
|
|
229
|
+
let imported = 0;
|
|
230
|
+
for (let i = 0; i < data.length; i++) {
|
|
231
|
+
if (data[i].parent != null) {
|
|
232
|
+
let spirit = await req.db.Spirit.create(data[i].service, data[i].data, user.memory._id);
|
|
233
|
+
if (spirit) imported++;
|
|
234
|
+
}
|
|
233
235
|
}
|
|
234
|
-
}
|
|
235
236
|
|
|
236
|
-
|
|
237
|
+
success(res, "Data Imported", imported);
|
|
238
|
+
}
|
|
237
239
|
}
|
|
238
240
|
}
|
|
239
241
|
}
|