Skip to content

Commit 3038f5c

Browse files
authored
fix: history api failed cause by circular dependency (#1252)
fix: history api failed cause by circular dependency
2 parents 18abc20 + 044b6b9 commit 3038f5c

File tree

2 files changed

+81
-82
lines changed

2 files changed

+81
-82
lines changed

lib/history.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ var logger = require('./logger')
99
var response = require('./response')
1010
var models = require('./models')
1111

12-
// public
13-
var History = {
14-
historyGet: historyGet,
15-
historyPost: historyPost,
16-
historyDelete: historyDelete,
17-
updateHistory: updateHistory
18-
}
19-
2012
function getHistory (userid, callback) {
2113
models.User.findOne({
2214
where: {
@@ -200,4 +192,8 @@ function historyDelete (req, res) {
200192
}
201193
}
202194

203-
module.exports = History
195+
// public
196+
exports.historyGet = historyGet
197+
exports.historyPost = historyPost
198+
exports.historyDelete = historyDelete
199+
exports.updateHistory = updateHistory

lib/response.js

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,62 @@
11
'use strict'
22
// response
33
// external modules
4-
var fs = require('fs')
5-
var path = require('path')
6-
var markdownpdf = require('markdown-pdf')
7-
var shortId = require('shortid')
8-
var querystring = require('querystring')
9-
var request = require('request')
10-
var moment = require('moment')
4+
const fs = require('fs')
5+
const path = require('path')
6+
const markdownpdf = require('markdown-pdf')
7+
const shortId = require('shortid')
8+
const querystring = require('querystring')
9+
const request = require('request')
10+
const moment = require('moment')
1111

1212
// core
13-
var config = require('./config')
14-
var logger = require('./logger')
15-
var models = require('./models')
16-
var utils = require('./utils')
17-
var history = require('./history')
13+
const config = require('./config')
14+
const logger = require('./logger')
15+
const models = require('./models')
16+
const utils = require('./utils')
17+
const history = require('./history')
1818

1919
// public
20-
var response = {
21-
errorForbidden: function (res) {
22-
const { req } = res
23-
if (req.user) {
24-
responseError(res, '403', 'Forbidden', 'oh no.')
25-
} else {
26-
req.flash('error', 'You are not allowed to access this page. Maybe try logging in?')
27-
res.redirect(config.serverURL + '/')
28-
}
29-
},
30-
errorNotFound: function (res) {
31-
responseError(res, '404', 'Not Found', 'oops.')
32-
},
33-
errorBadRequest: function (res) {
34-
responseError(res, '400', 'Bad Request', 'something not right.')
35-
},
36-
errorTooLong: function (res) {
37-
responseError(res, '413', 'Payload Too Large', 'Shorten your note!')
38-
},
39-
errorInternalError: function (res) {
40-
responseError(res, '500', 'Internal Error', 'wtf.')
41-
},
42-
errorServiceUnavailable: function (res) {
43-
res.status(503).send("I'm busy right now, try again later.")
44-
},
45-
newNote: newNote,
46-
showNote: showNote,
47-
showPublishNote: showPublishNote,
48-
showPublishSlide: showPublishSlide,
49-
showIndex: showIndex,
50-
noteActions: noteActions,
51-
publishNoteActions: publishNoteActions,
52-
publishSlideActions: publishSlideActions,
53-
githubActions: githubActions,
54-
gitlabActions: gitlabActions
20+
exports.errorForbidden = errorForbidden
21+
exports.errorNotFound = errorNotFound
22+
exports.errorBadRequest = errorBadRequest
23+
exports.errorTooLong = errorTooLong
24+
exports.errorInternalError = errorInternalError
25+
exports.errorServiceUnavailable = errorServiceUnavailable
26+
exports.newNote = newNote
27+
exports.showNote = showNote
28+
exports.showPublishNote = showPublishNote
29+
exports.showPublishSlide = showPublishSlide
30+
exports.showIndex = showIndex
31+
exports.noteActions = noteActions
32+
exports.publishNoteActions = publishNoteActions
33+
exports.publishSlideActions = publishSlideActions
34+
exports.githubActions = githubActions
35+
exports.gitlabActions = gitlabActions
36+
37+
function errorForbidden (res) {
38+
const { req } = res
39+
if (req.user) {
40+
responseError(res, '403', 'Forbidden', 'oh no.')
41+
} else {
42+
req.flash('error', 'You are not allowed to access this page. Maybe try logging in?')
43+
res.redirect(config.serverURL + '/')
44+
}
45+
}
46+
function errorNotFound (res) {
47+
responseError(res, '404', 'Not Found', 'oops.')
48+
}
49+
function errorBadRequest (res) {
50+
responseError(res, '400', 'Bad Request', 'something not right.')
51+
}
52+
function errorTooLong (res) {
53+
responseError(res, '413', 'Payload Too Large', 'Shorten your note!')
54+
}
55+
function errorInternalError (res) {
56+
responseError(res, '500', 'Internal Error', 'wtf.')
57+
}
58+
function errorServiceUnavailable (res) {
59+
res.status(503).send("I'm busy right now, try again later.")
5560
}
5661

5762
function responseError (res, code, detail, msg) {
@@ -117,15 +122,15 @@ function newNote (req, res, next) {
117122
var owner = null
118123
var body = ''
119124
if (req.body && req.body.length > config.documentMaxLength) {
120-
return response.errorTooLong(res)
125+
return errorTooLong(res)
121126
} else if (req.body) {
122127
body = req.body
123128
}
124129
body = body.replace(/[\r]/g, '')
125130
if (req.isAuthenticated()) {
126131
owner = req.user.id
127132
} else if (!config.allowAnonymous) {
128-
return response.errorForbidden(res)
133+
return errorForbidden(res)
129134
}
130135
models.Note.create({
131136
ownerId: owner,
@@ -139,7 +144,7 @@ function newNote (req, res, next) {
139144
return res.redirect(config.serverURL + '/' + models.Note.encodeNoteId(note.id))
140145
}).catch(function (err) {
141146
logger.error(err)
142-
return response.errorInternalError(res)
147+
return errorInternalError(res)
143148
})
144149
}
145150

@@ -159,7 +164,7 @@ function findNote (req, res, callback, include) {
159164
models.Note.parseNoteId(id, function (err, _id) {
160165
if (err) {
161166
logger.error(err)
162-
return response.errorInternalError(res)
167+
return errorInternalError(res)
163168
}
164169
models.Note.findOne({
165170
where: {
@@ -172,17 +177,17 @@ function findNote (req, res, callback, include) {
172177
req.alias = noteId
173178
return newNote(req, res)
174179
} else {
175-
return response.errorNotFound(res)
180+
return errorNotFound(res)
176181
}
177182
}
178183
if (!checkViewPermission(req, note)) {
179-
return response.errorForbidden(res)
184+
return errorForbidden(res)
180185
} else {
181186
return callback(note)
182187
}
183188
}).catch(function (err) {
184189
logger.error(err)
185-
return response.errorInternalError(res)
190+
return errorInternalError(res)
186191
})
187192
})
188193
}
@@ -213,7 +218,7 @@ function showPublishNote (req, res, next) {
213218
}
214219
note.increment('viewcount').then(function (note) {
215220
if (!note) {
216-
return response.errorNotFound(res)
221+
return errorNotFound(res)
217222
}
218223
var body = note.content
219224
var extracted = models.Note.extractMeta(body)
@@ -242,7 +247,7 @@ function showPublishNote (req, res, next) {
242247
return renderPublish(data, res)
243248
}).catch(function (err) {
244249
logger.error(err)
245-
return response.errorInternalError(res)
250+
return errorInternalError(res)
246251
})
247252
}, include)
248253
}
@@ -319,7 +324,7 @@ function actionPDF (req, res, note) {
319324
markdownpdf().from.string(content).to(path, function () {
320325
if (!fs.existsSync(path)) {
321326
logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + path)
322-
return response.errorInternalError(res)
327+
return errorInternalError(res)
323328
}
324329
var stream = fs.createReadStream(path)
325330
var filename = title
@@ -354,10 +359,10 @@ function actionRevision (req, res, note) {
354359
models.Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
355360
if (err) {
356361
logger.error(err)
357-
return response.errorInternalError(res)
362+
return errorInternalError(res)
358363
}
359364
if (!content) {
360-
return response.errorNotFound(res)
365+
return errorNotFound(res)
361366
}
362367
res.set({
363368
'Access-Control-Allow-Origin': '*', // allow CORS as API
@@ -369,13 +374,13 @@ function actionRevision (req, res, note) {
369374
res.send(content)
370375
})
371376
} else {
372-
return response.errorNotFound(res)
377+
return errorNotFound(res)
373378
}
374379
} else {
375380
models.Revision.getNoteRevisions(note, function (err, data) {
376381
if (err) {
377382
logger.error(err)
378-
return response.errorInternalError(res)
383+
return errorInternalError(res)
379384
}
380385
var out = {
381386
revision: data
@@ -415,7 +420,7 @@ function noteActions (req, res, next) {
415420
actionPDF(req, res, note)
416421
} else {
417422
logger.error('PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details')
418-
response.errorForbidden(res)
423+
errorForbidden(res)
419424
}
420425
break
421426
case 'gist':
@@ -480,7 +485,7 @@ function githubActionGist (req, res, note) {
480485
var code = req.query.code
481486
var state = req.query.state
482487
if (!code || !state) {
483-
return response.errorForbidden(res)
488+
return errorForbidden(res)
484489
} else {
485490
var data = {
486491
client_id: config.github.clientID,
@@ -520,14 +525,14 @@ function githubActionGist (req, res, note) {
520525
res.setHeader('referer', '')
521526
res.redirect(body.html_url)
522527
} else {
523-
return response.errorForbidden(res)
528+
return errorForbidden(res)
524529
}
525530
})
526531
} else {
527-
return response.errorForbidden(res)
532+
return errorForbidden(res)
528533
}
529534
} else {
530-
return response.errorForbidden(res)
535+
return errorForbidden(res)
531536
}
532537
})
533538
}
@@ -555,7 +560,7 @@ function gitlabActionProjects (req, res, note) {
555560
id: req.user.id
556561
}
557562
}).then(function (user) {
558-
if (!user) { return response.errorNotFound(res) }
563+
if (!user) { return errorNotFound(res) }
559564
var ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version }
560565
ret.accesstoken = user.accessToken
561566
ret.profileid = user.profileid
@@ -572,10 +577,10 @@ function gitlabActionProjects (req, res, note) {
572577
)
573578
}).catch(function (err) {
574579
logger.error('gitlab action projects failed: ' + err)
575-
return response.errorInternalError(res)
580+
return errorInternalError(res)
576581
})
577582
} else {
578-
return response.errorForbidden(res)
583+
return errorForbidden(res)
579584
}
580585
}
581586

@@ -593,7 +598,7 @@ function showPublishSlide (req, res, next) {
593598
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid)) }
594599
note.increment('viewcount').then(function (note) {
595600
if (!note) {
596-
return response.errorNotFound(res)
601+
return errorNotFound(res)
597602
}
598603
var body = note.content
599604
var extracted = models.Note.extractMeta(body)
@@ -624,7 +629,7 @@ function showPublishSlide (req, res, next) {
624629
return renderPublishSlide(data, res)
625630
}).catch(function (err) {
626631
logger.error(err)
627-
return response.errorInternalError(res)
632+
return errorInternalError(res)
628633
})
629634
}, include)
630635
}
@@ -635,5 +640,3 @@ function renderPublishSlide (data, res) {
635640
})
636641
res.render('slide.ejs', data)
637642
}
638-
639-
module.exports = response

0 commit comments

Comments
 (0)