2014-06-03 17:05:25 +04:00
|
|
|
// # Notifications API
|
|
|
|
// RESTful API for creating notifications
|
2014-08-17 10:17:23 +04:00
|
|
|
var Promise = require('bluebird'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2015-08-18 16:08:52 +03:00
|
|
|
permissions = require('../permissions'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors'),
|
2014-06-16 09:24:19 +04:00
|
|
|
utils = require('./utils'),
|
2015-08-10 07:42:10 +03:00
|
|
|
pipeline = require('../utils/pipeline'),
|
2015-08-18 16:08:52 +03:00
|
|
|
canThis = permissions.canThis,
|
2014-05-09 14:11:29 +04:00
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
// Holds the persistent notifications
|
|
|
|
notificationsStore = [],
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// Holds the last used id
|
2014-04-29 00:58:18 +04:00
|
|
|
notificationCounter = 0,
|
2013-12-06 12:51:35 +04:00
|
|
|
notifications;
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ## Notification API Methods
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
notifications = {
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Browse
|
|
|
|
* Fetch all notifications
|
|
|
|
* @returns {Promise(Notifications)}
|
|
|
|
*/
|
2014-07-17 12:48:39 +04:00
|
|
|
browse: function browse(options) {
|
|
|
|
return canThis(options.context).browse.notification().then(function () {
|
2014-09-10 08:06:24 +04:00
|
|
|
return {notifications: notificationsStore};
|
2014-07-17 12:48:39 +04:00
|
|
|
}, function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.NoPermissionError('You do not have permission to browse notifications.'));
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Add
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* **takes:** a notification object of the form
|
|
|
|
* ```
|
2014-06-16 09:24:19 +04:00
|
|
|
* msg = { notifications: [{
|
2014-07-17 12:48:39 +04:00
|
|
|
* type: 'error', // this can be 'error', 'success', 'warn' and 'info'
|
|
|
|
* message: 'This is an error', // A string. Should fit in one line.
|
|
|
|
* location: 'bottom', // A string where this notification should appear. can be 'bottom' or 'top'
|
|
|
|
* dismissible: true // A Boolean. Whether the notification is dismissible or not.
|
|
|
|
* }] };
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* ```
|
|
|
|
*/
|
2014-07-17 12:48:39 +04:00
|
|
|
add: function add(object, options) {
|
2015-08-10 07:42:10 +03:00
|
|
|
var tasks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Handle Permissions
|
|
|
|
* We need to be an authorised user to perform this action
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function handlePermissions(options) {
|
2015-08-18 16:08:52 +03:00
|
|
|
if (permissions.parseContext(options.context).internal) {
|
|
|
|
return Promise.resolve(options);
|
|
|
|
}
|
|
|
|
|
2015-08-10 07:42:10 +03:00
|
|
|
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.'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Save Notifications
|
|
|
|
* Save the notifications
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function saveNotifications(options) {
|
|
|
|
var defaults = {
|
|
|
|
dismissible: true,
|
|
|
|
location: 'bottom',
|
|
|
|
status: 'alert'
|
|
|
|
},
|
|
|
|
addedNotifications = [];
|
|
|
|
|
|
|
|
_.each(options.data.notifications, function (notification) {
|
|
|
|
notificationCounter = notificationCounter + 1;
|
|
|
|
|
|
|
|
notification = _.assign(defaults, notification, {
|
|
|
|
id: notificationCounter
|
|
|
|
// status: 'alert'
|
2014-06-16 09:24:19 +04:00
|
|
|
});
|
2014-04-29 00:58:18 +04:00
|
|
|
|
2015-08-10 07:42:10 +03:00
|
|
|
notificationsStore.push(notification);
|
|
|
|
addedNotifications.push(notification);
|
2014-07-17 12:48:39 +04:00
|
|
|
});
|
2015-08-10 07:42:10 +03:00
|
|
|
|
|
|
|
return addedNotifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
utils.validate('notifications'),
|
|
|
|
handlePermissions,
|
|
|
|
saveNotifications
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, object, options).then(function formatResponse(result) {
|
|
|
|
return {notifications: result};
|
2014-07-17 12:48:39 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Destroy
|
|
|
|
* Remove a specific notification
|
|
|
|
*
|
|
|
|
* @param {{id (required), context}} options
|
|
|
|
* @returns {Promise(Notifications)}
|
|
|
|
*/
|
|
|
|
destroy: function destroy(options) {
|
2015-08-10 07:42:10 +03:00
|
|
|
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) {
|
2014-07-17 12:48:39 +04:00
|
|
|
var notification = _.find(notificationsStore, function (element) {
|
|
|
|
return element.id === parseInt(options.id, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (notification && !notification.dismissible) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(
|
2014-07-17 12:48:39 +04:00
|
|
|
new errors.NoPermissionError('You do not have permission to dismiss this notification.')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!notification) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.NotFoundError('Notification does not exist.'));
|
2014-07-17 12:48:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
notificationsStore = _.reject(notificationsStore, function (element) {
|
|
|
|
return element.id === parseInt(options.id, 10);
|
2014-06-16 09:24:19 +04:00
|
|
|
});
|
2015-08-10 07:42:10 +03:00
|
|
|
notificationCounter = notificationCounter - 1;
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
utils.validate('notifications', {opts: utils.idDefaultOptions}),
|
|
|
|
handlePermissions,
|
|
|
|
destroyNotification
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options).then(function formatResponse(result) {
|
|
|
|
return {notifications: [result]};
|
2014-07-17 12:48:39 +04:00
|
|
|
});
|
|
|
|
},
|
2014-06-16 09:24:19 +04:00
|
|
|
|
2014-07-17 12:48:39 +04:00
|
|
|
/**
|
|
|
|
* ### DestroyAll
|
|
|
|
* Clear all notifications, used for tests
|
|
|
|
*
|
|
|
|
* @private Not exposed over HTTP
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
destroyAll: function destroyAll(options) {
|
|
|
|
return canThis(options.context).destroy.notification().then(function () {
|
|
|
|
notificationsStore = [];
|
|
|
|
notificationCounter = 0;
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
return notificationsStore;
|
2014-07-17 12:48:39 +04:00
|
|
|
}, function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.NoPermissionError('You do not have permission to destroy notifications.'));
|
2014-06-16 09:24:19 +04:00
|
|
|
});
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-30 01:45:03 +04:00
|
|
|
module.exports = notifications;
|