From 2bff35bcc2980678f08ce7cea05b7af20dfc5df0 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Mon, 16 Jun 2014 05:24:19 +0000 Subject: [PATCH] Complete moveover to new Notification API format fixes #2775 - Fix all occurences of notifications.add to use proper API format --- core/server/api/notifications.js | 37 ++++++++++++------- core/server/controllers/admin.js | 12 +++--- core/server/index.js | 12 +++--- core/server/middleware/middleware.js | 2 +- .../routes/api/notifications_test.js | 4 +- .../integration/api/api_notifications_spec.js | 8 ++-- core/test/unit/middleware_spec.js | 12 +++--- 7 files changed, 48 insertions(+), 39 deletions(-) diff --git a/core/server/api/notifications.js b/core/server/api/notifications.js index e276741df8..b8fbdebefa 100644 --- a/core/server/api/notifications.js +++ b/core/server/api/notifications.js @@ -3,6 +3,7 @@ var when = require('when'), _ = require('lodash'), errors = require('../errors'), + utils = require('./utils'), // Holds the persistent notifications notificationsStore = [], @@ -73,31 +74,39 @@ notifications = { * * **takes:** a notification object of the form * ``` - * msg = { + * msg = { notifications: [{ * 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) { + add: function add(object) { var defaults = { - dismissable: true, - location: 'bottom', - status: 'persistent' - }; + dismissable: true, + location: 'bottom', + status: 'persistent' + }, + addedNotifications = []; - notificationCounter = notificationCounter + 1; - notification = _.assign(defaults, notification, { - id: notificationCounter - //status: 'persistent' + return utils.checkObject(object, 'notifications').then(function (checkedNotificationData) { + _.each(checkedNotificationData.notifications, function (notification) { + 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]}); } }; diff --git a/core/server/controllers/admin.js b/core/server/controllers/admin.js index d5242426de..f1ec23f40d 100644 --- a/core/server/controllers/admin.js +++ b/core/server/controllers/admin.js @@ -149,7 +149,7 @@ adminControllers = { 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'); }); }); @@ -190,7 +190,7 @@ adminControllers = { status: 'passive' }; - return api.notifications.add(notification).then(function () { + return api.notifications.add({ notifications: [notification] }).then(function () { res.redirect(config().paths.subdir + '/ghost/signin/'); }); }, @@ -220,7 +220,7 @@ adminControllers = { 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}); }); @@ -408,7 +408,7 @@ adminControllers = { 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/'}); }); @@ -444,7 +444,7 @@ adminControllers = { 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'); }); }); @@ -464,7 +464,7 @@ adminControllers = { 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/'}); }); }).otherwise(function (err) { diff --git a/core/server/index.js b/core/server/index.js index ff2a7b37a1..88d579420b 100644 --- a/core/server/index.js +++ b/core/server/index.js @@ -44,10 +44,10 @@ function doFirstRun() { 'See http://docs.ghost.org for instructions.' ]; - return api.notifications.add({ + return api.notifications.add({ notifications: [{ type: 'info', message: firstRunMessage.join(' ') - }); + }] }); } function initDbHashAndFirstRun() { @@ -169,23 +169,23 @@ function ghostStartMessages() { // in the future apps will want to hook into here function initNotifications() { if (mailer.state && mailer.state.usingSendmail) { - api.notifications.add({ + api.notifications.add({ notifications: [{ type: 'info', message: [ "Ghost is attempting to use your server's sendmail to send e-mail.", "It is recommended that you explicitly configure an e-mail service,", "See http://docs.ghost.org/mail for instructions" ].join(' ') - }); + }] }); } if (mailer.state && mailer.state.emailDisabled) { - api.notifications.add({ + api.notifications.add({ notifications: [{ type: 'warn', message: [ "Ghost is currently unable to send e-mail.", "See http://docs.ghost.org/mail for instructions" ].join(' ') - }); + }] }); } } diff --git a/core/server/middleware/middleware.js b/core/server/middleware/middleware.js index 21f2b61deb..de8742dee1 100644 --- a/core/server/middleware/middleware.js +++ b/core/server/middleware/middleware.js @@ -77,7 +77,7 @@ var middleware = { }; // let's only add the notification once if (!_.contains(_.pluck(notifications, 'id'), 'failedauth')) { - api.notifications.add(msg); + api.notifications.add({ notifications: [msg] }); } redirect = '?r=' + encodeURIComponent(reqPath); } diff --git a/core/test/functional/routes/api/notifications_test.js b/core/test/functional/routes/api/notifications_test.js index d4fc894e5a..00a6738542 100644 --- a/core/test/functional/routes/api/notifications_test.js +++ b/core/test/functional/routes/api/notifications_test.js @@ -85,7 +85,7 @@ describe('Notifications API', function () { it('creates a new notification', function (done) { request.post(testUtils.API.getApiQuery('notifications/')) .set('X-CSRF-Token', csrfToken) - .send(newNotification) + .send({ notifications: [newNotification] }) .expect(201) .end(function (err, res) { if (err) { @@ -118,7 +118,7 @@ describe('Notifications API', function () { // create the notification that is to be deleted request.post(testUtils.API.getApiQuery('notifications/')) .set('X-CSRF-Token', csrfToken) - .send(newNotification) + .send({ notifications: [newNotification] }) .expect(201) .end(function (err, res) { if (err) { diff --git a/core/test/integration/api/api_notifications_spec.js b/core/test/integration/api/api_notifications_spec.js index 1493adc9cd..9a4b150597 100644 --- a/core/test/integration/api/api_notifications_spec.js +++ b/core/test/integration/api/api_notifications_spec.js @@ -35,7 +35,7 @@ describe('Notifications API', function () { type: 'error', // this can be 'error', 'success', 'warn' and 'info' 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) { should.exist(results); should.exist(results.notifications); @@ -52,7 +52,7 @@ describe('Notifications API', function () { message: 'Hello, this is dog' }; - NotificationsAPI.add(msg).then(function (result) { + NotificationsAPI.add({ notifications: [msg] }).then(function (result) { var notification; should.exist(result); @@ -74,7 +74,7 @@ describe('Notifications API', function () { id: 99 }; - NotificationsAPI.add(msg).then(function (result) { + NotificationsAPI.add({ notifications: [msg] }).then(function (result) { var notification; should.exist(result); @@ -96,7 +96,7 @@ describe('Notifications API', function () { message: 'Goodbye, cruel world!' }; - NotificationsAPI.add(msg).then(function (result) { + NotificationsAPI.add({ notifications: [msg] }).then(function (result) { var notification = result.notifications[0]; NotificationsAPI.destroy({ id: notification.id }).then(function (result) { diff --git a/core/test/unit/middleware_spec.js b/core/test/unit/middleware_spec.js index eeb67a8f86..fede4ab9e1 100644 --- a/core/test/unit/middleware_spec.js +++ b/core/test/unit/middleware_spec.js @@ -121,22 +121,22 @@ describe('Middleware', function () { describe('cleanNotifications', function () { beforeEach(function (done) { - api.notifications.add({ + api.notifications.add({ notifications: [{ id: 0, status: 'passive', message: 'passive-one' - }).then(function () { - return api.notifications.add({ + }] }).then(function () { + return api.notifications.add({ notifications: [{ id: 1, status: 'passive', message: 'passive-two' - }); + }] }); }).then(function () { - return api.notifications.add({ + return api.notifications.add({ notifications: [{ id: 2, status: 'aggressive', message: 'aggressive' - }); + }] }); }).then(function () { done(); }).catch(done);