edit mode

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

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("%%", "%")
.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, "/");
}