mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
c02ebb0dcf
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
81 lines
No EOL
2.4 KiB
JavaScript
81 lines
No EOL
2.4 KiB
JavaScript
var when = require('when'),
|
|
_ = require('lodash'),
|
|
errors = require('../errors'),
|
|
|
|
// Holds the persistent notifications
|
|
notificationsStore = [],
|
|
// Holds the last used id
|
|
notificationCounter = 0,
|
|
notifications;
|
|
|
|
// ## Notifications
|
|
notifications = {
|
|
|
|
browse: function browse() {
|
|
return when({ 'notifications': notificationsStore });
|
|
},
|
|
|
|
destroy: function destroy(options) {
|
|
var notification = _.find(notificationsStore, function (element) {
|
|
return element.id === parseInt(options.id, 10);
|
|
});
|
|
|
|
if (notification && !notification.dismissable) {
|
|
return when.reject(
|
|
new errors.NoPermissionError('You do not have permission to dismiss this notification.')
|
|
);
|
|
}
|
|
|
|
if (!notification) {
|
|
return when.reject(new errors.NotFoundError('Notification does not exist.'));
|
|
}
|
|
|
|
notificationsStore = _.reject(notificationsStore, function (element) {
|
|
return element.id === parseInt(options.id, 10);
|
|
});
|
|
// **returns:** a promise for the deleted object
|
|
return when({notifications: [notification]});
|
|
},
|
|
|
|
destroyAll: function destroyAll() {
|
|
notificationsStore = [];
|
|
notificationCounter = 0;
|
|
return when(notificationsStore);
|
|
},
|
|
|
|
/**
|
|
* ### Add
|
|
*
|
|
*
|
|
* **takes:** a notification object of the form
|
|
* ```
|
|
* msg = {
|
|
* 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'
|
|
* dismissable: true // A Boolean. Whether the notification is dismissable or not.
|
|
* };
|
|
* ```
|
|
*/
|
|
add: function add(notification) {
|
|
|
|
var defaults = {
|
|
dismissable: true,
|
|
location: 'bottom',
|
|
status: 'persistent'
|
|
};
|
|
|
|
notificationCounter = notificationCounter + 1;
|
|
|
|
notification = _.assign(defaults, notification, {
|
|
id: notificationCounter
|
|
//status: 'persistent'
|
|
});
|
|
|
|
notificationsStore.push(notification);
|
|
// **returns:** a promise of the new notification object
|
|
return when({ notifications: [notification]});
|
|
}
|
|
};
|
|
|
|
module.exports = notifications; |