json
This commit is contained in:
116
app.js
116
app.js
@@ -23,10 +23,10 @@ let config // contains a set of configuration for the site, see example-config.j
|
||||
|
||||
try {
|
||||
// We're going to try and import the modules,
|
||||
users = require('./users.js');
|
||||
posts = require('./posts.js');
|
||||
comments = require('./comments.js');
|
||||
config = require('./config.js');
|
||||
users = require('./users.json');
|
||||
posts = require('./posts.json');
|
||||
comments = require('./comments.json');
|
||||
config = require('./config.json');
|
||||
}
|
||||
catch (error) {
|
||||
// if they don't all import then
|
||||
@@ -56,7 +56,6 @@ const app = express();
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
app.use(express.static(config.root_path));
|
||||
|
||||
// Initialise the program by creating users.js, comments.js, posts.js and config.js
|
||||
// All require default content in them to start off with
|
||||
// Then exit successfully
|
||||
@@ -67,21 +66,21 @@ function initialise() {
|
||||
}
|
||||
catch (error) {
|
||||
console.log("Creating users file")
|
||||
fs.writeFileSync(`${__dirname}/users.js`, `export const users = []`)
|
||||
fs.writeFileSync(`${__dirname}/users.json`, `{\n"users": []\n}`)
|
||||
}
|
||||
try {
|
||||
const posts = require("./posts.js");
|
||||
const posts = require("./posts.json");
|
||||
}
|
||||
catch (error) {
|
||||
console.log("Creating posts file")
|
||||
fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = []`)
|
||||
fs.writeFileSync(`${__dirname}/posts.json`, `{\n"posts": []\n}`)
|
||||
}
|
||||
try {
|
||||
const comments = require("./comments.js");
|
||||
const comments = require("./comments.json");
|
||||
}
|
||||
catch (error) {
|
||||
console.log("Creating comments file")
|
||||
fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = []\nexport const counter = 0`)
|
||||
fs.writeFileSync(`${__dirname}/comments.json`, `{\n"comments": [],\n"counter": 0}`)
|
||||
}
|
||||
try {
|
||||
const config = require("./config.js");
|
||||
@@ -103,8 +102,8 @@ function initialise() {
|
||||
// if the user is present, it returns the index of the user (integer)
|
||||
// if the user is not present it returns -1
|
||||
function get_userID(username) {
|
||||
for (let i = 0; i < users.users.length; i++) { // Loop over every user
|
||||
if (users.users[i]['username'] == username) {
|
||||
for (let i = 0; i < users.length; i++) { // Loop over every user
|
||||
if (users[i]['username'] == username) {
|
||||
return i // If the username matches then return the index of that user
|
||||
}
|
||||
}
|
||||
@@ -169,22 +168,22 @@ function replace_format_indicators(template, post_index=-1, tag_name="tag", user
|
||||
.replaceAll("%Z", config.attribution)
|
||||
.replaceAll("%S", config.seperator)
|
||||
if (post_index >= 0) { // These can only be replaced if a post is specified (by default the post id is -1)
|
||||
post_object = posts.posts[post_index] // Defines the post object for easy reference
|
||||
post_object = posts[post_index] // Defines the post object for easy reference
|
||||
output_string = output_string
|
||||
.replaceAll("%A", (post_object["tags"]))
|
||||
.replaceAll("%B", (hyperlink_tags(post_object["tags"])))
|
||||
.replaceAll("%C", converter.makeHtml(post_object["content"]))
|
||||
.replaceAll("%D", unix_time_to_date_format(post_object["pubdate"]))
|
||||
.replaceAll("%E", unix_time_to_date_format(post_object["editdate"]))
|
||||
.replaceAll("%F", users.users[post_object["userID"]]['prettyname'])
|
||||
.replaceAll("%F", users[post_object["userID"]]['prettyname'])
|
||||
.replaceAll("%G", tag_name)
|
||||
.replaceAll("%I", converter.makeHtml(users.users[post_object['userID']]['description']))
|
||||
.replaceAll("%I", converter.makeHtml(users[post_object['userID']]['description']))
|
||||
.replaceAll("%L", `/post/${post_index}`)
|
||||
.replaceAll("%M", return_comments(post_index))
|
||||
.replaceAll("%N", users.users[post_object["userID"]]['username'])
|
||||
.replaceAll("%N", users[post_object["userID"]]['username'])
|
||||
.replaceAll("%S", config.seperator)
|
||||
.replaceAll("%T", post_object["title"])
|
||||
.replaceAll("%U", `/user/${users.users[post_object["userID"]]['username']}`)
|
||||
.replaceAll("%U", `/user/${users[post_object["userID"]]['username']}`)
|
||||
.replaceAll("%X", `<form method="POST" action="/submit_comment">
|
||||
<input type="hidden" name="post_index" value="${post_index}">
|
||||
<input placeholder="username" name="name"><br/>
|
||||
@@ -194,13 +193,13 @@ function replace_format_indicators(template, post_index=-1, tag_name="tag", user
|
||||
}
|
||||
if (user_index >= 0) { // these should only be replaced if a user is specified (by default the user id is -1)
|
||||
output_string = output_string
|
||||
.replaceAll("%F", users.users[user_index]['prettyname'])
|
||||
.replaceAll("%F", users[user_index]['prettyname'])
|
||||
.replaceAll("%G", tag_name)
|
||||
.replaceAll("%I", converter.makeHtml(users.users[user_index]['description']))
|
||||
.replaceAll("%I", converter.makeHtml(users[user_index]['description']))
|
||||
.replaceAll("%L", `/post/${post_index}`)
|
||||
.replaceAll("%N", users.users[user_index]['username'])
|
||||
.replaceAll("%N", users[user_index]['username'])
|
||||
.replaceAll("%S", config.seperator)
|
||||
.replaceAll("%U", `/user/${users.users[user_index]['username']}`)
|
||||
.replaceAll("%U", `/user/${users[user_index]['username']}`)
|
||||
}
|
||||
if (config.enable_hitcount == true) { // Finally, the hitcounter should only be replaced if config.enable_hitcount is true
|
||||
output_string = output_string
|
||||
@@ -254,16 +253,16 @@ app.get(config.rss_path, (req,res) => {
|
||||
<link>${config.site_url}</link>
|
||||
<description>${config.site_description}</description>
|
||||
`
|
||||
for (let i = posts.posts.length-1; i >= 0; i--) {
|
||||
for (let i = posts.length-1; i >= 0; i--) {
|
||||
rss_content += `
|
||||
<item>
|
||||
<title>${posts.posts[i]["title"]}</title>
|
||||
<title>${posts[i]["title"]}</title>
|
||||
<link>${config.site_url}/post/${i}</link>
|
||||
<description><![CDATA[${converter.makeHtml(posts.posts[i]["content"])}]]></description>
|
||||
<description><![CDATA[${converter.makeHtml(posts[i]["content"])}]]></description>
|
||||
<guid isPermaLink="true">${config.site_url}/post/${i}</guid>
|
||||
<pubDate>${unix_time_to_rss_date(posts.posts[i]['pubdate'])}</pubDate>`
|
||||
for (let j = 0; j < posts.posts[i]['tags'].length; j++) {
|
||||
rss_content += `<category><![CDATA[${posts.posts[i]['tags'][j]}]]></category>`
|
||||
<pubDate>${unix_time_to_rss_date(posts[i]['pubdate'])}</pubDate>`
|
||||
for (let j = 0; j < posts[i]['tags'].length; j++) {
|
||||
rss_content += `<category><![CDATA[${posts[i]['tags'][j]}]]></category>`
|
||||
};
|
||||
rss_content += "</item>"
|
||||
}
|
||||
@@ -286,8 +285,8 @@ app.get("/", (req,res) => {
|
||||
header_div = config.timeline_header
|
||||
header_div = replace_format_indicators(header_div);
|
||||
posts_div = "";
|
||||
counter = posts.posts.length - 1;
|
||||
while ((counter >= 0) && (counter > (posts.posts.length - (config.timeline_length + 1))))
|
||||
counter = posts.length - 1;
|
||||
while ((counter >= 0) && (counter > (posts.length - (config.timeline_length + 1))))
|
||||
{
|
||||
let post = config.timeline_post_format;
|
||||
posts_div += replace_format_indicators(post, counter);
|
||||
@@ -299,8 +298,8 @@ app.get("/user/:username", (req, res) => {
|
||||
header_div = config.user_page_header
|
||||
header_div = replace_format_indicators(header_div,-1,"tag",get_userID(req.params.username))
|
||||
posts_div = "";
|
||||
for (let post_index = posts.posts.length-1; post_index >= 0; post_index--) {
|
||||
if (users.users[posts.posts[post_index]["userID"]]["username"] == req.params.username) {
|
||||
for (let post_index = posts.length-1; post_index >= 0; post_index--) {
|
||||
if (users[posts[post_index]["userID"]]["username"] == req.params.username) {
|
||||
let post = config.user_post_format;
|
||||
posts_div += replace_format_indicators(post, post_index);
|
||||
}
|
||||
@@ -318,8 +317,8 @@ app.get("/tag/:tag", (req,res) => {
|
||||
let header_div = config.tag_page_header
|
||||
header_div = replace_format_indicators(header_div,0,tag)
|
||||
let page_content = ""
|
||||
for (let i = posts.posts.length-1; i >= 0; i--) {
|
||||
if (posts.posts[i]['tags'].includes(tag)) {
|
||||
for (let i = posts.length-1; i >= 0; i--) {
|
||||
if (posts[i]['tags'].includes(tag)) {
|
||||
let post = config.tag_post_format;
|
||||
page_content += replace_format_indicators(post, i);
|
||||
};
|
||||
@@ -369,8 +368,8 @@ app.get("/delete_account", (req,res) => {
|
||||
}); // /delete_account
|
||||
app.get("/edit/:post_id", (req,res) => {
|
||||
const post_id = req.params.post_id
|
||||
const post = posts.posts[post_id]
|
||||
const user = users.users[post['userID']]
|
||||
const post = posts[post_id]
|
||||
const user = users[post['userID']]
|
||||
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head>
|
||||
<form action="/submit_edit" method="POST" onsubmit="sha512password()">
|
||||
<input name="userID" type="hidden" value="${post['userID']}">
|
||||
@@ -399,7 +398,7 @@ app.post("/submit_comment", (req,res) => {
|
||||
};
|
||||
let counter = comments.counter+1;
|
||||
comments.comments[req.body.post_index].push(new_comment);
|
||||
fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = ${JSON.stringify(comments.comments)}\nexport const counter = ${counter}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/comments.json`, `${JSON.stringify(comments)}`, 'utf-8');
|
||||
|
||||
res.redirect(301,`/post/${req.body.post_index}`)
|
||||
}); // /submit_comment
|
||||
@@ -416,8 +415,8 @@ app.post("/submit_post", (req,res) => {
|
||||
res.send("User does not exist")
|
||||
}
|
||||
|
||||
else if (users.users[get_userID(username)]['hash'] == password) { // Password matches
|
||||
posts.posts.push({
|
||||
else if (users[get_userID(username)]['hash'] == password) { // Password matches
|
||||
posts.push({
|
||||
"userID": get_userID(username),
|
||||
"title": title,
|
||||
"content": content,
|
||||
@@ -425,9 +424,9 @@ app.post("/submit_post", (req,res) => {
|
||||
"editdate": unix_timestamp,
|
||||
"tags": tags,
|
||||
})
|
||||
fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
|
||||
comments.comments.push([])
|
||||
fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = ${JSON.stringify(comments.comments)}\nexport const counter = ${comments.counter}`)
|
||||
fs.writeFileSync(`${__dirname}/comments.json`, `${JSON.stringify(comments)}`)
|
||||
res.redirect(302, "/");
|
||||
}
|
||||
else {
|
||||
@@ -445,13 +444,13 @@ app.post("/submit_signup", (req,res) => {
|
||||
// get_userID will return -1 if the user does not exist
|
||||
// so this checks that the user does not exist
|
||||
if (get_userID(username) == -1) {
|
||||
users.users.push({
|
||||
users.push({
|
||||
"username": username,
|
||||
"prettyname": prettyname,
|
||||
"hash": password,
|
||||
"description": description,
|
||||
})
|
||||
fs.writeFileSync(`${__dirname}/users.js`, `export const users = ${JSON.stringify(users.users)}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/users.json`, `${JSON.stringify(users)}`, 'utf-8');
|
||||
res.redirect(301, `/user/${username}`)
|
||||
}
|
||||
// if the user does exist then
|
||||
@@ -476,21 +475,21 @@ app.post("/submit_delete_account", (req,res) => {
|
||||
const userID = get_userID(username)
|
||||
|
||||
if (userID >= 0) { // The user exists
|
||||
if (password == users.users[userID]['hash']) { // password matches
|
||||
if (password == users[userID]['hash']) { // password matches
|
||||
console.log(username, "(userID:", userID, ") is trying deleting their account")
|
||||
// Delete the user
|
||||
users.users.splice(userID,1)
|
||||
users.splice(userID,1)
|
||||
// Delete all their posts
|
||||
for (let postid = 0; postid < posts.posts.length; postid++) { // loop over all posts
|
||||
if (posts.posts[postid]['userID'] == userID) { // if userID matches
|
||||
posts.posts.splice(postid,1) // delete the post
|
||||
for (let postid = 0; postid < posts.length; postid++) { // loop over all posts
|
||||
if (posts[postid]['userID'] == userID) { // if userID matches
|
||||
posts.splice(postid,1) // delete the post
|
||||
comments.comments.splice(postid,1) // the comments for this post should also be delete
|
||||
}
|
||||
};
|
||||
// Write these changes
|
||||
fs.writeFileSync(`${__dirname}/users.js`, `export const users = ${JSON.stringify(users.users)}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = ${JSON.stringify(comments.comments)}\nexport const counter = ${comments.counter}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/users.json`, `${JSON.stringify(users)}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/comments.json`, `${JSON.stringify(comments.comments)}\nexport const counter = ${comments.counter}`, 'utf-8');
|
||||
res.redirect(301,"/")
|
||||
}
|
||||
else { // password does not match
|
||||
@@ -510,28 +509,29 @@ app.post("/submit_edit", (req,res) => {
|
||||
const tags = req.body.tags.split(',');
|
||||
const delete_bool = req.body.delete
|
||||
const unix_timestamp = getUnixTime(new Date())
|
||||
console.log(users.users[userID]['prettyname'], "is editting the post titled:", title);
|
||||
console.log(users[userID]['prettyname'], "is editting the post titled:", title);
|
||||
|
||||
if (users.users[userID]['hash'] == password) { // password matches
|
||||
let post = posts.posts[postID]
|
||||
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.posts.splice(postID,1)
|
||||
posts.splice(postID,1)
|
||||
comments.comments.splice(postID,1)
|
||||
fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = ${JSON.stringify(comments.comments)}\nexport const counter = ${comments.counter}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/comments.json`, `${JSON.stringify(comments.comments)}\nexport const counter = ${comments.counter}`, 'utf-8');
|
||||
}
|
||||
fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8');
|
||||
fs.writeFileSync(`${__dirname}/posts.json`, `${JSON.stringify(posts)}`, 'utf-8');
|
||||
res.redirect(302, "/");
|
||||
}
|
||||
else {
|
||||
res.send(`Invalid Password for user`,users.users[userID]['prettyname']);
|
||||
res.send(`Invalid Password for user`,users[userID]['prettyname']);
|
||||
}
|
||||
}); // /submit_edit
|
||||
|
||||
app.listen(config.port, () => {
|
||||
console.log(`Server is running at http://localhost:${config.port} in ${config.root_path}`);
|
||||
console.log(`Server is running at http://localhost:${config.port} webroot: ${config.root_path}`);
|
||||
console.log("Running in: ", __dirname)
|
||||
});
|
||||
|
Reference in New Issue
Block a user