theforum/index.js

143 lines
6.1 KiB
JavaScript
Raw Normal View History

2022-05-19 12:09:29 +02:00
require('hashcode.js')
2022-01-17 08:04:31 +01:00
const Express = require('express')
const bdb = require('bdb.js')
const posts = bdb.load('posts.json', 1)
const webname = 'TheForum'
2022-01-19 16:32:41 +01:00
const email = null
2022-01-17 08:04:31 +01:00
const server = new Express()
server.set('view engine', 'ejs')
function replace(req, regex, repl) {
const url = req.url.replaceAll(regex, repl)
console.log(url)
const parsedUrl = new URL('http://localhost' + url)
req.url = url
req.originalUrl = url
req.path = parsedUrl.pathname
req.search = parsedUrl.search
req._parsedUrl = parsedUrl
const query = {}
for(const entry of parsedUrl.searchParams) {
query[entry[0]] = entry[1]
}
req.query = query
}
server.use(function replacer(req, res, next) {
2022-01-17 10:38:09 +01:00
replace(req, /^\/post\/([0-9]+)_?/g, '/post?id=$1')
replace(req, /^\/comment\/([0-9_]+)\/([0-9_]*)(\?(.*))?/g, '/comment?id=$1&comment=$2&$4')
2022-01-17 08:04:31 +01:00
next()
})
2022-01-23 05:37:20 +01:00
server.use(require('body-parser').urlencoded({extended: false}))
2022-01-17 08:04:31 +01:00
2022-01-23 05:37:20 +01:00
server.all('/', function get(req, res) {
2022-01-23 06:31:05 +01:00
const fake = req.body.fake === 'yes'
2022-01-17 08:04:31 +01:00
let mainPage = {author: webname, title: 'All posts', content: 'These are all the posts on the board:', comments: []}
2022-02-27 23:00:42 +01:00
for (let i = 0; i < posts.length - 2000; i++)
mainPage.comments.push(null)
for (let i = Math.max(0, posts.length - 2000); i < posts.length; i++) {
2022-01-17 17:54:39 +01:00
mainPage.comments.push({timestamp: posts[i].timestamp, author: posts[i].author, title: posts[i].title, content: posts[i].content, comments: []})
2022-01-17 08:04:31 +01:00
}
2023-03-19 16:23:42 +01:00
if(req.body.name && req.body.content) {
if(!req.body.title) req.body.title = '';
2022-05-19 12:40:47 +02:00
let post = {timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []}
2022-01-23 06:31:05 +01:00
if(fake) {
mainPage.comments.push(post)
2022-05-19 12:40:47 +02:00
res.render('post.ejs', {post: mainPage, postid: '-1', webname: webname, email: email, comment: '', fake: true, secret: req.body.secret || ''})
2022-01-23 06:31:05 +01:00
} else {
2022-05-19 12:40:47 +02:00
post.content += (req.body.secret && req.body.secret !== '' ? ('\n\n[* Signed: [" ' + req.body.secret.sha512().sha512().sha256() + ' "] *]') : '\n\n[* Not signed *]')
2022-01-23 06:31:05 +01:00
posts.push(post)
res.redirect(`/`)
}
return
}
res.render('post.ejs', {post: mainPage, postid: '-1', webname: webname, email: email, comment: '', fake: false})
2022-01-17 08:04:31 +01:00
})
server.get('/post', function get(req, res) {
if(req.query.id) {
let id = req.query.id
if(posts[id]) {
2022-01-23 06:47:47 +01:00
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: null, fake: false})
2022-01-17 08:04:31 +01:00
}
}
})
2022-01-23 05:37:20 +01:00
server.all('/comment', function get(req, res) {
2022-01-17 10:38:09 +01:00
if(req.query.id) {
2022-01-17 08:04:31 +01:00
let id = req.query.id
2022-01-17 10:38:09 +01:00
let comment = req.query.comment
2022-01-17 08:04:31 +01:00
if(posts[id]) {
2022-01-23 06:18:51 +01:00
const fake = req.body.fake === 'yes'
let toRemove = null
2022-01-17 10:38:09 +01:00
let cid = ''
2022-01-23 06:18:51 +01:00
try {
function recurse(post) {
console.log(String(cid) + ' ' + comment)
if(String(cid) === comment) {
2023-03-27 22:05:58 +02:00
if(req.body.name && req.body.content) {
if(!req.body.title) req.body.title = '';
2022-05-19 12:54:57 +02:00
let p = {timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []}
post.comments.push(p)
2022-01-23 06:18:51 +01:00
if(fake) {
2022-05-19 12:40:47 +02:00
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake, secret: req.body.secret || ''})
2022-01-23 06:18:51 +01:00
toRemove = post
2022-05-19 12:40:47 +02:00
} else {
2022-05-19 12:54:57 +02:00
p.content += (req.body.secret && req.body.secret !== '' ? ('\n\n[* Signed: [" ' + req.body.secret.sha512().sha512().sha256() + ' "] *]') : '\n\n[* Not signed *]')
2022-01-23 06:18:51 +01:00
res.redirect(`/post/${id}`)
2022-05-19 12:40:47 +02:00
}
2022-01-23 06:18:51 +01:00
cid = -1
}
return true
}
for(let i = 0; i < post.comments.length; i++) {
const pid = cid
cid += i + '_'
if(recurse(post.comments[i]))
return true
cid = pid
2022-01-17 08:04:31 +01:00
}
2022-01-23 06:18:51 +01:00
return false
2022-01-17 08:04:31 +01:00
}
2022-01-23 06:18:51 +01:00
const f = recurse(posts[id]);
if(f && cid == -1)
return
if(f) {
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
2022-01-17 08:04:31 +01:00
}
2022-01-23 06:18:51 +01:00
else {
2023-03-19 16:23:42 +01:00
if(req.body.name && req.body.content) {
if(!req.body.title) req.body.title = '';
2022-05-19 12:54:57 +02:00
let p = {timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []}
posts[id].comments.push(p)
2022-01-23 06:18:51 +01:00
if(fake) {
2022-05-19 12:40:47 +02:00
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake, secret: req.body.secret || ''})
2022-01-23 06:18:51 +01:00
toRemove = posts[id]
2022-05-19 12:40:47 +02:00
} else {
2022-05-19 12:54:57 +02:00
p.content += (req.body.secret && req.body.secret !== '' ? ('\n\n[* Signed: [" ' + req.body.secret.sha512().sha512().sha256() + ' "] *]') : '\n\n[* Not signed *]')
2022-01-23 06:18:51 +01:00
res.redirect(`/post/${id}`)
2022-05-19 12:40:47 +02:00
}
2022-01-23 06:18:51 +01:00
}
else
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: ''})
}
} finally {
if(fake) {
toRemove.comments.pop()
2022-01-17 10:38:09 +01:00
}
}
2022-01-17 08:04:31 +01:00
}
}
else
res.send("err1")
})
2022-01-17 08:21:43 +01:00
let PORT = process.argv[2]
if(!PORT) PORT = process.env.PORT
if(!PORT) PORT = 8080
server.listen(Number(PORT), () => console.log(PORT))