2019-02-25 17:47:17 +03:00
|
|
|
import RSVP from 'rsvp';
|
2017-10-30 12:38:01 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2019-02-26 08:37:50 +03:00
|
|
|
import timezoneData from '@tryghost/timezone-data';
|
2022-10-07 17:24:03 +03:00
|
|
|
import {TrackedObject} from 'tracked-built-ins';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
export default class ConfigService extends Service {
|
2022-02-03 22:04:43 +03:00
|
|
|
@service ajax;
|
|
|
|
@service ghostPaths;
|
|
|
|
@service session;
|
2022-02-03 01:11:11 +03:00
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
@tracked content = new TrackedObject();
|
2017-11-24 21:53:19 +03:00
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
availableTimezones = timezoneData;
|
2016-01-19 18:43:09 +03:00
|
|
|
|
2016-10-28 16:07:50 +03:00
|
|
|
fetch() {
|
2019-02-26 06:38:00 +03:00
|
|
|
let promises = [];
|
|
|
|
|
|
|
|
promises.push(this.fetchUnauthenticated());
|
|
|
|
|
|
|
|
if (this.session.isAuthenticated) {
|
|
|
|
promises.push(this.fetchAuthenticated());
|
|
|
|
}
|
|
|
|
|
|
|
|
return RSVP.all(promises);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2019-02-26 06:38:00 +03:00
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
async fetchUnauthenticated() {
|
|
|
|
const siteUrl = this.ghostPaths.url.api('site');
|
|
|
|
const {site} = await this.ajax.request(siteUrl);
|
|
|
|
|
|
|
|
// normalize url to non-trailing-slash
|
|
|
|
site.blogUrl = site.url.replace(/\/$/, '');
|
|
|
|
site.blogTitle = site.title;
|
|
|
|
delete site.url;
|
|
|
|
delete site.title;
|
2019-02-26 06:38:00 +03:00
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
Object.assign(this.content, site);
|
|
|
|
this._defineProperties(site);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2017-08-02 10:05:59 +03:00
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
async fetchAuthenticated() {
|
|
|
|
const configUrl = this.ghostPaths.url.api('config');
|
|
|
|
const {config} = await this.ajax.request(configUrl);
|
|
|
|
|
|
|
|
Object.assign(this.content, config);
|
|
|
|
this._defineProperties(config);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2016-09-30 14:43:40 +03:00
|
|
|
|
2022-02-03 01:11:11 +03:00
|
|
|
get blogDomain() {
|
2022-10-07 17:24:03 +03:00
|
|
|
const blogDomain = this.blogUrl
|
2017-08-03 14:45:14 +03:00
|
|
|
.replace(/^https?:\/\//, '')
|
|
|
|
.replace(/\/?$/, '');
|
|
|
|
|
|
|
|
return blogDomain;
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2021-06-11 18:42:27 +03:00
|
|
|
|
2022-02-03 01:11:11 +03:00
|
|
|
get emailDomain() {
|
2022-10-07 17:24:03 +03:00
|
|
|
const blogDomain = this.blogDomain || '';
|
2021-06-11 18:42:27 +03:00
|
|
|
const domainExp = blogDomain.match(new RegExp('^([^/:?#]+)(?:[/:?#]|$)', 'i'));
|
|
|
|
const domain = (domainExp && domainExp[1]) || '';
|
|
|
|
if (domain.startsWith('www.')) {
|
|
|
|
return domain.replace(/^(www)\.(?=[^/]*\..{2,5})/, '');
|
|
|
|
}
|
|
|
|
return domain;
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2021-08-20 16:33:23 +03:00
|
|
|
|
|
|
|
getSiteUrl(path) {
|
2022-10-07 17:24:03 +03:00
|
|
|
const siteUrl = new URL(this.blogUrl);
|
2021-08-20 16:33:23 +03:00
|
|
|
const subdir = siteUrl.pathname.endsWith('/') ? siteUrl.pathname : `${siteUrl.pathname}/`;
|
|
|
|
const fullPath = `${subdir}${path.replace(/^\//, '')}`;
|
|
|
|
|
|
|
|
return `${siteUrl.origin}${fullPath}`;
|
|
|
|
}
|
2022-10-07 17:24:03 +03:00
|
|
|
|
|
|
|
_defineProperties(obj) {
|
|
|
|
for (const name of Object.keys(obj)) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(this, name)) {
|
|
|
|
Object.defineProperty(this, name, {
|
|
|
|
get() {
|
|
|
|
return this.content[name];
|
|
|
|
},
|
|
|
|
set(newValue) {
|
|
|
|
this.content[name] = newValue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|