akf-forum/routes/users.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-03-21 23:53:22 +03:00
const { User } = require("../classes");
const db = require("quick.db")
const { Router } = require("express")
const error = require("../errors/error")
const app = Router();
app.get("/", (req, res) => {
const user = new User().getId(req.session.userid)
const users = db.get("users").slice(0);
const links = users.filter(user => !user.deleted).map(user => "/users/" + user.id);
return res.render("users", { users, links, user })
});
2022-04-03 21:01:55 +03:00
2022-03-21 23:53:22 +03:00
app.get("/:id", (req, res) => {
const user = new User().getId(req.session.userid)
const { id = null } = req.params;
const member = new User().getId(req.params.id);
2022-04-03 21:01:55 +03:00
if (member && (user?.admin || !member.deleted)) {
2022-03-21 23:53:22 +03:00
const message = db.get("messages").filter(message => message.author.id === Number(id)).length
const thread = db.get("threads").filter(thread => thread.author.id === Number(id)).length
const counts = { message, thread }
res.render("user", { user, member, counts })
}
else error(res, 404, "We have not got this user.");
});
2022-04-03 21:01:55 +03:00
app.use(require("../middlewares/login"));
2022-04-03 19:55:33 +03:00
app.post("/:id/delete/", (req, res) => {
2022-03-21 23:53:22 +03:00
const user = new User().getId(req.session.userid);
2022-04-03 21:01:55 +03:00
if (!user?.admin)
2022-03-21 23:53:22 +03:00
return error(res, 403, "You have not got permission for this.");
const id = req.url.slice(9 + 3)
const member = new User().getId(id);
if (!member || member.deleted) return error(res, 404, "We have not got any user declared as this id.");
member.deleted = true;
member.write();
res.redirect("/admin");
});
module.exports = app;