diff --git a/README.md b/README.md index 1002c35..e45fdfb 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ And, you can learn about API in `util/APIDOCS.md`. | Send message | 🟢 | MEDIUM | | Create thread | 🟢 | MEDIUM | | Get info about thread | 🟢 | MEDIUM | -| Delete message & thread | 🔴 | HIGH | +| Delete message & thread | 🟢 | HIGH | | Edit message & thread | 🔴 | HIGH | ### Other diff --git a/errors/error.js b/errors/error.js deleted file mode 100644 index 738804e..0000000 --- a/errors/error.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = (res, type, error) => - res.status(type).render("error", { type, error }); diff --git a/index.js b/index.js index 31ee785..4d6cf89 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,6 @@ app.use(require("./middlewares/user")); for (const file of fs.readdirSync("./routes")) app.use("/" + file.replace(".js", ""), require(`./routes/${file}`)); -app.all("*", (req, res) => res.error(404, "We have not got this page.")); +app.all("*", (req, res) => res.status(404).render("error", { type: 404, error: "We have not got this page." })); app.listen(port, () => console.log("akf-forum on port:", port)); \ No newline at end of file diff --git a/public/js/send.js b/public/js/thread.js similarity index 57% rename from public/js/send.js rename to public/js/thread.js index 266d156..86208f7 100644 --- a/public/js/send.js +++ b/public/js/thread.js @@ -1,4 +1,32 @@ import request from "./request.js"; +document.addEventListener("click", async e => { + + if (e.target.id === "delete_thread") { + const response = await request("/api/threads/"+e.target.value+"/delete"); + if (response.result.deleted) { + alert("Thread deleted"); + window.location.href = "/threads"; + } + + } else if (e.target.id === "delete_message") { + const response = await request("/api/messages/" + e.target.value + "/delete"); + if (response.result.deleted) { + alert("Message deleted"); + location.reload(); + } + } /*else if (e.target.id === "edit_thread") { + window.location.href = "/threads/<%= thread.id %>/edit"; + } */ + + + + if (!e.target.id.includes("like")) return; + const res = await request("/api/messages/" + e.target.value + "/react/" + e.target.id) + + document.getElementById("count" + e.target.value).innerHTML = res.result; + +}); + document.getElementById("send").addEventListener("submit", async e => { diff --git a/routes/api/index.js b/routes/api/index.js index b9950b2..19fe9be 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -59,8 +59,8 @@ app.use(async (req, res, next) => { }); /* will add for loop */ -app.use("/messages", require("./routes/message")) -app.use("/users", require("./routes/user")) +app.use("/messages", require("./routes/messages")) +app.use("/users", require("./routes/users")) app.use("/threads", require("./routes/threads")) app.all("*", (req, res) => res.error(400, "Bad request")); diff --git a/routes/api/routes/message.js b/routes/api/routes/messages.js similarity index 79% rename from routes/api/routes/message.js rename to routes/api/routes/messages.js index e585f91..3be1e7c 100644 --- a/routes/api/routes/message.js +++ b/routes/api/routes/messages.js @@ -59,4 +59,17 @@ 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."); + const user = req.user; + if (user.id != message.authorID && !user.admin) + return res.error( 403, "You have not got permission for this."); + message.deleted = true; + await message.save(); + + res.complate(message); + +}) + module.exports = app; \ No newline at end of file diff --git a/routes/api/routes/threads.js b/routes/api/routes/threads.js index 363e566..a13dede 100644 --- a/routes/api/routes/threads.js +++ b/routes/api/routes/threads.js @@ -34,4 +34,18 @@ app.post("/", async (req, res) => { }); +app.post("/:id/delete", async (req, res) => { + const thread = await ThreadModel.get(req.params.id); + if (!thread || thread.deleted) return res.error( 404, "We have not got any thread declared as this id."); + const user = req.user; + if (user.id != thread.authorID && !user.admin) + return res.error( 403, "You have not got permission for this."); + + thread.deleted = true; + await thread.save(); + + res.complate(thread); + +}) + module.exports = app; \ No newline at end of file diff --git a/routes/api/routes/user.js b/routes/api/routes/users.js similarity index 100% rename from routes/api/routes/user.js rename to routes/api/routes/users.js diff --git a/routes/message.js b/routes/message.js index b19a92f..e28ba83 100644 --- a/routes/message.js +++ b/routes/message.js @@ -12,22 +12,4 @@ app.get("/:id", async (req, res) => { }); -app.use(require("../middlewares/login")); - -app.post("/:id/delete", async (req, res) => { - const message = await MessageModel.get(req.params.id); - if (!message || message.deleted) 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."); - message.deleted = true; - await message.save(); - - - res.status(200).redirect("/threads/" + message.threadID); - -}) - - - module.exports = app; \ No newline at end of file diff --git a/routes/threads.js b/routes/threads.js index 4a8d472..ad975d8 100644 --- a/routes/threads.js +++ b/routes/threads.js @@ -45,47 +45,4 @@ app.get("/:id", async (req, res) => { }); - - -app.use(require("../middlewares/login")); - - -app.post("/", rateLimit({ - windowMs: 10 * 60_000, max: 1, standardHeaders: true, legacyHeaders: false, - handler: (request, response, next, options) => - !request.user.admin ? - res.error(options.statusCode, "You are begin ratelimited") - : next() -}), async (req, res) => { - - const { title = null, content = null } = req.body; - - if (!title || !content) return res.error( 400, "Title and/or content is missing"); - const user = req.user - const thread = await new ThreadModel({ title, author: user }).takeId() - - const message = await new MessageModel({ content, author: user, threadID: thread.id }).takeId() - - await thread.push(message.id).save(); - - await message.save(); - - res.redirect('/threads/' + thread.id); -}) - -app.post("/:id/delete", async (req, res) => { - const thread = await ThreadModel.get(req.params.id); - if (!thread || thread.deleted) return res.error( 404, "We have not got any thread declared as this id."); - const user = req.user; - if (user.id != thread.authorID && !user.admin) - return res.error( 403, "You have not got permission for this."); - - thread.deleted = true; - await thread.save(); - - - res.status(200).redirect("/threads/"); - -}) - module.exports = app; \ No newline at end of file diff --git a/util/APIDOCS.md b/util/APIDOCS.md index 814a463..8f9ee36 100644 --- a/util/APIDOCS.md +++ b/util/APIDOCS.md @@ -17,10 +17,13 @@ You need this headers for send request to API: ## How to request? ### Request types: -- GET `/api/messages/:id` - GET `/api/users/:id` +- POST `/api/threads` - GET `/api/threads/:id` -- POST `/api/messages` +- POST `/api/threads/:id/delete` +- GET `/api/messages/:id` +- POST `/api/messages` +- POST `/api/messages/:id/delete` - POST `/api/messages/:id/react/:type` ### Example request: diff --git a/views/createThread.ejs b/views/createThread.ejs index 82cd198..4cf3dcb 100644 --- a/views/createThread.ejs +++ b/views/createThread.ejs @@ -9,7 +9,7 @@ -
+ <%- include("extra/footer") %>