Compare commits

...

2 Commits

Author SHA1 Message Date
max
bfaf957ae2 removed func.get_comment()
It is no longer needed

Signed-off-by: max <deadvey@localhost.localdomain>
2025-09-24 17:06:55 +01:00
max
e6476dcd4e 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
2025-09-24 17:06:13 +01:00
7 changed files with 47 additions and 54 deletions

View File

@@ -1 +1 @@
{"hitcount":8,"comment_counter":0}
{"hitcount":16,"comment_counter":1}

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

@@ -68,17 +68,6 @@ export function get_userID(username) {
return -1 // If user is not present, return -1
}
export function get_comment(commentID) { // TODO scales like shit
const comments = require("../data/comments.json")
for (let i = 0; i < comments.comments.length; i++) { // Loop over every post
for (let j = 0; j < comments.comments[i].length; j++) { // Then every comment in that post
if (comments.comments[i][j]["id"] == commentID) {
return comments.comments[i][j]
};
}
}
return -1 // If comment is not present, return -1
}
// This escapes some potentially dangerous HTML characters with their HTML entities
// https://www.freeformatter.com/html-entities.html
// accepts a string

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,21 +55,23 @@ 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,
@@ -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,
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,14 +116,13 @@ 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,
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 ////////////////////////

View File

@@ -1,3 +1,5 @@
<b><%= comment.name %></b> <%= func.unix_time_to_date_format(comment.pubdate) %> <i>No. <%= comment.id %></i>:<br/>
<b><%= comment.name %></b>
<%= func.unix_time_to_date_format(comment.pubdate) %>
<a href='/comment/<%= postID %>-<%= comment.id %>'>No. <%= postID %>-<%= comment.id %></a>:<br/>
<%- func.render_comment(comment.content) %>
<br/>