2022-01-21 22:25:47 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2019-08-23 12:01:27 +03:00
|
|
|
import fetch from 'fetch';
|
2022-09-23 20:15:08 +03:00
|
|
|
import moment from 'moment-timezone';
|
2022-01-21 22:25:47 +03:00
|
|
|
import {action, computed} from '@ember/object';
|
2019-08-23 12:01:27 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
|
|
|
|
export default Service.extend({
|
|
|
|
session: service(),
|
2024-05-21 13:36:28 +03:00
|
|
|
store: service(),
|
|
|
|
response: null,
|
2019-08-23 12:01:27 +03:00
|
|
|
|
|
|
|
entries: null,
|
|
|
|
changelogUrl: 'https://ghost.org/blog/',
|
|
|
|
isShowingModal: false,
|
|
|
|
|
|
|
|
_user: null,
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.entries = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
whatsNewSettings: computed('_user.accessibility', function () {
|
|
|
|
let settingsJson = this.get('_user.accessibility') || '{}';
|
|
|
|
let settings = JSON.parse(settingsJson);
|
|
|
|
return settings.whatsNew;
|
|
|
|
}),
|
|
|
|
|
|
|
|
hasNew: computed('whatsNewSettings.lastSeenDate', 'entries.[]', function () {
|
|
|
|
if (isEmpty(this.entries)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let [latestEntry] = this.entries;
|
|
|
|
|
|
|
|
let lastSeenDate = this.get('whatsNewSettings.lastSeenDate') || '2019-01-01 00:00:00';
|
|
|
|
let lastSeenMoment = moment(lastSeenDate);
|
|
|
|
let latestDate = latestEntry.published_at;
|
|
|
|
let latestMoment = moment(latestDate || lastSeenDate);
|
|
|
|
return latestMoment.isAfter(lastSeenMoment);
|
|
|
|
}),
|
|
|
|
|
2024-05-21 13:36:28 +03:00
|
|
|
hasNewFeatured: computed('entries.[]', function () {
|
|
|
|
if (!this.hasNew) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let [latestEntry] = this.entries;
|
|
|
|
return latestEntry.featured;
|
|
|
|
}),
|
|
|
|
|
|
|
|
seen: action(function () {
|
|
|
|
this.updateLastSeen.perform();
|
|
|
|
}),
|
|
|
|
|
|
|
|
openFeaturedModal: action(function () {
|
2019-08-23 12:01:27 +03:00
|
|
|
this.set('isShowingModal', true);
|
|
|
|
}),
|
|
|
|
|
2024-05-21 13:36:28 +03:00
|
|
|
closeFeaturedModal: action(function () {
|
2019-08-23 12:01:27 +03:00
|
|
|
this.set('isShowingModal', false);
|
2024-05-21 13:36:28 +03:00
|
|
|
this.seen();
|
2019-08-23 12:01:27 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
fetchLatest: task(function* () {
|
|
|
|
try {
|
2024-05-21 13:36:28 +03:00
|
|
|
if (!this.response) {
|
|
|
|
// we should already be logged in at this point so lets grab the user
|
|
|
|
// record and store it locally so that we don't have to deal with
|
|
|
|
// session.user being a promise and causing issues with CPs
|
|
|
|
let user = yield this.session.user;
|
|
|
|
this.set('_user', user);
|
|
|
|
|
|
|
|
this.response = yield fetch('https://ghost.org/changelog.json');
|
|
|
|
if (!this.response.ok) {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
return console.error('Failed to fetch changelog', {response});
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = yield this.response.json();
|
|
|
|
this.set('entries', result.posts || []);
|
|
|
|
this.set('changelogUrl', result.changelogUrl);
|
2019-08-23 12:01:27 +03:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e); // eslint-disable-line
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
updateLastSeen: task(function* () {
|
|
|
|
let settingsJson = this._user.accessibility || '{}';
|
|
|
|
let settings = JSON.parse(settingsJson);
|
|
|
|
let [latestEntry] = this.entries;
|
|
|
|
|
|
|
|
if (!latestEntry) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!settings.whatsNew) {
|
|
|
|
settings.whatsNew = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.whatsNew.lastSeenDate = latestEntry.published_at;
|
|
|
|
|
|
|
|
this._user.set('accessibility', JSON.stringify(settings));
|
|
|
|
yield this._user.save();
|
|
|
|
})
|
|
|
|
});
|