2018-10-12 20:44:03 +03:00
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const semver = require('semver');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const settingsCache = require('../../services/settings/cache');
|
|
|
|
const ghostVersion = require('../../lib/ghost-version');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const errors = require('@tryghost/errors');
|
2018-10-12 20:44:03 +03:00
|
|
|
const ObjectId = require('bson-objectid');
|
|
|
|
const api = require('./index');
|
|
|
|
const internalContext = {context: {internal: true}};
|
|
|
|
const _private = {};
|
|
|
|
|
|
|
|
_private.fetchAllNotifications = () => {
|
|
|
|
let allNotifications = settingsCache.get('notifications');
|
|
|
|
|
|
|
|
allNotifications.forEach((notification) => {
|
|
|
|
notification.addedAt = moment(notification.addedAt).toDate();
|
|
|
|
});
|
|
|
|
|
|
|
|
return allNotifications;
|
|
|
|
};
|
|
|
|
|
2019-04-18 12:25:17 +03:00
|
|
|
_private.wasSeen = (notification, user) => {
|
|
|
|
if (notification.seenBy === undefined) {
|
|
|
|
return notification.seen;
|
|
|
|
} else {
|
|
|
|
return notification.seenBy.includes(user.id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-12 20:44:03 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'notifications',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
permissions: true,
|
2019-04-18 12:25:17 +03:00
|
|
|
query(frame) {
|
2018-10-12 20:44:03 +03:00
|
|
|
let allNotifications = _private.fetchAllNotifications();
|
|
|
|
allNotifications = _.orderBy(allNotifications, 'addedAt', 'desc');
|
|
|
|
|
|
|
|
allNotifications = allNotifications.filter((notification) => {
|
2019-04-17 15:48:09 +03:00
|
|
|
// NOTE: Filtering by version below is just a patch for bigger problem - notifications are not removed
|
|
|
|
// after Ghost update. Logic below should be removed when Ghost upgrade detection
|
|
|
|
// is done (https://github.com/TryGhost/Ghost/issues/10236) and notifications are
|
|
|
|
// be removed permanently on upgrade event.
|
2019-04-16 19:12:33 +03:00
|
|
|
const ghost20RegEx = /Ghost 2.0 is now available/gi;
|
|
|
|
|
2018-10-12 20:44:03 +03:00
|
|
|
// CASE: do not return old release notification
|
2019-04-16 19:12:33 +03:00
|
|
|
if (notification.message && (!notification.custom || notification.message.match(ghost20RegEx))) {
|
|
|
|
let notificationVersion = notification.message.match(/(\d+\.)(\d+\.)(\d+)/);
|
|
|
|
|
|
|
|
if (notification.message.match(ghost20RegEx)) {
|
|
|
|
notificationVersion = '2.0.0';
|
2019-04-17 15:48:09 +03:00
|
|
|
} else if (notificationVersion){
|
2019-04-16 19:12:33 +03:00
|
|
|
notificationVersion = notificationVersion[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
const blogVersion = ghostVersion.full.match(/^(\d+\.)(\d+\.)(\d+)/);
|
2018-10-12 20:44:03 +03:00
|
|
|
|
2019-04-16 19:12:33 +03:00
|
|
|
if (notificationVersion && blogVersion && semver.gt(notificationVersion, blogVersion[0])) {
|
2018-10-12 20:44:03 +03:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 12:25:17 +03:00
|
|
|
return !_private.wasSeen(notification, frame.user);
|
2018-10-12 20:44:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return allNotifications;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode(result) {
|
|
|
|
if (result.notifications.length) {
|
|
|
|
return 201;
|
|
|
|
} else {
|
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
const defaults = {
|
|
|
|
dismissible: true,
|
|
|
|
location: 'bottom',
|
|
|
|
status: 'alert',
|
|
|
|
id: ObjectId.generate()
|
|
|
|
};
|
|
|
|
|
|
|
|
const overrides = {
|
|
|
|
seen: false,
|
|
|
|
addedAt: moment().toDate()
|
|
|
|
};
|
|
|
|
|
|
|
|
let notificationsToCheck = frame.data.notifications;
|
2019-04-17 20:59:20 +03:00
|
|
|
let notificationsToAdd = [];
|
2018-10-12 20:44:03 +03:00
|
|
|
|
|
|
|
const allNotifications = _private.fetchAllNotifications();
|
|
|
|
|
|
|
|
notificationsToCheck.forEach((notification) => {
|
|
|
|
const isDuplicate = allNotifications.find((n) => {
|
|
|
|
return n.id === notification.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!isDuplicate) {
|
2019-04-17 20:59:20 +03:00
|
|
|
notificationsToAdd.push(Object.assign({}, defaults, notification, overrides));
|
2018-10-12 20:44:03 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const hasReleaseNotification = notificationsToCheck.find((notification) => {
|
|
|
|
return !notification.custom;
|
|
|
|
});
|
|
|
|
|
|
|
|
// CASE: remove any existing release notifications if a new release notification comes in
|
|
|
|
if (hasReleaseNotification) {
|
|
|
|
_.remove(allNotifications, (el) => {
|
|
|
|
return !el.custom;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: nothing to add, skip
|
2019-04-17 20:59:20 +03:00
|
|
|
if (!notificationsToAdd.length) {
|
2018-10-12 20:44:03 +03:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:59:20 +03:00
|
|
|
const releaseNotificationsToAdd = notificationsToAdd.filter((notification) => {
|
2018-10-12 20:44:03 +03:00
|
|
|
return !notification.custom;
|
|
|
|
});
|
|
|
|
|
2019-04-17 20:59:20 +03:00
|
|
|
// CASE: reorder notifications before save
|
|
|
|
if (releaseNotificationsToAdd.length > 1) {
|
|
|
|
notificationsToAdd = notificationsToAdd.filter((notification) => {
|
2018-10-12 20:44:03 +03:00
|
|
|
return notification.custom;
|
|
|
|
});
|
2019-04-17 20:59:20 +03:00
|
|
|
notificationsToAdd.push(_.orderBy(releaseNotificationsToAdd, 'created_at', 'desc')[0]);
|
2018-10-12 20:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return api.settings.edit({
|
|
|
|
settings: [{
|
|
|
|
key: 'notifications',
|
2019-04-17 20:59:20 +03:00
|
|
|
// @NOTE: We always need to store all notifications!
|
|
|
|
value: allNotifications.concat(notificationsToAdd)
|
2018-10-12 20:44:03 +03:00
|
|
|
}]
|
|
|
|
}, internalContext).then(() => {
|
2019-04-17 20:59:20 +03:00
|
|
|
return notificationsToAdd;
|
2018-10-12 20:44:03 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
options: ['notification_id'],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
notification_id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
const allNotifications = _private.fetchAllNotifications();
|
|
|
|
|
|
|
|
const notificationToMarkAsSeen = allNotifications.find((notification) => {
|
2020-04-29 18:44:27 +03:00
|
|
|
return notification.id === frame.options.notification_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
const notificationToMarkAsSeenIndex = allNotifications.findIndex((notification) => {
|
|
|
|
return notification.id === frame.options.notification_id;
|
|
|
|
});
|
2018-10-12 20:44:03 +03:00
|
|
|
|
|
|
|
if (notificationToMarkAsSeenIndex > -1 && !notificationToMarkAsSeen.dismissible) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NoPermissionError({
|
|
|
|
message: i18n.t('errors.api.notifications.noPermissionToDismissNotif')
|
2018-10-12 20:44:03 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notificationToMarkAsSeenIndex < 0) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.notifications.notificationDoesNotExist')
|
2018-10-12 20:44:03 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-04-18 12:25:17 +03:00
|
|
|
if (_private.wasSeen(notificationToMarkAsSeen, frame.user)) {
|
2018-10-12 20:44:03 +03:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:59:20 +03:00
|
|
|
// @NOTE: We don't remove the notifications, because otherwise we will receive them again from the service.
|
2018-10-12 20:44:03 +03:00
|
|
|
allNotifications[notificationToMarkAsSeenIndex].seen = true;
|
|
|
|
|
2019-04-18 12:25:17 +03:00
|
|
|
if (!allNotifications[notificationToMarkAsSeenIndex].seenBy) {
|
|
|
|
allNotifications[notificationToMarkAsSeenIndex].seenBy = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
allNotifications[notificationToMarkAsSeenIndex].seenBy.push(frame.user.id);
|
|
|
|
|
2018-10-12 20:44:03 +03:00
|
|
|
return api.settings.edit({
|
|
|
|
settings: [{
|
|
|
|
key: 'notifications',
|
|
|
|
value: allNotifications
|
|
|
|
}]
|
|
|
|
}, internalContext).return();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-04-18 12:25:17 +03:00
|
|
|
/**
|
|
|
|
* Clears all notifications. Method used in tests only
|
|
|
|
*
|
|
|
|
* @private Not exposed over HTTP
|
|
|
|
*/
|
2018-10-12 20:44:03 +03:00
|
|
|
destroyAll: {
|
|
|
|
statusCode: 204,
|
|
|
|
permissions: {
|
|
|
|
method: 'destroy'
|
|
|
|
},
|
|
|
|
query() {
|
|
|
|
const allNotifications = _private.fetchAllNotifications();
|
|
|
|
|
|
|
|
allNotifications.forEach((notification) => {
|
2019-04-17 20:59:20 +03:00
|
|
|
// @NOTE: We don't remove the notifications, because otherwise we will receive them again from the service.
|
2018-10-12 20:44:03 +03:00
|
|
|
notification.seen = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return api.settings.edit({
|
|
|
|
settings: [{
|
|
|
|
key: 'notifications',
|
|
|
|
value: allNotifications
|
|
|
|
}]
|
|
|
|
}, internalContext).return();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|