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:
@@ -1 +1 @@
|
|||||||
{"hitcount":8,"comment_counter":0}
|
{"hitcount":16,"comment_counter":1}
|
14
src/data.js
14
src/data.js
@@ -4,22 +4,18 @@ const require = createRequire(import.meta.url);
|
|||||||
const config = require("../config.json")
|
const config = require("../config.json")
|
||||||
const fs = require("fs")
|
const fs = require("fs")
|
||||||
|
|
||||||
export function getdata(data, key='', value='') {
|
export function getdata(data, index=-1) {
|
||||||
|
|
||||||
if (config["data_storage"] == "json") {
|
if (config["data_storage"] == "json") {
|
||||||
if (data == "posts" || data == 'users' || data == 'comments') {
|
if (data == "posts" || data == 'users' || data == 'comments') {
|
||||||
let result = require(`../data/${data}.json`)
|
let result = require(`../data/${data}.json`)
|
||||||
if (key != '') {
|
if (index != -1) {
|
||||||
result.forEach((object, index) => {
|
return result[index]
|
||||||
if (object[key] == value) {
|
|
||||||
return object
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
else if (data == "hitcount") {
|
else if (data == "other_data") {
|
||||||
let result = fs.readFileSync("../data/hitcount.txt")
|
let result = require('../data/data.json') // This file is actually called data.json
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@@ -23,12 +23,16 @@ router.post("/submit_comment", (req,res) => {
|
|||||||
new_comment = {
|
new_comment = {
|
||||||
"name": name,
|
"name": name,
|
||||||
"content": func.escape_input(req.body.content),
|
"content": func.escape_input(req.body.content),
|
||||||
"id": comments.counter,
|
"id": data.getdata('other_data').comment_counter,
|
||||||
"pubdate": unix_timestamp
|
"pubdate": unix_timestamp,
|
||||||
|
"postID": req.body.post_index,
|
||||||
};
|
};
|
||||||
|
let other_data = data.getdata('other_data')
|
||||||
other_data.comment_counter += 1;
|
other_data.comment_counter += 1;
|
||||||
|
let comments = data.getdata('comments')
|
||||||
comments[req.body.post_index].push(new_comment);
|
comments[req.body.post_index].push(new_comment);
|
||||||
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
|
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}`)
|
res.redirect(301,`/post/${req.body.post_index}`)
|
||||||
}); // /submit_comment
|
}); // /submit_comment
|
||||||
|
@@ -29,14 +29,14 @@ router.get("/", (req,res) => {
|
|||||||
}); // /
|
}); // /
|
||||||
router.get("/user/:username", (req, res) => {
|
router.get("/user/:username", (req, res) => {
|
||||||
const userID = func.get_userID(req.params.username)
|
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) {
|
if (userID != -1) {
|
||||||
res.render("pages/user",
|
res.render("pages/user",
|
||||||
{
|
{
|
||||||
config,
|
config,
|
||||||
locale,
|
locale,
|
||||||
posts: data.getdata('posts'),
|
posts: data.getdata('posts'),
|
||||||
user: data.getdata('users', 'id', userID),
|
user,
|
||||||
userID: userID,
|
userID: userID,
|
||||||
comments: data.getdata('comments'),
|
comments: data.getdata('comments'),
|
||||||
fromUnixTime,
|
fromUnixTime,
|
||||||
@@ -55,21 +55,23 @@ router.get("/user/:username", (req, res) => {
|
|||||||
}); // /user/:username
|
}); // /user/:username
|
||||||
router.get("/post/:post_index", (req, res) => {
|
router.get("/post/:post_index", (req, res) => {
|
||||||
const postID = req.params.post_index
|
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", {
|
res.render("partials/message", {
|
||||||
message: locale.post_doesnt_exist,
|
message: locale.post_doesnt_exist,
|
||||||
config,
|
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",
|
res.render("pages/post",
|
||||||
{
|
{
|
||||||
config,
|
config,
|
||||||
locale,
|
locale,
|
||||||
post: posts[postID],
|
post,
|
||||||
postID: postID,
|
postID,
|
||||||
user: users[posts[postID].userID],
|
user: data.getdata('users', post.userID),
|
||||||
comments: comments.comments[postID],
|
comments: data.getdata('comments', postID),
|
||||||
fromUnixTime,
|
fromUnixTime,
|
||||||
format,
|
format,
|
||||||
getUnixTime,
|
getUnixTime,
|
||||||
@@ -81,6 +83,8 @@ router.get("/post/:post_index", (req, res) => {
|
|||||||
res.redirect(301,"/")
|
res.redirect(301,"/")
|
||||||
}
|
}
|
||||||
}); // /post/:post_index
|
}); // /post/:post_index
|
||||||
|
|
||||||
|
|
||||||
router.get("/tag/:tag", (req,res) => {
|
router.get("/tag/:tag", (req,res) => {
|
||||||
const tag = req.params.tag
|
const tag = req.params.tag
|
||||||
res.render("pages/tag",
|
res.render("pages/tag",
|
||||||
@@ -88,18 +92,21 @@ router.get("/tag/:tag", (req,res) => {
|
|||||||
config,
|
config,
|
||||||
locale,
|
locale,
|
||||||
tag,
|
tag,
|
||||||
posts,
|
posts: data.getdata('posts'),
|
||||||
users,
|
users: data.getdata('users'),
|
||||||
comments: comments.comments,
|
comments: data.getdata('comments'),
|
||||||
fromUnixTime: fromUnixTime,
|
fromUnixTime,
|
||||||
format: format,
|
format,
|
||||||
getUnixTime: getUnixTime,
|
getUnixTime,
|
||||||
func,
|
func,
|
||||||
})
|
})
|
||||||
}); // /tag/:tag
|
}); // /tag/:tag
|
||||||
router.get("/comment/:commentID", (req,res) => {
|
router.get("/comment/:postID-:commentID", (req,res) => {
|
||||||
const commentID = req.params.commentID;
|
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) {
|
if (comment == -1) {
|
||||||
res.render("partials/message", {
|
res.render("partials/message", {
|
||||||
config,
|
config,
|
||||||
@@ -109,14 +116,13 @@ router.get("/comment/:commentID", (req,res) => {
|
|||||||
else {
|
else {
|
||||||
res.render("pages/comment",
|
res.render("pages/comment",
|
||||||
{
|
{
|
||||||
config: config,
|
config,
|
||||||
locale,
|
locale,
|
||||||
post: posts[comment["id"]],
|
|
||||||
users,
|
|
||||||
comment,
|
comment,
|
||||||
fromUnixTime: fromUnixTime,
|
postID,
|
||||||
format: format,
|
fromUnixTime,
|
||||||
getUnixTime: getUnixTime,
|
format,
|
||||||
|
getUnixTime,
|
||||||
func,
|
func,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -3,10 +3,6 @@ const config = require('../../config')
|
|||||||
const data = require('../data')
|
const data = require('../data')
|
||||||
const func = require('../functions')
|
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();
|
const router = express.Router();
|
||||||
|
|
||||||
////////////////////// SYNDICATION ////////////////////////
|
////////////////////// SYNDICATION ////////////////////////
|
||||||
|
@@ -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) %>
|
<%- func.render_comment(comment.content) %>
|
||||||
<br/>
|
<br/>
|
||||||
|
Reference in New Issue
Block a user