This commit is contained in:
2025-07-20 22:47:07 +01:00
parent 74f2e4eaec
commit 3e745e6842
6 changed files with 108 additions and 64 deletions

8
.gitignore vendored
View File

@@ -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

View File

@@ -2,7 +2,7 @@ export const seperator = "<hr/>"
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 = `<h1>%Y</h1>
<h2>%W</h2>
<a href="%P">Create Post</a><br/>
<a href="%R">RSS Feed</a><br/>
<a href="%Q">Sign Up</a><br/>
<a href="%D">Delete Account</a><br/>
Hit count: %H
%S`
export const user_page_header = `<h1>%F's posts:</h1>
@@ -96,6 +98,13 @@ export const tag_post_format = `<h3>%T</h3>
export const site_wide_footer = `Site is ran by DeaDvey<br/>
%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 = `

View File

@@ -19,6 +19,9 @@ In action on my website: [deadvey.com](https://deadvey.com)<br/>
* probably insecure as hell
# planned features/todo list
* seperate functions into modules
* builtin crypto
* ejs
* user specific RSS feeds
* atom
* federation (looks tricky)

116
app.js
View File

@@ -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)
});

33
example-config.json Executable file
View File

@@ -0,0 +1,33 @@
{
"seperator": "<hr/>",
"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": "<h1>%Y</h1><h2>%W</h2><a href='%P'>Create Post</a><br/><a href='%R'>RSS Feed</a><br/><a href='%Q'>Sign Up</a><br/><a href='%D'>Delete Account</a><br/>Hit count: %H%S",
"user_page_header": "<h1>%F's posts:</h1>%I%S",
"tag_page_header": "<h1>Posts tagged: %G</h1>%S",
"user_post_format": "<h2>%T</h2><p>%C</p><i>%B</i><br/><a href='%L'>Permalink</a><br/>%X%M%S",
"post_page_format": "<h1>%T</h1><p>%C</p><i>%B</i><br/><i>By <a href='%U'>%N</a></i><br/><a href='%O'>Edit Post</a><br/><i>Posted: %D</i><br/><i>Edited: %E</i>%S%X%M%S",
"timeline_post_format": "<h3>%T</h3><p>%C</p><a href='%L'>Permalink</a><br/><i>By <a href='%U'>%N</a></i>%X%M%S",
"tag_post_format": "<h3>%T</h3><p>%C</p><i>%B</i><br/><a href='%L'>Permalink</a><br/><i>By <a href='%U'>%N</a></i>%S",
"site_wide_footer": "Site is ran by DeaDvey<br/>%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: <a href='https://git.javalsai.tuxcord.net/deadvey/blogger-nodejs'>Source Code</a>, <a href='https://git.javalsai.tuxcord.net/deadvey/blogger-nodejs/raw/branch/master/LICENSE'>license (WTFPL)</a>"
}

View File

@@ -1,6 +1,5 @@
{
"dependencies": {
"crypto": "^1.0.1",
"date-fns": "^4.1.0",
"express": "^5.1.0",
"showdown": "^2.1.0"