Ghost/core/frontend/services/routing/controllers/email-post.js
Naz 74280cfbea Added "email post" frontend routing
refs https://github.com/TryGhost/Team/issues/953

- Emails posts should be not explorable by the rest of the frontend similarly to the draft or scheduled posts. Email posts should also keep the content gating, so that specific parts of content can still be gated based on the post's visibility setup
- A separate frontend router was chosen to implement this part of the system instead of a moutable express app due to increased complexity to introduce the latter approach.
- All "sent" email-only posts will be accessible through the `/email/:slug/` route
2021-08-13 12:09:11 +04:00

66 lines
2.2 KiB
JavaScript

const debug = require('@tryghost/debug')('services:routing:controllers:emailpost');
const config = require('../../../../shared/config');
const urlService = require('../../url');
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 = {
slug: req.params.slug,
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, urlService.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));
};