Move showPublishNote and publishNoteActions to note controller

Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
David Mehren 2019-10-27 15:22:14 +01:00
parent dee62ce571
commit 2bc4233ba8
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
6 changed files with 99 additions and 125 deletions

View file

@ -9,14 +9,11 @@ var config = require('./config')
var logger = require('./logger')
var models = require('./models')
const noteUtil = require('./web/note/util')
const noteController = require('./web/note/controller')
const errors = require('./errors')
// public
var response = {
showPublishNote: showPublishNote,
showIndex: showIndex,
publishNoteActions: publishNoteActions,
githubActions: githubActions,
gitlabActions: gitlabActions
}
@ -51,85 +48,6 @@ function showIndex (req, res, next) {
}
}
function showPublishNote (req, res, next) {
var include = [{
model: models.User,
as: 'owner'
}, {
model: models.User,
as: 'lastchangeuser'
}]
noteUtil.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 errors.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 ogdata = models.Note.parseOpengraph(meta, 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,
dnt: req.headers.dnt,
opengraph: ogdata
}
return renderPublish(data, res)
}).catch(function (err) {
logger.error(err)
return errors.errorInternalError(res)
})
}, include)
}
function renderPublish (data, res) {
res.set({
'Cache-Control': 'private' // only cache by client
})
res.render('pretty.ejs', data)
}
function publishNoteActions (req, res, next) {
noteUtil.findNote(req, res, function (note) {
var action = req.params.action
switch (action) {
case 'download':
noteController.downloadMarkdown(req, res, note)
break
case 'edit':
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)) + '?both')
break
default:
res.redirect(config.serverURL + '/s/' + note.shortid)
break
}
})
}
function githubActions (req, res, next) {
var noteId = req.params.noteId
noteUtil.findNote(req, res, function (note) {

View file

@ -8,7 +8,6 @@ const markdownpdf = require('markdown-pdf')
const moment = require('moment')
const querystring = require('querystring')
exports.getInfo = function getInfo (req, res, note) {
const body = note.content
const extracted = models.Note.extractMeta(body)

View file

@ -8,6 +8,58 @@ const errors = require('../../errors')
const noteUtil = require('./util')
const noteActions = require('./actions')
exports.publishNoteActions = function (req, res, next) {
noteUtil.findNote(req, res, function (note) {
const action = req.params.action
switch (action) {
case 'download':
exports.downloadMarkdown(req, res, note)
break
case 'edit':
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)) + '?both')
break
default:
res.redirect(config.serverURL + '/s/' + note.shortid)
break
}
})
}
exports.showPublishNote = function (req, res, next) {
const include = [{
model: models.User,
as: 'owner'
}, {
model: models.User,
as: 'lastchangeuser'
}]
noteUtil.findNote(req, res, function (note) {
// force to use short id
const 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 errors.errorNotFound(res)
}
noteUtil.getPublishData(req, res, note, (data) => {
return renderPublish(data, res)
})
}).catch(function (err) {
logger.error(err)
return errors.errorInternalError(res)
})
}, include)
}
function renderPublish (data, res) {
res.set({
'Cache-Control': 'private' // only cache by client
})
res.render('pretty.ejs', data)
}
exports.showNote = function (req, res, next) {
noteUtil.findNote(req, res, function (note) {
// force to use note id

View file

@ -1,9 +1,6 @@
'use strict'
const Router = require('express').Router
const response = require('../../response')
const { markdownParser } = require('../utils')
const router = module.exports = Router()
@ -18,9 +15,9 @@ router.post('/new', markdownParser, noteController.createFromPOST)
// post new note with content and alias
router.post('/new/:noteId', markdownParser, noteController.createFromPOST)
// get publish note
router.get('/s/:shortid', response.showPublishNote)
router.get('/s/:shortid', noteController.showPublishNote)
// publish note actions
router.get('/s/:shortid/:action', response.publishNoteActions)
router.get('/s/:shortid/:action', noteController.publishNoteActions)
// get publish slide
router.get('/p/:shortid', slide.showPublishSlide)
// publish slide actions

View file

@ -3,8 +3,6 @@ const models = require('../../models')
const errors = require('../../errors')
const logger = require('../../logger')
const config = require('../../config')
const fs = require('fs')
const path = require('path')
exports.publishSlideActions = function (req, res, next) {
noteUtil.findNote(req, res, function (note) {
@ -33,34 +31,9 @@ exports.showPublishSlide = function (req, res, next) {
if (!note) {
return errors.errorNotFound(res)
}
const body = note.content
const extracted = models.Note.extractMeta(body)
const markdown = extracted.markdown
const meta = models.Note.parseMeta(extracted.meta)
const createtime = note.createdAt
const updatetime = note.lastchangeAt
let title = models.Note.decodeTitle(note.title)
title = models.Note.generateWebTitle(meta.title || title)
const data = {
title: title,
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
viewcount: note.viewcount,
createtime: createtime,
updatetime: updatetime,
body: markdown,
theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme),
meta: JSON.stringify(extracted.meta),
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,
dnt: req.headers.dnt
}
return renderPublishSlide(data, res)
noteUtil.getPublishData(req, res, note, (data) => {
return renderPublishSlide(data, res)
})
}).catch(function (err) {
logger.error(err)
return errors.errorInternalError(res)
@ -74,10 +47,3 @@ function renderPublishSlide (data, res) {
})
res.render('slide.ejs', data)
}
function isRevealTheme (theme) {
if (fs.existsSync(path.join(__dirname, '..', 'public', 'build', 'reveal.js', 'css', 'theme', theme + '.css'))) {
return theme
}
return undefined
}

View file

@ -2,6 +2,8 @@ const models = require('../../models')
const logger = require('../../logger')
const config = require('../../config')
const errors = require('../../errors')
const fs = require('fs')
const path = require('path')
exports.findNote = function (req, res, callback, include) {
const id = req.params.noteId || req.params.shortid
@ -65,3 +67,43 @@ exports.newNote = function (req, res, body) {
return errors.errorInternalError(res)
})
}
exports.getPublishData = function (req, res, note, callback) {
const body = note.content
const extracted = models.Note.extractMeta(body)
const markdown = extracted.markdown
const meta = models.Note.parseMeta(extracted.meta)
const createtime = note.createdAt
const updatetime = note.lastchangeAt
let title = models.Note.decodeTitle(note.title)
title = models.Note.generateWebTitle(meta.title || title)
const ogdata = models.Note.parseOpengraph(meta, title)
const data = {
title: title,
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
viewcount: note.viewcount,
createtime: createtime,
updatetime: updatetime,
body: markdown,
theme: meta.slideOptions && isRevealTheme(meta.slideOptions.theme),
meta: JSON.stringify(extracted.meta),
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,
dnt: req.headers.dnt,
opengraph: ogdata
}
callback(data)
}
function isRevealTheme (theme) {
if (fs.existsSync(path.join(__dirname, '..', 'public', 'build', 'reveal.js', 'css', 'theme', theme + '.css'))) {
return theme
}
return undefined
}