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 <dmehren1@gmail.com>
---
 lib/web/note/actions.js    |  1 -
 lib/web/note/controller.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/web/note/router.js     |  7 ++-----
 lib/web/note/slide.js      | 40 +++--------------------------------
 lib/web/note/util.js       | 42 +++++++++++++++++++++++++++++++++++++
 5 files changed, 99 insertions(+), 43 deletions(-)

(limited to 'lib/web')

diff --git a/lib/web/note/actions.js b/lib/web/note/actions.js
index a97eebb6..9ff7fedb 100644
--- a/lib/web/note/actions.js
+++ b/lib/web/note/actions.js
@@ -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)
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
diff --git a/lib/web/note/router.js b/lib/web/note/router.js
index e4f867b2..cf6fdf43 100644
--- a/lib/web/note/router.js
+++ b/lib/web/note/router.js
@@ -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
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
-}
diff --git a/lib/web/note/util.js b/lib/web/note/util.js
index bda74ac4..eadfb1a3 100644
--- a/lib/web/note/util.js
+++ b/lib/web/note/util.js
@@ -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
+}
-- 
cgit v1.2.3