Skip to content
Permalink
Browse files

Merge pull request #213 from davidmehren/refactor_backend_notes

First steps in refactoring the backend code
  • Loading branch information
SISheogorath committed Nov 20, 2019
2 parents f894d3c + b5cccef commit 689f5a0a9583fdd774a271a9e6265ee5356d72a0
6 app.js
@@ -22,7 +22,7 @@ var flash = require('connect-flash')
// core
var config = require('./lib/config')
var logger = require('./lib/logger')
var response = require('./lib/response')
var errors = require('./lib/errors')
var models = require('./lib/models')
var csp = require('./lib/csp')

@@ -212,11 +212,11 @@ app.use(require('./lib/web/auth'))
app.use(require('./lib/web/historyRouter'))
app.use(require('./lib/web/userRouter'))
app.use(require('./lib/web/imageRouter'))
app.use(require('./lib/web/noteRouter'))
app.use(require('./lib/web/note/router'))

// response not found if no any route matxches
app.get('*', function (req, res) {
response.errorNotFound(res)
errors.errorNotFound(res)
})

// socket.io secure
@@ -0,0 +1,38 @@
const config = require('./config')

module.exports = {
errorForbidden: function (res) {
const { req } = res
if (req.user) {
responseError(res, '403', 'Forbidden', 'oh no.')
} else {
req.flash('error', 'You are not allowed to access this page. Maybe try logging in?')
res.redirect(config.serverURL + '/')
}
},
errorNotFound: function (res) {
responseError(res, '404', 'Not Found', 'oops.')
},
errorBadRequest: function (res) {
responseError(res, '400', 'Bad Request', 'something not right.')
},
errorTooLong: function (res) {
responseError(res, '413', 'Payload Too Large', 'Shorten your note!')
},
errorInternalError: function (res) {
responseError(res, '500', 'Internal Error', 'wtf.')
},
errorServiceUnavailable: function (res) {
res.status(503).send('I\'m busy right now, try again later.')
}
}

function responseError (res, code, detail, msg) {
res.status(code).render('error.ejs', {
title: code + ' ' + detail + ' ' + msg,
code: code,
detail: detail,
msg: msg,
opengraph: []
})
}
@@ -5,8 +5,8 @@ var LZString = require('lz-string')

// core
var logger = require('./logger')
var response = require('./response')
var models = require('./models')
const errors = require('./errors')

// public
var History = {
@@ -121,55 +121,55 @@ function parseHistoryToObject (history) {
function historyGet (req, res) {
if (req.isAuthenticated()) {
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res)
if (!history) return response.errorNotFound(res)
if (err) return errors.errorInternalError(res)
if (!history) return errors.errorNotFound(res)
res.send({
history: parseHistoryToArray(history)
})
})
} else {
return response.errorForbidden(res)
return errors.errorForbidden(res)
}
}

function historyPost (req, res) {
if (req.isAuthenticated()) {
var noteId = req.params.noteId
if (!noteId) {
if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res)
if (typeof req.body['history'] === 'undefined') return errors.errorBadRequest(res)
logger.debug(`SERVER received history from [${req.user.id}]: ${req.body.history}`)
try {
var history = JSON.parse(req.body.history)
} catch (err) {
return response.errorBadRequest(res)
return errors.errorBadRequest(res)
}
if (Array.isArray(history)) {
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res)
if (err) return errors.errorInternalError(res)
res.end()
})
} else {
return response.errorBadRequest(res)
return errors.errorBadRequest(res)
}
} else {
if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res)
if (typeof req.body['pinned'] === 'undefined') return errors.errorBadRequest(res)
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res)
if (!history) return response.errorNotFound(res)
if (!history[noteId]) return response.errorNotFound(res)
if (err) return errors.errorInternalError(res)
if (!history) return errors.errorNotFound(res)
if (!history[noteId]) return errors.errorNotFound(res)
if (req.body.pinned === 'true' || req.body.pinned === 'false') {
history[noteId].pinned = (req.body.pinned === 'true')
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res)
if (err) return errors.errorInternalError(res)
res.end()
})
} else {
return response.errorBadRequest(res)
return errors.errorBadRequest(res)
}
})
}
} else {
return response.errorForbidden(res)
return errors.errorForbidden(res)
}
}

@@ -178,22 +178,22 @@ function historyDelete (req, res) {
var noteId = req.params.noteId
if (!noteId) {
setHistory(req.user.id, [], function (err, count) {
if (err) return response.errorInternalError(res)
if (err) return errors.errorInternalError(res)
res.end()
})
} else {
getHistory(req.user.id, function (err, history) {
if (err) return response.errorInternalError(res)
if (!history) return response.errorNotFound(res)
if (err) return errors.errorInternalError(res)
if (!history) return errors.errorNotFound(res)
delete history[noteId]
setHistory(req.user.id, history, function (err, count) {
if (err) return response.errorInternalError(res)
if (err) return errors.errorInternalError(res)
res.end()
})
})
}
} else {
return response.errorForbidden(res)
return errors.errorForbidden(res)
}
}

0 comments on commit 689f5a0

Please sign in to comment.
You can’t perform that action at this time.