mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Refactored notifications module into a class
refs #12537 - Classes with DI friendly constructors are the pattern that are followed across the codebase
This commit is contained in:
parent
d2f0f0d7bc
commit
935ffdd0f6
4 changed files with 154 additions and 146 deletions
|
@ -1,4 +1,4 @@
|
|||
const notifications = require('../../services/notifications/notifications');
|
||||
const {notifications} = require('../../services/notifications');
|
||||
const api = require('./index');
|
||||
const internalContext = {context: {internal: true}};
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const notifications = require('../../services/notifications/notifications');
|
||||
const {notifications} = require('../../services/notifications');
|
||||
const api = require('./index');
|
||||
const internalContext = {context: {internal: true}};
|
||||
|
||||
|
|
6
core/server/services/notifications/index.js
Normal file
6
core/server/services/notifications/index.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
const settingsCache = require('../settings/cache');
|
||||
const {i18n} = require('../../lib/common');
|
||||
const ghostVersion = require('../../lib/ghost-version');
|
||||
const Notifications = require('./notifications');
|
||||
|
||||
module.exports.notifications = new Notifications({settingsCache, i18n, ghostVersion});
|
|
@ -2,32 +2,36 @@ const moment = require('moment-timezone');
|
|||
const semver = require('semver');
|
||||
const Promise = require('bluebird');
|
||||
const _ = require('lodash');
|
||||
const settingsCache = require('../settings/cache');
|
||||
const ghostVersion = require('../../lib/ghost-version');
|
||||
const {i18n} = require('../../lib/common');
|
||||
const errors = require('@tryghost/errors');
|
||||
const ObjectId = require('bson-objectid');
|
||||
|
||||
const fetchAllNotifications = () => {
|
||||
let allNotifications = settingsCache.get('notifications');
|
||||
class Notifications {
|
||||
constructor({settingsCache, i18n, ghostVersion}) {
|
||||
this.settingsCache = settingsCache;
|
||||
this.i18n = i18n;
|
||||
this.ghostVersion = ghostVersion;
|
||||
}
|
||||
|
||||
fetchAllNotifications() {
|
||||
let allNotifications = this.settingsCache.get('notifications');
|
||||
|
||||
allNotifications.forEach((notification) => {
|
||||
notification.addedAt = moment(notification.addedAt).toDate();
|
||||
});
|
||||
|
||||
return allNotifications;
|
||||
};
|
||||
}
|
||||
|
||||
const wasSeen = (notification, user) => {
|
||||
wasSeen(notification, user) {
|
||||
if (notification.seenBy === undefined) {
|
||||
return notification.seen;
|
||||
} else {
|
||||
return notification.seenBy.includes(user.id);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const browse = ({user}) => {
|
||||
let allNotifications = fetchAllNotifications();
|
||||
browse({user}) {
|
||||
let allNotifications = this.fetchAllNotifications();
|
||||
allNotifications = _.orderBy(allNotifications, 'addedAt', 'desc');
|
||||
|
||||
allNotifications = allNotifications.filter((notification) => {
|
||||
|
@ -47,7 +51,7 @@ const browse = ({user}) => {
|
|||
notificationVersion = notificationVersion[0];
|
||||
}
|
||||
|
||||
const blogVersion = ghostVersion.full.match(/^(\d+\.)(\d+\.)(\d+)/);
|
||||
const blogVersion = this.ghostVersion.full.match(/^(\d+\.)(\d+\.)(\d+)/);
|
||||
|
||||
if (notificationVersion && blogVersion && semver.gt(notificationVersion, blogVersion[0])) {
|
||||
return true;
|
||||
|
@ -56,13 +60,13 @@ const browse = ({user}) => {
|
|||
}
|
||||
}
|
||||
|
||||
return !wasSeen(notification, user);
|
||||
return !this.wasSeen(notification, user);
|
||||
});
|
||||
|
||||
return allNotifications;
|
||||
};
|
||||
}
|
||||
|
||||
const add = ({notifications}) => {
|
||||
add({notifications}) {
|
||||
const defaults = {
|
||||
dismissible: true,
|
||||
location: 'bottom',
|
||||
|
@ -78,7 +82,7 @@ const add = ({notifications}) => {
|
|||
let notificationsToCheck = notifications;
|
||||
let notificationsToAdd = [];
|
||||
|
||||
const allNotifications = fetchAllNotifications();
|
||||
const allNotifications = this.fetchAllNotifications();
|
||||
|
||||
notificationsToCheck.forEach((notification) => {
|
||||
const isDuplicate = allNotifications.find((n) => {
|
||||
|
@ -119,10 +123,10 @@ const add = ({notifications}) => {
|
|||
}
|
||||
|
||||
return {allNotifications, notificationsToAdd};
|
||||
};
|
||||
}
|
||||
|
||||
const destroy = ({notificationId, user}) => {
|
||||
const allNotifications = fetchAllNotifications();
|
||||
destroy({notificationId, user}) {
|
||||
const allNotifications = this.fetchAllNotifications();
|
||||
|
||||
const notificationToMarkAsSeen = allNotifications.find((notification) => {
|
||||
return notification.id === notificationId;
|
||||
|
@ -134,17 +138,17 @@ const destroy = ({notificationId, user}) => {
|
|||
|
||||
if (notificationToMarkAsSeenIndex > -1 && !notificationToMarkAsSeen.dismissible) {
|
||||
return Promise.reject(new errors.NoPermissionError({
|
||||
message: i18n.t('errors.api.notifications.noPermissionToDismissNotif')
|
||||
message: this.i18n.t('errors.api.notifications.noPermissionToDismissNotif')
|
||||
}));
|
||||
}
|
||||
|
||||
if (notificationToMarkAsSeenIndex < 0) {
|
||||
return Promise.reject(new errors.NotFoundError({
|
||||
message: i18n.t('errors.api.notifications.notificationDoesNotExist')
|
||||
message: this.i18n.t('errors.api.notifications.notificationDoesNotExist')
|
||||
}));
|
||||
}
|
||||
|
||||
if (wasSeen(notificationToMarkAsSeen, user)) {
|
||||
if (this.wasSeen(notificationToMarkAsSeen, user)) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
|
@ -158,10 +162,10 @@ const destroy = ({notificationId, user}) => {
|
|||
allNotifications[notificationToMarkAsSeenIndex].seenBy.push(user.id);
|
||||
|
||||
return allNotifications;
|
||||
};
|
||||
}
|
||||
|
||||
const destroyAll = () => {
|
||||
const allNotifications = fetchAllNotifications();
|
||||
destroyAll() {
|
||||
const allNotifications = this.fetchAllNotifications();
|
||||
|
||||
allNotifications.forEach((notification) => {
|
||||
// @NOTE: We don't remove the notifications, because otherwise we will receive them again from the service.
|
||||
|
@ -169,9 +173,7 @@ const destroyAll = () => {
|
|||
});
|
||||
|
||||
return allNotifications;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.browse = browse;
|
||||
module.exports.add = add;
|
||||
module.exports.destroy = destroy;
|
||||
module.exports.destroyAll = destroyAll;
|
||||
module.exports = Notifications;
|
||||
|
|
Loading…
Add table
Reference in a new issue