From 3e745e6842b057c60f48fadcaf6c2c9310bad5e2 Mon Sep 17 00:00:00 2001 From: DeaDvey Date: Sun, 20 Jul 2025 22:47:07 +0100 Subject: [PATCH] json --- .gitignore | 8 +-- example-config.js => CONFIG.md | 11 +++- README.md | 3 + app.js | 116 ++++++++++++++++----------------- example-config.json | 33 ++++++++++ package.json | 1 - 6 files changed, 108 insertions(+), 64 deletions(-) rename example-config.js => CONFIG.md (83%) create mode 100755 example-config.json diff --git a/.gitignore b/.gitignore index 189278f..858c562 100755 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ node_modules package-lock.json -posts.js -comments.js -users.js -config.js +posts.json +comments.json +users.json +config.json hitcount.txt *.swp diff --git a/example-config.js b/CONFIG.md similarity index 83% rename from example-config.js rename to CONFIG.md index 1171d86..d9eb315 100755 --- a/example-config.js +++ b/CONFIG.md @@ -2,7 +2,7 @@ export const seperator = "
" export const site_name = "My Blog" export const site_url = "https://example.com" export const port = 8080 -export const allow_signup = false +export const allow_signup = true export const site_description = "Read my blogs!" export const timeline_length = 20 export const enable_hitcount = true // Can slow down page loading a bit @@ -54,6 +54,8 @@ export const timeline_header = `

%Y

%W

Create Post
RSS Feed
+Sign Up
+Delete Account
Hit count: %H %S` export const user_page_header = `

%F's posts:

@@ -96,6 +98,13 @@ export const tag_post_format = `

%T

export const site_wide_footer = `Site is ran by DeaDvey
%Z` +// Custom Strings +export const signup_agreement = "I agree to not post illegal or hateful content" +export const signups_unavailable = "Sorry, this server does not allow signups" +export const user_exists = "Sorry, this user already exists, try a different username" +export const user_doesnt_exist = "Sorry, this user does not exist" +export const delete_account_confirmation = "I agree that my account and all of my posts will be permanently deleted instantly" +export const incorrect_password = "Incorrect Password" /// Custom CSS to be applied to every page export const css = ` diff --git a/README.md b/README.md index 3cf6633..1bc5170 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ In action on my website: [deadvey.com](https://deadvey.com)
* probably insecure as hell # planned features/todo list +* seperate functions into modules +* builtin crypto +* ejs * user specific RSS feeds * atom * federation (looks tricky) diff --git a/app.js b/app.js index 8f2e59d..7f9675f 100755 --- a/app.js +++ b/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", `

@@ -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) => { ${config.site_url} ${config.site_description} ` - for (let i = posts.posts.length-1; i >= 0; i--) { + for (let i = posts.length-1; i >= 0; i--) { rss_content += ` - ${posts.posts[i]["title"]} + ${posts[i]["title"]} ${config.site_url}/post/${i} - + ${config.site_url}/post/${i} - ${unix_time_to_rss_date(posts.posts[i]['pubdate'])}` - for (let j = 0; j < posts.posts[i]['tags'].length; j++) { - rss_content += `` + ${unix_time_to_rss_date(posts[i]['pubdate'])}` + for (let j = 0; j < posts[i]['tags'].length; j++) { + rss_content += `` }; rss_content += "" } @@ -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(` @@ -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) }); diff --git a/example-config.json b/example-config.json new file mode 100755 index 0000000..779c818 --- /dev/null +++ b/example-config.json @@ -0,0 +1,33 @@ +{ + "seperator": "
", + "site_name": "My Blog", + "site_url": "https://example.com", + "port": 8080, + "allow_signup": true, + "site_description": "Read my blogs!", + "timeline_length": 20, + "enable_hitcount": true, + "charset": "UTF-8", + "root_path": "/path/to/root/of/website", + "default_username": "Anon", + "rss": true, + "rss_path": "/rss", + "date_format": "yyyy-MM-dd", + "time_zone": "+0000", + "timeline_header": "

%Y

%W

Create Post
RSS Feed
Sign Up
Delete Account
Hit count: %H%S", + "user_page_header": "

%F's posts:

%I%S", + "tag_page_header": "

Posts tagged: %G

%S", + "user_post_format": "

%T

%C

%B
Permalink
%X%M%S", + "post_page_format": "

%T

%C

%B
By %N
Edit Post
Posted: %D
Edited: %E%S%X%M%S", + "timeline_post_format": "

%T

%C

Permalink
By %N%X%M%S", + "tag_post_format": "

%T

%C

%B
Permalink
By %N%S", + "site_wide_footer": "Site is ran by DeaDvey
%Z", + "signup_agreement": "I agree to not post illegal or hateful content", + "signups_unavailable": "Sorry, this server does not allow signups", + "user_exists": "Sorry, this user already exists, try a different username", + "user_doesnt_exist": "Sorry, this user does not exist", + "delete_account_confirmation": "I agree that my account and all of my posts will be permanently deleted instantly", + "incorrect_password": "Incorrect Password", + "css": "", + "attribution": "Powered by blogger-nodejs: Source Code, license (WTFPL)" +} diff --git a/package.json b/package.json index 49e1ebb..c02b2a9 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,5 @@ { "dependencies": { - "crypto": "^1.0.1", "date-fns": "^4.1.0", "express": "^5.1.0", "showdown": "^2.1.0"