mirror of
https://github.com/status-im/codimd.git
synced 2025-02-20 04:28:12 +00:00
refactor: showPublishNote
Signed-off-by: BoHong Li <raccoon@hackmd.io>
This commit is contained in:
parent
66edff87c5
commit
7f9970449a
@ -1,17 +1,31 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const config = require('../config')
|
const config = require('../config')
|
||||||
const { Note } = require('../models')
|
const { Note, User } = require('../models')
|
||||||
|
|
||||||
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound } = require('../response')
|
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound } = require('../response')
|
||||||
const { updateHistory } = require('../history')
|
const { updateHistory } = require('../history')
|
||||||
|
|
||||||
async function getNoteById (noteId) {
|
async function getNoteById (noteId, { includeUser } = { includeUser: false }) {
|
||||||
const id = await Note.parseNoteIdAsync(noteId)
|
const id = await Note.parseNoteIdAsync(noteId)
|
||||||
|
|
||||||
|
const includes = []
|
||||||
|
|
||||||
|
if (includeUser) {
|
||||||
|
includes.push({
|
||||||
|
model: User,
|
||||||
|
as: 'owner'
|
||||||
|
}, {
|
||||||
|
model: User,
|
||||||
|
as: 'lastchangeuser'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const note = await Note.findOne({
|
const note = await Note.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: id
|
id: id
|
||||||
}
|
},
|
||||||
|
include: includes
|
||||||
})
|
})
|
||||||
return note
|
return note
|
||||||
}
|
}
|
||||||
@ -23,7 +37,7 @@ async function createNote (userId, noteAlias) {
|
|||||||
|
|
||||||
const note = await Note.create({
|
const note = await Note.create({
|
||||||
ownerId: userId,
|
ownerId: userId,
|
||||||
alias: noteAlias,
|
alias: noteAlias
|
||||||
})
|
})
|
||||||
|
|
||||||
if (userId) {
|
if (userId) {
|
||||||
@ -60,4 +74,54 @@ async function showNote (req, res) {
|
|||||||
return responseCodiMD(res, note)
|
return responseCodiMD(res, note)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function showPublishNote (req, res) {
|
||||||
|
const shortid = req.params.shortid
|
||||||
|
|
||||||
|
const note = await getNoteById(shortid, {
|
||||||
|
includeUser: true
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!note) {
|
||||||
|
return errorNotFound(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
||||||
|
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||||
|
}
|
||||||
|
|
||||||
|
await note.increment('viewcount')
|
||||||
|
|
||||||
|
const body = note.content
|
||||||
|
const extracted = Note.extractMeta(body)
|
||||||
|
const markdown = extracted.markdown
|
||||||
|
const meta = Note.parseMeta(extracted.meta)
|
||||||
|
const createTime = note.createdAt
|
||||||
|
const updateTime = note.lastchangeAt
|
||||||
|
const title = Note.generateWebTitle(meta.title || Note.decodeTitle(note.title))
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
title: title,
|
||||||
|
description: meta.description || (markdown ? Note.generateDescription(markdown) : null),
|
||||||
|
viewcount: note.viewcount,
|
||||||
|
createtime: createTime,
|
||||||
|
updatetime: updateTime,
|
||||||
|
body: body,
|
||||||
|
owner: note.owner ? note.owner.id : null,
|
||||||
|
ownerprofile: note.owner ? User.getProfile(note.owner) : null,
|
||||||
|
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
||||||
|
lastchangeuserprofile: note.lastchangeuser ? User.getProfile(note.lastchangeuser) : null,
|
||||||
|
robots: meta.robots || false, // default allow robots
|
||||||
|
GA: meta.GA,
|
||||||
|
disqus: meta.disqus,
|
||||||
|
cspNonce: res.locals.nonce
|
||||||
|
}
|
||||||
|
|
||||||
|
res.set({
|
||||||
|
'Cache-Control': 'private' // only cache by client
|
||||||
|
})
|
||||||
|
|
||||||
|
res.render('pretty.ejs', data)
|
||||||
|
}
|
||||||
|
|
||||||
exports.showNote = showNote
|
exports.showNote = showNote
|
||||||
|
exports.showPublishNote = showPublishNote
|
||||||
|
@ -25,7 +25,6 @@ exports.errorTooLong = errorTooLong
|
|||||||
exports.errorInternalError = errorInternalError
|
exports.errorInternalError = errorInternalError
|
||||||
exports.errorServiceUnavailable = errorServiceUnavailable
|
exports.errorServiceUnavailable = errorServiceUnavailable
|
||||||
exports.newNote = newNote
|
exports.newNote = newNote
|
||||||
exports.showPublishNote = showPublishNote
|
|
||||||
exports.showPublishSlide = showPublishSlide
|
exports.showPublishSlide = showPublishSlide
|
||||||
exports.showIndex = showIndex
|
exports.showIndex = showIndex
|
||||||
exports.noteActions = noteActions
|
exports.noteActions = noteActions
|
||||||
@ -210,63 +209,6 @@ function findNote (req, res, callback, include) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function showPublishNote (req, res, next) {
|
|
||||||
var include = [{
|
|
||||||
model: models.User,
|
|
||||||
as: 'owner'
|
|
||||||
}, {
|
|
||||||
model: models.User,
|
|
||||||
as: 'lastchangeuser'
|
|
||||||
}]
|
|
||||||
findNote(req, res, function (note) {
|
|
||||||
// force to use short id
|
|
||||||
var shortid = req.params.shortid
|
|
||||||
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
|
||||||
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
|
||||||
}
|
|
||||||
note.increment('viewcount').then(function (note) {
|
|
||||||
if (!note) {
|
|
||||||
return errorNotFound(res)
|
|
||||||
}
|
|
||||||
var body = note.content
|
|
||||||
var extracted = models.Note.extractMeta(body)
|
|
||||||
var markdown = extracted.markdown
|
|
||||||
var meta = models.Note.parseMeta(extracted.meta)
|
|
||||||
var createtime = note.createdAt
|
|
||||||
var updatetime = note.lastchangeAt
|
|
||||||
var title = models.Note.decodeTitle(note.title)
|
|
||||||
title = models.Note.generateWebTitle(meta.title || title)
|
|
||||||
var data = {
|
|
||||||
title: title,
|
|
||||||
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
|
||||||
viewcount: note.viewcount,
|
|
||||||
createtime: createtime,
|
|
||||||
updatetime: updatetime,
|
|
||||||
body: body,
|
|
||||||
owner: note.owner ? note.owner.id : null,
|
|
||||||
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null,
|
|
||||||
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
|
||||||
lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null,
|
|
||||||
robots: meta.robots || false, // default allow robots
|
|
||||||
GA: meta.GA,
|
|
||||||
disqus: meta.disqus,
|
|
||||||
cspNonce: res.locals.nonce
|
|
||||||
}
|
|
||||||
return renderPublish(data, res)
|
|
||||||
}).catch(function (err) {
|
|
||||||
logger.error(err)
|
|
||||||
return errorInternalError(res)
|
|
||||||
})
|
|
||||||
}, include)
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderPublish (data, res) {
|
|
||||||
res.set({
|
|
||||||
'Cache-Control': 'private' // only cache by client
|
|
||||||
})
|
|
||||||
res.render('pretty.ejs', data)
|
|
||||||
}
|
|
||||||
|
|
||||||
function actionPublish (req, res, note) {
|
function actionPublish (req, res, note) {
|
||||||
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ appRouter.get('/new', response.newNote)
|
|||||||
// post new note with content
|
// post new note with content
|
||||||
appRouter.post('/new', markdownParser, response.newNote)
|
appRouter.post('/new', markdownParser, response.newNote)
|
||||||
// get publish note
|
// get publish note
|
||||||
appRouter.get('/s/:shortid', response.showPublishNote)
|
appRouter.get('/s/:shortid', noteController.showPublishNote)
|
||||||
// publish note actions
|
// publish note actions
|
||||||
appRouter.get('/s/:shortid/:action', response.publishNoteActions)
|
appRouter.get('/s/:shortid/:action', response.publishNoteActions)
|
||||||
// get publish slide
|
// get publish slide
|
||||||
|
Loading…
x
Reference in New Issue
Block a user