Publishing fixes #4

Merged
hughrun merged 2 commits from publishing-fixes into main 2023-01-30 16:59:41 +11:00
3 changed files with 32 additions and 15 deletions

View file

@ -50,7 +50,7 @@ app.set('view engine', 'spy');
app.get('/', requireLoggedIn, (req, res) => { app.get('/', requireLoggedIn, (req, res) => {
let data = { let data = {
title: 'Home', title: 'Home',
disabled: '', disabled: '',
message: getSavedFile(req.session.user.username) message: getSavedFile(req.session.user.username)
} }
let today = getNow().toISOString().slice(0,10) let today = getNow().toISOString().slice(0,10)
@ -66,7 +66,7 @@ app.get('/login', (req, res) => {
if (req.session.user) { if (req.session.user) {
res.redirect('/') res.redirect('/')
} else { } else {
res.render('login.spy', {title: 'Log In'}) res.render('login.spy', {title: 'Log In'})
} }
}) })
@ -77,15 +77,19 @@ app.get('/edit', requireLoggedIn, (req, res) => {
}) })
app.get('/settings', requireLoggedIn, (req, res) => { app.get('/settings', requireLoggedIn, (req, res) => {
res.render('settings.spy', {title: 'Settings'}) res.render('settings.spy', {title: 'Settings'})
}) })
app.get('/try-again', requireLoggedIn, (req, res, next) => { app.get('/try-again', requireLoggedIn, (req, res, next) => {
res.render('try-again.spy', {title: 'Log In'}) res.render('try-again.spy', {title: 'Log In'})
}) })
app.get('/help', requireLoggedIn, (req, res, next) => { app.get('/help', requireLoggedIn, (req, res, next) => {
res.render('help.spy', {title: 'Help'}) res.render('help.spy', {title: 'Help'})
})
app.get('/published', requireLoggedIn, (req, res, next) => {
res.render('published.spy', {title: 'You published a note!'})
}) })
// POST // POST
@ -108,7 +112,7 @@ app.post('/logout', function(req, res, next){
app.post('/publish', requireLoggedIn, (req, res) => { app.post('/publish', requireLoggedIn, (req, res) => {
publishNewPost(req, () => { publishNewPost(req, () => {
res.redirect('/') res.redirect('/published')
}) })
}) })

12
templates/published.spy Normal file
View file

@ -0,0 +1,12 @@
<< partials/head >>
<body>
<section>
<< partials/header >>
<main>
<h2> {{ title }}</h2>
<p>Hooray 🎉</p>
</main>
</section>
<footer></footer>
</body>
</html>

View file

@ -86,9 +86,9 @@ const verifyUser = function (req, res, next) {
if (!user) { if (!user) {
return next() return next()
} }
pbkdf2(password, user.salt, 310000, 32, 'sha512', function(err, hashedPassword) { pbkdf2(password, user.salt, 310000, 32, 'sha512', function(err, hashedPassword) {
if (err) { if (err) {
return next() return next()
} }
if (user.password !== hashedPassword.toString('hex')) { if (user.password !== hashedPassword.toString('hex')) {
@ -168,16 +168,17 @@ const publishNewPost = function(req, cb) {
}) })
} }
} else { } else {
let newlines = []
let links = data.split('## Latest notes') let links = data.split('## Latest notes')
let lines = links[1].split('\n') let lines = links[1].split('\n')
for (let i = 6; i < 2; i--) { for (let line of lines) {
if (lines[i] && lines[i].startsWith('=>')) { if (line.startsWith('=>') && newlines.length < 4) {
lines[i] = lines[i-1] newlines.push(line)
} }
} }
lines[0] = '## Latest notes' lines[0] = '## Latest notes'
lines[2] = `=> /${year}/${dateString}.gmi ${dateString} (${title})` newlines.unshift(`## Latest notes\n\n=> /${year}/${dateString}.gmi ${dateString} (${title})`)
updated = links[0] + lines.join('\n') updated = newlines.join('\n')
writeFile(indexFile, updated, (err) => { writeFile(indexFile, updated, (err) => {
if (err) { if (err) {
// if the directory doesn't exist, create it and try again // if the directory doesn't exist, create it and try again
@ -213,7 +214,7 @@ const publishNewPost = function(req, cb) {
} }
let getLatestPost = function(directory, callback) { let getLatestPost = function(directory, callback) {
// we check the index file because // we check the index file because
// a new post could have come from // a new post could have come from
// somewhere other than the app // somewhere other than the app
// e.g. from a CLI on a laptop etc // e.g. from a CLI on a laptop etc
@ -298,7 +299,7 @@ let getSavedFile = function(user) {
// TODO: // TODO:
let savePictures = async function(text) { let savePictures = async function(text) {
// we will need to save pictures to the server // we will need to save pictures to the server
// separately when publishing // separately when publishing
} }