mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 10:21:36 +03:00
9bdb25d184
refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
235 lines
7.8 KiB
JavaScript
235 lines
7.8 KiB
JavaScript
import Service, {inject as service} from '@ember/service';
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
|
|
|
export default class MembersUtilsService extends Service {
|
|
@service settings;
|
|
@service feature;
|
|
@service session;
|
|
@service store;
|
|
|
|
@inject config;
|
|
|
|
paidTiers = null;
|
|
|
|
get isMembersEnabled() {
|
|
return this.settings.membersEnabled;
|
|
}
|
|
|
|
get paidMembersEnabled() {
|
|
return this.settings.paidMembersEnabled;
|
|
}
|
|
|
|
get isMembersInviteOnly() {
|
|
return this.settings.membersInviteOnly;
|
|
}
|
|
|
|
get hasMultipleTiers() {
|
|
return this.paidMembersEnabled && this.paidTiers && this.paidTiers.length > 1;
|
|
}
|
|
|
|
async fetch() {
|
|
if (this.paidTiers !== null) {
|
|
return;
|
|
}
|
|
|
|
// contributors don't have permissions to fetch tiers
|
|
if (this.session.user && !this.session.user.isContributor) {
|
|
return this.store.query('tier', {filter: 'type:paid+active:true', limit: 'all'}).then((tiers) => {
|
|
this.paidTiers = tiers;
|
|
});
|
|
}
|
|
}
|
|
|
|
async reload() {
|
|
// contributors don't have permissions to fetch tiers
|
|
if (this.session.user && !this.session.user.isContributor) {
|
|
return this.store.query('tier', {filter: 'type:paid+active:true', limit: 'all'}).then((tiers) => {
|
|
this.paidTiers = tiers;
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Note: always use paidMembersEnabled! Only use this getter for the Stripe Connection UI.
|
|
*/
|
|
get isStripeEnabled() {
|
|
const stripeDirect = this.config.stripeDirect;
|
|
|
|
const hasDirectKeys = !!this.settings.stripeSecretKey && !!this.settings.stripePublishableKey;
|
|
const hasConnectKeys = !!this.settings.stripeConnectSecretKey && !!this.settings.stripeConnectPublishableKey;
|
|
|
|
if (stripeDirect) {
|
|
return hasDirectKeys;
|
|
}
|
|
|
|
return hasConnectKeys || hasDirectKeys;
|
|
}
|
|
|
|
// Button / Icon helpers ---------------------------------------------------
|
|
|
|
get defaultButtonIcons() {
|
|
return [
|
|
{
|
|
icon: 'portal-icon-1',
|
|
value: 'icon-1'
|
|
},
|
|
{
|
|
icon: 'portal-icon-2',
|
|
value: 'icon-2'
|
|
},
|
|
{
|
|
icon: 'portal-icon-3',
|
|
value: 'icon-3'
|
|
},
|
|
{
|
|
icon: 'portal-icon-4',
|
|
value: 'icon-4'
|
|
},
|
|
{
|
|
icon: 'portal-icon-5',
|
|
value: 'icon-5'
|
|
}
|
|
];
|
|
}
|
|
|
|
get defaultIconKeys() {
|
|
return this.defaultButtonIcons.map(buttonIcon => buttonIcon.value);
|
|
}
|
|
|
|
get buttonIcon() {
|
|
return this.settings.portalButtonIcon || this.defaultIconKeys[0];
|
|
}
|
|
|
|
// Plan helpers ------------------------------------------------------------
|
|
|
|
get isFreeChecked() {
|
|
const allowedPlans = this.settings.portalPlans || [];
|
|
return !!(this.settings.membersSignupAccess === 'all' && allowedPlans.includes('free'));
|
|
}
|
|
|
|
get isMonthlyChecked() {
|
|
const allowedPlans = this.settings.portalPlans || [];
|
|
return !!(this.isStripeConfigured && allowedPlans.includes('monthly'));
|
|
}
|
|
|
|
get isYearlyChecked() {
|
|
const allowedPlans = this.settings.portalPlans || [];
|
|
return !!(this.isStripeConfigured && allowedPlans.includes('yearly'));
|
|
}
|
|
|
|
// Portal preview ----------------------------------------------------------
|
|
|
|
getPortalPreviewUrl(overrides) {
|
|
let {
|
|
disableBackground = false,
|
|
page = 'signup',
|
|
button = this.settings.portalButton,
|
|
buttonIcon = this.buttonIcon,
|
|
isFreeChecked = this.isFreeChecked,
|
|
isMonthlyChecked = this.isMonthlyChecked,
|
|
isYearlyChecked = this.isYearlyChecked,
|
|
monthlyPrice,
|
|
yearlyPrice,
|
|
portalPlans = this.settings.portalPlans,
|
|
portalTiers,
|
|
currency,
|
|
membersSignupAccess = this.settings.membersSignupAccess
|
|
} = overrides;
|
|
|
|
const tiers = this.store.peekAll('tier') || [];
|
|
|
|
portalTiers = portalTiers || tiers.filter((t) => {
|
|
return t.visibility === 'public' && t.type === 'paid';
|
|
}).map(t => t.id);
|
|
|
|
const baseUrl = this.config.blogUrl;
|
|
const portalBase = '/#/portal/preview';
|
|
const settingsParam = new URLSearchParams();
|
|
const signupButtonText = this.settings.portalButtonSignupText || '';
|
|
const allowSelfSignup = membersSignupAccess === 'all' && (!this.isStripeEnabled || isFreeChecked);
|
|
|
|
settingsParam.append('button', button);
|
|
settingsParam.append('name', this.settings.portalName);
|
|
settingsParam.append('isFree', isFreeChecked);
|
|
settingsParam.append('isMonthly', isMonthlyChecked);
|
|
settingsParam.append('isYearly', isYearlyChecked);
|
|
settingsParam.append('page', page);
|
|
settingsParam.append('buttonIcon', encodeURIComponent(buttonIcon));
|
|
settingsParam.append('signupButtonText', encodeURIComponent(signupButtonText));
|
|
settingsParam.append('membersSignupAccess', membersSignupAccess);
|
|
settingsParam.append('allowSelfSignup', allowSelfSignup);
|
|
|
|
if (portalPlans) {
|
|
settingsParam.append('portalPrices', encodeURIComponent(portalPlans));
|
|
}
|
|
|
|
if (portalTiers) {
|
|
settingsParam.append('portalProducts', encodeURIComponent(portalTiers));
|
|
}
|
|
|
|
if (this.settings.accentColor === '' || this.settings.accentColor) {
|
|
settingsParam.append('accentColor', encodeURIComponent(`${this.settings.accentColor}`));
|
|
}
|
|
if (this.settings.portalButtonStyle) {
|
|
settingsParam.append('buttonStyle', encodeURIComponent(this.settings.portalButtonStyle));
|
|
}
|
|
|
|
if (monthlyPrice) {
|
|
settingsParam.append('monthlyPrice', monthlyPrice);
|
|
}
|
|
if (yearlyPrice) {
|
|
settingsParam.append('yearlyPrice', yearlyPrice);
|
|
}
|
|
if (currency) {
|
|
settingsParam.append('currency', currency);
|
|
}
|
|
|
|
if (disableBackground) {
|
|
settingsParam.append('disableBackground', true);
|
|
}
|
|
|
|
return `${baseUrl}${portalBase}?${settingsParam.toString()}`;
|
|
}
|
|
|
|
getOfferPortalPreviewUrl(overrides) {
|
|
const {
|
|
disableBackground = false,
|
|
name,
|
|
code,
|
|
displayTitle = '',
|
|
displayDescription = '',
|
|
type,
|
|
cadence,
|
|
amount = 0,
|
|
duration,
|
|
durationInMonths,
|
|
currency = 'usd',
|
|
status,
|
|
tierId
|
|
} = overrides;
|
|
|
|
const baseUrl = this.config.blogUrl;
|
|
const portalBase = '/#/portal/preview/offer';
|
|
const settingsParam = new URLSearchParams();
|
|
|
|
settingsParam.append('name', encodeURIComponent(name));
|
|
settingsParam.append('code', encodeURIComponent(code));
|
|
settingsParam.append('display_title', encodeURIComponent(displayTitle));
|
|
settingsParam.append('display_description', encodeURIComponent(displayDescription));
|
|
settingsParam.append('type', encodeURIComponent(type));
|
|
settingsParam.append('cadence', encodeURIComponent(cadence));
|
|
settingsParam.append('amount', encodeURIComponent(amount));
|
|
settingsParam.append('duration', encodeURIComponent(duration));
|
|
settingsParam.append('duration_in_months', encodeURIComponent(durationInMonths));
|
|
settingsParam.append('currency', encodeURIComponent(currency));
|
|
settingsParam.append('status', encodeURIComponent(status));
|
|
settingsParam.append('tier_id', encodeURIComponent(tierId));
|
|
|
|
if (disableBackground) {
|
|
settingsParam.append('disableBackground', 'true');
|
|
}
|
|
|
|
return `${baseUrl}${portalBase}?${settingsParam.toString()}`;
|
|
}
|
|
}
|