notherbase-fs 4.2.1 → 4.2.2

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.
@@ -58,7 +58,6 @@ export default class Creation {
58
58
  await stats.commit();
59
59
 
60
60
  let context = {
61
- userID: null,
62
61
  user: null,
63
62
  siteTitle: req.siteTitle,
64
63
  main: req.main,
@@ -69,7 +68,17 @@ export default class Creation {
69
68
  }
70
69
 
71
70
  if (req.session.currentUser) {
72
- context.user = await req.db.Spirit.recallOne("user", null, { username: req.session.currentUser });
71
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session.currentUser });
72
+ context.user = {
73
+ memory: {
74
+ data: user.memory.data,
75
+ backups: user.memory.backups,
76
+ _id: user.memory._id,
77
+ parent: user.memory.parent,
78
+ service: user.memory.service,
79
+ _lastUpdate: user.memory._lastUpdate
80
+ }
81
+ }
73
82
  }
74
83
 
75
84
  //preprocess
@@ -19,20 +19,10 @@ export default class SpiritWorld {
19
19
 
20
20
  this.user = new User();
21
21
  this.router = express.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
- }
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);
36
26
  }
37
27
 
38
28
  /**
@@ -40,46 +30,22 @@ export default class SpiritWorld {
40
30
  * @param {*} socket
41
31
  */
42
32
  setupChat = (socket) => {
43
- let roomName = socket.handshake.query.room;
44
- socket.join(roomName);
45
- let room = this.rooms[roomName];
46
- if (room) room.users.push(socket.handshake.query.name);
47
- else {
48
- this.rooms[roomName] = {
49
- users: [ socket.handshake.query.name ]
50
- }
51
- room = this.rooms[roomName];
52
- }
53
-
54
- this.io.to(roomName).emit("chat message", {
55
- name: "Server",
56
- time: Date.now(),
57
- text: `${socket.handshake.query.name} has joined the room.`
58
- });
59
-
60
- this.io.to(roomName).emit("chat info", {
61
- name: socket.handshake.query.room,
62
- time: Date.now(),
63
- data: {
64
- users: room.users
33
+ try {
34
+ let roomName = socket.handshake.query.room;
35
+ socket.join(roomName);
36
+ let room = this.rooms[roomName];
37
+ if (room) room.users.push(socket.handshake.query.name);
38
+ else {
39
+ this.rooms[roomName] = {
40
+ users: [ socket.handshake.query.name ]
41
+ }
42
+ room = this.rooms[roomName];
65
43
  }
66
- });
67
-
68
- socket.on("chat message", (msg) => {
69
- this.io.to(roomName).emit("chat message", {
70
- name: msg.name,
71
- time: msg.time,
72
- text: stripHtml(msg.text).result
73
- });
74
- });
75
-
76
- socket.on('disconnect', () => {
77
- room.users.splice(room.users.indexOf(socket.handshake.query.name));
78
-
44
+
79
45
  this.io.to(roomName).emit("chat message", {
80
46
  name: "Server",
81
47
  time: Date.now(),
82
- text: `${socket.handshake.query.name} has left the room.`
48
+ text: `${socket.handshake.query.name} has joined the room.`
83
49
  });
84
50
 
85
51
  this.io.to(roomName).emit("chat info", {
@@ -89,9 +55,38 @@ export default class SpiritWorld {
89
55
  users: room.users
90
56
  }
91
57
  });
92
-
93
- if (room.users.length < 1) delete this.rooms[roomName];
94
- });
58
+
59
+ socket.on("chat message", (msg) => {
60
+ this.io.to(roomName).emit("chat message", {
61
+ name: msg.name,
62
+ time: msg.time,
63
+ text: stripHtml(msg.text).result
64
+ });
65
+ });
66
+
67
+ socket.on('disconnect', () => {
68
+ room.users.splice(room.users.indexOf(socket.handshake.query.name));
69
+
70
+ this.io.to(roomName).emit("chat message", {
71
+ name: "Server",
72
+ time: Date.now(),
73
+ text: `${socket.handshake.query.name} has left the room.`
74
+ });
75
+
76
+ this.io.to(roomName).emit("chat info", {
77
+ name: socket.handshake.query.room,
78
+ time: Date.now(),
79
+ data: {
80
+ users: room.users
81
+ }
82
+ });
83
+
84
+ if (room.users.length < 1) delete this.rooms[roomName];
85
+ });
86
+ } catch (error) {
87
+ console.log(error);
88
+ fail(res, "Server error: Sockets");
89
+ }
95
90
  }
96
91
 
97
92
  /**
@@ -101,24 +96,29 @@ export default class SpiritWorld {
101
96
  * @returns {Object} The requested spirits.
102
97
  */
103
98
  loadAll = async (req, res) => {
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
- }
99
+ try {
100
+ let parent = null;
101
+ let data = req.body.data ? req.body.data : {};
102
+ let id = req.body.id ? req.body.id : null;
103
+
104
+ // if the scope is local, the parent is the user's id
105
+ if (req.body.scope === "local") {
106
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
107
+ if (user?.memory?._id) parent = user.memory._id;
108
+ else {
109
+ fail(res, "User had no id on load()");
110
+ return;
111
+ }
112
+ }
117
113
 
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);
114
+ // recall all spirits with the given service name and parent
115
+ let spirits = await req.db.Spirit.recallAll(req.body.service, parent, data, id);
120
116
 
121
- res.send(spirits);
117
+ res.send(spirits);
118
+ } catch (error) {
119
+ console.log(error);
120
+ fail(res, "Server error");
121
+ }
122
122
  }
123
123
 
124
124
  /**
@@ -128,24 +128,29 @@ export default class SpiritWorld {
128
128
  * @returns {Object} The requested spirit.
129
129
  */
130
130
  load = async (req, res) => {
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
- }
131
+ try {
132
+ let parent = null;
133
+ let data = req.body.data ? req.body.data : {};
134
+ let id = req.body.id ? req.body.id : null;
135
+
136
+ // if the scope is local, the parent is the user's id
137
+ if (req.body.scope === "local") {
138
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
139
+ if (user?.memory?._id) parent = user.memory._id;
140
+ else {
141
+ fail(res, "User had no id on load()");
142
+ return;
143
+ }
144
+ }
144
145
 
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);
146
+ // recall all spirits with the given service name and parent
147
+ let spirit = await req.db.Spirit.recallOne(req.body.service, parent, data, id);
147
148
 
148
- res.send(spirit);
149
+ res.send(spirit);
150
+ } catch (error) {
151
+ console.log(error);
152
+ fail(res, "Server error");
153
+ }
149
154
  }
150
155
 
151
156
  /**
@@ -154,17 +159,22 @@ export default class SpiritWorld {
154
159
  * @param {Object} res
155
160
  */
156
161
  serve = async (req, res) => {
157
- let scriptPath = `${req.contentPath}${req.body.route}/${req.body.script}.js`;
162
+ try {
163
+ let scriptPath = `${req.contentPath}${req.body.route}/${req.body.script}.js`;
158
164
 
159
- let script, result = null;
165
+ let script, result = null;
160
166
 
161
- if (fs.existsSync(scriptPath)) {
162
- let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
167
+ if (fs.existsSync(scriptPath)) {
168
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
163
169
 
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);
167
- }
168
- else fail(res, `Script not found: ${req.body.script} at ${scriptPath}`);
170
+ script = await import(process.env.WINDOWS == "true" ? `file://${scriptPath}` : scriptPath);
171
+ result = await script.default(req, user, this.io);
172
+ success(res, "Served.", result);
173
+ }
174
+ else fail(res, `Script not found: ${req.body.script} at ${scriptPath}`);
175
+ } catch (error) {
176
+ console.log(error);
177
+ fail(res, "Server error");
178
+ }
169
179
  }
170
180
  }
package/models/index.js CHANGED
@@ -19,10 +19,7 @@ mongoose.connection.on('disconnected', () => {
19
19
  });
20
20
 
21
21
  try {
22
- mongoose.connect(process.env.MONGODB_URI, {
23
- useNewUrlParser: true,
24
- useUnifiedTopology: true
25
- });
22
+ mongoose.connect(process.env.MONGODB_URI);
26
23
  }
27
24
  catch (err) {
28
25
  console.log(`Mongoose on connect: ${err}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "4.2.1",
3
+ "version": "4.2.2",
4
4
  "description": "Functions to help make developing for NotherBase easier.",
5
5
  "exports": "./notherbase-fs.js",
6
6
  "scripts": {
@@ -19,15 +19,15 @@
19
19
  },
20
20
  "homepage": "https://github.com/Mosshide/notherbase-fs#readme",
21
21
  "dependencies": {
22
- "bcrypt": "^5.0.1",
23
- "connect-mongo": "^4.6.0",
24
- "dotenv": "^14.2.0",
22
+ "bcrypt": "^5.1.1",
23
+ "connect-mongo": "^5.1.0",
24
+ "dotenv": "^16.4.5",
25
25
  "ejs": "^3.1.6",
26
- "express": "^4.17.1",
26
+ "express": "^4.19.2",
27
27
  "express-session": "^1.17.2",
28
28
  "express-subdomain": "^1.0.6",
29
29
  "googleapis": "^100.0.0",
30
- "mongoose": "^6.1.7",
30
+ "mongoose": "^8.6.2",
31
31
  "nodemailer": "^6.9.14",
32
32
  "serve-favicon": "^2.5.0",
33
33
  "socket.io": "^4.4.1",