3lü ve güçlü

Co-authored-by: Sapphire1525 <Sapphire1525@users.noreply.github.com>

Co-authored-by: inoaa79 <inoaa79@users.noreply.github.com>
This commit is contained in:
Akif9748 2022-08-24 22:09:21 +03:00
parent 0d4dd2c369
commit 4be2069587
11 changed files with 36 additions and 29 deletions

View file

@ -75,7 +75,7 @@ And, you can learn about API in `util/APIDOCS.md`.
| auto-scroll | 🟢 | LOW | | auto-scroll | 🟢 | LOW |
| Multi-theme support | 🔴 | LOW | | Multi-theme support | 🔴 | LOW |
| Search | 🔴 | MEDIUM | | Search | 🔴 | MEDIUM |
| Better view | 🟢 | MEDIUM | | Better view, page support | 🔴 | MEDIUM |
| Sending message etc. will use fetch API | 🟢 | HIGH | | Sending message etc. will use fetch API | 🟢 | HIGH |
## Screenshot ## Screenshot

View file

@ -18,6 +18,10 @@ app.use(express.json());
app.use(async (req, res, next) => { app.use(async (req, res, next) => {
res.error = (type, error) => res.status(type).render("error", { type, error }); res.error = (type, error) => res.status(type).render("error", { type, error });
req.user = await UserModel.get(req.session.userid); req.user = await UserModel.get(req.session.userid);
if (user.deleted) {
req.session.destroy();
return res.error(403, "Your account has been deleted.");
}
next(); next();
}); });

View file

@ -16,6 +16,10 @@ const schema = new mongoose.Schema({
}) })
schema.virtual('authorID').get(function() { return this.author?.id; }); 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 () { schema.methods.takeId = async function () {
this.id = String(await model.count() || 0); this.id = String(await model.count() || 0);

View file

@ -8,7 +8,7 @@ export default async function request(link, method = "POST", body={}) {
} }
}).then(res => res.json()) }).then(res => res.json())
if (res.result.error) return alert(res.result.error); if (res.error) return alert(res.error);
return res; return res;

View file

@ -11,10 +11,9 @@ const { SecretModel, UserModel } = require("../../models")
*/ */
app.use(async (req, res, next) => { app.use(async (req, res, next) => {
res.error = (status, error) => res.error = (status, error) => res.status(status).json(error);
res.status(status).json({ status, result: { error } })
res.complate = result => res.status(200).json({ status: 200, result }); res.complate = result => res.status(200).json(result);
if (req.user) return next(); if (req.user) return next();
const { username = null, password = null } = req.headers; const { username = null, password = null } = req.headers;

View file

@ -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}.`); 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); 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(); const message = await new MessageModel({ content, author: req.user, threadID: thread.id }).takeId();
await message.save(); await message.save();
await thread.push(message.id).save(); await thread.push(message.id).save();
res.complate(message); res.complate(message.toObject({ virtuals: true }));
}) })
app.post("/:id/react/:type", async (req, res) => { app.post("/:id/react/:type", async (req, res) => {
@ -50,8 +50,8 @@ app.post("/:id/react/:type", async (req, res) => {
message.markModified("react"); message.markModified("react");
await message.save(); 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}.`); } 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) => { app.post("/:id/delete", async (req, res) => {
const message = await MessageModel.get(req.params.id); 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; const user = req.user;
if (user.id != message.authorID && !user.admin) 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; message.deleted = true;
await message.save(); await message.save();
res.complate(message); res.complate(message.toObject({ virtuals: true }));
}) })

View file

@ -9,9 +9,9 @@ app.get("/:id", async (req, res) => {
const thread = await ThreadModel.get(id); const thread = await ThreadModel.get(id);
if (thread && (req.user?.admin || !thread.deleted)) if (thread && (req.user?.admin || !thread.deleted))
res.complate(thread); res.complate(thread.toObject({ virtuals: true }));
else 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 limit = Number(req.query.limit);
const query = { threadID: id }; const query = { threadID: id };
if (!req.user.admin) query.deleted = false; if (!req.user.admin) query.deleted = false;
const options = { sort: { date: -1 } }; const options = { sort: { date: -1 } };
if (limit) options.limit = limit; 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."); 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 thread.push(message.id).save();
await message.save(); await message.save();
res.complate(thread); res.complate(thread.toObject({ virtuals: true }));
}); });
app.post("/:id/delete", async (req, res) => { app.post("/:id/delete", async (req, res) => {
const thread = await ThreadModel.get(req.params.id); 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; const user = req.user;
if (user.id != thread.authorID && !user.admin) if (user.id != thread.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.");
@ -62,7 +62,7 @@ app.post("/:id/delete", async (req, res) => {
thread.deleted = true; thread.deleted = true;
await thread.save(); await thread.save();
res.complate(thread); res.complate(thread.toObject({ virtuals: true }));
}) })

View file

@ -17,8 +17,8 @@ app.get("/:id", async (req, res) => {
if (member && (user?.admin || !member.deleted)) { if (member && (user?.admin || !member.deleted)) {
const message = await MessageModel.count({ authorID: id }); const message = await MessageModel.count({ "author.id": id });// this place was having problem. fixed
const thread = await ThreadModel.count({ authorID: id }); const thread = await ThreadModel.count({ "author.id": id });
res.render("user", { user, member, counts: { message, thread } }) res.render("user", { user, member, counts: { message, thread } })
} }
else res.error(404, "We have not got this user."); else res.error(404, "We have not got this user.");

View file

@ -25,8 +25,8 @@
const response = await request("/api/users/" + e.target[0].value + "/admin"); const response = await request("/api/users/" + e.target[0].value + "/admin");
if (response.result.admin) if (response.admin)
alert("Making admin of "+response.result.name+" is success!"); alert("Making admin of "+response.name+" is success!");
}); });

View file

@ -36,9 +36,9 @@
}); });
if (response.result) { if (response) {
alert("Thread opened"); alert("Thread opened");
window.location.href = "/threads/" + response.result.id; window.location.href = "/threads/" + response.id;
} }
}); });

View file

@ -60,14 +60,14 @@
const response = await request("/api/users/<%= member.id %>/admin"); const response = await request("/api/users/<%= member.id %>/admin");
if (response.result.admin) if (response.admin)
return alert("Making admin of " + response.result.name + " is success!"); return alert("Making admin of " + response.name + " is success!");
} }
const response = await request("/api/users/<%= member.id %>/delete"); const response = await request("/api/users/<%= member.id %>/delete");
if (response.result.deleted) if (response.deleted)
alert("User Deleted"); alert("User Deleted");
}); });