Ghost/core/frontend/services/routing/controllers/email-post.js
Naz 979474a8cc Refactored bootstrap module into RouterManager class
refs https://linear.app/tryghost/issue/CORE-104/decouple-frontend-routing-events-from-urlserver-events

- The "bootstrap" didn't give enough credit to everything this module was doing - it's responsible for managing correct initialization and reinitialization of the frontend Routes as well as passing router creation information back to the frontend's URL service
- The refactor is done in two steps - the "bootstrap.js" will be renamed in the follow-up commit to have a clean history of how the file evolved
2021-10-19 07:29:09 +13:00

66 lines
2.2 KiB
JavaScript

const debug = require('@tryghost/debug')('services:routing:controllers:emailpost');
const config = require('../../../../shared/config');
const {bootstrap} = require('../');
const urlUtils = require('../../../../shared/url-utils');
const helpers = require('../helpers');
/**
* @description Email Post Controller.
* @param {Object} req
* @param {Object} res
* @param {Function} next
* @returns {Promise}
*/
module.exports = function emailPostController(req, res, next) {
debug('emailPostController');
const api = require('../../proxy').api[res.locals.apiVersion];
const params = {
uuid: req.params.uuid,
include: 'authors,tags',
context: {
member: res.locals.member
}
};
return api[res.routerOptions.query.controller]
.read(params)
.then(function then(result) {
const post = result[res.routerOptions.query.resource][0];
if (!post) {
return next();
}
if (req.params.options && req.params.options.toLowerCase() === 'edit') {
// CASE: last param of the url is /edit but admin redirects are disabled
if (!config.get('admin:redirects')) {
return next();
}
// CASE: last param of the url is /edit, redirect to admin
// NOTE: only 'post' resources support email-only mode
return urlUtils.redirectToAdmin(302, res, `/#/editor/post/${post.id}`);
} else if (req.params.options) {
// CASE: unknown options param detected, ignore
return next();
}
if (post.status === 'published') {
return urlUtils.redirect301(res, bootstrap.getUrlByResourceId(post.id, {withSubdirectory: true}));
}
if (res.locals.apiVersion !== 'v0.1' && res.locals.apiVersion !== 'v2') {
post.access = !!post.html;
}
// @TODO: See helpers/secure
helpers.secure(req, post);
const renderer = helpers.renderEntry(req, res);
return renderer(post);
})
.catch(helpers.handleError(next));
};