diff --git a/src/controllers/admin/social.js b/src/controllers/admin/social.js index da12f3341e..75bdfd5967 100644 --- a/src/controllers/admin/social.js +++ b/src/controllers/admin/social.js @@ -1,20 +1,20 @@ 'use strict'; +var async = require('async'); + var social = require('../../social'); -var socialController = {}; - +var socialController = module.exports; socialController.get = function (req, res, next) { - social.getPostSharing(function (err, posts) { - if (err) { - return next(err); - } - - res.render('admin/general/social', { - posts: posts, - }); - }); + async.waterfall([ + function (next) { + social.getPostSharing(next); + }, + function (posts) { + res.render('admin/general/social', { + posts: posts, + }); + }, + ], next); }; - -module.exports = socialController; diff --git a/src/social.js b/src/social.js index 71780b5b81..e26b497d21 100644 --- a/src/social.js +++ b/src/social.js @@ -4,7 +4,7 @@ var plugins = require('./plugins'); var db = require('./database'); var async = require('async'); -var social = {}; +var social = module.exports; social.postSharing = null; @@ -36,32 +36,31 @@ social.getPostSharing = function (callback) { plugins.fireHook('filter:social.posts', networks, next); }, function (networks, next) { - db.getSetMembers('social:posts.activated', function (err, activated) { - if (err) { - return next(err); - } - - networks.forEach(function (network, i) { - networks[i].activated = (activated.indexOf(network.id) !== -1); - }); - - social.postSharing = networks; - next(null, networks); + db.getSetMembers('social:posts.activated', next); + }, + function (activated, next) { + networks.forEach(function (network, i) { + networks[i].activated = (activated.indexOf(network.id) !== -1); }); + + social.postSharing = networks; + next(null, networks); }, ], callback); }; social.getActivePostSharing = function (callback) { - social.getPostSharing(function (err, networks) { - if (err) { - return callback(err); - } - networks = networks.filter(function (network) { - return network && network.activated; - }); - callback(null, networks); - }); + async.waterfall([ + function (next) { + social.getPostSharing(next); + }, + function (networks, next) { + networks = networks.filter(function (network) { + return network && network.activated; + }); + next(null, networks); + }, + ], callback); }; social.setActivePostSharingNetworks = function (networkIDs, callback) { @@ -81,5 +80,3 @@ social.setActivePostSharingNetworks = function (networkIDs, callback) { }, ], callback); }; - -module.exports = social; diff --git a/src/socket.io/admin/social.js b/src/socket.io/admin/social.js index 5f422eff6b..ce22acdb3c 100644 --- a/src/socket.io/admin/social.js +++ b/src/socket.io/admin/social.js @@ -1,10 +1,8 @@ 'use strict'; var social = require('../../social'); -var SocketSocial = {}; +var SocketSocial = module.exports; SocketSocial.savePostSharingNetworks = function (socket, data, callback) { social.setActivePostSharingNetworks(data, callback); }; - -module.exports = SocketSocial; diff --git a/test/controllers-admin.js b/test/controllers-admin.js index 83d3b94fad..dbc1f5fd53 100644 --- a/test/controllers-admin.js +++ b/test/controllers-admin.js @@ -391,10 +391,20 @@ describe('Admin Controllers', function () { }); it('should load /admin/general/social', function (done) { - request(nconf.get('url') + '/api/admin/general/social', { jar: jar, json: true }, function (err, res, body) { + var socketAdmin = require('../src/socket.io/admin'); + socketAdmin.social.savePostSharingNetworks({ uid: adminUid }, ['facebook', 'twitter', 'google'], function (err) { assert.ifError(err); - assert(body); - done(); + request(nconf.get('url') + '/api/admin/general/social', { jar: jar, json: true }, function (err, res, body) { + assert.ifError(err); + assert(body); + body = body.posts.map(function (network) { + return network && network.id; + }); + assert(body.indexOf('facebook') !== -1); + assert(body.indexOf('twitter') !== -1); + assert(body.indexOf('google') !== -1); + done(); + }); }); });