2022-04-06 21:14:46 +03:00
|
|
|
const { Router } = require("express");
|
|
|
|
const app = Router();
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
const error = require("../errors/error");
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
const { User } = require("../classes");
|
|
|
|
const { UserModel, MessageModel, ThreadModel } = require("../models");
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
app.get("/", async (req, res) => {
|
|
|
|
const user = req.user
|
|
|
|
const users = await UserModel.find({});
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
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-04-06 21:14:46 +03:00
|
|
|
app.get("/:id", async (req, res) => {
|
|
|
|
const user = req.user
|
2022-03-21 23:53:22 +03:00
|
|
|
const { id = null } = req.params;
|
2022-04-06 21:14:46 +03:00
|
|
|
const member = await new User().getById(req.params.id);
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
|
2022-04-03 21:01:55 +03:00
|
|
|
if (member && (user?.admin || !member.deleted)) {
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
const message = await MessageModel.count({ authorID: id });
|
|
|
|
const thread = await ThreadModel.count({ authorID: id });
|
2022-03-21 23:53:22 +03:00
|
|
|
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-06 21:14:46 +03:00
|
|
|
app.post("/:id/delete/", async (req, res) => {
|
|
|
|
const user = req.user;
|
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.");
|
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
const { id = null } = req.params;
|
|
|
|
const member = await new User().getById(id);
|
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
if (!member || member.deleted) return error(res, 404, "We have not got any user declared as this id.");
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
member.deleted = true;
|
|
|
|
member.write();
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
res.redirect("/admin");
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = app;
|