2019-07-18 10:37:11 +03:00
|
|
|
const MembersSSR = require('@tryghost/members-ssr');
|
|
|
|
|
|
|
|
const createMembersApiInstance = require('./api');
|
|
|
|
const common = require('../../lib/common');
|
|
|
|
const urlUtils = require('../../lib/url-utils');
|
|
|
|
const settingsCache = require('../settings/cache');
|
|
|
|
|
|
|
|
let membersApi;
|
|
|
|
|
|
|
|
// Bind to events to automatically keep subscription info up-to-date from settings
|
|
|
|
common.events.on('settings.edited', function updateSettingFromModel(settingModel) {
|
2019-09-06 10:14:21 +03:00
|
|
|
if (!['members_subscription_settings'].includes(settingModel.get('key'))) {
|
2019-07-18 10:37:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const reconfiguredMembersAPI = createMembersApiInstance();
|
|
|
|
reconfiguredMembersAPI.bus.on('ready', function () {
|
|
|
|
membersApi = reconfiguredMembersAPI;
|
|
|
|
});
|
|
|
|
reconfiguredMembersAPI.bus.on('error', function (err) {
|
|
|
|
common.logging.error(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const membersService = {
|
|
|
|
isPaymentConfigured() {
|
|
|
|
const settings = settingsCache.get('members_subscription_settings');
|
|
|
|
return !!settings && settings.isPaid && settings.paymentProcessors.length !== 0;
|
|
|
|
},
|
|
|
|
|
2019-11-05 05:47:12 +03:00
|
|
|
contentGating: require('./content-gating'),
|
|
|
|
|
2019-11-07 13:13:20 +03:00
|
|
|
config: require('./config'),
|
|
|
|
|
2018-12-11 10:57:01 +03:00
|
|
|
get api() {
|
2019-07-18 10:37:11 +03:00
|
|
|
if (!membersApi) {
|
|
|
|
membersApi = createMembersApiInstance();
|
|
|
|
|
|
|
|
membersApi.bus.on('error', function (err) {
|
|
|
|
common.logging.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return membersApi;
|
2019-05-08 15:08:25 +03:00
|
|
|
},
|
|
|
|
|
2019-07-18 10:37:11 +03:00
|
|
|
ssr: MembersSSR({
|
|
|
|
cookieSecure: urlUtils.isSSL(urlUtils.getSiteUrl()),
|
|
|
|
cookieKeys: [settingsCache.get('theme_session_secret')],
|
2019-09-09 12:39:46 +03:00
|
|
|
cookieName: 'ghost-members-ssr',
|
|
|
|
cookieCacheName: 'ghost-members-ssr-cache',
|
2019-09-16 10:58:10 +03:00
|
|
|
getMembersApi: () => membersService.api
|
2019-09-03 07:39:00 +03:00
|
|
|
})
|
2018-12-11 10:57:01 +03:00
|
|
|
};
|
2019-07-18 10:37:11 +03:00
|
|
|
|
|
|
|
module.exports = membersService;
|