From afb317b55155eed2cfcad0fee5aba2107dc0b106 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 27 Oct 2019 14:27:15 +0100 Subject: Move slide actions to own file Signed-off-by: David Mehren --- lib/web/note/slide.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 lib/web/note/slide.js (limited to 'lib/web/note/slide.js') diff --git a/lib/web/note/slide.js b/lib/web/note/slide.js new file mode 100644 index 00000000..58e46102 --- /dev/null +++ b/lib/web/note/slide.js @@ -0,0 +1,83 @@ +const noteUtil = require('./util') +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) { + const action = req.params.action + if (action === 'edit') { + res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)) + '?both') + } else { res.redirect(config.serverURL + '/p/' + note.shortid) } + }) +} + +exports.showPublishSlide = 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 + '/p/' + (note.alias || note.shortid)) + } + note.increment('viewcount').then(function (note) { + 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) + }).catch(function (err) { + logger.error(err) + return errors.errorInternalError(res) + }) + }, include) +} + +function renderPublishSlide (data, res) { + res.set({ + 'Cache-Control': 'private' // only cache by client + }) + 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 +} -- cgit v1.2.3 From 2bc4233ba80346e60ed4840714a9aa347ccdb361 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 27 Oct 2019 15:22:14 +0100 Subject: Move showPublishNote and publishNoteActions to note controller Signed-off-by: David Mehren --- lib/web/note/slide.js | 40 +++------------------------------------- 1 file changed, 3 insertions(+), 37 deletions(-) (limited to 'lib/web/note/slide.js') diff --git a/lib/web/note/slide.js b/lib/web/note/slide.js index 58e46102..e6ac9dd0 100644 --- a/lib/web/note/slide.js +++ b/lib/web/note/slide.js @@ -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 -} -- cgit v1.2.3 From b5ccceff59002034fbb089935076f40b8aa16e58 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 27 Oct 2019 15:44:23 +0100 Subject: Inline renderPublishSlide Signed-off-by: David Mehren --- lib/web/note/slide.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'lib/web/note/slide.js') diff --git a/lib/web/note/slide.js b/lib/web/note/slide.js index e6ac9dd0..d2d2ccfc 100644 --- a/lib/web/note/slide.js +++ b/lib/web/note/slide.js @@ -32,7 +32,10 @@ exports.showPublishSlide = function (req, res, next) { return errors.errorNotFound(res) } noteUtil.getPublishData(req, res, note, (data) => { - return renderPublishSlide(data, res) + res.set({ + 'Cache-Control': 'private' // only cache by client + }) + return res.render('slide.ejs', data) }) }).catch(function (err) { logger.error(err) @@ -40,10 +43,3 @@ exports.showPublishSlide = function (req, res, next) { }) }, include) } - -function renderPublishSlide (data, res) { - res.set({ - 'Cache-Control': 'private' // only cache by client - }) - res.render('slide.ejs', data) -} -- cgit v1.2.3