all files / src/ social.js

26.47% Statements 9/34
0% Branches 0/10
0% Functions 0/12
26.47% Lines 9/34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                                                                                                                                                       
"use strict";
 
var plugins = require('./plugins');
var db = require('./database');
var async = require('async');
 
var social = {};
 
social.postSharing = null;
 
social.getPostSharing = function(callback) {
	if (social.postSharing) {
		return callback(null, social.postSharing);
	}
 
	var networks = [
		{
			id: "facebook",
			name: "Facebook",
			class: "fa-facebook"
		},
		{
			id: "twitter",
			name: "Twitter",
			class: "fa-twitter"
		},
		{
			id: "google",
			name: "Google+",
			class: "fa-google-plus"
		}
	];
 
	async.waterfall([
		function(next) {
			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);
			});
		}
	], 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);
	});
};
 
social.setActivePostSharingNetworks = function(networkIDs, callback) {
	async.waterfall([
		function (next) {
			db.delete('social:posts.activated', next);
		},
		function (next) {
			if (!networkIDs.length) {
				return next();
			}
			db.setAdd('social:posts.activated', networkIDs, next);
		},
		function (next) {
			social.postSharing = null;
			next();
		}
	], callback);
};
 
module.exports = social;