akf-forum/routes/users.js

28 lines
806 B
JavaScript
Raw Normal View History

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 { UserModel, MessageModel, ThreadModel } = require("../models");
2022-03-21 23:53:22 +03:00
2022-08-09 19:48:10 +03:00
app.get("/", async ({ user }, res) => {
2022-08-10 00:44:13 +03:00
const users = await UserModel.find(user?.admin ? {} : { deleted: false });
return res.render("users", { users, user })
2022-03-21 23:53:22 +03:00
});
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;
const member = await UserModel.get(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-08-09 19:48:10 +03:00
res.render("user", { user, member, counts: { message, thread } })
2022-03-21 23:53:22 +03:00
}
2022-08-11 00:49:44 +03:00
else res.error(404, "We have not got this user.");
2022-03-21 23:53:22 +03:00
});
2022-04-03 21:01:55 +03:00
2022-03-21 23:53:22 +03:00
module.exports = app;