diff --git a/README.md b/README.md index e4bc180..9170f0e 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ And, you can learn about API in `util/APIDOCS.md`. | auto-scroll | 🟢 | LOW | | Multi-theme support | 🔴 | LOW | | Search | 🔴 | MEDIUM | -| Better view | 🟢 | MEDIUM | +| Better view, page support | 🔴 | MEDIUM | | Sending message etc. will use fetch API | 🟢 | HIGH | ## Screenshot diff --git a/index.js b/index.js index 0f67fb0..81e751a 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,10 @@ app.use(express.json()); app.use(async (req, res, next) => { res.error = (type, error) => res.status(type).render("error", { type, error }); req.user = await UserModel.get(req.session.userid); + if (user.deleted) { + req.session.destroy(); + return res.error(403, "Your account has been deleted."); + } next(); }); diff --git a/models/Message.js b/models/Message.js index aa28eb9..8501fc2 100644 --- a/models/Message.js +++ b/models/Message.js @@ -16,6 +16,10 @@ const schema = new mongoose.Schema({ }) schema.virtual('authorID').get(function() { return this.author?.id; }); +schema.virtual('reactCount').get(function() { + const arr = Object.values(this.react) + return arr.filter(Boolean).length - arr.filter(x => !x).length; +}); schema.methods.takeId = async function () { this.id = String(await model.count() || 0); diff --git a/public/js/request.js b/public/js/request.js index 928e68d..acdde64 100644 --- a/public/js/request.js +++ b/public/js/request.js @@ -8,7 +8,7 @@ export default async function request(link, method = "POST", body={}) { } }).then(res => res.json()) - if (res.result.error) return alert(res.result.error); + if (res.error) return alert(res.error); return res; diff --git a/routes/api/index.js b/routes/api/index.js index dc46bef..f3f37d3 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -11,10 +11,9 @@ const { SecretModel, UserModel } = require("../../models") */ app.use(async (req, res, next) => { - res.error = (status, error) => - res.status(status).json({ status, result: { error } }) + res.error = (status, error) => res.status(status).json(error); - res.complate = result => res.status(200).json({ status: 200, result }); + res.complate = result => res.status(200).json(result); if (req.user) return next(); const { username = null, password = null } = req.headers; diff --git a/routes/api/routes/messages.js b/routes/api/routes/messages.js index 0687156..13210fe 100644 --- a/routes/api/routes/messages.js +++ b/routes/api/routes/messages.js @@ -12,7 +12,7 @@ app.get("/:id", async (req, res) => { if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, `We don't have any thread with id ${id}.`); - res.complate(message); + res.complate(message.toObject({ virtuals: true })); }) @@ -29,13 +29,13 @@ app.post("/", rateLimit({ const thread = await ThreadModel.get(threadID); - if (!thread) return res.error(404, `We don't have any thread with id ${threadID}.`); + if (!thread) return res.error(404, `We don't have any thread with id ${threadID}.`); const message = await new MessageModel({ content, author: req.user, threadID: thread.id }).takeId(); await message.save(); await thread.push(message.id).save(); - res.complate(message); + res.complate(message.toObject({ virtuals: true })); }) app.post("/:id/react/:type", async (req, res) => { @@ -50,8 +50,8 @@ app.post("/:id/react/:type", async (req, res) => { message.markModified("react"); await message.save(); - const arr = Object.values(message.react) - res.complate(arr.filter(Boolean).length - arr.filter(x => !x).length) + + res.complate(message.toObject({ virtuals: true })); } else error(res, 404, `We don't have any message with id ${req.params.id}.`); @@ -59,14 +59,14 @@ app.post("/:id/react/:type", async (req, res) => { app.post("/:id/delete", async (req, res) => { const message = await MessageModel.get(req.params.id); - if (!message || (message.deleted && req.user && !req.user.admin)) return res.error( 404, "We have not got any message declared as this id."); + if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, "We have not got any message declared as this id."); const user = req.user; if (user.id != message.authorID && !user.admin) - return res.error( 403, "You have not got permission for this."); + return res.error(403, "You have not got permission for this."); message.deleted = true; await message.save(); - res.complate(message); + res.complate(message.toObject({ virtuals: true })); }) diff --git a/routes/api/routes/threads.js b/routes/api/routes/threads.js index cfb8251..961d828 100644 --- a/routes/api/routes/threads.js +++ b/routes/api/routes/threads.js @@ -9,9 +9,9 @@ app.get("/:id", async (req, res) => { const thread = await ThreadModel.get(id); if (thread && (req.user?.admin || !thread.deleted)) - res.complate(thread); + res.complate(thread.toObject({ virtuals: true })); else - return res.error(404, `We don't have any thread with id ${id}.`); + return res.error(404, `We don't have any thread with id ${id}.`); }); @@ -23,7 +23,7 @@ app.get("/:id/messages/", async (req, res) => { const limit = Number(req.query.limit); const query = { threadID: id }; - if (!req.user.admin) query.deleted = false; + if (!req.user.admin) query.deleted = false; const options = { sort: { date: -1 } }; if (limit) options.limit = limit; @@ -32,7 +32,7 @@ app.get("/:id/messages/", async (req, res) => { if (!messages.length) return res.error(404, "We don't have any messages in this thread."); - res.complate(messages); + res.complate(messages.toObject({ virtuals: true })); }) @@ -48,13 +48,13 @@ app.post("/", async (req, res) => { await thread.push(message.id).save(); await message.save(); - res.complate(thread); + res.complate(thread.toObject({ virtuals: true })); }); app.post("/:id/delete", async (req, res) => { const thread = await ThreadModel.get(req.params.id); - if (!thread || thread.deleted) return res.error(404, `We don't have any thread with id ${req.params.id}.`); + if (!thread || thread.deleted) return res.error(404, `We don't have any thread with id ${req.params.id}.`); const user = req.user; if (user.id != thread.authorID && !user.admin) return res.error(403, "You have not got permission for this."); @@ -62,7 +62,7 @@ app.post("/:id/delete", async (req, res) => { thread.deleted = true; await thread.save(); - res.complate(thread); + res.complate(thread.toObject({ virtuals: true })); }) diff --git a/routes/users.js b/routes/users.js index a62f381..32e301c 100644 --- a/routes/users.js +++ b/routes/users.js @@ -17,8 +17,8 @@ app.get("/:id", async (req, res) => { if (member && (user?.admin || !member.deleted)) { - const message = await MessageModel.count({ authorID: id }); - const thread = await ThreadModel.count({ authorID: id }); + const message = await MessageModel.count({ "author.id": id });// this place was having problem. fixed + const thread = await ThreadModel.count({ "author.id": id }); res.render("user", { user, member, counts: { message, thread } }) } else res.error(404, "We have not got this user."); diff --git a/views/admin.ejs b/views/admin.ejs index 2495828..59a52ec 100644 --- a/views/admin.ejs +++ b/views/admin.ejs @@ -25,8 +25,8 @@ const response = await request("/api/users/" + e.target[0].value + "/admin"); - if (response.result.admin) - alert("Making admin of "+response.result.name+" is success!"); + if (response.admin) + alert("Making admin of "+response.name+" is success!"); }); diff --git a/views/createThread.ejs b/views/createThread.ejs index db9204b..1e9007c 100644 --- a/views/createThread.ejs +++ b/views/createThread.ejs @@ -36,9 +36,9 @@ }); - if (response.result) { + if (response) { alert("Thread opened"); - window.location.href = "/threads/" + response.result.id; + window.location.href = "/threads/" + response.id; } }); diff --git a/views/user.ejs b/views/user.ejs index af2c5e7..cc2eda2 100644 --- a/views/user.ejs +++ b/views/user.ejs @@ -60,14 +60,14 @@ const response = await request("/api/users/<%= member.id %>/admin"); - if (response.result.admin) - return alert("Making admin of " + response.result.name + " is success!"); + if (response.admin) + return alert("Making admin of " + response.name + " is success!"); } const response = await request("/api/users/<%= member.id %>/delete"); - if (response.result.deleted) + if (response.deleted) alert("User Deleted"); });