Changed how comments are stored and how data is retrieved

This commit is contained in:
2025-11-27 11:34:12 +00:00
parent ef8711b0e1
commit 7d38752f34
14 changed files with 130 additions and 128 deletions

View File

@@ -31,11 +31,10 @@ router.post("/submit_comment", (req,res) => {
new_comment = {
"name": name,
"content": content,
"id": comments[postID].length,
"id": comments[postID]['comments'].length,
"pubdate": unix_timestamp,
"postID": postID,
};
comments[postID].push(new_comment);
comments[postID]['comments'].push(new_comment);
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
}
@@ -59,8 +58,9 @@ router.post("/submit_post", (req,res) => {
else if (users[func.get_userID(username)]['hash'] == password) { // Password matches
console.log(username, "is submitting a post titled:", title);
id = posts.length
posts.push({
"id": posts.length,
"id": id,
"userID": func.get_userID(username),
"title": title,
"content": content,
@@ -69,7 +69,7 @@ router.post("/submit_post", (req,res) => {
"tags": tags,
})
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
comments.push([])
comments.push({'id': id, 'comments': []})
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`)
res.redirect(302, "/");
}
@@ -216,7 +216,7 @@ router.get('/search', (req, res) => {
console.log('searching for: ', search_term);
const search_results = data.searchdata(search_term, search_type); // data.searchdata returns an array of search results
res.render('partials/search', {
res.render('pages/search', {
config,
locale,
search_results,

View File

@@ -13,7 +13,7 @@ router.get("/index/pages", (req,res) => {
users: data.getdata('users'),
comments: data.getdata('comments'),
});
}); // /index/posts
}); // /index/pages
router.get("/index/posts", (req,res) => {
res.render("indexes/posts", {
config,
@@ -25,13 +25,13 @@ router.get("/index/users", (req,res) => {
config,
users: data.getdata('users'),
});
}); // /index/posts
}); // /index/users
router.get("/index/comments", (req,res) => {
res.render("indexes/comments", {
config,
comments: data.getdata('comments'),
});
}); // /index/posts
}); // /index/comments
module.exports = router;

View File

@@ -33,7 +33,7 @@ router.get("/", (req,res) => {
// Users
router.get("/user/:username", (req, res) => {
const userID = func.get_userID(req.params.username)
let user = data.getdata('users', userID)
let user = data.getdata('users', 'id', userID)
if (userID != -1) {
res.render("pages/user",
{
@@ -61,7 +61,7 @@ router.get("/user/:username", (req, res) => {
// Posts
router.get("/post/:post_index", (req, res) => {
const postID = parseInt(req.params.post_index)
let post = data.getdata('posts', postID)
let post = data.getdata('posts','id', postID)
if (post == 1) { // data.getdata returns error code 1 if nothing is available
res.render("partials/message", {
message: locale.post_doesnt_exist,
@@ -69,7 +69,7 @@ router.get("/post/:post_index", (req, res) => {
})
}
if (config.enable_hitcount) {
data.increment_hitcount(postID)
data.increment_hitcount(postID)
}
if (typeof post["deleted"] == "undefined" || post["deleted"] == false) {
res.render("pages/post",
@@ -78,8 +78,8 @@ router.get("/post/:post_index", (req, res) => {
locale,
post,
postID,
user: data.getdata('users', post.userID),
comments: data.getdata('comments', postID),
user: data.getdata('users','id', post.userID),
comments: data.getdata('comments','id', postID)["comments"],
fromUnixTime,
format,
getUnixTime,
@@ -117,7 +117,7 @@ router.get("/comment/:postID-:commentID", (req,res) => {
const commentID = parseInt(req.params.commentID);
const postID = parseInt(req.params.postID);
let posts_comments = data.getdata('comments', postID)
let posts_comments = data.getdata('comments', 'id', postID)["comments"]
let comment = 1
// For loop to find the comment with matching ID
posts_comments.forEach((current_comment, index) => {