From 044b6b9422711ba5ee382671e1fce9a3e6a7685d Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Mon, 5 Aug 2019 10:21:20 +0800 Subject: [PATCH] refactor: fix lint Signed-off-by: BoHong Li --- lib/response.js | 127 +++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 60 deletions(-) diff --git a/lib/response.js b/lib/response.js index a2e92dc5..10b57c79 100644 --- a/lib/response.js +++ b/lib/response.js @@ -1,46 +1,28 @@ 'use strict' // response // external modules -var fs = require('fs') -var path = require('path') -var markdownpdf = require('markdown-pdf') -var shortId = require('shortid') -var querystring = require('querystring') -var request = require('request') -var moment = require('moment') +const fs = require('fs') +const path = require('path') +const markdownpdf = require('markdown-pdf') +const shortId = require('shortid') +const querystring = require('querystring') +const request = require('request') +const moment = require('moment') // core -var config = require('./config') -var logger = require('./logger') -var models = require('./models') -var utils = require('./utils') -var history = require('./history') +const config = require('./config') +const logger = require('./logger') +const models = require('./models') +const utils = require('./utils') +const history = require('./history') // public -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 + '/') - } -} -exports.errorNotFound = function (res) { - responseError(res, '404', 'Not Found', 'oops.') -} -exports.errorBadRequest = function (res) { - responseError(res, '400', 'Bad Request', 'something not right.') -} -exports.errorTooLong = function (res) { - responseError(res, '413', 'Payload Too Large', 'Shorten your note!') -} -exports.errorInternalError = function (res) { - responseError(res, '500', 'Internal Error', 'wtf.') -} -exports.errorServiceUnavailable = function (res) { - res.status(503).send("I'm busy right now, try again later.") -} +exports.errorForbidden = errorForbidden +exports.errorNotFound = errorNotFound +exports.errorBadRequest = errorBadRequest +exports.errorTooLong = errorTooLong +exports.errorInternalError = errorInternalError +exports.errorServiceUnavailable = errorServiceUnavailable exports.newNote = newNote exports.showNote = showNote exports.showPublishNote = showPublishNote @@ -52,6 +34,31 @@ exports.publishSlideActions = publishSlideActions exports.githubActions = githubActions exports.gitlabActions = gitlabActions +function errorForbidden (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 + '/') + } +} +function errorNotFound (res) { + responseError(res, '404', 'Not Found', 'oops.') +} +function errorBadRequest (res) { + responseError(res, '400', 'Bad Request', 'something not right.') +} +function errorTooLong (res) { + responseError(res, '413', 'Payload Too Large', 'Shorten your note!') +} +function errorInternalError (res) { + responseError(res, '500', 'Internal Error', 'wtf.') +} +function errorServiceUnavailable (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, @@ -115,7 +122,7 @@ function newNote (req, res, next) { var owner = null var body = '' if (req.body && req.body.length > config.documentMaxLength) { - return response.errorTooLong(res) + return errorTooLong(res) } else if (req.body) { body = req.body } @@ -123,7 +130,7 @@ function newNote (req, res, next) { if (req.isAuthenticated()) { owner = req.user.id } else if (!config.allowAnonymous) { - return response.errorForbidden(res) + return errorForbidden(res) } models.Note.create({ ownerId: owner, @@ -137,7 +144,7 @@ function newNote (req, res, next) { return res.redirect(config.serverURL + '/' + models.Note.encodeNoteId(note.id)) }).catch(function (err) { logger.error(err) - return response.errorInternalError(res) + return errorInternalError(res) }) } @@ -157,7 +164,7 @@ function findNote (req, res, callback, include) { models.Note.parseNoteId(id, function (err, _id) { if (err) { logger.error(err) - return response.errorInternalError(res) + return errorInternalError(res) } models.Note.findOne({ where: { @@ -170,17 +177,17 @@ function findNote (req, res, callback, include) { req.alias = noteId return newNote(req, res) } else { - return response.errorNotFound(res) + return errorNotFound(res) } } if (!checkViewPermission(req, note)) { - return response.errorForbidden(res) + return errorForbidden(res) } else { return callback(note) } }).catch(function (err) { logger.error(err) - return response.errorInternalError(res) + return errorInternalError(res) }) }) } @@ -211,7 +218,7 @@ function showPublishNote (req, res, next) { } note.increment('viewcount').then(function (note) { if (!note) { - return response.errorNotFound(res) + return errorNotFound(res) } var body = note.content var extracted = models.Note.extractMeta(body) @@ -240,7 +247,7 @@ function showPublishNote (req, res, next) { return renderPublish(data, res) }).catch(function (err) { logger.error(err) - return response.errorInternalError(res) + return errorInternalError(res) }) }, include) } @@ -317,7 +324,7 @@ function actionPDF (req, res, note) { markdownpdf().from.string(content).to(path, function () { if (!fs.existsSync(path)) { logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + path) - return response.errorInternalError(res) + return errorInternalError(res) } var stream = fs.createReadStream(path) var filename = title @@ -352,10 +359,10 @@ function actionRevision (req, res, note) { models.Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) { if (err) { logger.error(err) - return response.errorInternalError(res) + return errorInternalError(res) } if (!content) { - return response.errorNotFound(res) + return errorNotFound(res) } res.set({ 'Access-Control-Allow-Origin': '*', // allow CORS as API @@ -367,13 +374,13 @@ function actionRevision (req, res, note) { res.send(content) }) } else { - return response.errorNotFound(res) + return errorNotFound(res) } } else { models.Revision.getNoteRevisions(note, function (err, data) { if (err) { logger.error(err) - return response.errorInternalError(res) + return errorInternalError(res) } var out = { revision: data @@ -413,7 +420,7 @@ function noteActions (req, res, next) { actionPDF(req, res, note) } else { logger.error('PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details') - response.errorForbidden(res) + errorForbidden(res) } break case 'gist': @@ -478,7 +485,7 @@ function githubActionGist (req, res, note) { var code = req.query.code var state = req.query.state if (!code || !state) { - return response.errorForbidden(res) + return errorForbidden(res) } else { var data = { client_id: config.github.clientID, @@ -518,14 +525,14 @@ function githubActionGist (req, res, note) { res.setHeader('referer', '') res.redirect(body.html_url) } else { - return response.errorForbidden(res) + return errorForbidden(res) } }) } else { - return response.errorForbidden(res) + return errorForbidden(res) } } else { - return response.errorForbidden(res) + return errorForbidden(res) } }) } @@ -553,7 +560,7 @@ function gitlabActionProjects (req, res, note) { id: req.user.id } }).then(function (user) { - if (!user) { return response.errorNotFound(res) } + if (!user) { return errorNotFound(res) } var ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version } ret.accesstoken = user.accessToken ret.profileid = user.profileid @@ -570,10 +577,10 @@ function gitlabActionProjects (req, res, note) { ) }).catch(function (err) { logger.error('gitlab action projects failed: ' + err) - return response.errorInternalError(res) + return errorInternalError(res) }) } else { - return response.errorForbidden(res) + return errorForbidden(res) } } @@ -591,7 +598,7 @@ function showPublishSlide (req, res, next) { if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid)) } note.increment('viewcount').then(function (note) { if (!note) { - return response.errorNotFound(res) + return errorNotFound(res) } var body = note.content var extracted = models.Note.extractMeta(body) @@ -622,7 +629,7 @@ function showPublishSlide (req, res, next) { return renderPublishSlide(data, res) }).catch(function (err) { logger.error(err) - return response.errorInternalError(res) + return errorInternalError(res) }) }, include) }