0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Complete moveover to new Notification API format

fixes #2775
- Fix all occurences of notifications.add to use proper API format
This commit is contained in:
Fabian Becker 2014-06-16 05:24:19 +00:00
parent 4227508c20
commit 2bff35bcc2
7 changed files with 48 additions and 39 deletions

View file

@ -3,6 +3,7 @@
var when = require('when'), var when = require('when'),
_ = require('lodash'), _ = require('lodash'),
errors = require('../errors'), errors = require('../errors'),
utils = require('./utils'),
// Holds the persistent notifications // Holds the persistent notifications
notificationsStore = [], notificationsStore = [],
@ -73,31 +74,39 @@ notifications = {
* *
* **takes:** a notification object of the form * **takes:** a notification object of the form
* ``` * ```
* msg = { * msg = { notifications: [{
* type: 'error', // this can be 'error', 'success', 'warn' and 'info' * type: 'error', // this can be 'error', 'success', 'warn' and 'info'
* message: 'This is an error', // A string. Should fit in one line. * 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' * 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. * dismissable: true // A Boolean. Whether the notification is dismissable or not.
* }; * }] };
* ``` * ```
*/ */
add: function add(notification) { add: function add(object) {
var defaults = { var defaults = {
dismissable: true, dismissable: true,
location: 'bottom', location: 'bottom',
status: 'persistent' status: 'persistent'
}; },
addedNotifications = [];
notificationCounter = notificationCounter + 1;
notification = _.assign(defaults, notification, { return utils.checkObject(object, 'notifications').then(function (checkedNotificationData) {
id: notificationCounter _.each(checkedNotificationData.notifications, function (notification) {
//status: 'persistent' notificationCounter = notificationCounter + 1;
notification = _.assign(defaults, notification, {
id: notificationCounter
//status: 'persistent'
});
notificationsStore.push(notification);
addedNotifications.push(notification);
});
return when({ notifications: addedNotifications});
}); });
notificationsStore.push(notification);
return when({ notifications: [notification]});
} }
}; };

View file

@ -149,7 +149,7 @@ adminControllers = {
errors.logError(err, 'admin.js', "Your export file could not be generated."); errors.logError(err, 'admin.js', "Your export file could not be generated.");
return api.notifications.add(notification).then(function () { return api.notifications.add({ notifications: [notification]}).then(function () {
res.redirect(config().paths.subdir + '/ghost/debug'); res.redirect(config().paths.subdir + '/ghost/debug');
}); });
}); });
@ -190,7 +190,7 @@ adminControllers = {
status: 'passive' status: 'passive'
}; };
return api.notifications.add(notification).then(function () { return api.notifications.add({ notifications: [notification] }).then(function () {
res.redirect(config().paths.subdir + '/ghost/signin/'); res.redirect(config().paths.subdir + '/ghost/signin/');
}); });
}, },
@ -220,7 +220,7 @@ adminControllers = {
errorMessage = 'There was a problem logging out. Please try again.'; errorMessage = 'There was a problem logging out. Please try again.';
} }
return api.notifications.add(notification).then(function () { return api.notifications.add({ notifications: [notification] }).then(function () {
res.json(statusCode, {error: errorMessage, redirect: redirectUrl}); res.json(statusCode, {error: errorMessage, redirect: redirectUrl});
}); });
@ -408,7 +408,7 @@ adminControllers = {
status: 'passive' status: 'passive'
}; };
return api.notifications.add(notification).then(function () { return api.notifications.add({ notifications: [notification] }).then(function () {
res.json(200, {redirect: config().paths.subdir + '/ghost/signin/'}); res.json(200, {redirect: config().paths.subdir + '/ghost/signin/'});
}); });
@ -444,7 +444,7 @@ adminControllers = {
errors.logError(err, 'admin.js', "Please check the provided token for validity and expiration."); errors.logError(err, 'admin.js', "Please check the provided token for validity and expiration.");
return api.notifications.add(notification).then(function () { return api.notifications.add({ notifications: [notification] }).then(function () {
res.redirect(config().paths.subdir + '/ghost/forgotten'); res.redirect(config().paths.subdir + '/ghost/forgotten');
}); });
}); });
@ -464,7 +464,7 @@ adminControllers = {
status: 'passive' status: 'passive'
}; };
return api.notifications.add(notification).then(function () { return api.notifications.add({ notifications: [notification] }).then(function () {
res.json(200, {redirect: config().paths.subdir + '/ghost/signin/'}); res.json(200, {redirect: config().paths.subdir + '/ghost/signin/'});
}); });
}).otherwise(function (err) { }).otherwise(function (err) {

View file

@ -44,10 +44,10 @@ function doFirstRun() {
'See <a href="http://docs.ghost.org/">http://docs.ghost.org</a> for instructions.' 'See <a href="http://docs.ghost.org/">http://docs.ghost.org</a> for instructions.'
]; ];
return api.notifications.add({ return api.notifications.add({ notifications: [{
type: 'info', type: 'info',
message: firstRunMessage.join(' ') message: firstRunMessage.join(' ')
}); }] });
} }
function initDbHashAndFirstRun() { function initDbHashAndFirstRun() {
@ -169,23 +169,23 @@ function ghostStartMessages() {
// in the future apps will want to hook into here // in the future apps will want to hook into here
function initNotifications() { function initNotifications() {
if (mailer.state && mailer.state.usingSendmail) { if (mailer.state && mailer.state.usingSendmail) {
api.notifications.add({ api.notifications.add({ notifications: [{
type: 'info', type: 'info',
message: [ message: [
"Ghost is attempting to use your server's <b>sendmail</b> to send e-mail.", "Ghost is attempting to use your server's <b>sendmail</b> to send e-mail.",
"It is recommended that you explicitly configure an e-mail service,", "It is recommended that you explicitly configure an e-mail service,",
"See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions" "See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions"
].join(' ') ].join(' ')
}); }] });
} }
if (mailer.state && mailer.state.emailDisabled) { if (mailer.state && mailer.state.emailDisabled) {
api.notifications.add({ api.notifications.add({ notifications: [{
type: 'warn', type: 'warn',
message: [ message: [
"Ghost is currently unable to send e-mail.", "Ghost is currently unable to send e-mail.",
"See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions" "See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions"
].join(' ') ].join(' ')
}); }] });
} }
} }

View file

@ -77,7 +77,7 @@ var middleware = {
}; };
// let's only add the notification once // let's only add the notification once
if (!_.contains(_.pluck(notifications, 'id'), 'failedauth')) { if (!_.contains(_.pluck(notifications, 'id'), 'failedauth')) {
api.notifications.add(msg); api.notifications.add({ notifications: [msg] });
} }
redirect = '?r=' + encodeURIComponent(reqPath); redirect = '?r=' + encodeURIComponent(reqPath);
} }

View file

@ -85,7 +85,7 @@ describe('Notifications API', function () {
it('creates a new notification', function (done) { it('creates a new notification', function (done) {
request.post(testUtils.API.getApiQuery('notifications/')) request.post(testUtils.API.getApiQuery('notifications/'))
.set('X-CSRF-Token', csrfToken) .set('X-CSRF-Token', csrfToken)
.send(newNotification) .send({ notifications: [newNotification] })
.expect(201) .expect(201)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
@ -118,7 +118,7 @@ describe('Notifications API', function () {
// create the notification that is to be deleted // create the notification that is to be deleted
request.post(testUtils.API.getApiQuery('notifications/')) request.post(testUtils.API.getApiQuery('notifications/'))
.set('X-CSRF-Token', csrfToken) .set('X-CSRF-Token', csrfToken)
.send(newNotification) .send({ notifications: [newNotification] })
.expect(201) .expect(201)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {

View file

@ -35,7 +35,7 @@ describe('Notifications API', function () {
type: 'error', // this can be 'error', 'success', 'warn' and 'info' type: 'error', // this can be 'error', 'success', 'warn' and 'info'
message: 'This is an error', // A string. Should fit in one line. message: 'This is an error', // A string. Should fit in one line.
}; };
NotificationsAPI.add(msg).then(function (notification) { NotificationsAPI.add({ notifications: [msg] }).then(function (notification) {
NotificationsAPI.browse().then(function (results) { NotificationsAPI.browse().then(function (results) {
should.exist(results); should.exist(results);
should.exist(results.notifications); should.exist(results.notifications);
@ -52,7 +52,7 @@ describe('Notifications API', function () {
message: 'Hello, this is dog' message: 'Hello, this is dog'
}; };
NotificationsAPI.add(msg).then(function (result) { NotificationsAPI.add({ notifications: [msg] }).then(function (result) {
var notification; var notification;
should.exist(result); should.exist(result);
@ -74,7 +74,7 @@ describe('Notifications API', function () {
id: 99 id: 99
}; };
NotificationsAPI.add(msg).then(function (result) { NotificationsAPI.add({ notifications: [msg] }).then(function (result) {
var notification; var notification;
should.exist(result); should.exist(result);
@ -96,7 +96,7 @@ describe('Notifications API', function () {
message: 'Goodbye, cruel world!' message: 'Goodbye, cruel world!'
}; };
NotificationsAPI.add(msg).then(function (result) { NotificationsAPI.add({ notifications: [msg] }).then(function (result) {
var notification = result.notifications[0]; var notification = result.notifications[0];
NotificationsAPI.destroy({ id: notification.id }).then(function (result) { NotificationsAPI.destroy({ id: notification.id }).then(function (result) {

View file

@ -121,22 +121,22 @@ describe('Middleware', function () {
describe('cleanNotifications', function () { describe('cleanNotifications', function () {
beforeEach(function (done) { beforeEach(function (done) {
api.notifications.add({ api.notifications.add({ notifications: [{
id: 0, id: 0,
status: 'passive', status: 'passive',
message: 'passive-one' message: 'passive-one'
}).then(function () { }] }).then(function () {
return api.notifications.add({ return api.notifications.add({ notifications: [{
id: 1, id: 1,
status: 'passive', status: 'passive',
message: 'passive-two' message: 'passive-two'
}); }] });
}).then(function () { }).then(function () {
return api.notifications.add({ return api.notifications.add({ notifications: [{
id: 2, id: 2,
status: 'aggressive', status: 'aggressive',
message: 'aggressive' message: 'aggressive'
}); }] });
}).then(function () { }).then(function () {
done(); done();
}).catch(done); }).catch(done);