| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var groups = require('../../groups'), | 
					
						
							|  |  |  | 	user = require('../../user'), | 
					
						
							|  |  |  | 	categories = require('../../categories'), | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 	privileges = require('../../privileges'), | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 	Categories = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.create = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	categories.create(data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.update = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	categories.update(data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.search = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var	username = data.username, | 
					
						
							|  |  |  | 		cid = data.cid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	user.search(username, function(err, data) { | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 		async.map(data.users, function(userObj, next) { | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 			privileges.categories.userPrivileges(cid, userObj.uid, function(err, privileges) { | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				userObj.privileges = privileges; | 
					
						
							|  |  |  | 				next(null, userObj); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.setPrivilege = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 	groups[data.set ? 'join' : 'leave']('cid:' + data.cid + ':privileges:' + data.privilege, data.uid, callback); | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.getPrivilegeSettings = function(socket, cid, callback) { | 
					
						
							| 
									
										
										
										
											2014-05-16 15:47:04 -04:00
										 |  |  | 	var privileges = ['find', 'read', 'topics:create', 'topics:reply', 'mods']; | 
					
						
							| 
									
										
										
										
											2014-05-12 17:16:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	async.reduce(privileges, [], function(members, privilege, next) { | 
					
						
							|  |  |  | 		groups.get('cid:' + cid + ':privileges:' + privilege, { expand: true }, function(err, groupObj) { | 
					
						
							|  |  |  | 			if (!err) { | 
					
						
							|  |  |  | 				members = members.concat(groupObj.members); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-12 17:16:29 -04:00
										 |  |  | 			next(null, members); | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-05-12 17:16:29 -04:00
										 |  |  | 	}, function(err, members) { | 
					
						
							|  |  |  | 		// Remove duplicates
 | 
					
						
							|  |  |  | 		var	present = [], | 
					
						
							|  |  |  | 			x = members.length, | 
					
						
							|  |  |  | 			uid; | 
					
						
							|  |  |  | 		while(x--) { | 
					
						
							|  |  |  | 			uid = parseInt(members[x].uid, 10); | 
					
						
							|  |  |  | 			if (present.indexOf(uid) !== -1) { | 
					
						
							|  |  |  | 				members.splice(x, 1); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				present.push(uid); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		callback(err, members); | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.setGroupPrivilege = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (data.set) { | 
					
						
							|  |  |  | 		groups.join('cid:' + data.cid + ':privileges:' + data.privilege, data.name, callback); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		groups.leave('cid:' + data.cid + ':privileges:' + data.privilege, data.name, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.groupsList = function(socket, cid, callback) { | 
					
						
							|  |  |  | 	groups.list({ | 
					
						
							|  |  |  | 		expand: false, | 
					
						
							|  |  |  | 		showSystemGroups: true | 
					
						
							|  |  |  | 	}, function(err, data) { | 
					
						
							|  |  |  | 		if(err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.map(data, function(groupObj, next) { | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 			privileges.categories.groupPrivileges(cid, groupObj.name, function(err, privileges) { | 
					
						
							| 
									
										
										
										
											2014-04-15 02:27:58 -04:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				groupObj.privileges = privileges; | 
					
						
							|  |  |  | 				next(null, groupObj); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = Categories; |