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 |
| Multi-theme support | 🔴 | LOW |
| Search | 🔴 | MEDIUM |
| Better view | 🟢 | MEDIUM |
| Better view, page support | 🔴 | MEDIUM |
| Sending message etc. will use fetch API | 🟢 | HIGH |
## Screenshot

View file

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

View file

@ -16,6 +16,10 @@ const schema = new mongoose.Schema({
})
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 () {
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())
if (res.result.error) return alert(res.result.error);
if (res.error) return alert(res.error);
return res;

View file

@ -11,10 +11,9 @@ const { SecretModel, UserModel } = require("../../models")
*/
app.use(async (req, res, next) => {
res.error = (status, error) =>
res.status(status).json({ status, result: { error } })
res.error = (status, error) => res.status(status).json(error);
res.complate = result => res.status(200).json({ status: 200, result });
res.complate = result => res.status(200).json(result);
if (req.user) return next();
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}.`);
res.complate(message);
res.complate(message.toObject({ virtuals: true }));
})
@ -29,13 +29,13 @@ app.post("/", rateLimit({
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();
await message.save();
await thread.push(message.id).save();
res.complate(message);
res.complate(message.toObject({ virtuals: true }));
})
app.post("/:id/react/:type", async (req, res) => {
@ -50,8 +50,8 @@ app.post("/:id/react/:type", async (req, res) => {
message.markModified("react");
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}.`);
@ -59,14 +59,14 @@ app.post("/:id/react/:type", async (req, res) => {
app.post("/:id/delete", async (req, res) => {
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;
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;
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);
if (thread && (req.user?.admin || !thread.deleted))
res.complate(thread);
res.complate(thread.toObject({ virtuals: true }));
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 query = { threadID: id };
if (!req.user.admin) query.deleted = false;
if (!req.user.admin) query.deleted = false;
const options = { sort: { date: -1 } };
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.");
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 message.save();
res.complate(thread);
res.complate(thread.toObject({ virtuals: true }));
});
app.post("/:id/delete", async (req, res) => {
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;
if (user.id != thread.authorID && !user.admin)
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;
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)) {
const message = await MessageModel.count({ authorID: id });
const thread = await ThreadModel.count({ authorID: id });
const message = await MessageModel.count({ "author.id": id });// this place was having problem. fixed
const thread = await ThreadModel.count({ "author.id": id });
res.render("user", { user, member, counts: { message, thread } })
}
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");
if (response.result.admin)
alert("Making admin of "+response.result.name+" is success!");
if (response.admin)
alert("Making admin of "+response.name+" is success!");
});

View file

@ -36,9 +36,9 @@
});
if (response.result) {
if (response) {
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");
if (response.result.admin)
return alert("Making admin of " + response.result.name + " is success!");
if (response.admin)
return alert("Making admin of " + response.name + " is success!");
}
const response = await request("/api/users/<%= member.id %>/delete");
if (response.result.deleted)
if (response.deleted)
alert("User Deleted");
});