0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-15 03:01:37 -05:00

fix: notification store without duplicates (#7135)

closes #7133
- ensure we don't add duplicate notifications to the in process notification store
This commit is contained in:
Katharina Irrgang 2016-08-11 09:58:51 +02:00 committed by Hannah Wolfe
parent dd5775c018
commit 9cd9e03fdb
3 changed files with 51 additions and 7 deletions

View file

@ -41,6 +41,9 @@ notifications = {
*
*
* **takes:** a notification object of the form
*
* If notification message already exists, we return the existing notification object.
*
* ```
* msg = { notifications: [{
* type: 'error', // this can be 'error', 'success', 'warn' and 'info'
@ -84,18 +87,23 @@ notifications = {
location: 'bottom',
status: 'alert'
},
addedNotifications = [];
addedNotifications = [], existingNotification;
_.each(options.data.notifications, function (notification) {
notificationCounter = notificationCounter + 1;
notification = _.assign(defaults, notification, {
id: notificationCounter
// status: 'alert'
});
notificationsStore.push(notification);
addedNotifications.push(notification);
existingNotification = _.find(notificationsStore, {message:notification.message});
if (!existingNotification) {
notificationsStore.push(notification);
addedNotifications.push(notification);
} else {
addedNotifications.push(existingNotification);
}
});
return addedNotifications;

View file

@ -39,7 +39,7 @@ describe('Notifications API', function () {
it('can add, adds defaults (owner)', function (done) {
var msg = {
type: 'info',
message: 'Hello, this is dog'
message: 'Hello, this is another dog'
};
NotificationsAPI.add({notifications: [msg]}, testUtils.context.owner).then(function (result) {
@ -60,7 +60,7 @@ describe('Notifications API', function () {
it('can add, adds id and status (internal)', function (done) {
var msg = {
type: 'info',
message: 'Hello, this is dog',
message: 'Hello, this is dog number 3',
id: 99
};
@ -157,7 +157,7 @@ describe('Notifications API', function () {
custom: true,
uuid: uuid.v4(),
dismissible: true,
message: 'Hello, this is dog'
message: 'Hello, this is dog number 4'
};
NotificationsAPI.add({notifications: [customNotification]}, testUtils.context.internal).then(function (result) {

View file

@ -0,0 +1,36 @@
var rewire = require('rewire'),
/*jshint unused:false*/
should = require('should'),
NotificationAPI = rewire('../../../server/api/notifications');
describe('UNIT: Notification API', function () {
it('ensure non duplicates', function (done) {
var options = {context: {internal: true}},
notifications = [{
type: 'info',
message: 'Hello, this is dog'
}],
notificationStore = NotificationAPI.__get__('notificationsStore');
NotificationAPI.add({notifications: notifications}, options)
.then(function () {
notificationStore.length.should.eql(1);
return NotificationAPI.add({notifications: notifications}, options);
})
.then(function () {
notificationStore.length.should.eql(1);
notifications.push({
type: 'info',
message: 'Hello, this is cat'
});
return NotificationAPI.add({notifications: notifications}, options);
})
.then(function () {
notificationStore.length.should.eql(2);
done();
})
.catch(done);
});
});