2022-01-21 22:25:47 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2021-06-11 13:44:42 +03:00
|
|
|
import moment from 'moment';
|
2022-05-05 00:51:34 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-06-11 13:44:42 +03:00
|
|
|
import {ghPluralize} from 'ghost-admin/helpers/gh-pluralize';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-06-11 13:44:42 +03:00
|
|
|
|
|
|
|
export default class MembersCountCacheService extends Service {
|
|
|
|
@service session;
|
|
|
|
@service store;
|
|
|
|
|
|
|
|
cache = {};
|
2022-05-10 15:58:22 +03:00
|
|
|
hasMultipleNewsletters = null;
|
2021-06-11 13:44:42 +03:00
|
|
|
|
2022-05-05 00:51:34 +03:00
|
|
|
@action
|
|
|
|
async count(query) {
|
|
|
|
if (typeof query === 'string') {
|
|
|
|
query = {filter: query};
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheKey = JSON.stringify(query);
|
|
|
|
const cachedValue = this.cache[cacheKey];
|
2021-06-11 13:44:42 +03:00
|
|
|
|
2022-03-31 10:53:59 +03:00
|
|
|
if (cachedValue && moment().diff(cachedValue.time, 'seconds') <= 60) {
|
2021-06-11 13:44:42 +03:00
|
|
|
return cachedValue.count;
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:51:34 +03:00
|
|
|
const count = this._countMembersTask.perform(query);
|
2021-06-11 13:44:42 +03:00
|
|
|
|
2022-05-05 00:51:34 +03:00
|
|
|
this.cache[cacheKey] = {count, time: moment()};
|
2021-06-11 13:44:42 +03:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:51:34 +03:00
|
|
|
@action
|
2022-05-10 15:58:22 +03:00
|
|
|
async countString(filter = '', {knownCount, newsletter} = {}) {
|
|
|
|
// Determine if we need to show the name of the newsletter or not
|
|
|
|
// TODO: replace this with a service or a settings boolean if we ever add a shortcut for this
|
|
|
|
if (this.hasMultipleNewsletters === null) {
|
|
|
|
const allNewsletters = await this.store.query('newsletter', {status: 'active', limit: 'all'});
|
|
|
|
this.hasMultipleNewsletters = allNewsletters.length > 1;
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
const user = this.session.user;
|
2021-06-11 13:44:42 +03:00
|
|
|
|
2022-05-10 15:58:22 +03:00
|
|
|
const nounSingular = newsletter && this.hasMultipleNewsletters ? 'subscriber' : 'member';
|
|
|
|
const nounPlural = nounSingular + 's';
|
|
|
|
const suffix = newsletter && this.hasMultipleNewsletters ? (' of ' + newsletter.name) : '';
|
|
|
|
|
|
|
|
const basicFilter = newsletter ? filter.replace(newsletter.recipientFilter, '').replace(/^\+\((.*)\)$/, '$1') : filter;
|
2021-06-11 13:44:42 +03:00
|
|
|
const filterParts = basicFilter.split(',');
|
|
|
|
const isFree = filterParts.length === 1 && filterParts[0] === 'status:free';
|
|
|
|
const isPaid = filterParts.length === 1 && filterParts[0] === 'status:-free';
|
2021-06-11 14:22:12 +03:00
|
|
|
const isAll = !filter || (filterParts.includes('status:free') && filterParts.includes('status:-free'));
|
2021-06-11 13:44:42 +03:00
|
|
|
|
|
|
|
// editors don't have permission to browse members so can't retrieve a count
|
|
|
|
// TODO: remove when editors have relevant permissions or we have a different way of fetching counts
|
|
|
|
if (user.isEditor && knownCount === undefined) {
|
|
|
|
if (isFree) {
|
2022-05-10 15:58:22 +03:00
|
|
|
return 'all free ' + nounPlural + suffix;
|
2021-06-11 13:44:42 +03:00
|
|
|
}
|
|
|
|
if (isPaid) {
|
2022-05-10 15:58:22 +03:00
|
|
|
return 'all paid members' + nounPlural + suffix;
|
2021-06-11 13:44:42 +03:00
|
|
|
}
|
|
|
|
if (isAll) {
|
2022-05-10 15:58:22 +03:00
|
|
|
return 'all members' + nounPlural + suffix;
|
2021-06-11 13:44:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 'a custom members segment';
|
|
|
|
}
|
|
|
|
|
|
|
|
const recipientCount = knownCount !== undefined ? knownCount : await this.count(filter);
|
|
|
|
|
|
|
|
if (isFree) {
|
2022-05-10 15:58:22 +03:00
|
|
|
return ghPluralize(recipientCount, 'free ' + nounSingular) + suffix;
|
2021-06-11 13:44:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isPaid) {
|
2022-05-10 15:58:22 +03:00
|
|
|
return ghPluralize(recipientCount, 'paid ' + nounSingular) + suffix;
|
2021-06-11 13:44:42 +03:00
|
|
|
}
|
|
|
|
|
2022-05-10 15:58:22 +03:00
|
|
|
return ghPluralize(recipientCount, nounSingular) + suffix;
|
2021-06-11 13:44:42 +03:00
|
|
|
}
|
|
|
|
|
2022-05-05 00:51:34 +03:00
|
|
|
@action
|
|
|
|
clear() {
|
|
|
|
this.cache = {};
|
|
|
|
}
|
|
|
|
|
2021-06-11 13:44:42 +03:00
|
|
|
@task
|
2022-05-05 00:51:34 +03:00
|
|
|
*_countMembersTask(query) {
|
|
|
|
if (!query) {
|
2021-06-11 13:44:42 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-05-05 00:51:34 +03:00
|
|
|
const result = yield this.store.query('member', {...query, limit: 1, page: 1});
|
2021-06-11 13:44:42 +03:00
|
|
|
return result.meta.pagination.total;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e); // eslint-disable-line
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|