mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-24 23:48:13 -05:00
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:
parent
fc6574f0ed
commit
75245f0d3d
2 changed files with 21 additions and 3 deletions
|
@ -1,5 +1,5 @@
|
||||||
import Service, {inject as service} from '@ember/service';
|
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 {A as emberA, isArray as isEmberArray} from '@ember/array';
|
||||||
import {filter} from '@ember/object/computed';
|
import {filter} from '@ember/object/computed';
|
||||||
import {get, set} from '@ember/object';
|
import {get, set} from '@ember/object';
|
||||||
|
@ -43,7 +43,7 @@ export default Service.extend({
|
||||||
handleNotification(message, delayed) {
|
handleNotification(message, delayed) {
|
||||||
// If this is an alert message from the server, treat it as html safe
|
// If this is an alert message from the server, treat it as html safe
|
||||||
if (typeof message.toJSON === 'function' && message.get('status') === 'alert') {
|
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')) {
|
if (!get(message, 'status')) {
|
||||||
|
@ -55,6 +55,13 @@ export default Service.extend({
|
||||||
this._removeItems(get(message, 'status'), get(message, 'key'));
|
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) {
|
if (!delayed) {
|
||||||
this.content.pushObject(message);
|
this.content.pushObject(message);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -83,7 +83,7 @@ describe('Unit: Service: notifications', function () {
|
||||||
// in order to cater for complex keys that are suitable for i18n
|
// in order to cater for complex keys that are suitable for i18n
|
||||||
// we split on the second period and treat the resulting base as
|
// we split on the second period and treat the resulting base as
|
||||||
// the key for duplicate checking
|
// the key for duplicate checking
|
||||||
it('#showAlert clears duplicates', function () {
|
it('#showAlert clears duplicates using keys', function () {
|
||||||
let notifications = this.subject();
|
let notifications = this.subject();
|
||||||
|
|
||||||
run(() => {
|
run(() => {
|
||||||
|
@ -101,6 +101,17 @@ describe('Unit: Service: notifications', function () {
|
||||||
expect(notifications.get('alerts.lastObject.message')).to.equal('Duplicate with new message');
|
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 () {
|
it('#showNotification adds POJO notifications', function () {
|
||||||
let notifications = this.subject();
|
let notifications = this.subject();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue