2017-10-30 12:38:01 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2022-11-03 13:08:48 +03:00
|
|
|
import Setting from '../models/setting';
|
2017-03-17 20:16:21 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2022-11-03 13:08:48 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
|
|
|
import {computed, defineProperty, get} from '@ember/object';
|
2022-10-07 16:23:21 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2022-11-03 13:08:48 +03:00
|
|
|
@classic
|
2022-10-07 16:23:21 +03:00
|
|
|
export default class SettingsService extends Service.extend(ValidationEngine) {
|
2022-02-03 22:04:43 +03:00
|
|
|
@service store;
|
2017-03-17 20:16:21 +03:00
|
|
|
|
|
|
|
// will be set to the single Settings model, it's a reference so any later
|
|
|
|
// changes to the settings object in the store will be reflected
|
2022-11-03 13:08:48 +03:00
|
|
|
@tracked settingsModel = null;
|
2017-03-17 20:16:21 +03:00
|
|
|
|
2022-02-03 01:11:11 +03:00
|
|
|
validationType = 'setting';
|
|
|
|
_loadingPromise = null;
|
2017-03-17 20:16:21 +03:00
|
|
|
|
2017-04-19 19:57:56 +03:00
|
|
|
// this is an odd case where we only want to react to changes that we get
|
|
|
|
// back from the API rather than local updates
|
2022-10-07 16:23:21 +03:00
|
|
|
@tracked settledIcon = '';
|
|
|
|
|
2022-11-03 13:08:48 +03:00
|
|
|
init() {
|
|
|
|
super.init(...arguments);
|
|
|
|
|
|
|
|
// TODO: update to use .getSchemaDefinitionService().attributesDefinitionFor({type: 'settings'});
|
|
|
|
// in later Ember versions
|
|
|
|
const attributes = get(Setting, 'attributes');
|
|
|
|
|
|
|
|
for (const [name] of attributes) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(this, name)) {
|
|
|
|
// a standard defineProperty here was not autotracking correctly - retry in a later Ember version
|
|
|
|
defineProperty(this, name, computed(`settingsModel.${name}`, {
|
|
|
|
get() {
|
|
|
|
return this.settingsModel?.[name];
|
|
|
|
},
|
|
|
|
set(keyName, value) {
|
|
|
|
return this.settingsModel[name] = value;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
get hasDirtyAttributes() {
|
|
|
|
return this.settingsModel?.hasDirtyAttributes || false;
|
|
|
|
}
|
|
|
|
|
|
|
|
get mailgunIsConfigured() {
|
|
|
|
return this.mailgunApiKey && this.mailgunDomain && this.mailgunBaseUrl;
|
|
|
|
}
|
2017-04-19 19:57:56 +03:00
|
|
|
|
2017-03-17 20:16:21 +03:00
|
|
|
// the settings API endpoint is a little weird as it's singular and we have
|
|
|
|
// to pass in all types - if we ever fetch settings without all types then
|
|
|
|
// save we have problems with the missing settings being removed or reset
|
|
|
|
_loadSettings() {
|
|
|
|
if (!this._loadingPromise) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this._loadingPromise = this.store
|
2023-04-20 18:50:07 +03:00
|
|
|
.queryRecord('setting', {group: 'site,theme,private,members,portal,newsletter,email,amp,labs,slack,unsplash,views,firstpromoter,editor,comments,analytics,announcement,pintura'})
|
2017-03-17 20:16:21 +03:00
|
|
|
.then((settings) => {
|
|
|
|
this._loadingPromise = null;
|
|
|
|
return settings;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._loadingPromise;
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2017-03-17 20:16:21 +03:00
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
async fetch() {
|
|
|
|
if (!this.settingsModel) {
|
2017-03-17 20:16:21 +03:00
|
|
|
return this.reload();
|
|
|
|
} else {
|
2022-10-07 16:23:21 +03:00
|
|
|
return this;
|
2017-03-17 20:16:21 +03:00
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2017-03-17 20:16:21 +03:00
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
async reload() {
|
|
|
|
const settingsModel = await this._loadSettings();
|
|
|
|
|
|
|
|
this.settingsModel = settingsModel;
|
|
|
|
this.settledIcon = settingsModel.icon;
|
|
|
|
|
|
|
|
return this;
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2017-03-17 20:16:21 +03:00
|
|
|
|
2020-06-04 23:30:06 +03:00
|
|
|
async save() {
|
2022-10-07 16:23:21 +03:00
|
|
|
const {settingsModel} = this;
|
2017-03-17 20:16:21 +03:00
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
if (!settingsModel) {
|
2017-03-17 20:16:21 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
await settingsModel.save();
|
2022-10-04 19:32:49 +03:00
|
|
|
await this.validate();
|
2022-10-07 16:23:21 +03:00
|
|
|
|
|
|
|
this.settledIcon = settingsModel.icon;
|
|
|
|
|
|
|
|
return this;
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2017-03-17 20:16:21 +03:00
|
|
|
|
|
|
|
rollbackAttributes() {
|
2022-10-07 16:23:21 +03:00
|
|
|
return this.settingsModel?.rollbackAttributes();
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2020-02-24 12:08:47 +03:00
|
|
|
|
2018-03-09 17:42:27 +03:00
|
|
|
changedAttributes() {
|
2022-10-07 16:23:21 +03:00
|
|
|
return this.settingsModel?.changedAttributes();
|
2017-03-17 20:16:21 +03:00
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|