diff --git a/index.js b/index.js index dbeb6d3..8d734e2 100644 --- a/index.js +++ b/index.js @@ -22,8 +22,7 @@ app.use( session({ secret: 'secret', resave: true, saveUninitialized: true }), express.static("public"), express.json(), ipBlock(app.ips), async (req, res, next) => { - if (req.session.userID) - req.user = await UserModel.findOneAndUpdate({ id: req.session.userID }, { lastSeen: Date.now() }); + req.user = req.session.userID ? await UserModel.findOneAndUpdate({ id: req.session.userID }, { lastSeen: Date.now() }) : null; res.reply = (page, options = {}, status = 200) => res.status(status) .render(page, { user: req.user, theme: req.user?.theme || def_theme, forum_name, desp, ...options }); diff --git a/models/Category.js b/models/Category.js index c43f7f5..15c278a 100644 --- a/models/Category.js +++ b/models/Category.js @@ -11,6 +11,11 @@ schema.methods.takeId = async function () { this.id = String(await model.count() || 0); return this; } + +schema.methods.getLink = function (id = this.id) { + return "/categories/" + this.id; +} + const model = mongoose.model('category', schema); module.exports = model; \ No newline at end of file diff --git a/routes/api/routes/categories.js b/routes/api/routes/categories.js index cac0c3a..ed85e65 100644 --- a/routes/api/routes/categories.js +++ b/routes/api/routes/categories.js @@ -40,8 +40,8 @@ app.post("/", async (req, res) => { if (!name) return res.error(400, "You have to give a name for the category."); if (await CategoryModel.exists({ name })) return res.error(400, "This category is already opened."); - - res.complate(await CategoryModel.create({ name, desp, authorID: req.user.id }).then(c => c.takeId())); + const category = await new CategoryModel({ name, desp, authorID: req.user.id }).takeId(); + res.complate(await category.save()); }); diff --git a/routes/api/routes/threads.js b/routes/api/routes/threads.js index e3fed7a..2895605 100644 --- a/routes/api/routes/threads.js +++ b/routes/api/routes/threads.js @@ -39,7 +39,7 @@ app.get("/:id/messages/", async (req, res) => { app.post("/", async (req, res) => { const { title, content } = req.body; - +return console.log(req.body) if (!content || !title) return res.error(400, "Missing content/title in request body."); const { user } = req; diff --git a/routes/categories.js b/routes/categories.js new file mode 100644 index 0000000..ba3a3d7 --- /dev/null +++ b/routes/categories.js @@ -0,0 +1,25 @@ +const { CategoryModel,ThreadModel } = require("../models"); + +const { Router } = require("express"); + +const app = Router(); +app.get("/", async (req, res) => { + const categories = await CategoryModel.find({}); + res.reply("categories", { categories }); +}); + +app.get("/create",(req,res)=>res.reply("create_category")); +app.get("/:id", async (req, res) => { + const category = await CategoryModel.findOne({ id: req.params.id }); + if (!category) return res.error(404, "Category not found."); + const page = Number(req.query.page) || 0; + const query = { categoryID: category.id }; + if (!req.user?.admin) query.deleted= false; + + let threads = await ThreadModel.find(query).limit(10).skip(page * 10); + threads = await Promise.all(threads.map(thread => thread.get_author())); + + res.reply("threads", { threads, page, title: `Threads in ${category.name}`, desp: category.desp, pages: Math.ceil(await ThreadModel.count(query) / 10) }); +}); + +module.exports = app; \ No newline at end of file diff --git a/routes/search.js b/routes/search.js index ee7eb19..14807d3 100644 --- a/routes/search.js +++ b/routes/search.js @@ -42,8 +42,7 @@ app.get("/threads", async (req, res) => { if (req.query.authorID) query.authorID = req.query.authorID; const threads = await ThreadModel.find(query, null, req.so) res.reply("threads", { - threads, - page: null, page: req.page, + threads, page: req.page, title: "Threads with query " + req.query.q, pages: Math.ceil(await ThreadModel.count(query) / 10) }); }); diff --git a/routes/threads.js b/routes/threads.js index e58b0bf..f8a9010 100644 --- a/routes/threads.js +++ b/routes/threads.js @@ -1,7 +1,7 @@ const { Router } = require("express"); const app = Router(); const { clearContent } = require("../lib"); -const { ThreadModel, MessageModel } = require("../models") +const { ThreadModel, MessageModel, CategoryModel } = require("../models") app.get("/", async (req, res) => { const page = Number(req.query.page) || 0; @@ -9,11 +9,11 @@ app.get("/", async (req, res) => { let threads = await ThreadModel.find(query).limit(10).skip(page * 10); threads = await Promise.all(threads.map(thread => thread.get_author())); - return res.reply("threads", { threads, page, pages: Math.ceil(await ThreadModel.count(query) / 10) }); + return res.reply("threads", { threads, page, title: "Threads", desp: threads.length + " thread listed", pages: Math.ceil(await ThreadModel.count(query) / 10) }); }); -app.get("/create/", (req, res) => res.reply("create_thread")); +app.get("/create/", async (req, res) => res.reply("create_thread", { categories: await CategoryModel.find() })); app.get("/:id/", async (req, res) => { diff --git a/views/categories.ejs b/views/categories.ejs new file mode 100644 index 0000000..31a1a06 --- /dev/null +++ b/views/categories.ejs @@ -0,0 +1,58 @@ + + +<%- include("extra/meta", {title: "Thread list!" }) %> + + + + + + + + <%- include("extra/navbar") %> +
+ <%if(user?.admin) {%> + Create Category + <% }; %> + <% categories.forEach(category=>{ %> + +
+
+ <%= category.name %> +
+ +
<%if(user?.admin) {%> + + <% }; %><%= category.desp %> +
+ +
+ +
+ <% }); %> + +
+ <% if(typeof page === "number"){ %> + + + <% } %> + + \ No newline at end of file diff --git a/views/create_category.ejs b/views/create_category.ejs new file mode 100644 index 0000000..c9e6717 --- /dev/null +++ b/views/create_category.ejs @@ -0,0 +1,38 @@ + + +<%- include("extra/meta", {title: "Create Category!" }) %> + + + + + <%- include("extra/navbar") %> + +
+

Name:

+ +

Description:

+ + +
+ + + + + \ No newline at end of file diff --git a/views/create_thread.ejs b/views/create_thread.ejs index 3b23a43..54b3d6b 100644 --- a/views/create_thread.ejs +++ b/views/create_thread.ejs @@ -12,11 +12,13 @@

Title:

- -

Content:

- +
@@ -32,7 +34,8 @@ const response = await request("/api/threads/", "POST", { title: data.get("title"), - content: data.get("content") + content: data.get("content"), + categord: data.get("category") }); diff --git a/views/extra/navbar.ejs b/views/extra/navbar.ejs index b40e075..14057f6 100644 --- a/views/extra/navbar.ejs +++ b/views/extra/navbar.ejs @@ -43,6 +43,7 @@