mirror of https://github.com/status-im/codimd.git
fix: history api failed cause by circular dependency (#1252)
fix: history api failed cause by circular dependency
This commit is contained in:
commit
3038f5cfd1
|
@ -9,14 +9,6 @@ var logger = require('./logger')
|
|||
var response = require('./response')
|
||||
var models = require('./models')
|
||||
|
||||
// public
|
||||
var History = {
|
||||
historyGet: historyGet,
|
||||
historyPost: historyPost,
|
||||
historyDelete: historyDelete,
|
||||
updateHistory: updateHistory
|
||||
}
|
||||
|
||||
function getHistory (userid, callback) {
|
||||
models.User.findOne({
|
||||
where: {
|
||||
|
@ -200,4 +192,8 @@ function historyDelete (req, res) {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports = History
|
||||
// public
|
||||
exports.historyGet = historyGet
|
||||
exports.historyPost = historyPost
|
||||
exports.historyDelete = historyDelete
|
||||
exports.updateHistory = updateHistory
|
||||
|
|
149
lib/response.js
149
lib/response.js
|
@ -1,57 +1,62 @@
|
|||
'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
|
||||
var response = {
|
||||
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.")
|
||||
},
|
||||
newNote: newNote,
|
||||
showNote: showNote,
|
||||
showPublishNote: showPublishNote,
|
||||
showPublishSlide: showPublishSlide,
|
||||
showIndex: showIndex,
|
||||
noteActions: noteActions,
|
||||
publishNoteActions: publishNoteActions,
|
||||
publishSlideActions: publishSlideActions,
|
||||
githubActions: githubActions,
|
||||
gitlabActions: gitlabActions
|
||||
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
|
||||
exports.showPublishSlide = showPublishSlide
|
||||
exports.showIndex = showIndex
|
||||
exports.noteActions = noteActions
|
||||
exports.publishNoteActions = publishNoteActions
|
||||
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) {
|
||||
|
@ -117,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
|
||||
}
|
||||
|
@ -125,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,
|
||||
|
@ -139,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)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -159,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: {
|
||||
|
@ -172,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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -213,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)
|
||||
|
@ -242,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)
|
||||
}
|
||||
|
@ -319,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
|
||||
|
@ -354,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
|
||||
|
@ -369,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
|
||||
|
@ -415,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':
|
||||
|
@ -480,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,
|
||||
|
@ -520,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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -555,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
|
||||
|
@ -572,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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -593,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)
|
||||
|
@ -624,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)
|
||||
}
|
||||
|
@ -635,5 +640,3 @@ function renderPublishSlide (data, res) {
|
|||
})
|
||||
res.render('slide.ejs', data)
|
||||
}
|
||||
|
||||
module.exports = response
|
||||
|
|
Loading…
Reference in New Issue