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/controller.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'lib/web/note/controller.js') diff --git a/lib/web/note/controller.js b/lib/web/note/controller.js index 726d3577..991b891d 100644 --- a/lib/web/note/controller.js +++ b/lib/web/note/controller.js @@ -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 -- cgit v1.2.3