2021-01-14 05:55:55 +03:00
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const semver = require('semver');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const ObjectId = require('bson-objectid');
|
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
class Notifications {
|
2021-03-23 05:22:14 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Object} options.settingsCache - settings cache instance
|
|
|
|
* @param {Object} options.i18n - i18n instance
|
|
|
|
* @param {Object} options.ghostVersion
|
|
|
|
* @param {String} options.ghostVersion.full - Ghost instance version in "full" format - major.minor.patch
|
|
|
|
*/
|
2021-01-14 06:19:15 +03:00
|
|
|
constructor({settingsCache, i18n, ghostVersion}) {
|
|
|
|
this.settingsCache = settingsCache;
|
|
|
|
this.i18n = i18n;
|
|
|
|
this.ghostVersion = ghostVersion;
|
|
|
|
}
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
fetchAllNotifications() {
|
|
|
|
let allNotifications = this.settingsCache.get('notifications');
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
allNotifications.forEach((notification) => {
|
|
|
|
notification.addedAt = moment(notification.addedAt).toDate();
|
|
|
|
});
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
return allNotifications;
|
2021-01-14 05:55:55 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
wasSeen(notification, user) {
|
|
|
|
if (notification.seenBy === undefined) {
|
|
|
|
return notification.seen;
|
|
|
|
} else {
|
|
|
|
return notification.seenBy.includes(user.id);
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
browse({user}) {
|
|
|
|
let allNotifications = this.fetchAllNotifications();
|
|
|
|
allNotifications = _.orderBy(allNotifications, 'addedAt', 'desc');
|
|
|
|
|
|
|
|
allNotifications = allNotifications.filter((notification) => {
|
|
|
|
// 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.
|
2021-03-23 06:42:46 +03:00
|
|
|
const ghostMajorRegEx = /Ghost (?<major>\d).0 is now available/gi;
|
2021-01-14 06:19:15 +03:00
|
|
|
|
|
|
|
// CASE: do not return old release notification
|
2021-03-23 06:42:46 +03:00
|
|
|
if (notification.message && (!notification.custom || notification.message.match(ghostMajorRegEx))) {
|
2021-01-14 06:19:15 +03:00
|
|
|
let notificationVersion = notification.message.match(/(\d+\.)(\d+\.)(\d+)/);
|
|
|
|
|
2021-03-23 06:42:46 +03:00
|
|
|
const ghostMajorMatch = ghostMajorRegEx.exec(notification.message);
|
2021-03-23 08:02:52 +03:00
|
|
|
if (ghostMajorMatch && ghostMajorMatch.groups && ghostMajorMatch.groups.major) {
|
2021-03-23 06:42:46 +03:00
|
|
|
notificationVersion = `${ghostMajorMatch.groups.major}.0.0`;
|
2021-01-14 06:19:15 +03:00
|
|
|
} else if (notificationVersion){
|
|
|
|
notificationVersion = notificationVersion[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
const blogVersion = this.ghostVersion.full.match(/^(\d+\.)(\d+\.)(\d+)/);
|
|
|
|
|
|
|
|
if (notificationVersion && blogVersion && semver.gt(notificationVersion, blogVersion[0])) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-14 05:55:55 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
return !this.wasSeen(notification, user);
|
|
|
|
});
|
|
|
|
|
|
|
|
return allNotifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
add({notifications}) {
|
|
|
|
const defaults = {
|
|
|
|
dismissible: true,
|
|
|
|
location: 'bottom',
|
|
|
|
status: 'alert',
|
2021-04-21 18:02:02 +03:00
|
|
|
id: ObjectId().toHexString()
|
2021-01-14 06:19:15 +03:00
|
|
|
};
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
const overrides = {
|
|
|
|
seen: false,
|
|
|
|
addedAt: moment().toDate()
|
|
|
|
};
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
let notificationsToCheck = notifications;
|
|
|
|
let notificationsToAdd = [];
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
const allNotifications = this.fetchAllNotifications();
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
notificationsToCheck.forEach((notification) => {
|
|
|
|
const isDuplicate = allNotifications.find((n) => {
|
|
|
|
return n.id === notification.id;
|
|
|
|
});
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
if (!isDuplicate) {
|
|
|
|
notificationsToAdd.push(Object.assign({}, defaults, notification, overrides));
|
|
|
|
}
|
|
|
|
});
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
const hasReleaseNotification = notificationsToCheck.find((notification) => {
|
|
|
|
return !notification.custom;
|
2021-01-14 05:55:55 +03:00
|
|
|
});
|
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
// CASE: remove any existing release notifications if a new release notification comes in
|
|
|
|
if (hasReleaseNotification) {
|
|
|
|
_.remove(allNotifications, (el) => {
|
|
|
|
return !el.custom;
|
|
|
|
});
|
2021-01-14 05:55:55 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
// CASE: nothing to add, skip
|
|
|
|
if (!notificationsToAdd.length) {
|
2021-01-14 08:30:09 +03:00
|
|
|
return {allNotifications, notificationsToAdd};
|
2021-01-14 06:19:15 +03:00
|
|
|
}
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
const releaseNotificationsToAdd = notificationsToAdd.filter((notification) => {
|
|
|
|
return !notification.custom;
|
2021-01-14 05:55:55 +03:00
|
|
|
});
|
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
// CASE: reorder notifications before save
|
|
|
|
if (releaseNotificationsToAdd.length > 1) {
|
|
|
|
notificationsToAdd = notificationsToAdd.filter((notification) => {
|
|
|
|
return notification.custom;
|
|
|
|
});
|
|
|
|
notificationsToAdd.push(_.orderBy(releaseNotificationsToAdd, 'created_at', 'desc')[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {allNotifications, notificationsToAdd};
|
2021-01-14 05:55:55 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
destroy({notificationId, user}) {
|
|
|
|
const allNotifications = this.fetchAllNotifications();
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
const notificationToMarkAsSeen = allNotifications.find((notification) => {
|
|
|
|
return notification.id === notificationId;
|
2021-01-14 05:55:55 +03:00
|
|
|
});
|
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
const notificationToMarkAsSeenIndex = allNotifications.findIndex((notification) => {
|
|
|
|
return notification.id === notificationId;
|
|
|
|
});
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
if (notificationToMarkAsSeenIndex > -1 && !notificationToMarkAsSeen.dismissible) {
|
|
|
|
return Promise.reject(new errors.NoPermissionError({
|
|
|
|
message: this.i18n.t('errors.api.notifications.noPermissionToDismissNotif')
|
|
|
|
}));
|
|
|
|
}
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
if (notificationToMarkAsSeenIndex < 0) {
|
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: this.i18n.t('errors.api.notifications.notificationDoesNotExist')
|
|
|
|
}));
|
|
|
|
}
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
if (this.wasSeen(notificationToMarkAsSeen, user)) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
// @NOTE: We don't remove the notifications, because otherwise we will receive them again from the service.
|
|
|
|
allNotifications[notificationToMarkAsSeenIndex].seen = true;
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
if (!allNotifications[notificationToMarkAsSeenIndex].seenBy) {
|
|
|
|
allNotifications[notificationToMarkAsSeenIndex].seenBy = [];
|
|
|
|
}
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
allNotifications[notificationToMarkAsSeenIndex].seenBy.push(user.id);
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
return allNotifications;
|
2021-01-14 05:55:55 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
destroyAll() {
|
|
|
|
const allNotifications = this.fetchAllNotifications();
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
allNotifications.forEach((notification) => {
|
|
|
|
// @NOTE: We don't remove the notifications, because otherwise we will receive them again from the service.
|
|
|
|
notification.seen = true;
|
|
|
|
});
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
return allNotifications;
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 05:55:55 +03:00
|
|
|
|
2021-01-14 06:19:15 +03:00
|
|
|
module.exports = Notifications;
|