mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-13 22:41:32 -05:00
Merge pull request #5639 from acburdine/notifications-pipeline
Apply pipeline to notifications API endpoints
This commit is contained in:
commit
7d8bd3d8d4
1 changed files with 78 additions and 23 deletions
|
@ -5,6 +5,7 @@ var Promise = require('bluebird'),
|
||||||
canThis = require('../permissions').canThis,
|
canThis = require('../permissions').canThis,
|
||||||
errors = require('../errors'),
|
errors = require('../errors'),
|
||||||
utils = require('./utils'),
|
utils = require('./utils'),
|
||||||
|
pipeline = require('../utils/pipeline'),
|
||||||
|
|
||||||
// Holds the persistent notifications
|
// Holds the persistent notifications
|
||||||
notificationsStore = [],
|
notificationsStore = [],
|
||||||
|
@ -47,31 +48,59 @@ notifications = {
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
add: function add(object, options) {
|
add: function add(object, options) {
|
||||||
var defaults = {
|
var tasks;
|
||||||
dismissible: true,
|
|
||||||
location: 'bottom',
|
|
||||||
status: 'alert'
|
|
||||||
},
|
|
||||||
addedNotifications = [];
|
|
||||||
|
|
||||||
return canThis(options.context).add.notification().then(function () {
|
/**
|
||||||
return utils.checkObject(object, 'notifications').then(function (checkedNotificationData) {
|
* ### Handle Permissions
|
||||||
_.each(checkedNotificationData.notifications, function (notification) {
|
* We need to be an authorised user to perform this action
|
||||||
notificationCounter = notificationCounter + 1;
|
* @param {Object} options
|
||||||
|
* @returns {Object} options
|
||||||
|
*/
|
||||||
|
function handlePermissions(options) {
|
||||||
|
return canThis(options.context).add.notification().then(function () {
|
||||||
|
return options;
|
||||||
|
}, function () {
|
||||||
|
return Promise.reject(new errors.NoPermissionError('You do not have permission to add notifications.'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
notification = _.assign(defaults, notification, {
|
/**
|
||||||
id: notificationCounter
|
* ### Save Notifications
|
||||||
// status: 'alert'
|
* Save the notifications
|
||||||
});
|
* @param {Object} options
|
||||||
|
* @returns {Object} options
|
||||||
|
*/
|
||||||
|
function saveNotifications(options) {
|
||||||
|
var defaults = {
|
||||||
|
dismissible: true,
|
||||||
|
location: 'bottom',
|
||||||
|
status: 'alert'
|
||||||
|
},
|
||||||
|
addedNotifications = [];
|
||||||
|
|
||||||
notificationsStore.push(notification);
|
_.each(options.data.notifications, function (notification) {
|
||||||
addedNotifications.push(notification);
|
notificationCounter = notificationCounter + 1;
|
||||||
|
|
||||||
|
notification = _.assign(defaults, notification, {
|
||||||
|
id: notificationCounter
|
||||||
|
// status: 'alert'
|
||||||
});
|
});
|
||||||
|
|
||||||
return {notifications: addedNotifications};
|
notificationsStore.push(notification);
|
||||||
|
addedNotifications.push(notification);
|
||||||
});
|
});
|
||||||
}, function () {
|
|
||||||
return Promise.reject(new errors.NoPermissionError('You do not have permission to add notifications.'));
|
return addedNotifications;
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks = [
|
||||||
|
utils.validate('notifications'),
|
||||||
|
handlePermissions,
|
||||||
|
saveNotifications
|
||||||
|
];
|
||||||
|
|
||||||
|
return pipeline(tasks, object, options).then(function formatResponse(result) {
|
||||||
|
return {notifications: result};
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -83,7 +112,23 @@ notifications = {
|
||||||
* @returns {Promise(Notifications)}
|
* @returns {Promise(Notifications)}
|
||||||
*/
|
*/
|
||||||
destroy: function destroy(options) {
|
destroy: function destroy(options) {
|
||||||
return canThis(options.context).destroy.notification().then(function () {
|
var tasks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ### Handle Permissions
|
||||||
|
* We need to be an authorised user to perform this action
|
||||||
|
* @param {Object} options
|
||||||
|
* @returns {Object} options
|
||||||
|
*/
|
||||||
|
function handlePermissions(options) {
|
||||||
|
return canThis(options.context).destroy.notification().then(function () {
|
||||||
|
return options;
|
||||||
|
}, function () {
|
||||||
|
return Promise.reject(new errors.NoPermissionError('You do not have permission to destroy notifications.'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function destroyNotification(options) {
|
||||||
var notification = _.find(notificationsStore, function (element) {
|
var notification = _.find(notificationsStore, function (element) {
|
||||||
return element.id === parseInt(options.id, 10);
|
return element.id === parseInt(options.id, 10);
|
||||||
});
|
});
|
||||||
|
@ -101,9 +146,19 @@ notifications = {
|
||||||
notificationsStore = _.reject(notificationsStore, function (element) {
|
notificationsStore = _.reject(notificationsStore, function (element) {
|
||||||
return element.id === parseInt(options.id, 10);
|
return element.id === parseInt(options.id, 10);
|
||||||
});
|
});
|
||||||
return {notifications: [notification]};
|
notificationCounter = notificationCounter - 1;
|
||||||
}, function () {
|
|
||||||
return Promise.reject(new errors.NoPermissionError('You do not have permission to destroy notifications.'));
|
return notification;
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks = [
|
||||||
|
utils.validate('notifications', {opts: utils.idDefaultOptions}),
|
||||||
|
handlePermissions,
|
||||||
|
destroyNotification
|
||||||
|
];
|
||||||
|
|
||||||
|
return pipeline(tasks, options).then(function formatResponse(result) {
|
||||||
|
return {notifications: [result]};
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue