remove async

It was messing things up.
This commit is contained in:
Hugh Rundle 2023-01-11 19:05:13 +11:00
parent 22aea522bb
commit 1682d097af
2 changed files with 13 additions and 13 deletions

View file

@ -113,8 +113,7 @@ app.post('/publish', requireLoggedIn, (req, res) => {
}) })
app.post('/save', requireLoggedIn, (req, res) => { app.post('/save', requireLoggedIn, (req, res) => {
saveFile(req.session.user.username, req.body.textarea,) saveFile(req.session.user.username, req.body.textarea, () => {
.then( () => {
res.redirect('/') res.redirect('/')
}) })
}) })

View file

@ -2,6 +2,7 @@ const GEMINI_PATH = process.env.GEMINI_PATH
const Database = require("better-sqlite3") const Database = require("better-sqlite3")
const {mkdir, readFile, writeFile} = require('node:fs') const {mkdir, readFile, writeFile} = require('node:fs')
const { pbkdf2, randomBytes } = require('node:crypto') const { pbkdf2, randomBytes } = require('node:crypto')
const { resolve } = require("node:path")
db = new Database('soyuz.db', {}) db = new Database('soyuz.db', {})
function getNow() { function getNow() {
@ -75,13 +76,13 @@ const updateLatestPostDate = function(username, callback) {
} }
// AUTHORISATION MIDDLEWARE // AUTHORISATION MIDDLEWARE
const verifyUser = async function (req, res, next) { const verifyUser = function (req, res, next) {
let username = req.body.username let username = req.body.username
let password = req.body.password let password = req.body.password
let stmt = db.prepare( let stmt = db.prepare(
'SELECT * FROM users WHERE username = ?' 'SELECT * FROM users WHERE username = ?'
) )
user = await stmt.get(username) user = stmt.get(username)
if (!user) { if (!user) {
return next() return next()
@ -147,11 +148,11 @@ const publishNewPost = function(req, cb) {
}) })
}) })
// clear any saved post now that it is published // clear any saved post now that it is published
saveFile(req.session.user.username, '# Title of my note').then( () => { saveFile(req.session.user.username, '# Title of my note', () => {
return updateLatestPostDate(req.session.user.username, datestring => { return updateLatestPostDate(req.session.user.username, datestring => {
req.session.user.latest_post = datestring req.session.user.latest_post = datestring
return cb() return cb()
}) })
}) })
} }
@ -279,20 +280,20 @@ let updatePost = function(req, callback) {
}) })
} }
let saveFile = async function(user, text) { let saveFile = function(user, text, callback) {
let stmt = db.prepare( let stmt = db.prepare(
'UPDATE users SET saved_post = ? WHERE username = ?' 'UPDATE users SET saved_post = ? WHERE username = ?'
); );
saved = await stmt.run(text, user); saved = stmt.run(text, user);
return saved return callback();
} }
let getSavedFile = async function(user) { let getSavedFile = function(user) {
let stmt = db.prepare( let stmt = db.prepare(
'SELECT saved_post FROM users WHERE username = ?' 'SELECT saved_post FROM users WHERE username = ?'
) )
stmt.pluck(true) stmt.pluck(true)
let post = await stmt.get(user) let post = stmt.get(user)
return post return post
} }