edit mode

This commit is contained in:
deadvey 2025-07-09 20:09:50 +01:00
parent b6d47711b4
commit d23268fe89
4 changed files with 64 additions and 12 deletions

View File

@ -7,9 +7,10 @@ In action on my website: [deadvey.com](https://deadvey.com)<br/>
* powerful customisation
* rss
* timeline, user page, post page and tag specific page
* edit/delete posts
* probably insecure as hell
# planned features
* atom
* federation
* sign up
* edit/delete posts

64
app.js
View File

@ -48,6 +48,7 @@ function hyperlink_tags(tags) {
function replace_format_indicators(input_string, post_index=0, tag_name="tag") {
post_object = posts.posts[post_index]
output_string = input_string
.replaceAll("%%", "&#37;")
.replaceAll("%A", (post_object["tags"]))
.replaceAll("%B", (hyperlink_tags(post_object["tags"])))
.replaceAll("%C", post_object["content"].replaceAll("\n","<br/>"))
@ -59,7 +60,7 @@ function replace_format_indicators(input_string, post_index=0, tag_name="tag") {
.replaceAll("%L", `/post/${post_index}`)
.replaceAll("%N", users.users[post_object["userID"]]['username'])
.replaceAll("%P", "/post")
.replaceAll("%O", "/edit")
.replaceAll("%O", `/edit/${post_index}`)
.replaceAll("%R", "/rss")
.replaceAll("%S", config.seperator)
.replaceAll("%T", post_object["title"])
@ -114,11 +115,11 @@ app.get("/", (req,res) => {
posts_div += replace_format_indicators(post, counter);
counter -= 1;
}
res.send(`<html><style>${config.css}</style><body><div id="header">${header_div}</div><div id="posts">${posts_div}</div></body></html>`);
res.send(`<html><head><meta charset="${config.charset}"><style>${config.css}</style></head><body><div id="header">${header_div}</div><div id="posts">${posts_div}</div></body></html>`);
});
app.get("/post", (req,res) => {
res.send(`</html><style>${config.css}</style><form action="/submit_post" method="POST" onsubmit="sha512password()">
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head><form action="/submit_post" method="POST" onsubmit="sha512password()">
<label>Username: </label><input required name="username"><br/>
<label>Password: </label><input type="password" required id="password" name="password"><br/>
<label>Title: </label><input required name="title"><br/>
@ -127,10 +128,29 @@ app.get("/post", (req,res) => {
<input type="submit" value="Submit">
</form></html>`);
});
app.get("/edit", (req,res) => {
res.send(`Edit page under construction`);
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']]
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']}">
<input name="postID" type="hidden" value="${post_id}">
<label>${user.prettyname}'s Password: </label><input type="password" required id="password" name="password"><br/>
<label>Title: </label><input value="${post['title']}" required name="title"><br/>
<label>Content: </label>
<textarea required name="content">${post['content']
.replaceAll('"', "&#34;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll("\\", "&#92;")}</textarea><br/>
<label>Tags (comma seperated): </label><input value="${post['tags']}" name="tags"><br/>
<label>Delete forever (no undo): </label><input name="delete" type="checkbox"><br/>
<input type="submit" value="Submit">
</form></html>`);
});
app.get("/user/:username", (req, res) => {
header_div = config.user_page_header
header_div = replace_format_indicators(header_div)
@ -141,13 +161,13 @@ app.get("/user/:username", (req, res) => {
posts_div += replace_format_indicators(post, post_index);
}
}
res.send(`<html><style>${config.css}</style><body><div id="header">${header_div}</div><div id="posts">${posts_div}</div></body></html>`);
res.send(`<html><head><meta charset="${config.charset}"><style>${config.css}</style></head><body><div id="header">${header_div}</div><div id="posts">${posts_div}</div></body></html>`);
});
app.get("/post/:post_index", (req, res) => {
post_div = "";
let post = config.post_page_format;
post_div += replace_format_indicators(post, req.params.post_index);
res.send(`<html><style>${config.css}</style><body><div id="posts">${post_div}</div></body></html>`);
res.send(`<html><head><meta charset="${config.charset}"><style>${config.css}</style></head><body><div id="posts">${post_div}</div></body></html>`);
});
app.get("/tag/:tag", (req,res) => {
const tag = req.params.tag
@ -163,6 +183,34 @@ app.get("/tag/:tag", (req,res) => {
res.send(`<html><style>${config.css}</style><body><div id="header">${header_div}</div><div id="posts">${page_content}</div></body></html>`);
});
app.post("/submit_edit", (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 = req.body.title
const content = req.body.content
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);
if (users.users[userID]['hash'] == password) { // password matches
let post = posts.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)
}
fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8');
res.redirect(302, "/");
}
else {
res.send(`Invalid Password for user`,users.users[userID]['prettyname']);
}
});
app.post("/submit_post", (req,res) => {
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const username = req.body.username
@ -184,7 +232,7 @@ app.post("/submit_post", (req,res) => {
"pubdate": unix_timestamp,
"editdate": unix_timestamp,
"tags": tags,
})
})
fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8');
res.redirect(302, "/");
}

1
backupposts.js Executable file

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,7 @@ export const site_name = "Deadvey's Blog"
export const site_url = "https://deadvey.com"
export const site_description = "Films, tech, random shit"
export const timeline_length = 20
export const charset = "UTF-8" // Don't change unless you know why
// Anything in this directory will be in the webroot, so put favicon.ico and anything else here.
export const root_path = "/var/www/deadvey.com/blog"
@ -17,6 +18,7 @@ export const time_zone = "+0000"
//// Format /////
// The syntax for this is pretty simple
// %% - A literal %
// %A - List of tags
// %B - List of tags, each one with a hyperlink to that tag page
// %C - Post content
@ -28,7 +30,7 @@ export const time_zone = "+0000"
// %L - URL Permanent link to the post
// %N - the username of the user (poster)
// %P - URL to create a new post
// %O - URL to edit a post
// %O - URL to edit this post
// %R - Site wide RSS feed
// %S - post seperator as defined by post_seperator
// %T - Title
@ -39,7 +41,6 @@ export const time_zone = "+0000"
export const timeline_header = `<h1>%Y</h1>
<h2>%W</h2>
<a href="%P">Create Post</a><br/>
<a href="%O">Edit Post</a><br/>
<a href="%R">RSS Feed</a><br/>
%S`
export const user_page_header = `<h1>%F's posts:</h1>
@ -55,7 +56,8 @@ export const user_post_format = `<h2>%T</h2>
export const post_page_format = `<h1>%T</h1>
<p>%C</p>
<i>%B</i><br/>
<i>By <a href="%U">%N</a></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>`
export const timeline_post_format = `<h3>%T</h3>