From e6476dcd4e124f2475b970b1edeef68d5adf85b4 Mon Sep 17 00:00:00 2001 From: max Date: Wed, 24 Sep 2025 17:06:13 +0100 Subject: [PATCH] Everything uses data.getdata() data.getdata() excepts two parameters, if desired, the second will be the index, however if you don't specify anything, the whole array will be returned Also, comments now have a composite key of postID-commentID, with each post's comments having their own set starting at 0, this makes it easier to index and find a specific comment, and making the getcomment() function unessesary --- data/data.json | 2 +- src/data.js | 14 ++++----- src/routes/form_actions.js | 8 +++-- src/routes/standard_pages.js | 58 ++++++++++++++++++++---------------- src/routes/syndication.js | 4 --- views/partials/comment.ejs | 4 ++- 6 files changed, 47 insertions(+), 43 deletions(-) diff --git a/data/data.json b/data/data.json index 37101c4..7172056 100644 --- a/data/data.json +++ b/data/data.json @@ -1 +1 @@ -{"hitcount":8,"comment_counter":0} \ No newline at end of file +{"hitcount":16,"comment_counter":1} \ No newline at end of file diff --git a/src/data.js b/src/data.js index 5a26083..e49df2f 100644 --- a/src/data.js +++ b/src/data.js @@ -4,22 +4,18 @@ const require = createRequire(import.meta.url); const config = require("../config.json") const fs = require("fs") -export function getdata(data, key='', value='') { +export function getdata(data, index=-1) { if (config["data_storage"] == "json") { if (data == "posts" || data == 'users' || data == 'comments') { let result = require(`../data/${data}.json`) - if (key != '') { - result.forEach((object, index) => { - if (object[key] == value) { - return object - } - }) + if (index != -1) { + return result[index] } return result } - else if (data == "hitcount") { - let result = fs.readFileSync("../data/hitcount.txt") + else if (data == "other_data") { + let result = require('../data/data.json') // This file is actually called data.json return result } else { diff --git a/src/routes/form_actions.js b/src/routes/form_actions.js index 921a644..ed4ebfc 100644 --- a/src/routes/form_actions.js +++ b/src/routes/form_actions.js @@ -23,12 +23,16 @@ router.post("/submit_comment", (req,res) => { new_comment = { "name": name, "content": func.escape_input(req.body.content), - "id": comments.counter, - "pubdate": unix_timestamp + "id": data.getdata('other_data').comment_counter, + "pubdate": unix_timestamp, + "postID": req.body.post_index, }; + let other_data = data.getdata('other_data') other_data.comment_counter += 1; + let comments = data.getdata('comments') comments[req.body.post_index].push(new_comment); fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8'); + fs.writeFileSync('../data/data.json', JSON.stringify(other_data), 'utf-8'); res.redirect(301,`/post/${req.body.post_index}`) }); // /submit_comment diff --git a/src/routes/standard_pages.js b/src/routes/standard_pages.js index d2e8624..05072d0 100644 --- a/src/routes/standard_pages.js +++ b/src/routes/standard_pages.js @@ -29,14 +29,14 @@ router.get("/", (req,res) => { }); // / router.get("/user/:username", (req, res) => { const userID = func.get_userID(req.params.username) - console.log(data.getdata('users', 'id', userID)[0]) + let user = data.getdata('users', userID) if (userID != -1) { res.render("pages/user", { config, locale, posts: data.getdata('posts'), - user: data.getdata('users', 'id', userID), + user, userID: userID, comments: data.getdata('comments'), fromUnixTime, @@ -55,25 +55,27 @@ router.get("/user/:username", (req, res) => { }); // /user/:username router.get("/post/:post_index", (req, res) => { const postID = req.params.post_index - if (postID > posts.length-1 || posts[postID]["deleted"] == true) { + let post = data.getdata('posts', postID) + + if (post["deleted"] == true || post == 1) { // data.getdata returns error code 1 if nothing is available res.render("partials/message", { message: locale.post_doesnt_exist, config, }) } - else if (typeof posts[postID]["deleted"] == "undefined" || posts[postID]["deleted"] == false) { + else if (typeof post["deleted"] == "undefined" || post["deleted"] == false) { res.render("pages/post", { config, locale, - post: posts[postID], - postID: postID, - user: users[posts[postID].userID], - comments: comments.comments[postID], + post, + postID, + user: data.getdata('users', post.userID), + comments: data.getdata('comments', postID), fromUnixTime, format, - getUnixTime, - func, + getUnixTime, + func, }) } else { @@ -81,6 +83,8 @@ router.get("/post/:post_index", (req, res) => { res.redirect(301,"/") } }); // /post/:post_index + + router.get("/tag/:tag", (req,res) => { const tag = req.params.tag res.render("pages/tag", @@ -88,18 +92,21 @@ router.get("/tag/:tag", (req,res) => { config, locale, tag, - posts, - users, - comments: comments.comments, - fromUnixTime: fromUnixTime, - format: format, - getUnixTime: getUnixTime, - func, + posts: data.getdata('posts'), + users: data.getdata('users'), + comments: data.getdata('comments'), + fromUnixTime, + format, + getUnixTime, + func, }) }); // /tag/:tag -router.get("/comment/:commentID", (req,res) => { +router.get("/comment/:postID-:commentID", (req,res) => { const commentID = req.params.commentID; - const comment = func.get_comment(commentID) + const postID = req.params.postID; + + let posts_comments = data.getdata('comments', postID) + let comment = posts_comments[commentID] if (comment == -1) { res.render("partials/message", { config, @@ -109,15 +116,14 @@ router.get("/comment/:commentID", (req,res) => { else { res.render("pages/comment", { - config: config, + config, locale, - post: posts[comment["id"]], - users, comment, - fromUnixTime: fromUnixTime, - format: format, - getUnixTime: getUnixTime, - func, + postID, + fromUnixTime, + format, + getUnixTime, + func, }) } }); diff --git a/src/routes/syndication.js b/src/routes/syndication.js index cb62337..603532e 100644 --- a/src/routes/syndication.js +++ b/src/routes/syndication.js @@ -3,10 +3,6 @@ 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 //////////////////////// diff --git a/views/partials/comment.ejs b/views/partials/comment.ejs index f8b0433..d8491df 100644 --- a/views/partials/comment.ejs +++ b/views/partials/comment.ejs @@ -1,3 +1,5 @@ -<%= comment.name %> <%= func.unix_time_to_date_format(comment.pubdate) %> No. <%= comment.id %>:
+<%= comment.name %> +<%= func.unix_time_to_date_format(comment.pubdate) %> +No. <%= postID %>-<%= comment.id %>:
<%- func.render_comment(comment.content) %>