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
This commit is contained in:
max
2025-09-24 17:06:13 +01:00
parent 93c5f13750
commit e6476dcd4e
6 changed files with 47 additions and 43 deletions

View File

@@ -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 {

View File

@@ -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

View File

@@ -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,
})
}
});

View File

@@ -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 ////////////////////////