Improved duplication checking in notifications service

closes https://github.com/TryGhost/Ghost/issues/10514
- deduplicates alerts based on message content as well as key
This commit is contained in:
Kevin Ansfield 2019-03-12 17:50:45 +00:00
parent fc6574f0ed
commit 75245f0d3d
2 changed files with 21 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import Service, {inject as service} from '@ember/service';
import {dasherize} from '@ember/string';
import {dasherize, htmlSafe} from '@ember/string';
import {A as emberA, isArray as isEmberArray} from '@ember/array';
import {filter} from '@ember/object/computed';
import {get, set} from '@ember/object';
@ -43,7 +43,7 @@ export default Service.extend({
handleNotification(message, delayed) {
// If this is an alert message from the server, treat it as html safe
if (typeof message.toJSON === 'function' && message.get('status') === 'alert') {
message.set('message', message.get('message').htmlSafe());
message.set('message', htmlSafe(message.get('message')));
}
if (!get(message, 'status')) {
@ -55,6 +55,13 @@ export default Service.extend({
this._removeItems(get(message, 'status'), get(message, 'key'));
}
// close existing alerts/notifications which have the same text to avoid stacking
let newText = get(message, 'message').string || get(message, 'message');
this.set('content', this.content.reject((notification) => {
let existingText = get(notification, 'message').string || get(notification, 'message');
return existingText === newText;
}));
if (!delayed) {
this.content.pushObject(message);
} else {

View File

@ -83,7 +83,7 @@ describe('Unit: Service: notifications', function () {
// in order to cater for complex keys that are suitable for i18n
// we split on the second period and treat the resulting base as
// the key for duplicate checking
it('#showAlert clears duplicates', function () {
it('#showAlert clears duplicates using keys', function () {
let notifications = this.subject();
run(() => {
@ -101,6 +101,17 @@ describe('Unit: Service: notifications', function () {
expect(notifications.get('alerts.lastObject.message')).to.equal('Duplicate with new message');
});
it('#showAlert clears duplicates using message text', function () {
let notifications = this.subject();
notifications.showAlert('Not duplicate');
notifications.showAlert('Duplicate', {key: 'duplicate'});
notifications.showAlert('Duplicate');
expect(notifications.get('alerts.length')).to.equal(2);
expect(notifications.get('alerts.lastObject.key')).to.not.exist;
});
it('#showNotification adds POJO notifications', function () {
let notifications = this.subject();