FINALLY fixed sorting etc

This commit is contained in:
Daniella 2022-01-17 10:38:09 +01:00
parent aee0668223
commit 3223b0f58a
3 changed files with 29 additions and 19 deletions

View file

@ -25,15 +25,15 @@ function replace(req, regex, repl) {
}
server.use(function replacer(req, res, next) {
replace(req, /^\/post\/([0-9]+)/g, '/post?id=$1')
replace(req, /^\/comment\/([0-9]+)\/([0-9]+)(\?(.*))?/g, '/comment?id=$1&comment=$2&$4')
replace(req, /^\/post\/([0-9]+)_?/g, '/post?id=$1')
replace(req, /^\/comment\/([0-9_]+)\/([0-9_]*)(\?(.*))?/g, '/comment?id=$1&comment=$2&$4')
next()
})
server.get('/', function get(req, res) {
if(req.query.name && req.query.title && req.query.content) {
posts.unshift({author: req.query.name, title: req.query.title, content: req.query.content, comments: []})
posts.push({author: req.query.name, title: req.query.title, content: req.query.content, comments: []})
res.redirect('/')
return
}
@ -41,7 +41,7 @@ server.get('/', function get(req, res) {
for (let i = 0; i < posts.length && i < 2000; i++) {
mainPage.comments.push({author: posts[i].author, title: posts[i].title, content: posts[i].content, comments: []})
}
res.render('post.ejs', {post: mainPage, postid: '-1', webname: webname, comment: 1})
res.render('post.ejs', {post: mainPage, postid: '-1', webname: webname, comment: ''})
})
server.get('/post', function get(req, res) {
if(req.query.id) {
@ -52,25 +52,27 @@ server.get('/post', function get(req, res) {
}
})
server.get('/comment', function get(req, res) {
if(req.query.id && req.query.comment) {
if(req.query.id) {
let id = req.query.id
let comment = req.query.comment
let comment = req.query.comment
if(posts[id]) {
let cid = 0
let cid = ''
function recurse(post) {
cid++
console.log(String(cid) + comment)
console.log(String(cid) + ' ' + comment)
if(String(cid) === comment) {
if(req.query.name && req.query.title && req.query.content) {
post.comments.unshift({author: req.query.name, title: req.query.title, content: req.query.content, comments: []})
post.comments.push({author: req.query.name, title: req.query.title, content: req.query.content, comments: []})
res.redirect(`/post/${id}`)
cid = -1
}
return true
}
for(const comment of post.comments) {
if(recurse(comment))
for(let i = 0; i < post.comments.length; i++) {
const pid = cid
cid += i + '_'
if(recurse(post.comments[i]))
return true
cid = pid
}
return false
}
@ -80,8 +82,14 @@ server.get('/comment', function get(req, res) {
if(f) {
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, comment: cid})
}
else
res.send(`err2 ${req.search} ${cid}`)
else {
if(req.query.name && req.query.title && req.query.content) {
posts[id].comments.push({author: req.query.name, title: req.query.title, content: req.query.content, comments: []})
res.redirect(`/post/${id}`)
}
else
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, comment: ''})
}
}
}
else

View file

@ -1 +1 @@
[{"author":"Anonymous","title":"asd","content":"asd","comments":[]},{"title":"Default post","content":"This is the first post, it was here since the beginning.\n(Remove the space after the '[' and before the ']')\n [ * [* text *] * ] [ ** [** text **] ** ] [ *** [*** text ***] *** ] [ _ [_ text _] _ ] [ *_ [*_ text _*] _* ] [ **_ [**_ text _**] _** ] [ ***_ [***_ text _***] _*** [ **_ [**_ text _**] _** ] [ ***_ [***_ text _***] _*** ] [ \" [\" text \"] \" ]","author":"root","comments":[{"author":"Anonymous","title":"wef","content":"tegw","comments":[{"author":"Anonymous","title":"asd","content":"asd","comments":[]},{"author":"Anonymous","title":"wef","content":"wef","comments":[]}]},{"title":"The first comment","content":"This is the first comment, it was here since the beginning.","author":"Anonymous","comments":[]}]}]
[{"author":"Anonymous","title":"asd","content":"asd","comments":[{"author":"Anonymous","title":"sd","content":"asd","comments":[{"author":"Anonymous","title":"asdasd","content":"asdasd","comments":[]},{"author":"Anonymous","title":"sdsd","content":"sdsd","comments":[]}]}]},{"title":"Default post","content":"This is the first post, it was here since the beginning.\n(Remove the space after the '[' and before the ']')\n [ * [* text *] * ] [ ** [** text **] ** ] [ *** [*** text ***] *** ] [ _ [_ text _] _ ] [ *_ [*_ text _*] _* ] [ **_ [**_ text _**] _** ] [ ***_ [***_ text _***] _*** [ **_ [**_ text _**] _** ] [ ***_ [***_ text _***] _*** ] [ \" [\" text \"] \" ]","author":"root","comments":[{"author":"Anonymous","title":"wef","content":"tegw","comments":[{"author":"Anonymous","title":"asd","content":"asd","comments":[]},{"author":"Anonymous","title":"wef","content":"wef","comments":[]}]},{"title":"The first comment","content":"This is the first comment, it was here since the beginning.","author":"Anonymous","comments":[]}]},{"author":"Anonymous","title":"asd","content":"asd\r\n","comments":[]},{"author":"Anonymous","title":"test","content":"test","comments":[]},{"author":"Anonymous","title":"asdf","content":"asf","comments":[{"author":"Anonymous","title":"ok seehr nice","content":"test","comments":[{"author":"Anonymous","title":"YES","content":"YES","comments":[{"author":"Anonymous","title":"Yes","content":"[**_ YES _**]","comments":[]}]},{"author":"Anonymous","title":"retf","content":"rigu","comments":[{"author":"Anonymous","title":"qw","content":"qw","comments":[]}]},{"author":"Anonymous","title":"ereufe","content":"a","comments":[]}]},{"author":"Anonymous","title":"A","content":"a","comments":[]}]},{"author":"Anonymous","title":"rw","content":"asd","comments":[{"author":"Anonymous","title":"asd","content":"sasd\r\n","comments":[]},{"author":"Anonymous","title":"testos","content":"testus","comments":[]},{"author":"Anonymous","title":"morhgor","content":"lif","comments":[{"author":"Anonymous","title":"asd","content":"asd","comments":[]}]},{"author":"Anonymous","title":"asd","content":"asd","comments":[{"author":"Anonymous","title":"asd","content":"asd","comments":[]}]},{"author":"Anonymous","title":"moin","content":"uhgr\r\n","comments":[]}]}]

View file

@ -71,9 +71,8 @@
<%- (function(){
let ret = ''
let mainPost = post
let id = postid == -1 ? -2 : 0
let id = ''
function recurse(post) {
id++
const makeForm = id == comment || postid == -1
ret +=
@ -89,7 +88,7 @@
.replaceAll('[" ', '<pre class="inline">').replaceAll(' "]', '</pre>')
.replaceAll('["" ', '<pre>').replaceAll(' ""]', '</pre>')
.replaceAll('\n\n', '<br/>')
if (postid != -1 || id == -1) {
if (postid != -1 || id == '') {
if(makeForm) {
if(postid != -1)
ret += `<a href="/post/${postid}">[Nevermind]</a>`
@ -104,8 +103,11 @@
}
else
ret += `<a href="/post/${id}">[Open]</a></div>`
for(let i = 0; i < post.comments.length; i++) {
for(let i = post.comments.length - 1; i >= 0; i--) {
const pid = id
id += i + '_'
recurse(post.comments[i])
id = pid
}
ret += '</post>'
}