Skip to content

Commit 7f99704

Browse files
committed
refactor: showPublishNote
Signed-off-by: BoHong Li <raccoon@hackmd.io>
1 parent 66edff8 commit 7f99704

3 files changed

Lines changed: 69 additions & 63 deletions

File tree

lib/note/index.js

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
'use strict'
22

33
const config = require('../config')
4-
const { Note } = require('../models')
4+
const { Note, User } = require('../models')
55

66
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound } = require('../response')
77
const { updateHistory } = require('../history')
88

9-
async function getNoteById (noteId) {
9+
async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
1010
const id = await Note.parseNoteIdAsync(noteId)
11+
12+
const includes = []
13+
14+
if (includeUser) {
15+
includes.push({
16+
model: User,
17+
as: 'owner'
18+
}, {
19+
model: User,
20+
as: 'lastchangeuser'
21+
})
22+
}
23+
1124
const note = await Note.findOne({
1225
where: {
1326
id: id
14-
}
27+
},
28+
include: includes
1529
})
1630
return note
1731
}
@@ -23,7 +37,7 @@ async function createNote (userId, noteAlias) {
2337

2438
const note = await Note.create({
2539
ownerId: userId,
26-
alias: noteAlias,
40+
alias: noteAlias
2741
})
2842

2943
if (userId) {
@@ -60,4 +74,54 @@ async function showNote (req, res) {
6074
return responseCodiMD(res, note)
6175
}
6276

77+
async function showPublishNote (req, res) {
78+
const shortid = req.params.shortid
79+
80+
const note = await getNoteById(shortid, {
81+
includeUser: true
82+
})
83+
84+
if (!note) {
85+
return errorNotFound(res)
86+
}
87+
88+
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
89+
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
90+
}
91+
92+
await note.increment('viewcount')
93+
94+
const body = note.content
95+
const extracted = Note.extractMeta(body)
96+
const markdown = extracted.markdown
97+
const meta = Note.parseMeta(extracted.meta)
98+
const createTime = note.createdAt
99+
const updateTime = note.lastchangeAt
100+
const title = Note.generateWebTitle(meta.title || Note.decodeTitle(note.title))
101+
102+
const data = {
103+
title: title,
104+
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
105+
viewcount: note.viewcount,
106+
createtime: createTime,
107+
updatetime: updateTime,
108+
body: body,
109+
owner: note.owner ? note.owner.id : null,
110+
ownerprofile: note.owner ? User.getProfile(note.owner) : null,
111+
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
112+
lastchangeuserprofile: note.lastchangeuser ? User.getProfile(note.lastchangeuser) : null,
113+
robots: meta.robots || false, // default allow robots
114+
GA: meta.GA,
115+
disqus: meta.disqus,
116+
cspNonce: res.locals.nonce
117+
}
118+
119+
res.set({
120+
'Cache-Control': 'private' // only cache by client
121+
})
122+
123+
res.render('pretty.ejs', data)
124+
}
125+
63126
exports.showNote = showNote
127+
exports.showPublishNote = showPublishNote

lib/response.js

Lines changed: 0 additions & 58 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.showPublishNote = showPublishNote
2928
exports.showPublishSlide = showPublishSlide
3029
exports.showIndex = showIndex
3130
exports.noteActions = noteActions
@@ -210,63 +209,6 @@ function findNote (req, res, callback, include) {
210209
})
211210
}
212211

213-
function showPublishNote (req, res, next) {
214-
var include = [{
215-
model: models.User,
216-
as: 'owner'
217-
}, {
218-
model: models.User,
219-
as: 'lastchangeuser'
220-
}]
221-
findNote(req, res, function (note) {
222-
// force to use short id
223-
var shortid = req.params.shortid
224-
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
225-
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
226-
}
227-
note.increment('viewcount').then(function (note) {
228-
if (!note) {
229-
return errorNotFound(res)
230-
}
231-
var body = note.content
232-
var extracted = models.Note.extractMeta(body)
233-
var markdown = extracted.markdown
234-
var meta = models.Note.parseMeta(extracted.meta)
235-
var createtime = note.createdAt
236-
var updatetime = note.lastchangeAt
237-
var title = models.Note.decodeTitle(note.title)
238-
title = models.Note.generateWebTitle(meta.title || title)
239-
var data = {
240-
title: title,
241-
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
242-
viewcount: note.viewcount,
243-
createtime: createtime,
244-
updatetime: updatetime,
245-
body: body,
246-
owner: note.owner ? note.owner.id : null,
247-
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null,
248-
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
249-
lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null,
250-
robots: meta.robots || false, // default allow robots
251-
GA: meta.GA,
252-
disqus: meta.disqus,
253-
cspNonce: res.locals.nonce
254-
}
255-
return renderPublish(data, res)
256-
}).catch(function (err) {
257-
logger.error(err)
258-
return errorInternalError(res)
259-
})
260-
}, include)
261-
}
262-
263-
function renderPublish (data, res) {
264-
res.set({
265-
'Cache-Control': 'private' // only cache by client
266-
})
267-
res.render('pretty.ejs', data)
268-
}
269-
270212
function actionPublish (req, res, note) {
271213
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
272214
}

lib/routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ appRouter.get('/new', response.newNote)
6464
// post new note with content
6565
appRouter.post('/new', markdownParser, response.newNote)
6666
// get publish note
67-
appRouter.get('/s/:shortid', response.showPublishNote)
67+
appRouter.get('/s/:shortid', noteController.showPublishNote)
6868
// publish note actions
6969
appRouter.get('/s/:shortid/:action', response.publishNoteActions)
7070
// get publish slide

0 commit comments

Comments
 (0)