Skip to content

Commit 66edff8

Browse files
committed
refactor: show note
Signed-off-by: BoHong Li <raccoon@hackmd.io>
1 parent b4ec353 commit 66edff8

4 files changed

Lines changed: 93 additions & 13 deletions

File tree

lib/models/note.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ module.exports = function (sequelize, DataTypes) {
186186
var result = id.match(uuidRegex)
187187
if (result && result.length === 1) { return true } else { return false }
188188
}
189+
Note.parseNoteIdAsync = function (noteId) {
190+
return new Promise((resolve, reject) => {
191+
Note.parseNoteId(noteId, (err, id) => {
192+
if (err) {
193+
return reject(err)
194+
}
195+
resolve(id)
196+
})
197+
})
198+
}
189199
Note.parseNoteId = function (noteId, callback) {
190200
async.series({
191201
parseNoteIdByAlias: function (_callback) {

lib/note/index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
const config = require('../config')
4+
const { Note } = require('../models')
5+
6+
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound } = require('../response')
7+
const { updateHistory } = require('../history')
8+
9+
async function getNoteById (noteId) {
10+
const id = await Note.parseNoteIdAsync(noteId)
11+
const note = await Note.findOne({
12+
where: {
13+
id: id
14+
}
15+
})
16+
return note
17+
}
18+
19+
async function createNote (userId, noteAlias) {
20+
if (!config.allowAnonymous && !!userId) {
21+
throw new Error('can not create note')
22+
}
23+
24+
const note = await Note.create({
25+
ownerId: userId,
26+
alias: noteAlias,
27+
})
28+
29+
if (userId) {
30+
updateHistory(userId, note)
31+
}
32+
33+
return note
34+
}
35+
36+
// controller
37+
async function showNote (req, res) {
38+
const noteId = req.params.noteId
39+
const userId = req.user ? req.user.id : null
40+
41+
let note = await getNoteById(noteId)
42+
43+
if (!note) {
44+
// if allow free url enable, auto create note
45+
if (!config.allowFreeURL || config.forbiddenNoteIDs.includes(noteId)) {
46+
return errorNotFound(res)
47+
}
48+
note = await createNote(userId, noteId)
49+
}
50+
51+
if (!newCheckViewPermission(note, req.isAuthenticated(), userId)) {
52+
return errorForbidden(res)
53+
}
54+
55+
// force to use note id
56+
const id = Note.encodeNoteId(note.id)
57+
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) {
58+
return res.redirect(config.serverURL + '/' + (note.alias || id))
59+
}
60+
return responseCodiMD(res, note)
61+
}
62+
63+
exports.showNote = showNote

lib/response.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ exports.errorTooLong = errorTooLong
2525
exports.errorInternalError = errorInternalError
2626
exports.errorServiceUnavailable = errorServiceUnavailable
2727
exports.newNote = newNote
28-
exports.showNote = showNote
2928
exports.showPublishNote = showPublishNote
3029
exports.showPublishSlide = showPublishSlide
3130
exports.showIndex = showIndex
@@ -35,6 +34,8 @@ exports.publishSlideActions = publishSlideActions
3534
exports.githubActions = githubActions
3635
exports.gitlabActions = gitlabActions
3736
exports.checkViewPermission = checkViewPermission
37+
exports.newCheckViewPermission = newCheckViewPermission
38+
exports.responseCodiMD = responseCodiMD
3839

3940
function errorForbidden (res) {
4041
const { req } = res
@@ -45,20 +46,25 @@ function errorForbidden (res) {
4546
res.redirect(config.serverURL + '/')
4647
}
4748
}
49+
4850
function errorNotFound (res) {
4951
responseError(res, '404', 'Not Found', 'oops.')
5052
}
53+
5154
function errorBadRequest (res) {
5255
responseError(res, '400', 'Bad Request', 'something not right.')
5356
}
57+
5458
function errorTooLong (res) {
5559
responseError(res, '413', 'Payload Too Large', 'Shorten your note!')
5660
}
61+
5762
function errorInternalError (res) {
5863
responseError(res, '500', 'Internal Error', 'wtf.')
5964
}
65+
6066
function errorServiceUnavailable (res) {
61-
res.status(503).send("I'm busy right now, try again later.")
67+
res.status(503).send('I\'m busy right now, try again later.')
6268
}
6369

6470
function responseError (res, code, detail, msg) {
@@ -150,6 +156,16 @@ function newNote (req, res, next) {
150156
})
151157
}
152158

159+
function newCheckViewPermission (note, isLogin, userId) {
160+
if (note.permission === 'private') {
161+
return note.ownerId === userId
162+
}
163+
if (note.permission === 'limited' || note.permission === 'protected') {
164+
return isLogin
165+
}
166+
return true
167+
}
168+
153169
function checkViewPermission (req, note) {
154170
if (note.permission === 'private') {
155171
if (!req.isAuthenticated() || note.ownerId !== req.user.id) { return false } else { return true }
@@ -194,16 +210,6 @@ function findNote (req, res, callback, include) {
194210
})
195211
}
196212

197-
function showNote (req, res, next) {
198-
findNote(req, res, function (note) {
199-
// force to use note id
200-
var noteId = req.params.noteId
201-
var id = models.Note.encodeNoteId(note.id)
202-
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) { return res.redirect(config.serverURL + '/' + (note.alias || id)) }
203-
return responseCodiMD(res, note)
204-
})
205-
}
206-
207213
function showPublishNote (req, res, next) {
208214
var include = [{
209215
model: models.User,

lib/routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const errorPageController = require('./errorPage')
1010
const statusController = require('./status')
1111
const historyController = require('./history')
1212
const userController = require('./user')
13+
const noteController = require('./note')
1314
const response = require('./response')
1415
const appRouter = Router()
1516

@@ -71,7 +72,7 @@ appRouter.get('/p/:shortid', response.showPublishSlide)
7172
// publish slide actions
7273
appRouter.get('/p/:shortid/:action', response.publishSlideActions)
7374
// get note by id
74-
appRouter.get('/:noteId', response.showNote)
75+
appRouter.get('/:noteId', wrap(noteController.showNote))
7576
// note actions
7677
appRouter.get('/:noteId/:action', response.noteActions)
7778
// note actions with action id

0 commit comments

Comments
 (0)