akf-forum/routes/get/threads.js

25 lines
895 B
JavaScript
Raw Normal View History

2022-02-26 21:12:54 +03:00
const { Thread, Message, User } = require("../../classes/index");
const db = require("quick.db");
2022-03-13 16:06:25 +03:00
const error = require("../../errors/error.js");
2022-02-26 21:12:54 +03:00
2022-03-13 16:06:25 +03:00
module.exports = (req, res) => {
2022-02-26 21:12:54 +03:00
if (!req.session.loggedin) return res.redirect('/login');
const id = req.url.slice(9);
const user = new User().getId(req.session.userid);
if (!id) {
const threads = db.get("threads").slice(0, 10)
const links = threads.map(thread => "/threads/" + threads.indexOf(thread))
2022-03-13 16:06:25 +03:00
return res.render("threads", { threads, links, user})
2022-02-26 21:12:54 +03:00
}
const thread = new Thread().getId(id);
2022-03-13 16:06:25 +03:00
if (thread) {
const messages = thread.messages.filter(id => !new Message().getId(id).deleted).map(id => new Message().getId(id));
res.render("thread", { thread, messages, user })
} else
2022-02-26 21:12:54 +03:00
error(res, 404, "We have not got this thread.");
}