| 
									
										
										
										
											2014-03-12 22:11:48 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-31 15:13:52 -05:00
										 |  |  | var	async = require('async'), | 
					
						
							| 
									
										
										
										
											2014-08-13 19:25:58 -04:00
										 |  |  | 	nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2014-01-31 15:13:52 -05:00
										 |  |  | 	user = require('../user'), | 
					
						
							| 
									
										
										
										
											2014-05-22 10:21:01 -04:00
										 |  |  | 	groups = require('../groups'), | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	topics = require('../topics'), | 
					
						
							| 
									
										
										
										
											2014-09-15 14:34:01 -04:00
										 |  |  | 	posts = require('../posts'), | 
					
						
							| 
									
										
										
										
											2014-08-13 19:25:58 -04:00
										 |  |  | 	notifications = require('../notifications'), | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 	messaging = require('../messaging'), | 
					
						
							| 
									
										
										
										
											2014-08-14 17:52:17 -04:00
										 |  |  | 	plugins = require('../plugins'), | 
					
						
							| 
									
										
										
										
											2014-09-06 22:08:55 -04:00
										 |  |  | 	utils = require('../../public/src/utils'), | 
					
						
							|  |  |  | 	websockets = require('./index'), | 
					
						
							| 
									
										
										
										
											2014-03-19 21:32:13 -04:00
										 |  |  | 	meta = require('../meta'), | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	SocketUser = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.exists = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if (data && data.username) { | 
					
						
							| 
									
										
										
										
											2014-05-22 10:21:01 -04:00
										 |  |  | 		meta.userOrGroupExists(utils.slugify(data.username), callback); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-26 13:47:48 -04:00
										 |  |  | SocketUser.deleteAccount = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if (socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-09-24 00:07:24 -04:00
										 |  |  | 		user.isAdministrator(socket.uid, function(err, isAdmin) { | 
					
						
							|  |  |  | 			if (err || isAdmin) { | 
					
						
							|  |  |  | 				return callback(err || new Error('[[error:cant-delete-admin]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			user.deleteAccount(socket.uid, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-08-26 13:47:48 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.count = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	user.count(callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.emailExists = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 18:18:42 -05:00
										 |  |  | 	if(data && data.email) { | 
					
						
							|  |  |  | 		user.email.exists(data.email, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-03 18:39:54 -04:00
										 |  |  | SocketUser.emailConfirm = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if (socket.uid && parseInt(meta.config.requireEmailConfirmation, 10) === 1) { | 
					
						
							|  |  |  | 		user.getUserField(socket.uid, 'email', function(err, email) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!email) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			user.email.verify(socket.uid, email); | 
					
						
							|  |  |  | 			callback(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-28 18:15:53 -04:00
										 |  |  | SocketUser.increaseViewCount = function(socket, uid, callback) { | 
					
						
							|  |  |  | 	if (uid) { | 
					
						
							|  |  |  | 		if (socket.uid !== parseInt(uid, 10)) { | 
					
						
							| 
									
										
										
										
											2014-05-28 18:20:02 -04:00
										 |  |  | 			user.incrementUserFieldBy(uid, 'profileviews', 1, callback); | 
					
						
							| 
									
										
										
										
											2014-05-28 18:15:53 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 22:07:29 -05:00
										 |  |  | SocketUser.search = function(socket, username, callback) { | 
					
						
							| 
									
										
										
										
											2014-06-23 19:10:59 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:not-logged-in]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-19 22:07:29 -05:00
										 |  |  | 	user.search(username, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | // Password Reset
 | 
					
						
							|  |  |  | SocketUser.reset = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-04 13:11:05 -04:00
										 |  |  | SocketUser.reset.send = function(socket, email, callback) { | 
					
						
							|  |  |  | 	if (email) { | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 		user.reset.send(email, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 18:18:42 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-04 13:11:05 -04:00
										 |  |  | SocketUser.reset.valid = function(socket, code, callback) { | 
					
						
							|  |  |  | 	if (code) { | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 		user.reset.validate(code, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 18:18:42 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.reset.commit = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 18:18:42 -05:00
										 |  |  | 	if(data && data.code && data.password) { | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 		user.reset.commit(data.code, data.password, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 18:18:42 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 22:08:55 -04:00
										 |  |  | SocketUser.checkStatus = function(socket, uid, callback) { | 
					
						
							|  |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return callback('[[error:invalid-uid]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var online = websockets.isUserOnline(uid); | 
					
						
							|  |  |  | 	if (!online) { | 
					
						
							|  |  |  | 		return callback(null, 'offline'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	user.getUserField(uid, 'status', function(err, status) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		status = status || 'online'; | 
					
						
							|  |  |  | 		callback(null, status); | 
					
						
							| 
									
										
										
										
											2014-08-14 07:51:21 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.changePassword = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 	if (data && socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-01-16 18:18:42 -05:00
										 |  |  | 		user.changePassword(socket.uid, data, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.updateProfile = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return callback('[[error:invalid-uid]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!data || !data.uid) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:44:00 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-02-11 20:39:07 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 	if (socket.uid === parseInt(data.uid, 10)) { | 
					
						
							| 
									
										
										
										
											2014-02-11 20:39:07 -05:00
										 |  |  | 		return user.updateProfile(socket.uid, data, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 18:18:42 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-11 20:39:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	user.isAdministrator(socket.uid, function(err, isAdmin) { | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 		if (err) { | 
					
						
							| 
									
										
										
										
											2014-02-11 20:39:07 -05:00
										 |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-03-12 22:11:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 		if (!isAdmin) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:44:00 -04:00
										 |  |  | 			return callback(new Error('[[error:no-privileges]]')); | 
					
						
							| 
									
										
										
										
											2014-02-11 20:39:07 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		user.updateProfile(data.uid, data, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.changePicture = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return callback('[[error:invalid-uid]]'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!data) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:44:00 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-16 18:18:42 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var type = data.type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-15 17:12:05 -05:00
										 |  |  | 	function changePicture(uid, callback) { | 
					
						
							|  |  |  | 		user.getUserField(uid, type, function(err, picture) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			user.setUserField(uid, 'picture', picture, callback); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (type === 'gravatar') { | 
					
						
							| 
									
										
										
										
											2014-01-16 17:52:46 -05:00
										 |  |  | 		type = 'gravatarpicture'; | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	} else if (type === 'uploaded') { | 
					
						
							| 
									
										
										
										
											2014-01-16 17:52:46 -05:00
										 |  |  | 		type = 'uploadedpicture'; | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:44:00 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-image-type]]')); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-16 17:52:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-15 17:12:05 -05:00
										 |  |  | 	if(socket.uid === parseInt(data.uid, 10)) { | 
					
						
							|  |  |  | 		changePicture(socket.uid, function(err) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	user.isAdministrator(socket.uid, function(err, isAdmin) { | 
					
						
							|  |  |  | 		if(err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(!isAdmin) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:44:00 -04:00
										 |  |  | 			return callback(new Error('[[error:no-privileges]]')); | 
					
						
							| 
									
										
										
										
											2014-02-15 17:12:05 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		changePicture(data.uid, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 17:52:46 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-24 17:13:12 -04:00
										 |  |  | SocketUser.uploadProfileImageFromUrl = function(socket, url, callback) { | 
					
						
							|  |  |  | 	if (!socket.uid || !url) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 19:29:45 -05:00
										 |  |  | 	plugins.fireHook('filter:uploadImage', {image: {url: url}, uid: socket.uid}, function(err, image) { | 
					
						
							| 
									
										
										
										
											2014-09-24 17:13:12 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		user.setUserFields(socket.uid, {uploadedpicture: image.url, picture: image.url}, function(err) { | 
					
						
							|  |  |  | 			callback(err, image.url); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-10-08 12:18:32 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-09-24 17:13:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.follow = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 	if (!socket.uid || !data) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	toggleFollow('follow', socket.uid, data.uid, function(err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		user.getUserFields(socket.uid, ['username', 'userslug'], function(err, userData) { | 
					
						
							| 
									
										
										
										
											2014-08-13 19:25:58 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 			notifications.create({ | 
					
						
							|  |  |  | 				bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]', | 
					
						
							|  |  |  | 				path: nconf.get('relative_path') + '/user/' + userData.userslug, | 
					
						
							| 
									
										
										
										
											2014-09-12 16:35:30 -04:00
										 |  |  | 				nid: 'follow:' + data.uid + ':uid:' + socket.uid, | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 				from: socket.uid | 
					
						
							|  |  |  | 			}, function(err, notification) { | 
					
						
							|  |  |  | 				if (!err && notification) { | 
					
						
							|  |  |  | 					notifications.push(notification, [data.uid]); | 
					
						
							| 
									
										
										
										
											2014-08-13 19:25:58 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 				callback(err); | 
					
						
							| 
									
										
										
										
											2014-08-13 19:25:58 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.unfollow = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 18:18:42 -05:00
										 |  |  | 	if (socket.uid && data) { | 
					
						
							| 
									
										
										
										
											2014-08-14 17:59:33 -04:00
										 |  |  | 		toggleFollow('unfollow', socket.uid, data.uid, callback); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-14 17:59:33 -04:00
										 |  |  | function toggleFollow(method, uid, theiruid, callback) { | 
					
						
							|  |  |  | 	user[method](uid, theiruid, function(err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		plugins.fireHook('action:user.' + method, { | 
					
						
							|  |  |  | 			fromUid: uid, | 
					
						
							|  |  |  | 			toUid: theiruid | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-08-17 00:14:45 -04:00
										 |  |  | 		callback(); | 
					
						
							| 
									
										
										
										
											2014-08-14 17:59:33 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-11 17:55:56 -05:00
										 |  |  | SocketUser.getSettings = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if (socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-07-20 21:10:23 -04:00
										 |  |  | 		if (socket.uid === parseInt(data.uid, 10)) { | 
					
						
							|  |  |  | 			return user.getSettings(socket.uid, callback); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		user.isAdministrator(socket.uid, function(err, isAdmin) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!isAdmin) { | 
					
						
							|  |  |  | 				return callback(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			user.getSettings(data.uid, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-11 17:55:56 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.saveSettings = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-07-20 21:10:23 -04:00
										 |  |  | 	if (!socket.uid || !data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-20 21:10:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (socket.uid === parseInt(data.uid, 10)) { | 
					
						
							|  |  |  | 		return user.saveSettings(socket.uid, data.settings, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	user.isAdministrator(socket.uid, function(err, isAdmin) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!isAdmin) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		user.saveSettings(data.uid, data.settings, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | SocketUser.setTopicSort = function(socket, sort, callback) { | 
					
						
							| 
									
										
										
										
											2014-07-20 21:10:23 -04:00
										 |  |  | 	if (socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 		user.setSetting(socket.uid, 'topicPostSort', sort, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.getOnlineAnonCount = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 18:10:38 -05:00
										 |  |  | 	callback(null, module.parent.exports.getOnlineAnonCount()); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.getUnreadCount = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return callback(null, 0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-09 14:02:30 -04:00
										 |  |  | 	topics.getTotalUnread(socket.uid, callback); | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | SocketUser.getUnreadChatCount = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-01 16:55:50 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return callback(null, 0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 	messaging.getUnreadCount(socket.uid, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.getActiveUsers = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | 	module.parent.exports.emitOnlineUserCount(callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | SocketUser.loadMore = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-31 15:47:26 -05:00
										 |  |  | 	if(!data || !data.set || parseInt(data.after, 10) < 0) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:44:00 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-31 15:47:26 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-19 21:32:13 -04:00
										 |  |  | 	if (!socket.uid && !!parseInt(meta.config.privateUserInfo, 10)) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:44:00 -04:00
										 |  |  | 		return callback(new Error('[[error:no-privileges]]')); | 
					
						
							| 
									
										
										
										
											2014-03-19 21:32:13 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-31 15:47:26 -05:00
										 |  |  | 	var start = data.after, | 
					
						
							|  |  |  | 		end = start + 19; | 
					
						
							| 
									
										
										
										
											2014-01-16 18:10:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 12:49:31 -04:00
										 |  |  | 	user.getUsersFromSet(data.set, start, end, function(err, userData) { | 
					
						
							| 
									
										
										
										
											2014-01-31 15:47:26 -05:00
										 |  |  | 		if(err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 17:12:13 -05:00
										 |  |  | 		user.isAdministrator(socket.uid, function (err, isAdministrator) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-31 15:47:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 17:12:13 -05:00
										 |  |  | 			if(!isAdministrator && data.set === 'users:online') { | 
					
						
							|  |  |  | 				userData = userData.filter(function(item) { | 
					
						
							|  |  |  | 					return item.status !== 'offline'; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, { | 
					
						
							|  |  |  | 				users: userData | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-01-16 18:10:38 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-01-31 15:47:26 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-31 15:13:52 -05:00
										 |  |  | SocketUser.setStatus = function(socket, status, callback) { | 
					
						
							| 
									
										
										
										
											2014-09-06 22:08:55 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return callback(new Error('[[invalid-uid]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-24 13:36:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var allowedStatus = ['online', 'offline', 'dnd', 'away']; | 
					
						
							|  |  |  | 	if (allowedStatus.indexOf(status) === -1) { | 
					
						
							|  |  |  | 		return callback(new Error('[[invalid-user-status]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-31 15:13:52 -05:00
										 |  |  | 	user.setUserField(socket.uid, 'status', status, function(err) { | 
					
						
							| 
									
										
										
										
											2014-09-06 22:08:55 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		var data = { | 
					
						
							|  |  |  | 			uid: socket.uid, | 
					
						
							|  |  |  | 			status: status | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		websockets.server.sockets.emit('event:user_status_change', data); | 
					
						
							|  |  |  | 		callback(null, data); | 
					
						
							| 
									
										
										
										
											2014-01-31 15:13:52 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 22:46:51 -05:00
										 |  |  | /* Exports */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = SocketUser; |