all files / src/user/ categories.js

29.03% Statements 9/31
0% Branches 0/12
15.38% Functions 2/13
29.03% Lines 9/31
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                                                                                                                       
'use strict';
 
var async = require('async');
 
var db = require('../database');
var categories = require('../categories');
 
module.exports = function(User) {
 
	User.getIgnoredCategories = function(uid, callback) {
		db.getSortedSetRange('uid:' + uid + ':ignored:cids', 0, -1, callback);
	};
 
	User.getWatchedCategories = function(uid, callback) {
		async.parallel({
			ignored: function(next) {
				User.getIgnoredCategories(uid, next);
			},
			all: function(next) {
				db.getSortedSetRange('categories:cid', 0, -1, next);
			}
		}, function(err, results) {
			if (err) {
				return callback(err);
			}
 
			var watched = results.all.filter(function(cid) {
				return cid && results.ignored.indexOf(cid) === -1;
			});
			callback(null, watched);
		});
	};
 
	User.ignoreCategory = function(uid, cid, callback) {
		if (!uid) {
			return callback();
		}
 
		async.waterfall([
			function (next) {
				categories.exists(cid, next);
			},
			function (exists, next) {
				if (!exists) {
					return next(new Error('[[error:no-category]]'));
				}
				db.sortedSetAdd('uid:' + uid + ':ignored:cids', Date.now(), cid, next);
			}
		], callback);
	};
 
	User.watchCategory = function(uid, cid, callback) {
		if (!uid) {
			return callback();
		}
 
		async.waterfall([
			function (next) {
				categories.exists(cid, next);
			},
			function (exists, next) {
				if (!exists) {
					return next(new Error('[[error:no-category]]'));
				}
				db.sortedSetRemove('uid:' + uid + ':ignored:cids', cid, next);
			}
		], callback);
	};
};