Clean up changes
Seperated all routes into seperate files for neatness, and I've made the comments.json store only the comments, comments_counter is in the new data.json file which also stores the hitcount. Signed-off-by: max <deadvey@localhost.localdomain>
This commit is contained in:
1
data/data.json
Normal file
1
data/data.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"hitcount":8,"comment_counter":0}
|
15
src/data.js
15
src/data.js
@@ -4,17 +4,20 @@ 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) {
|
export function getdata(data, key='', value='') {
|
||||||
|
|
||||||
if (config["data_storage"] == "json") {
|
if (config["data_storage"] == "json") {
|
||||||
if (data == "posts" || data == "users") {
|
if (data == "posts" || data == 'users' || data == 'comments') {
|
||||||
let result = require(`../data/${data}.json`)
|
let result = require(`../data/${data}.json`)
|
||||||
|
if (key != '') {
|
||||||
|
result.forEach((object, index) => {
|
||||||
|
if (object[key] == value) {
|
||||||
|
return object
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
else if (data == "comments") {
|
|
||||||
let result = require("../data/comments.json")
|
|
||||||
return result.comments
|
|
||||||
}
|
|
||||||
else if (data == "hitcount") {
|
else if (data == "hitcount") {
|
||||||
let result = fs.readFileSync("../data/hitcount.txt")
|
let result = fs.readFileSync("../data/hitcount.txt")
|
||||||
return result
|
return result
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { createRequire } from 'module';
|
import { createRequire } from 'module';
|
||||||
const require = createRequire(import.meta.url)
|
const require = createRequire(import.meta.url)
|
||||||
const config = require("../config.json")
|
const config = require("../config.json")
|
||||||
|
const fs = require('fs')
|
||||||
const locale = require(`../locales/${config.locale}.json`)
|
const locale = require(`../locales/${config.locale}.json`)
|
||||||
|
|
||||||
// The configuration defines a date format using the date-fns (a datetime library) syntax
|
// The configuration defines a date format using the date-fns (a datetime library) syntax
|
||||||
@@ -116,3 +117,13 @@ export function render_md(content) {
|
|||||||
})
|
})
|
||||||
return md.render(content)
|
return md.render(content)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Literally just +1 to the hitcount
|
||||||
|
export function increment_hitcount() {
|
||||||
|
if (config.data_storage == 'json') {
|
||||||
|
let other_data = require('../data/data.json');
|
||||||
|
other_data.hitcount += 1
|
||||||
|
console.log(`/ Is loaded, hitcount: ${other_data.hitcount}`)
|
||||||
|
fs.writeFileSync(`../data/data.json`, `${JSON.stringify(other_data)}`, 'utf-8');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@@ -25,7 +25,14 @@ export function initialise() {
|
|||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.log("Creating comments file")
|
console.log("Creating comments file")
|
||||||
fs.writeFileSync(`../data/comments.json`, `{\n"comments": [],\n"counter": 0}`)
|
fs.writeFileSync(`../data/comments.json`, `[]`)
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const comments = require("../data/data.json");
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.log("Creating generic data file")
|
||||||
|
fs.writeFileSync(`../data/data.json`, `{"hitcount": 0, "comment_counter": 0}`)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const config = require("../config.json");
|
const config = require("../config.json");
|
||||||
@@ -37,15 +44,7 @@ export function initialise() {
|
|||||||
console.log("Error copying file")
|
console.log("Error copying file")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
fs.readFileSync("../data/hitcount.txt")
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.log("Creating hitcount file")
|
|
||||||
fs.writeFileSync("../data/hitcount.txt", "0", (err) => {
|
|
||||||
console.log("Error creating hitcount.txt")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
console.log("Successfully initialised")
|
console.log("Successfully initialised")
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}
|
}
|
||||||
|
198
src/routes/form_actions.js
Normal file
198
src/routes/form_actions.js
Normal file
@@ -0,0 +1,198 @@
|
|||||||
|
const express = require('express');
|
||||||
|
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');
|
||||||
|
let other_data = require('../../data/data.json');
|
||||||
|
|
||||||
|
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
|
||||||
|
const fs = require('fs')
|
||||||
|
const crypto = require('crypto')
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
////////////////////// Form actions /////////////////////////
|
||||||
|
router.post("/submit_comment", (req,res) => {
|
||||||
|
const unix_timestamp = getUnixTime(new Date())
|
||||||
|
let name = func.escape_input(req.body.name)
|
||||||
|
if (name == "") {
|
||||||
|
name = config.default_commenter_username
|
||||||
|
}
|
||||||
|
new_comment = {
|
||||||
|
"name": name,
|
||||||
|
"content": func.escape_input(req.body.content),
|
||||||
|
"id": comments.counter,
|
||||||
|
"pubdate": unix_timestamp
|
||||||
|
};
|
||||||
|
other_data.comment_counter += 1;
|
||||||
|
comments[req.body.post_index].push(new_comment);
|
||||||
|
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
|
||||||
|
|
||||||
|
res.redirect(301,`/post/${req.body.post_index}`)
|
||||||
|
}); // /submit_comment
|
||||||
|
|
||||||
|
router.post("/submit_post", (req,res) => {
|
||||||
|
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
|
||||||
|
const username = func.escape_input(req.body.username)
|
||||||
|
const title = func.escape_input(req.body.title)
|
||||||
|
const content = req.body.content
|
||||||
|
const tags = func.escape_input(req.body.tags).split(',').map(str => str.trim());
|
||||||
|
const unix_timestamp = getUnixTime(new Date())
|
||||||
|
|
||||||
|
if (func.get_userID(username) == -1) {
|
||||||
|
res.render("partials/message", {
|
||||||
|
message: locale.user_doesnt_exit,
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (users[func.get_userID(username)]['hash'] == password) { // Password matches
|
||||||
|
console.log(username, "is submitting a post titled:", title);
|
||||||
|
posts.push({
|
||||||
|
"id": posts.length,
|
||||||
|
"userID": func.get_userID(username),
|
||||||
|
"title": title,
|
||||||
|
"content": content,
|
||||||
|
"pubdate": unix_timestamp,
|
||||||
|
"editdate": unix_timestamp,
|
||||||
|
"tags": tags,
|
||||||
|
})
|
||||||
|
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
|
||||||
|
comments.push([])
|
||||||
|
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`)
|
||||||
|
res.redirect(302, "/");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.render("partials/message", {
|
||||||
|
message: locale.incorrect_password,
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}); // /submit_post
|
||||||
|
|
||||||
|
router.post("/submit_signup", (req,res) => {
|
||||||
|
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
|
||||||
|
const username = func.escape_input(req.body.username)
|
||||||
|
const prettyname = func.escape_input(req.body.prettyname)
|
||||||
|
const description = req.body.description
|
||||||
|
|
||||||
|
// Check that signups are allowed
|
||||||
|
if (config.allow_signup == true) {
|
||||||
|
// func.get_userID will return -1 if the user does not exist
|
||||||
|
// so this checks that the user does not exist
|
||||||
|
if (func.get_userID(username) == -1) {
|
||||||
|
users.push({
|
||||||
|
"id": users.length,
|
||||||
|
"username": username,
|
||||||
|
"prettyname": prettyname,
|
||||||
|
"hash": password,
|
||||||
|
"description": description,
|
||||||
|
})
|
||||||
|
fs.writeFileSync(`../data/users.json`, `${JSON.stringify(users)}`, 'utf-8');
|
||||||
|
res.redirect(301, `/user/${username}`)
|
||||||
|
}
|
||||||
|
// if the user does exist then
|
||||||
|
else {
|
||||||
|
res.render("partials/message", {
|
||||||
|
message: locale.user_exists,
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (config.allow_signup == false) {
|
||||||
|
res.render("partials/message", {
|
||||||
|
message: locale.signups_unavailable,
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// If allow_signup is undefined or not a boolean, error
|
||||||
|
else {
|
||||||
|
res.redirect(301,"/")
|
||||||
|
console.log("Error, invalid value for allow_signup (bool)")
|
||||||
|
}
|
||||||
|
}); // /submit_signup
|
||||||
|
|
||||||
|
router.post("/submit_edit_user", (req,res) => {
|
||||||
|
// Get the form info
|
||||||
|
const password = crypto.createHash("sha512").update(req.body.password).digest("hex");
|
||||||
|
const userID = func.escape_input(req.body.userID)
|
||||||
|
const description = req.body.description
|
||||||
|
const prettyname = func.escape_input(req.body.prettyname)
|
||||||
|
const delete_bool = req.body.delete
|
||||||
|
|
||||||
|
if (userID >= 0) { // The user exists
|
||||||
|
if (password == users[userID]['hash']) { // password matches
|
||||||
|
console.log(userID, " (userID) is modifying their account")
|
||||||
|
users[userID]["prettyname"] = prettyname;
|
||||||
|
users[userID]["description"] = description;
|
||||||
|
|
||||||
|
if (delete_bool == true) {
|
||||||
|
// Delete the user
|
||||||
|
users[userID] = {"id": userID,"deleted": true}
|
||||||
|
// Delete all their posts
|
||||||
|
for (let postid = 0; postid < posts.length; postid++) { // loop over all posts
|
||||||
|
if (posts[postid]['userID'] == userID) { // if userID matches
|
||||||
|
posts[postid] = {"id": postid, "deleted": true} // delete the post
|
||||||
|
comments[postid] = [] // the comments for this post should also be deleted
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// Write these changes
|
||||||
|
fs.writeFileSync(`../data/users.json`, `${JSON.stringify(users)}`, 'utf-8');
|
||||||
|
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
|
||||||
|
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
|
||||||
|
res.redirect(301,`/user/${users[userID]["username"]}`)
|
||||||
|
}
|
||||||
|
else { // password does not match
|
||||||
|
res.render("partials/message", {
|
||||||
|
message: locale.incorrect_password,
|
||||||
|
config
|
||||||
|
}
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.render("partials/message", {
|
||||||
|
message: locale.user_doesnt_exist,
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}); // /submit_delete_account
|
||||||
|
|
||||||
|
router.post("/submit_edit_post", (req,res) => {
|
||||||
|
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
|
||||||
|
const postID = req.body.postID
|
||||||
|
const userID = req.body.userID
|
||||||
|
const title = func.escape_input(req.body.title)
|
||||||
|
const content = req.body.content
|
||||||
|
const tags = func.escape_input(req.body.tags).split(",").map(str => str.trim());
|
||||||
|
const delete_bool = req.body.delete
|
||||||
|
const unix_timestamp = getUnixTime(new Date())
|
||||||
|
console.log(users[userID]['prettyname'], "is editting the post titled:", title);
|
||||||
|
|
||||||
|
if (users[userID]['hash'] == password) { // password matches
|
||||||
|
let post = posts[postID]
|
||||||
|
post['title'] = title
|
||||||
|
post['content'] = content
|
||||||
|
post['tags'] = tags
|
||||||
|
post['editdate'] = unix_timestamp
|
||||||
|
if (typeof delete_bool != "undefined") {
|
||||||
|
console.log("Deleting post!")
|
||||||
|
posts[postID] = {"id": post["id"], "deleted": true}
|
||||||
|
comments[postID] = [];
|
||||||
|
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
|
||||||
|
}
|
||||||
|
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
|
||||||
|
res.redirect(302, "/");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.render("partials/message", {
|
||||||
|
message: locale.incorrect_password,
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}); // /submit_edit
|
||||||
|
|
||||||
|
module.exports = router;
|
58
src/routes/forms.js
Normal file
58
src/routes/forms.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const config = require('../../config')
|
||||||
|
const data = require('../data')
|
||||||
|
const func = require('../functions')
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
///////////////////// Form pages ////////////////////////////
|
||||||
|
router.get(config.new_post_url, (req,res) => {
|
||||||
|
res.render("forms/new_post", {
|
||||||
|
config,
|
||||||
|
locale,
|
||||||
|
});
|
||||||
|
}); // /post
|
||||||
|
router.get(config.signup_url, (req,res) => {
|
||||||
|
// if the server does allow signup
|
||||||
|
if (config.allow_signup == true) {
|
||||||
|
// Send the page for signing up to the server
|
||||||
|
res.render("forms/signup", {
|
||||||
|
config,
|
||||||
|
locale,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// if the server does not allow signup
|
||||||
|
else if (config.allow_signup == false) {
|
||||||
|
res.render("partials/message", {
|
||||||
|
message: locale.signups_unavailable,
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// If allow_signup is undefined or not a boolean, error
|
||||||
|
else {
|
||||||
|
res.redirect(301,"/")
|
||||||
|
console.log("Error, invalid value for allow_signup (bool)")
|
||||||
|
}
|
||||||
|
}); // /signup
|
||||||
|
router.get(`${config.edit_account_base_url}/:user_id`, (req,res) => {
|
||||||
|
const userID = parseInt(req.params.user_id);
|
||||||
|
res.render("forms/edit_account", {
|
||||||
|
config,
|
||||||
|
locale,
|
||||||
|
user: users[userID],
|
||||||
|
userID
|
||||||
|
});
|
||||||
|
}); // /delete_account
|
||||||
|
router.get(`${config.edit_post_base_url}/:post_id`, (req,res) => {
|
||||||
|
const post_id = req.params.post_id
|
||||||
|
const post = posts[post_id]
|
||||||
|
const user = users[post['userID']]
|
||||||
|
res.render("forms/edit_post", {
|
||||||
|
config,
|
||||||
|
locale,
|
||||||
|
post,
|
||||||
|
post_id,
|
||||||
|
user,
|
||||||
|
});
|
||||||
|
}); // /edit/:post_id
|
||||||
|
|
||||||
|
module.exports = router;
|
37
src/routes/indexes.js
Normal file
37
src/routes/indexes.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const config = require('../../config')
|
||||||
|
const data = require('../data')
|
||||||
|
const func = require('../functions')
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
///////////////////// Page index's ///////////////////////
|
||||||
|
router.get("/index/pages", (req,res) => {
|
||||||
|
res.render("indexes/all_pages", {
|
||||||
|
config,
|
||||||
|
posts,
|
||||||
|
users,
|
||||||
|
comments: comments.comments,
|
||||||
|
});
|
||||||
|
}); // /index/posts
|
||||||
|
router.get("/index/posts", (req,res) => {
|
||||||
|
res.render("indexes/posts", {
|
||||||
|
config,
|
||||||
|
posts: data.getdata('posts'),
|
||||||
|
});
|
||||||
|
}); // /index/posts
|
||||||
|
router.get("/index/users", (req,res) => {
|
||||||
|
res.render("indexes/users", {
|
||||||
|
config,
|
||||||
|
users: data.getdata('users'),
|
||||||
|
});
|
||||||
|
}); // /index/posts
|
||||||
|
router.get("/index/comments", (req,res) => {
|
||||||
|
res.render("indexes/comments", {
|
||||||
|
config,
|
||||||
|
comments: data.getdata('comments'),
|
||||||
|
});
|
||||||
|
}); // /index/posts
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = router;
|
125
src/routes/standard_pages.js
Normal file
125
src/routes/standard_pages.js
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const config = require('../../config')
|
||||||
|
const data = require('../data')
|
||||||
|
const func = require('../functions')
|
||||||
|
|
||||||
|
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
///////////////////// Standard Pages //////////////////////
|
||||||
|
router.get("/", (req,res) => {
|
||||||
|
// Increment the hitcount
|
||||||
|
if (config.enable_hitcount) {
|
||||||
|
func.increment_hitcount()
|
||||||
|
}
|
||||||
|
|
||||||
|
res.render("pages/timeline",
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
locale,
|
||||||
|
posts: data.getdata("posts"),
|
||||||
|
users: data.getdata("users"),
|
||||||
|
comments: data.getdata("comments"),
|
||||||
|
hitcount: data.getdata("hitcount"),
|
||||||
|
fromUnixTime,
|
||||||
|
format,
|
||||||
|
getUnixTime,
|
||||||
|
func,
|
||||||
|
})
|
||||||
|
}); // /
|
||||||
|
router.get("/user/:username", (req, res) => {
|
||||||
|
const userID = func.get_userID(req.params.username)
|
||||||
|
console.log(data.getdata('users', 'id', userID)[0])
|
||||||
|
if (userID != -1) {
|
||||||
|
res.render("pages/user",
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
locale,
|
||||||
|
posts: data.getdata('posts'),
|
||||||
|
user: data.getdata('users', 'id', userID),
|
||||||
|
userID: userID,
|
||||||
|
comments: data.getdata('comments'),
|
||||||
|
fromUnixTime,
|
||||||
|
format,
|
||||||
|
getUnixTime,
|
||||||
|
func,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else if (userID == -1) {
|
||||||
|
res.render("partials/message",
|
||||||
|
{
|
||||||
|
message: locale.user_doesnt_exist,
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}); // /user/:username
|
||||||
|
router.get("/post/:post_index", (req, res) => {
|
||||||
|
const postID = req.params.post_index
|
||||||
|
if (postID > posts.length-1 || posts[postID]["deleted"] == true) {
|
||||||
|
res.render("partials/message", {
|
||||||
|
message: locale.post_doesnt_exist,
|
||||||
|
config,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else if (typeof posts[postID]["deleted"] == "undefined" || posts[postID]["deleted"] == false) {
|
||||||
|
res.render("pages/post",
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
locale,
|
||||||
|
post: posts[postID],
|
||||||
|
postID: postID,
|
||||||
|
user: users[posts[postID].userID],
|
||||||
|
comments: comments.comments[postID],
|
||||||
|
fromUnixTime,
|
||||||
|
format,
|
||||||
|
getUnixTime,
|
||||||
|
func,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log("Error loading page")
|
||||||
|
res.redirect(301,"/")
|
||||||
|
}
|
||||||
|
}); // /post/:post_index
|
||||||
|
router.get("/tag/:tag", (req,res) => {
|
||||||
|
const tag = req.params.tag
|
||||||
|
res.render("pages/tag",
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
locale,
|
||||||
|
tag,
|
||||||
|
posts,
|
||||||
|
users,
|
||||||
|
comments: comments.comments,
|
||||||
|
fromUnixTime: fromUnixTime,
|
||||||
|
format: format,
|
||||||
|
getUnixTime: getUnixTime,
|
||||||
|
func,
|
||||||
|
})
|
||||||
|
}); // /tag/:tag
|
||||||
|
router.get("/comment/:commentID", (req,res) => {
|
||||||
|
const commentID = req.params.commentID;
|
||||||
|
const comment = func.get_comment(commentID)
|
||||||
|
if (comment == -1) {
|
||||||
|
res.render("partials/message", {
|
||||||
|
config,
|
||||||
|
message: locale.comment_doesnt_exist,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.render("pages/comment",
|
||||||
|
{
|
||||||
|
config: config,
|
||||||
|
locale,
|
||||||
|
post: posts[comment["id"]],
|
||||||
|
users,
|
||||||
|
comment,
|
||||||
|
fromUnixTime: fromUnixTime,
|
||||||
|
format: format,
|
||||||
|
getUnixTime: getUnixTime,
|
||||||
|
func,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
@@ -2,6 +2,11 @@ const express = require('express');
|
|||||||
const config = require('../../config')
|
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 ////////////////////////
|
||||||
|
393
src/server.js
393
src/server.js
@@ -62,390 +62,17 @@ app.set('view engine', 'ejs');
|
|||||||
app.set('views', '../views')
|
app.set('views', '../views')
|
||||||
|
|
||||||
// Express JS routes
|
// Express JS routes
|
||||||
var syndication_route = require('./routes/syndication.js');
|
const syndication_routes = require('./routes/syndication.js');
|
||||||
//var route2 = require('./routes/rout');
|
const indexes_routes = require('./routes/indexes.js');
|
||||||
app.use('/', syndication_route);
|
const standard_pages_routes = require('./routes/standard_pages.js');
|
||||||
//app.use('/route2', route2);
|
const forms_routes = require('./routes/forms.js');
|
||||||
|
const form_actions_routes = require('./routes/form_actions.js');
|
||||||
|
|
||||||
|
app.use('/', syndication_routes);
|
||||||
///////////////////// Page index's ///////////////////////
|
app.use('/', indexes_routes);
|
||||||
app.get("/index/pages", (req,res) => {
|
app.use('/', standard_pages_routes);
|
||||||
res.render("indexes/all_pages", {
|
app.use('/', forms_routes);
|
||||||
config,
|
app.use('/', form_actions_routes);
|
||||||
posts,
|
|
||||||
users,
|
|
||||||
comments: comments.comments,
|
|
||||||
});
|
|
||||||
}); // /index/posts
|
|
||||||
app.get("/index/posts", (req,res) => {
|
|
||||||
res.render("indexes/posts", {
|
|
||||||
config,
|
|
||||||
posts,
|
|
||||||
});
|
|
||||||
}); // /index/posts
|
|
||||||
app.get("/index/users", (req,res) => {
|
|
||||||
res.render("indexes/users", {
|
|
||||||
config,
|
|
||||||
users,
|
|
||||||
});
|
|
||||||
}); // /index/posts
|
|
||||||
app.get("/index/comments", (req,res) => {
|
|
||||||
res.render("indexes/comments", {
|
|
||||||
config,
|
|
||||||
comments: comments.comments,
|
|
||||||
});
|
|
||||||
}); // /index/posts
|
|
||||||
|
|
||||||
|
|
||||||
///////////////////// Standard Pages //////////////////////
|
|
||||||
app.get("/", (req,res) => {
|
|
||||||
// Increment the hitcount
|
|
||||||
if (config.enable_hitcount) {
|
|
||||||
let hitcount = parseInt(fs.readFileSync('../data/hitcount.txt'))
|
|
||||||
hitcount += 1
|
|
||||||
console.log(`/ Is loaded, hitcount: ${hitcount}`)
|
|
||||||
fs.writeFileSync(`../data/hitcount.txt`, `${hitcount}`, 'utf-8');
|
|
||||||
}
|
|
||||||
|
|
||||||
res.render("pages/timeline",
|
|
||||||
{
|
|
||||||
config,
|
|
||||||
locale,
|
|
||||||
posts: data.getdata("posts"),
|
|
||||||
users: data.getdata("users"),
|
|
||||||
comments: data.getdata("comments"),
|
|
||||||
hitcount: data.getdata("hitcount"),
|
|
||||||
fromUnixTime,
|
|
||||||
format,
|
|
||||||
getUnixTime,
|
|
||||||
func,
|
|
||||||
})
|
|
||||||
}); // /
|
|
||||||
app.get("/user/:username", (req, res) => {
|
|
||||||
const userID = func.get_userID(req.params.username)
|
|
||||||
console.log(userID)
|
|
||||||
if (userID != -1) {
|
|
||||||
res.render("pages/user",
|
|
||||||
{
|
|
||||||
config,
|
|
||||||
locale,
|
|
||||||
posts,
|
|
||||||
user: users[userID],
|
|
||||||
userID: userID,
|
|
||||||
comments: comments.comments,
|
|
||||||
fromUnixTime: fromUnixTime,
|
|
||||||
format: format,
|
|
||||||
getUnixTime: getUnixTime,
|
|
||||||
func,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
else if (userID == -1) {
|
|
||||||
res.render("partials/message",
|
|
||||||
{
|
|
||||||
message: locale.user_doesnt_exist,
|
|
||||||
config,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}); // /user/:username
|
|
||||||
app.get("/post/:post_index", (req, res) => {
|
|
||||||
const postID = req.params.post_index
|
|
||||||
if (postID > posts.length-1 || posts[postID]["deleted"] == true) {
|
|
||||||
res.render("partials/message", {
|
|
||||||
message: locale.post_doesnt_exist,
|
|
||||||
config,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
else if (typeof posts[postID]["deleted"] == "undefined" || posts[postID]["deleted"] == false) {
|
|
||||||
res.render("pages/post",
|
|
||||||
{
|
|
||||||
config,
|
|
||||||
locale,
|
|
||||||
post: posts[postID],
|
|
||||||
postID: postID,
|
|
||||||
user: users[posts[postID].userID],
|
|
||||||
comments: comments.comments[postID],
|
|
||||||
fromUnixTime,
|
|
||||||
format,
|
|
||||||
getUnixTime,
|
|
||||||
func,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.log("Error loading page")
|
|
||||||
res.redirect(301,"/")
|
|
||||||
}
|
|
||||||
}); // /post/:post_index
|
|
||||||
app.get("/tag/:tag", (req,res) => {
|
|
||||||
const tag = req.params.tag
|
|
||||||
res.render("pages/tag",
|
|
||||||
{
|
|
||||||
config,
|
|
||||||
locale,
|
|
||||||
tag,
|
|
||||||
posts,
|
|
||||||
users,
|
|
||||||
comments: comments.comments,
|
|
||||||
fromUnixTime: fromUnixTime,
|
|
||||||
format: format,
|
|
||||||
getUnixTime: getUnixTime,
|
|
||||||
func,
|
|
||||||
})
|
|
||||||
}); // /tag/:tag
|
|
||||||
app.get("/comment/:commentID", (req,res) => {
|
|
||||||
const commentID = req.params.commentID;
|
|
||||||
const comment = func.get_comment(commentID)
|
|
||||||
if (comment == -1) {
|
|
||||||
res.render("partials/message", {
|
|
||||||
config,
|
|
||||||
message: locale.comment_doesnt_exist,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
res.render("pages/comment",
|
|
||||||
{
|
|
||||||
config: config,
|
|
||||||
locale,
|
|
||||||
post: posts[comment["id"]],
|
|
||||||
users,
|
|
||||||
comment,
|
|
||||||
fromUnixTime: fromUnixTime,
|
|
||||||
format: format,
|
|
||||||
getUnixTime: getUnixTime,
|
|
||||||
func,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
///////////////////// Form pages ////////////////////////////
|
|
||||||
app.get(config.new_post_url, (req,res) => {
|
|
||||||
res.render("forms/new_post", {
|
|
||||||
config,
|
|
||||||
locale,
|
|
||||||
});
|
|
||||||
}); // /post
|
|
||||||
app.get(config.signup_url, (req,res) => {
|
|
||||||
// if the server does allow signup
|
|
||||||
if (config.allow_signup == true) {
|
|
||||||
// Send the page for signing up to the server
|
|
||||||
res.render("forms/signup", {
|
|
||||||
config,
|
|
||||||
locale,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// if the server does not allow signup
|
|
||||||
else if (config.allow_signup == false) {
|
|
||||||
res.render("partials/message", {
|
|
||||||
message: locale.signups_unavailable,
|
|
||||||
config,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
// If allow_signup is undefined or not a boolean, error
|
|
||||||
else {
|
|
||||||
res.redirect(301,"/")
|
|
||||||
console.log("Error, invalid value for allow_signup (bool)")
|
|
||||||
}
|
|
||||||
}); // /signup
|
|
||||||
app.get(`${config.edit_account_base_url}/:user_id`, (req,res) => {
|
|
||||||
const userID = parseInt(req.params.user_id);
|
|
||||||
res.render("forms/edit_account", {
|
|
||||||
config,
|
|
||||||
locale,
|
|
||||||
user: users[userID],
|
|
||||||
userID
|
|
||||||
});
|
|
||||||
}); // /delete_account
|
|
||||||
app.get(`${config.edit_post_base_url}/:post_id`, (req,res) => {
|
|
||||||
const post_id = req.params.post_id
|
|
||||||
const post = posts[post_id]
|
|
||||||
const user = users[post['userID']]
|
|
||||||
res.render("forms/edit_post", {
|
|
||||||
config,
|
|
||||||
locale,
|
|
||||||
post,
|
|
||||||
post_id,
|
|
||||||
user,
|
|
||||||
});
|
|
||||||
}); // /edit/:post_id
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////// Form actions /////////////////////////
|
|
||||||
app.post("/submit_comment", (req,res) => {
|
|
||||||
const unix_timestamp = getUnixTime(new Date())
|
|
||||||
let name = func.escape_input(req.body.name)
|
|
||||||
if (name == "") {
|
|
||||||
name = config.default_commenter_username
|
|
||||||
}
|
|
||||||
new_comment = {
|
|
||||||
"name": name,
|
|
||||||
"content": func.escape_input(req.body.content),
|
|
||||||
"id": comments.counter,
|
|
||||||
"pubdate": unix_timestamp
|
|
||||||
};
|
|
||||||
comments.counter += 1;
|
|
||||||
comments.comments[req.body.post_index].push(new_comment);
|
|
||||||
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
|
|
||||||
|
|
||||||
res.redirect(301,`/post/${req.body.post_index}`)
|
|
||||||
}); // /submit_comment
|
|
||||||
app.post("/submit_post", (req,res) => {
|
|
||||||
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
|
|
||||||
const username = func.escape_input(req.body.username)
|
|
||||||
const title = func.escape_input(req.body.title)
|
|
||||||
const content = req.body.content
|
|
||||||
const tags = func.escape_input(req.body.tags).split(',').map(str => str.trim());
|
|
||||||
const unix_timestamp = getUnixTime(new Date())
|
|
||||||
|
|
||||||
if (func.get_userID(username) == -1) {
|
|
||||||
res.render("partials/message", {
|
|
||||||
message: locale.user_doesnt_exit,
|
|
||||||
config,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (users[func.get_userID(username)]['hash'] == password) { // Password matches
|
|
||||||
console.log(username, "is submitting a post titled:", title);
|
|
||||||
posts.push({
|
|
||||||
"id": posts.length,
|
|
||||||
"userID": func.get_userID(username),
|
|
||||||
"title": title,
|
|
||||||
"content": content,
|
|
||||||
"pubdate": unix_timestamp,
|
|
||||||
"editdate": unix_timestamp,
|
|
||||||
"tags": tags,
|
|
||||||
})
|
|
||||||
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
|
|
||||||
comments.comments.push([])
|
|
||||||
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`)
|
|
||||||
res.redirect(302, "/");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
res.render("partials/message", {
|
|
||||||
message: locale.incorrect_password,
|
|
||||||
config,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}); // /submit_post
|
|
||||||
app.post("/submit_signup", (req,res) => {
|
|
||||||
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
|
|
||||||
const username = func.escape_input(req.body.username)
|
|
||||||
const prettyname = func.escape_input(req.body.prettyname)
|
|
||||||
const description = req.body.description
|
|
||||||
|
|
||||||
// Check that signups are allowed
|
|
||||||
if (config.allow_signup == true) {
|
|
||||||
// func.get_userID will return -1 if the user does not exist
|
|
||||||
// so this checks that the user does not exist
|
|
||||||
if (func.get_userID(username) == -1) {
|
|
||||||
users.push({
|
|
||||||
"id": users.length,
|
|
||||||
"username": username,
|
|
||||||
"prettyname": prettyname,
|
|
||||||
"hash": password,
|
|
||||||
"description": description,
|
|
||||||
})
|
|
||||||
fs.writeFileSync(`../data/users.json`, `${JSON.stringify(users)}`, 'utf-8');
|
|
||||||
res.redirect(301, `/user/${username}`)
|
|
||||||
}
|
|
||||||
// if the user does exist then
|
|
||||||
else {
|
|
||||||
res.render("partials/message", {
|
|
||||||
message: locale.user_exists,
|
|
||||||
config,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (config.allow_signup == false) {
|
|
||||||
res.render("partials/message", {
|
|
||||||
message: locale.signups_unavailable,
|
|
||||||
config,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
// If allow_signup is undefined or not a boolean, error
|
|
||||||
else {
|
|
||||||
res.redirect(301,"/")
|
|
||||||
console.log("Error, invalid value for allow_signup (bool)")
|
|
||||||
}
|
|
||||||
}); // /submit_signup
|
|
||||||
app.post("/submit_edit_user", (req,res) => {
|
|
||||||
// Get the form info
|
|
||||||
const password = crypto.createHash("sha512").update(req.body.password).digest("hex");
|
|
||||||
const userID = func.escape_input(req.body.userID)
|
|
||||||
const description = req.body.description
|
|
||||||
const prettyname = func.escape_input(req.body.prettyname)
|
|
||||||
const delete_bool = req.body.delete
|
|
||||||
|
|
||||||
if (userID >= 0) { // The user exists
|
|
||||||
if (password == users[userID]['hash']) { // password matches
|
|
||||||
console.log(userID, " (userID) is modifying their account")
|
|
||||||
users[userID]["prettyname"] = prettyname;
|
|
||||||
users[userID]["description"] = description;
|
|
||||||
|
|
||||||
if (delete_bool == true) {
|
|
||||||
// Delete the user
|
|
||||||
users[userID] = {"id": userID,"deleted": true}
|
|
||||||
// Delete all their posts
|
|
||||||
for (let postid = 0; postid < posts.length; postid++) { // loop over all posts
|
|
||||||
if (posts[postid]['userID'] == userID) { // if userID matches
|
|
||||||
posts[postid] = {"id": postid, "deleted": true} // delete the post
|
|
||||||
comments.comments[postid] = [] // the comments for this post should also be deleted
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
// Write these changes
|
|
||||||
fs.writeFileSync(`../data/users.json`, `${JSON.stringify(users)}`, 'utf-8');
|
|
||||||
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
|
|
||||||
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
|
|
||||||
res.redirect(301,`/user/${users[userID]["username"]}`)
|
|
||||||
}
|
|
||||||
else { // password does not match
|
|
||||||
res.render("partials/message", {
|
|
||||||
message: locale.incorrect_password,
|
|
||||||
config
|
|
||||||
}
|
|
||||||
)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
res.render("partials/message", {
|
|
||||||
message: locale.user_doesnt_exist,
|
|
||||||
config,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}); // /submit_delete_account
|
|
||||||
app.post("/submit_edit_post", (req,res) => {
|
|
||||||
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
|
|
||||||
const postID = req.body.postID
|
|
||||||
const userID = req.body.userID
|
|
||||||
const title = func.escape_input(req.body.title)
|
|
||||||
const content = req.body.content
|
|
||||||
const tags = func.escape_input(req.body.tags).split(",").map(str => str.trim());
|
|
||||||
const delete_bool = req.body.delete
|
|
||||||
const unix_timestamp = getUnixTime(new Date())
|
|
||||||
console.log(users[userID]['prettyname'], "is editting the post titled:", title);
|
|
||||||
|
|
||||||
if (users[userID]['hash'] == password) { // password matches
|
|
||||||
let post = posts[postID]
|
|
||||||
post['title'] = title
|
|
||||||
post['content'] = content
|
|
||||||
post['tags'] = tags
|
|
||||||
post['editdate'] = unix_timestamp
|
|
||||||
if (typeof delete_bool != "undefined") {
|
|
||||||
console.log("Deleting post!")
|
|
||||||
posts[postID] = {"id": post["id"], "deleted": true}
|
|
||||||
comments.comments[postID] = [];
|
|
||||||
fs.writeFileSync(`../data/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
|
|
||||||
}
|
|
||||||
fs.writeFileSync(`../data/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
|
|
||||||
res.redirect(302, "/");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
res.render("partials/message", {
|
|
||||||
message: locale.incorrect_password,
|
|
||||||
config,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}); // /submit_edit
|
|
||||||
|
|
||||||
app.listen(config.port, () => {
|
app.listen(config.port, () => {
|
||||||
console.log(`Server is running at http://localhost:${config.port} webroot: ${config.root_path}`);
|
console.log(`Server is running at http://localhost:${config.port} webroot: ${config.root_path}`);
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
<h1>
|
<h1>
|
||||||
<%= user.prettyname %>
|
<%= user.prettyname %>
|
||||||
</h1>
|
</h1>
|
||||||
<p><%- func.render_md(user.description) %></p>
|
<p><%# func.render_md(user.description) %></p>
|
||||||
<a href="<%= config.edit_account_base_url %>/<%= userID %>"><%= locale.edit_account %></a><br/>
|
<a href="<%= config.edit_account_base_url %>/<%= userID %>"><%= locale.edit_account %></a><br/>
|
||||||
<a href="/user/<%= user.username %>/rss"><%= locale.rss_feed %></a><br/>
|
<a href="/user/<%= user.username %>/rss"><%= locale.rss_feed %></a><br/>
|
||||||
<a href="/user/<%= user.username %>/atom"><%= locale.atom_feed %></a>
|
<a href="/user/<%= user.username %>/atom"><%= locale.atom_feed %></a>
|
||||||
|
Reference in New Issue
Block a user