2019-04-03 13:40:35 +03:00
|
|
|
const _ = require('lodash');
|
2021-04-23 17:16:59 +03:00
|
|
|
const hbs = require('./engine');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
2021-07-06 16:54:02 +03:00
|
|
|
const {api} = require('../proxy');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-07-06 16:54:02 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2021-06-30 16:56:57 +03:00
|
|
|
const settingsCache = require('../../../shared/settings-cache');
|
2021-09-23 14:44:39 +03:00
|
|
|
const customThemeSettingsCache = require('../../../shared/custom-theme-settings-cache');
|
2021-07-07 23:41:34 +03:00
|
|
|
const labs = require('../../../shared/labs');
|
2021-04-23 17:16:59 +03:00
|
|
|
const activeTheme = require('./active');
|
|
|
|
const preview = require('./preview');
|
2017-03-21 12:03:09 +03:00
|
|
|
|
2021-07-06 16:54:02 +03:00
|
|
|
const messages = {
|
|
|
|
missingTheme: 'The currently active theme "{theme}" is missing.'
|
|
|
|
};
|
|
|
|
|
2017-03-21 12:03:09 +03:00
|
|
|
// ### Ensure Active Theme
|
2019-09-10 12:37:04 +03:00
|
|
|
// Ensure there's a properly set & mounted active theme before attempting to serve a site request
|
2017-03-21 12:03:09 +03:00
|
|
|
// If there is no active theme, throw an error
|
|
|
|
// Else, ensure the active theme is mounted
|
2019-04-03 13:40:35 +03:00
|
|
|
function ensureActiveTheme(req, res, next) {
|
2017-05-31 19:42:42 +03:00
|
|
|
// CASE: this means that the theme hasn't been loaded yet i.e. there is no active theme
|
2017-03-21 12:03:09 +03:00
|
|
|
if (!activeTheme.get()) {
|
|
|
|
// This is the one place we ACTUALLY throw an error for a missing theme as it's a request we cannot serve
|
2020-05-22 21:22:20 +03:00
|
|
|
return next(new errors.InternalServerError({
|
2017-03-21 12:03:09 +03:00
|
|
|
// We use the settingsCache here, because the setting will be set,
|
|
|
|
// even if the theme itself is not usable because it is invalid or missing.
|
2021-07-06 16:54:02 +03:00
|
|
|
message: tpl(messages.missingTheme, {theme: settingsCache.get('active_theme')})
|
2017-03-21 12:03:09 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the active theme has not yet been mounted, mount it into express
|
|
|
|
if (!activeTheme.get().mounted) {
|
|
|
|
activeTheme.get().mount(req.app);
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
2019-04-03 13:40:35 +03:00
|
|
|
}
|
2017-03-21 12:03:09 +03:00
|
|
|
|
2021-07-21 13:28:13 +03:00
|
|
|
function calculateLegacyPriceData(products) {
|
2021-05-05 16:47:07 +03:00
|
|
|
const defaultPrice = {
|
|
|
|
amount: 0,
|
2021-05-12 16:27:30 +03:00
|
|
|
currency: 'usd',
|
|
|
|
interval: 'year',
|
|
|
|
nickname: ''
|
2020-03-04 08:15:21 +03:00
|
|
|
};
|
|
|
|
|
2021-05-05 16:47:07 +03:00
|
|
|
function makePriceObject(price) {
|
|
|
|
const numberAmount = 0 + price.amount;
|
|
|
|
const dollarAmount = numberAmount ? Math.round(numberAmount / 100) : 0;
|
|
|
|
return {
|
|
|
|
valueOf() {
|
|
|
|
return dollarAmount;
|
|
|
|
},
|
|
|
|
amount: numberAmount,
|
|
|
|
currency: price.currency,
|
|
|
|
nickname: price.name,
|
|
|
|
interval: price.interval
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-21 13:28:13 +03:00
|
|
|
const defaultProduct = products[0] || {};
|
2021-05-05 16:47:07 +03:00
|
|
|
|
2021-07-21 13:28:13 +03:00
|
|
|
const monthlyPrice = makePriceObject(defaultProduct.monthly_price || defaultPrice);
|
2021-05-26 20:28:26 +03:00
|
|
|
|
2021-07-21 13:28:13 +03:00
|
|
|
const yearlyPrice = makePriceObject(defaultProduct.yearly_price || defaultPrice);
|
2021-05-05 16:47:07 +03:00
|
|
|
|
2021-07-21 13:28:13 +03:00
|
|
|
const priceData = {
|
|
|
|
monthly: monthlyPrice,
|
|
|
|
yearly: yearlyPrice,
|
|
|
|
currency: monthlyPrice ? monthlyPrice.currency : defaultPrice.currency
|
|
|
|
};
|
2021-05-05 16:47:07 +03:00
|
|
|
|
2021-07-21 13:28:13 +03:00
|
|
|
return priceData;
|
2019-10-21 13:50:54 +03:00
|
|
|
}
|
|
|
|
|
2021-06-03 15:44:14 +03:00
|
|
|
async function getProductAndPricesData() {
|
|
|
|
try {
|
|
|
|
const page = await api.canary.productsPublic.browse({
|
|
|
|
include: ['monthly_price', 'yearly_price'],
|
|
|
|
limit: 'all'
|
|
|
|
});
|
|
|
|
|
|
|
|
return page.products;
|
|
|
|
} catch (err) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 12:36:05 +03:00
|
|
|
function getSiteData() {
|
2021-02-12 19:08:55 +03:00
|
|
|
let siteData = settingsCache.getPublic();
|
|
|
|
|
2021-04-21 14:10:09 +03:00
|
|
|
// theme-only computed property added to @site
|
|
|
|
if (settingsCache.get('members_signup_access') === 'none') {
|
|
|
|
const escapedUrl = encodeURIComponent(urlUtils.urlFor({relativeUrl: '/rss/'}, true));
|
|
|
|
siteData.signup_url = `https://feedly.com/i/subscription/feed/${escapedUrl}`;
|
|
|
|
} else {
|
|
|
|
siteData.signup_url = '#/portal';
|
|
|
|
}
|
|
|
|
|
2021-02-12 19:08:55 +03:00
|
|
|
return siteData;
|
|
|
|
}
|
|
|
|
|
2021-05-05 16:47:07 +03:00
|
|
|
async function updateGlobalTemplateOptions(req, res, next) {
|
2017-03-21 12:03:09 +03:00
|
|
|
// Static information, same for every request unless the settings change
|
|
|
|
// @TODO: bind this once and then update based on events?
|
2019-01-15 15:03:17 +03:00
|
|
|
// @TODO: decouple theme layer from settings cache using the Content API
|
2021-09-30 12:36:05 +03:00
|
|
|
const siteData = getSiteData();
|
2019-11-06 10:42:39 +03:00
|
|
|
const labsData = labs.getAll();
|
|
|
|
|
2019-04-03 14:01:23 +03:00
|
|
|
const themeData = {
|
|
|
|
posts_per_page: activeTheme.get().config('posts_per_page'),
|
|
|
|
image_sizes: activeTheme.get().config('image_sizes')
|
|
|
|
};
|
2021-09-23 14:44:39 +03:00
|
|
|
const themeSettingsData = customThemeSettingsCache.getAll();
|
2021-06-03 15:44:14 +03:00
|
|
|
const productData = await getProductAndPricesData();
|
2021-07-21 13:28:13 +03:00
|
|
|
const priceData = calculateLegacyPriceData(productData);
|
2021-06-03 15:44:14 +03:00
|
|
|
|
|
|
|
let products = null;
|
|
|
|
let product = null;
|
|
|
|
if (productData.length === 1) {
|
|
|
|
product = productData[0];
|
|
|
|
} else {
|
|
|
|
products = productData;
|
|
|
|
}
|
2017-03-21 12:03:09 +03:00
|
|
|
|
|
|
|
// @TODO: only do this if something changed?
|
2021-02-12 19:08:55 +03:00
|
|
|
// @TODO: remove blog in a major where we are happy to break more themes
|
|
|
|
{
|
|
|
|
hbs.updateTemplateOptions({
|
|
|
|
data: {
|
|
|
|
blog: siteData,
|
|
|
|
site: siteData,
|
|
|
|
labs: labsData,
|
|
|
|
config: themeData,
|
2021-06-03 15:44:14 +03:00
|
|
|
price: priceData,
|
|
|
|
product,
|
2021-09-23 14:44:39 +03:00
|
|
|
products,
|
|
|
|
custom: themeSettingsData
|
2021-02-12 19:08:55 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-03-21 12:03:09 +03:00
|
|
|
|
|
|
|
next();
|
2019-04-03 13:40:35 +03:00
|
|
|
}
|
2017-03-21 12:03:09 +03:00
|
|
|
|
2019-04-03 14:01:23 +03:00
|
|
|
function updateLocalTemplateData(req, res, next) {
|
|
|
|
// Pass 'secure' flag to the view engine
|
|
|
|
// so that templates can choose to render https or http 'url', see url utility
|
|
|
|
res.locals.secure = req.secure;
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateLocalTemplateOptions(req, res, next) {
|
|
|
|
const localTemplateOptions = hbs.getLocalTemplateOptions(res.locals);
|
2021-09-30 12:36:05 +03:00
|
|
|
|
|
|
|
// adjust @site.url for http/https based on the incoming request
|
2019-04-03 14:01:23 +03:00
|
|
|
const siteData = {
|
2019-06-18 16:13:55 +03:00
|
|
|
url: urlUtils.urlFor('home', {secure: req.secure, trailingSlash: false}, true)
|
2019-04-03 14:01:23 +03:00
|
|
|
};
|
|
|
|
|
2021-09-30 12:36:05 +03:00
|
|
|
// @TODO: it would be nicer if this was proper middleware somehow...
|
2021-09-30 15:23:39 +03:00
|
|
|
const previewData = preview.handle(req, Object.keys(customThemeSettingsCache.getAll()));
|
|
|
|
|
|
|
|
// strip custom off of preview data so it doesn't get merged into @site
|
|
|
|
const customThemeSettingsPreviewData = previewData.custom;
|
|
|
|
delete previewData.custom;
|
|
|
|
let customData = {};
|
|
|
|
if (labs.isSet('customThemeSettings')) {
|
|
|
|
customData = customThemeSettingsPreviewData;
|
|
|
|
}
|
2021-09-30 12:36:05 +03:00
|
|
|
|
|
|
|
// update site data with any preview values from the request
|
|
|
|
Object.assign(siteData, previewData);
|
|
|
|
|
2019-09-10 12:22:38 +03:00
|
|
|
const member = req.member ? {
|
2020-03-02 14:14:03 +03:00
|
|
|
uuid: req.member.uuid,
|
2019-09-10 12:22:38 +03:00
|
|
|
email: req.member.email,
|
2019-10-01 08:44:42 +03:00
|
|
|
name: req.member.name,
|
2019-10-09 07:47:01 +03:00
|
|
|
firstname: req.member.name && req.member.name.split(' ')[0],
|
2020-02-19 13:08:12 +03:00
|
|
|
avatar_image: req.member.avatar_image,
|
2021-02-24 11:54:54 +03:00
|
|
|
subscriptions: req.member.subscriptions && req.member.subscriptions.map((sub) => {
|
|
|
|
return Object.assign({}, sub, {
|
|
|
|
default_payment_card_last4: sub.default_payment_card_last4 || '****'
|
|
|
|
});
|
|
|
|
}),
|
2021-02-25 17:23:36 +03:00
|
|
|
paid: req.member.status !== 'free'
|
2019-09-10 12:22:38 +03:00
|
|
|
} : null;
|
|
|
|
|
2019-04-03 14:01:23 +03:00
|
|
|
hbs.updateLocalTemplateOptions(res.locals, _.merge({}, localTemplateOptions, {
|
|
|
|
data: {
|
2019-09-10 12:22:38 +03:00
|
|
|
member: member,
|
2019-04-03 14:01:23 +03:00
|
|
|
site: siteData,
|
2021-09-30 15:23:39 +03:00
|
|
|
custom: customData,
|
2021-04-21 14:10:09 +03:00
|
|
|
// @deprecated: a gscan warning for @blog was added before 3.0 which replaced it with @site
|
2019-04-03 14:01:23 +03:00
|
|
|
blog: siteData
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2017-03-21 12:03:09 +03:00
|
|
|
module.exports = [
|
2019-04-03 13:40:35 +03:00
|
|
|
ensureActiveTheme,
|
2019-04-03 14:01:23 +03:00
|
|
|
updateGlobalTemplateOptions,
|
|
|
|
updateLocalTemplateData,
|
|
|
|
updateLocalTemplateOptions
|
2017-03-21 12:03:09 +03:00
|
|
|
];
|