Compare commits
2 Commits
93c5f13750
...
bfaf957ae2
Author | SHA1 | Date | |
---|---|---|---|
|
bfaf957ae2 | ||
|
e6476dcd4e |
@@ -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 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 {
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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,
|
||||
})
|
||||
}
|
||||
});
|
||||
|
@@ -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 ////////////////////////
|
||||
|
@@ -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/>
|
||||
|
Reference in New Issue
Block a user