Skip to content

Commit b260093

Browse files
authored
Merge pull request #1416 from hackmdio/fix/user-type-error
Fix some issues after code refactoring
2 parents 05dd6eb + da3fd00 commit b260093

File tree

11 files changed

+74
-75
lines changed

11 files changed

+74
-75
lines changed

app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ app.use(require('./lib/routes').router)
222222

223223
// response not found if no any route matxches
224224
app.get('*', function (req, res) {
225-
response.errorNotFound(res)
225+
response.errorNotFound(req, res)
226226
})
227227

228228
// socket.io secure

lib/auth/email/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ passport.use(new LocalStrategy({
3333

3434
if (config.allowEmailRegister) {
3535
emailAuth.post('/register', urlencodedParser, function (req, res, next) {
36-
if (!req.body.email || !req.body.password) return response.errorBadRequest(res)
37-
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res)
36+
if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res)
37+
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res)
3838
models.User.findOrCreate({
3939
where: {
4040
email: req.body.email
@@ -57,14 +57,14 @@ if (config.allowEmailRegister) {
5757
return res.redirect(config.serverURL + '/')
5858
}).catch(function (err) {
5959
logger.error('auth callback failed: ' + err)
60-
return response.errorInternalError(res)
60+
return response.errorInternalError(req, res)
6161
})
6262
})
6363
}
6464

6565
emailAuth.post('/login', urlencodedParser, function (req, res, next) {
66-
if (!req.body.email || !req.body.password) return response.errorBadRequest(res)
67-
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res)
66+
if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res)
67+
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res)
6868
setReturnToFromReferer(req)
6969
passport.authenticate('local', {
7070
successReturnToOrRedirect: config.serverURL + '/',

lib/auth/ldap/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ passport.use(new LDAPStrategy({
8181
}))
8282

8383
ldapAuth.post('/auth/ldap', urlencodedParser, function (req, res, next) {
84-
if (!req.body.username || !req.body.password) return response.errorBadRequest(res)
84+
if (!req.body.username || !req.body.password) return response.errorBadRequest(req, res)
8585
setReturnToFromReferer(req)
8686
passport.authenticate('ldapauth', {
8787
successReturnToOrRedirect: config.serverURL + '/',

lib/history/index.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,55 +116,55 @@ function parseHistoryToObject (history) {
116116
function historyGet (req, res) {
117117
if (req.isAuthenticated()) {
118118
getHistory(req.user.id, function (err, history) {
119-
if (err) return response.errorInternalError(res)
120-
if (!history) return response.errorNotFound(res)
119+
if (err) return response.errorInternalError(req, res)
120+
if (!history) return response.errorNotFound(req, res)
121121
res.send({
122122
history: parseHistoryToArray(history)
123123
})
124124
})
125125
} else {
126-
return response.errorForbidden(res)
126+
return response.errorForbidden(req, res)
127127
}
128128
}
129129

130130
function historyPost (req, res) {
131131
if (req.isAuthenticated()) {
132132
var noteId = req.params.noteId
133133
if (!noteId) {
134-
if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res)
134+
if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(req, res)
135135
if (config.debug) { logger.info('SERVER received history from [' + req.user.id + ']: ' + req.body.history) }
136136
try {
137137
var history = JSON.parse(req.body.history)
138138
} catch (err) {
139-
return response.errorBadRequest(res)
139+
return response.errorBadRequest(req, res)
140140
}
141141
if (Array.isArray(history)) {
142142
setHistory(req.user.id, history, function (err, count) {
143-
if (err) return response.errorInternalError(res)
143+
if (err) return response.errorInternalError(req, res)
144144
res.end()
145145
})
146146
} else {
147-
return response.errorBadRequest(res)
147+
return response.errorBadRequest(req, res)
148148
}
149149
} else {
150-
if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res)
150+
if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(req, res)
151151
getHistory(req.user.id, function (err, history) {
152-
if (err) return response.errorInternalError(res)
153-
if (!history) return response.errorNotFound(res)
154-
if (!history[noteId]) return response.errorNotFound(res)
152+
if (err) return response.errorInternalError(req, res)
153+
if (!history) return response.errorNotFound(req, res)
154+
if (!history[noteId]) return response.errorNotFound(req, res)
155155
if (req.body.pinned === 'true' || req.body.pinned === 'false') {
156156
history[noteId].pinned = (req.body.pinned === 'true')
157157
setHistory(req.user.id, history, function (err, count) {
158-
if (err) return response.errorInternalError(res)
158+
if (err) return response.errorInternalError(req, res)
159159
res.end()
160160
})
161161
} else {
162-
return response.errorBadRequest(res)
162+
return response.errorBadRequest(req, res)
163163
}
164164
})
165165
}
166166
} else {
167-
return response.errorForbidden(res)
167+
return response.errorForbidden(req, res)
168168
}
169169
}
170170

@@ -173,22 +173,22 @@ function historyDelete (req, res) {
173173
var noteId = req.params.noteId
174174
if (!noteId) {
175175
setHistory(req.user.id, [], function (err, count) {
176-
if (err) return response.errorInternalError(res)
176+
if (err) return response.errorInternalError(req, res)
177177
res.end()
178178
})
179179
} else {
180180
getHistory(req.user.id, function (err, history) {
181-
if (err) return response.errorInternalError(res)
182-
if (!history) return response.errorNotFound(res)
181+
if (err) return response.errorInternalError(req, res)
182+
if (!history) return response.errorNotFound(req, res)
183183
delete history[noteId]
184184
setHistory(req.user.id, history, function (err, count) {
185-
if (err) return response.errorInternalError(res)
185+
if (err) return response.errorInternalError(req, res)
186186
res.end()
187187
})
188188
})
189189
}
190190
} else {
191-
return response.errorForbidden(res)
191+
return response.errorForbidden(req, res)
192192
}
193193
}
194194

lib/imageRouter/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ imageRouter.post('/uploadimage', function (req, res) {
2121

2222
form.parse(req, function (err, fields, files) {
2323
if (err || !files.image || !files.image.path) {
24-
response.errorForbidden(res)
24+
response.errorForbidden(req, res)
2525
} else {
2626
if (config.debug) {
2727
logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image))

lib/middleware/checkURIValid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = function (req, res, next) {
88
decodeURIComponent(req.path)
99
} catch (err) {
1010
logger.error(err)
11-
return response.errorBadRequest(res)
11+
return response.errorBadRequest(req, res)
1212
}
1313
next()
1414
}

lib/middleware/tooBusy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ toobusy.maxLag(config.responseMaxLag)
99

1010
module.exports = function (req, res, next) {
1111
if (toobusy()) {
12-
response.errorServiceUnavailable(res)
12+
response.errorServiceUnavailable(req, res)
1313
} else {
1414
next()
1515
}

lib/note/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ async function showNote (req, res) {
6060
if (!note) {
6161
// if allow free url enable, auto create note
6262
if (!config.allowFreeURL || config.forbiddenNoteIDs.includes(noteId)) {
63-
return errorNotFound(res)
63+
return errorNotFound(req, res)
6464
}
6565
note = await createNote(userId, noteId)
6666
}
6767

6868
if (!newCheckViewPermission(note, req.isAuthenticated(), userId)) {
69-
return errorForbidden(res)
69+
return errorForbidden(req, res)
7070
}
7171

7272
// force to use note id
@@ -94,12 +94,12 @@ async function showPublishNote (req, res) {
9494
includeUser: true
9595
})
9696

97-
if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) {
98-
return errorForbidden(req)
97+
if (!note) {
98+
return errorNotFound(req, res)
9999
}
100100

101-
if (!note) {
102-
return errorNotFound(res)
101+
if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) {
102+
return errorForbidden(req, res)
103103
}
104104

105105
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
@@ -146,11 +146,11 @@ async function noteActions (req, res) {
146146
const note = await getNoteById(noteId)
147147

148148
if (!note) {
149-
return errorNotFound(res)
149+
return errorNotFound(req, res)
150150
}
151151

152152
if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) {
153-
return errorForbidden(req)
153+
return errorForbidden(req, res)
154154
}
155155

156156
const action = req.params.action
@@ -171,7 +171,7 @@ async function noteActions (req, res) {
171171
actionPDF(req, res, note)
172172
} else {
173173
logger.error('PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details')
174-
errorForbidden(res)
174+
errorForbidden(req, res)
175175
}
176176
break
177177
case 'gist':

lib/note/noteActions.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function actionPDF (req, res, note) {
8484
markdownpdf(markdownpdfOptions).from.string(content).to(pdfPath, function () {
8585
if (!fs.existsSync(pdfPath)) {
8686
logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + pdfPath)
87-
return errorInternalError(res)
87+
return errorInternalError(req, res)
8888
}
8989
const stream = fs.createReadStream(pdfPath)
9090
let filename = title
@@ -173,15 +173,15 @@ function actionRevision (req, res, note) {
173173
if (actionId) {
174174
const time = moment(parseInt(actionId))
175175
if (!time.isValid()) {
176-
return errorNotFound(res)
176+
return errorNotFound(req, res)
177177
}
178178
Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
179179
if (err) {
180180
logger.error(err)
181-
return errorInternalError(res)
181+
return errorInternalError(req, res)
182182
}
183183
if (!content) {
184-
return errorNotFound(res)
184+
return errorNotFound(req, res)
185185
}
186186
res.set({
187187
'Access-Control-Allow-Origin': '*', // allow CORS as API
@@ -196,7 +196,7 @@ function actionRevision (req, res, note) {
196196
Revision.getNoteRevisions(note, function (err, data) {
197197
if (err) {
198198
logger.error(err)
199-
return errorInternalError(res)
199+
return errorInternalError(req, res)
200200
}
201201
const result = {
202202
revision: data

0 commit comments

Comments
 (0)