akf-forum/routes/users.js

55 lines
1.7 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) => {
if (!req.session.loggedin) return res.redirect('/login');
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 })
});
app.get("/:id", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid)
const { id = null } = req.params;
const member = new User().getId(req.params.id);
if (member && (user.admin || !member.deleted)) {
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.");
});
app.post("/delete/:id", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid);
if (!user.admin)
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;