Clean up changes

Seperated all routes into seperate files for neatness, and I've made the comments.json store only the comments, comments_counter is in the new data.json file which also stores the hitcount.

Signed-off-by: max <deadvey@localhost.localdomain>
This commit is contained in:
max
2025-09-24 10:02:28 +01:00
parent 0541b704db
commit 93c5f13750
11 changed files with 464 additions and 400 deletions

198
src/routes/form_actions.js Normal file
View File

@@ -0,0 +1,198 @@
const express = require('express');
const config = require('../../config')
const data = require('../data')
const func = require('../functions')
let users = require('../../data/users.json');
let posts = require('../../data/posts.json');
let comments = require('../../data/comments.json');
let other_data = require('../../data/data.json');
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
const fs = require('fs')
const crypto = require('crypto')
const router = express.Router();
////////////////////// Form actions /////////////////////////
router.post("/submit_comment", (req,res) => {
const unix_timestamp = getUnixTime(new Date())
let name = func.escape_input(req.body.name)
if (name == "") {
name = config.default_commenter_username
}
new_comment = {
"name": name,
"content": func.escape_input(req.body.content),
"id": comments.counter,
"pubdate": unix_timestamp
};
other_data.comment_counter += 1;
comments[req.body.post_index].push(new_comment);
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
res.redirect(301,`/post/${req.body.post_index}`)
}); // /submit_comment
router.post("/submit_post", (req,res) => {
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const username = func.escape_input(req.body.username)
const title = func.escape_input(req.body.title)
const content = req.body.content
const tags = func.escape_input(req.body.tags).split(',').map(str => str.trim());
const unix_timestamp = getUnixTime(new Date())
if (func.get_userID(username) == -1) {
res.render("partials/message", {
message: locale.user_doesnt_exit,
config,
})
}
else if (users[func.get_userID(username)]['hash'] == password) { // Password matches
console.log(username, "is submitting a post titled:", title);
posts.push({
"id": posts.length,
"userID": func.get_userID(username),
"title": title,
"content": content,
"pubdate": unix_timestamp,
"editdate": unix_timestamp,
"tags": tags,
})
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
comments.push([])
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`)
res.redirect(302, "/");
}
else {
res.render("partials/message", {
message: locale.incorrect_password,
config,
})
}
}); // /submit_post
router.post("/submit_signup", (req,res) => {
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const username = func.escape_input(req.body.username)
const prettyname = func.escape_input(req.body.prettyname)
const description = req.body.description
// Check that signups are allowed
if (config.allow_signup == true) {
// func.get_userID will return -1 if the user does not exist
// so this checks that the user does not exist
if (func.get_userID(username) == -1) {
users.push({
"id": users.length,
"username": username,
"prettyname": prettyname,
"hash": password,
"description": description,
})
fs.writeFileSync(`../data/users.json`, `${JSON.stringify(users)}`, 'utf-8');
res.redirect(301, `/user/${username}`)
}
// if the user does exist then
else {
res.render("partials/message", {
message: locale.user_exists,
config,
})
}
}
else if (config.allow_signup == false) {
res.render("partials/message", {
message: locale.signups_unavailable,
config,
})
}
// If allow_signup is undefined or not a boolean, error
else {
res.redirect(301,"/")
console.log("Error, invalid value for allow_signup (bool)")
}
}); // /submit_signup
router.post("/submit_edit_user", (req,res) => {
// Get the form info
const password = crypto.createHash("sha512").update(req.body.password).digest("hex");
const userID = func.escape_input(req.body.userID)
const description = req.body.description
const prettyname = func.escape_input(req.body.prettyname)
const delete_bool = req.body.delete
if (userID >= 0) { // The user exists
if (password == users[userID]['hash']) { // password matches
console.log(userID, " (userID) is modifying their account")
users[userID]["prettyname"] = prettyname;
users[userID]["description"] = description;
if (delete_bool == true) {
// Delete the user
users[userID] = {"id": userID,"deleted": true}
// Delete all their posts
for (let postid = 0; postid < posts.length; postid++) { // loop over all posts
if (posts[postid]['userID'] == userID) { // if userID matches
posts[postid] = {"id": postid, "deleted": true} // delete the post
comments[postid] = [] // the comments for this post should also be deleted
}
};
}
// Write these changes
fs.writeFileSync(`../data/users.json`, `${JSON.stringify(users)}`, 'utf-8');
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
res.redirect(301,`/user/${users[userID]["username"]}`)
}
else { // password does not match
res.render("partials/message", {
message: locale.incorrect_password,
config
}
)
};
}
else {
res.render("partials/message", {
message: locale.user_doesnt_exist,
config,
})
}
}); // /submit_delete_account
router.post("/submit_edit_post", (req,res) => {
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const postID = req.body.postID
const userID = req.body.userID
const title = func.escape_input(req.body.title)
const content = req.body.content
const tags = func.escape_input(req.body.tags).split(",").map(str => str.trim());
const delete_bool = req.body.delete
const unix_timestamp = getUnixTime(new Date())
console.log(users[userID]['prettyname'], "is editting the post titled:", title);
if (users[userID]['hash'] == password) { // password matches
let post = posts[postID]
post['title'] = title
post['content'] = content
post['tags'] = tags
post['editdate'] = unix_timestamp
if (typeof delete_bool != "undefined") {
console.log("Deleting post!")
posts[postID] = {"id": post["id"], "deleted": true}
comments[postID] = [];
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
}
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
res.redirect(302, "/");
}
else {
res.render("partials/message", {
message: locale.incorrect_password,
config,
})
}
}); // /submit_edit
module.exports = router;

58
src/routes/forms.js Normal file
View File

@@ -0,0 +1,58 @@
const express = require('express');
const config = require('../../config')
const data = require('../data')
const func = require('../functions')
const router = express.Router();
///////////////////// Form pages ////////////////////////////
router.get(config.new_post_url, (req,res) => {
res.render("forms/new_post", {
config,
locale,
});
}); // /post
router.get(config.signup_url, (req,res) => {
// if the server does allow signup
if (config.allow_signup == true) {
// Send the page for signing up to the server
res.render("forms/signup", {
config,
locale,
});
}
// if the server does not allow signup
else if (config.allow_signup == false) {
res.render("partials/message", {
message: locale.signups_unavailable,
config,
})
}
// If allow_signup is undefined or not a boolean, error
else {
res.redirect(301,"/")
console.log("Error, invalid value for allow_signup (bool)")
}
}); // /signup
router.get(`${config.edit_account_base_url}/:user_id`, (req,res) => {
const userID = parseInt(req.params.user_id);
res.render("forms/edit_account", {
config,
locale,
user: users[userID],
userID
});
}); // /delete_account
router.get(`${config.edit_post_base_url}/:post_id`, (req,res) => {
const post_id = req.params.post_id
const post = posts[post_id]
const user = users[post['userID']]
res.render("forms/edit_post", {
config,
locale,
post,
post_id,
user,
});
}); // /edit/:post_id
module.exports = router;

37
src/routes/indexes.js Normal file
View File

@@ -0,0 +1,37 @@
const express = require('express');
const config = require('../../config')
const data = require('../data')
const func = require('../functions')
const router = express.Router();
///////////////////// Page index's ///////////////////////
router.get("/index/pages", (req,res) => {
res.render("indexes/all_pages", {
config,
posts,
users,
comments: comments.comments,
});
}); // /index/posts
router.get("/index/posts", (req,res) => {
res.render("indexes/posts", {
config,
posts: data.getdata('posts'),
});
}); // /index/posts
router.get("/index/users", (req,res) => {
res.render("indexes/users", {
config,
users: data.getdata('users'),
});
}); // /index/posts
router.get("/index/comments", (req,res) => {
res.render("indexes/comments", {
config,
comments: data.getdata('comments'),
});
}); // /index/posts
module.exports = router;

View File

@@ -0,0 +1,125 @@
const express = require('express');
const config = require('../../config')
const data = require('../data')
const func = require('../functions')
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
const router = express.Router();
///////////////////// Standard Pages //////////////////////
router.get("/", (req,res) => {
// Increment the hitcount
if (config.enable_hitcount) {
func.increment_hitcount()
}
res.render("pages/timeline",
{
config,
locale,
posts: data.getdata("posts"),
users: data.getdata("users"),
comments: data.getdata("comments"),
hitcount: data.getdata("hitcount"),
fromUnixTime,
format,
getUnixTime,
func,
})
}); // /
router.get("/user/:username", (req, res) => {
const userID = func.get_userID(req.params.username)
console.log(data.getdata('users', 'id', userID)[0])
if (userID != -1) {
res.render("pages/user",
{
config,
locale,
posts: data.getdata('posts'),
user: data.getdata('users', 'id', userID),
userID: userID,
comments: data.getdata('comments'),
fromUnixTime,
format,
getUnixTime,
func,
})
}
else if (userID == -1) {
res.render("partials/message",
{
message: locale.user_doesnt_exist,
config,
})
}
}); // /user/:username
router.get("/post/:post_index", (req, res) => {
const postID = req.params.post_index
if (postID > posts.length-1 || posts[postID]["deleted"] == true) {
res.render("partials/message", {
message: locale.post_doesnt_exist,
config,
})
}
else if (typeof posts[postID]["deleted"] == "undefined" || posts[postID]["deleted"] == false) {
res.render("pages/post",
{
config,
locale,
post: posts[postID],
postID: postID,
user: users[posts[postID].userID],
comments: comments.comments[postID],
fromUnixTime,
format,
getUnixTime,
func,
})
}
else {
console.log("Error loading page")
res.redirect(301,"/")
}
}); // /post/:post_index
router.get("/tag/:tag", (req,res) => {
const tag = req.params.tag
res.render("pages/tag",
{
config,
locale,
tag,
posts,
users,
comments: comments.comments,
fromUnixTime: fromUnixTime,
format: format,
getUnixTime: getUnixTime,
func,
})
}); // /tag/:tag
router.get("/comment/:commentID", (req,res) => {
const commentID = req.params.commentID;
const comment = func.get_comment(commentID)
if (comment == -1) {
res.render("partials/message", {
config,
message: locale.comment_doesnt_exist,
})
}
else {
res.render("pages/comment",
{
config: config,
locale,
post: posts[comment["id"]],
users,
comment,
fromUnixTime: fromUnixTime,
format: format,
getUnixTime: getUnixTime,
func,
})
}
});
module.exports = router;

View File

@@ -2,6 +2,11 @@ const express = require('express');
const config = require('../../config')
const data = require('../data')
const func = require('../functions')
let users = require('../../data/users.json');
let posts = require('../../data/posts.json');
let comments = require('../../data/comments.json');
const router = express.Router();
////////////////////// SYNDICATION ////////////////////////