2021-08-13 11:09:11 +03:00
|
|
|
const debug = require('@tryghost/debug')('services:routing:controllers:emailpost');
|
|
|
|
const config = require('../../../../shared/config');
|
2021-10-18 21:01:38 +03:00
|
|
|
const {routerManager} = require('../');
|
2021-08-13 11:09:11 +03:00
|
|
|
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}
|
|
|
|
*/
|
2021-10-07 13:08:56 +03:00
|
|
|
module.exports = function emailPostController(req, res, next) {
|
2021-08-13 11:09:11 +03:00
|
|
|
debug('emailPostController');
|
|
|
|
|
|
|
|
const api = require('../../proxy').api[res.locals.apiVersion];
|
|
|
|
|
|
|
|
const params = {
|
2021-08-19 11:27:45 +03:00
|
|
|
uuid: req.params.uuid,
|
2021-08-13 11:09:11 +03:00
|
|
|
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') {
|
2021-10-18 21:01:38 +03:00
|
|
|
return urlUtils.redirect301(res, routerManager.getUrlByResourceId(post.id, {withSubdirectory: true}));
|
2021-08-13 11:09:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2021-10-07 13:08:56 +03:00
|
|
|
};
|