Ghost/ghost/admin/app/services/config.js

89 lines
2.5 KiB
JavaScript
Raw Normal View History

import Ember from 'ember';
import RSVP from 'rsvp';
import Service, {inject as service} from '@ember/service';
import timezoneData from '@tryghost/timezone-data';
import {computed} from '@ember/object';
// ember-cli-shims doesn't export _ProxyMixin
const {_ProxyMixin} = Ember;
export default Service.extend(_ProxyMixin, {
ajax: service(),
ghostPaths: service(),
session: service(),
Timezones: Always use the timezone of blog setting closes TryGhost/Ghost#6406 follow-up PR of #2 - adds a `timeZone` Service to provide the offset (=timezone reg. moment-timezone) of the users blog settings - `gh-datetime-input` will read the offset of the timezone now and adjust the `publishedAt` date with it. This is the date which will be shown in the PSM 'Publish Date' field. When the user writes a new date/time, the offset is considered and will be deducted again before saving it to the model. This way, we always work with a UTC publish date except for this input field. - gets `availableTimezones` from `configuration/timezones` API endpoint - adds a `moment-utc` transform on all date attr (`createdAt`, `updatedAt`, `publishedAt`, `unsubscribedAt` and `lastLogin`) to only work with UTC times on serverside - when switching the timezone in the select box, the user will be shown the local time of the selected timezone - `createdAt`-property in `gh-user-invited` returns now `moment(createdAt).fromNow()` as `createdAt` is a moment date already - added clock service to show actual time ticking below select box - default timezone is '(GMT) Greenwich Mean Time : Dublin, Edinburgh, London' - if no timezone is saved in the settings yet, the default value will be used - shows the local time in 'Publish Date' in PSM by default, until user overwrites it - adds dependency `moment-timezone 0.5.4` to `bower.json` --------- **Tests:** - sets except for clock service in test env - adds fixtures to mirage - adds `service.ajax` and `service:ghostPaths` to navigation-test.js - adds unit test for `gh-format-timeago` helper - updates acceptance test `general-setting` - adds acceptance test for `editor` - adds integration tests for `services/config` and `services/time-zone` --------- **Todos:** - [ ] Integration tests: ~~`services/config`~~, ~~`services/time-zone`~~, `components/gh-datetime-input` - [x] Acceptance test: `editor` - [ ] Unit tests: `utils/date-formatting` - [ ] write issue for renaming date properties (e. g. `createdAt` to `createdAtUTC`) and translate those for server side with serializers
2016-02-02 10:04:40 +03:00
content: null,
init() {
this._super(...arguments);
this.content = {};
},
fetch() {
let promises = [];
promises.push(this.fetchUnauthenticated());
if (this.session.isAuthenticated) {
promises.push(this.fetchAuthenticated());
}
return RSVP.all(promises);
},
fetchUnauthenticated() {
let siteUrl = this.ghostPaths.url.api('site');
return this.ajax.request(siteUrl).then(({site}) => {
// normalize url to non-trailing-slash
site.blogUrl = site.url.replace(/\/$/, '');
site.blogTitle = site.title;
delete site.url;
delete site.title;
Object.assign(this.content, site);
}).then(() => {
this.notifyPropertyChange('content');
});
},
fetchAuthenticated() {
let configUrl = this.ghostPaths.url.api('config');
return this.ajax.request(configUrl).then(({config}) => {
Object.assign(this.content, config);
}).then(() => {
this.notifyPropertyChange('content');
});
},
availableTimezones: computed(function () {
return RSVP.resolve(timezoneData);
}),
blogDomain: computed('blogUrl', function () {
let blogUrl = this.get('blogUrl');
let blogDomain = blogUrl
.replace(/^https?:\/\//, '')
.replace(/\/?$/, '');
return blogDomain;
}),
emailDomain: computed('blogDomain', function () {
let blogDomain = this.blogDomain || '';
const domainExp = blogDomain.match(new RegExp('^([^/:?#]+)(?:[/:?#]|$)', 'i'));
const domain = (domainExp && domainExp[1]) || '';
if (domain.startsWith('www.')) {
return domain.replace(/^(www)\.(?=[^/]*\..{2,5})/, '');
}
return domain;
}),
getSiteUrl(path) {
const siteUrl = new URL(this.get('blogUrl'));
const subdir = siteUrl.pathname.endsWith('/') ? siteUrl.pathname : `${siteUrl.pathname}/`;
const fullPath = `${subdir}${path.replace(/^\//, '')}`;
return `${siteUrl.origin}${fullPath}`;
}
});