| 
									
										
										
										
											2014-03-03 15:26:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | var nconf = require('nconf'), | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							| 
									
										
										
										
											2014-02-27 23:45:12 -05:00
										 |  |  | 	categories = require('../categories'), | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 	privileges = require('../privileges'), | 
					
						
							| 
									
										
										
										
											2014-09-18 17:44:24 -04:00
										 |  |  | 	plugins = require('../plugins'), | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | 	notifications = require('../notifications'), | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	threadTools = require('../threadTools'), | 
					
						
							| 
									
										
										
										
											2014-04-27 00:47:08 -04:00
										 |  |  | 	websockets = require('./index'), | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 	user = require('../user'), | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | 	db = require('../database'), | 
					
						
							|  |  |  | 	meta = require('../meta'), | 
					
						
							| 
									
										
										
										
											2014-04-19 14:31:46 -04:00
										 |  |  | 	utils = require('../../public/src/utils'), | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | 	SocketPosts = require('./posts'), | 
					
						
							| 
									
										
										
										
											2014-01-26 16:22:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	SocketTopics = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | SocketTopics.post = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | 	if(!data) { | 
					
						
							| 
									
										
										
										
											2014-04-09 14:12:46 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-27 00:47:08 -04:00
										 |  |  | 	topics.post({ | 
					
						
							|  |  |  | 		uid: socket.uid, | 
					
						
							|  |  |  | 		title: data.title, | 
					
						
							|  |  |  | 		content: data.content, | 
					
						
							|  |  |  | 		cid: data.category_id, | 
					
						
							|  |  |  | 		thumb: data.topic_thumb, | 
					
						
							| 
									
										
										
										
											2014-05-21 16:13:46 -04:00
										 |  |  | 		tags: data.tags, | 
					
						
							| 
									
										
										
										
											2014-04-27 00:47:08 -04:00
										 |  |  | 		req: websockets.reqFromSocket(socket) | 
					
						
							|  |  |  | 	}, function(err, result) { | 
					
						
							| 
									
										
										
										
											2014-09-14 14:19:36 -04:00
										 |  |  | 		if (err) { | 
					
						
							| 
									
										
										
										
											2014-01-23 15:46:39 -05:00
										 |  |  | 			return callback(err); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-14 14:19:36 -04:00
										 |  |  | 		callback(null, result.topicData); | 
					
						
							| 
									
										
										
										
											2014-10-09 18:12:40 -04:00
										 |  |  | 		socket.emit('event:new_post', {posts: [result.postData]}); | 
					
						
							| 
									
										
										
										
											2014-09-14 14:19:36 -04:00
										 |  |  | 		socket.emit('event:new_topic', result.topicData); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 14:25:11 -05:00
										 |  |  | 		user.getUidsFromSet('users:online', 0, -1, function(err, uids) { | 
					
						
							| 
									
										
										
										
											2014-09-14 14:19:36 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-09-16 12:38:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 14:25:11 -05:00
										 |  |  | 			privileges.categories.filterUids('read', result.topicData.cid, uids, function(err, uids) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: data.uid, type: "newTopic"}, function(err, data) { | 
					
						
							|  |  |  | 					uids = data.uidsTo; | 
					
						
							| 
									
										
										
										
											2014-09-18 17:36:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 14:25:11 -05:00
										 |  |  | 					for(var i=0; i<uids.length; ++i) { | 
					
						
							|  |  |  | 						if (parseInt(uids[i], 10) !== socket.uid) { | 
					
						
							|  |  |  | 							websockets.in('uid_' + uids[i]).emit('event:new_post', {posts: [result.postData]}); | 
					
						
							|  |  |  | 							websockets.in('uid_' + uids[i]).emit('event:new_topic', result.topicData); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2014-09-18 17:36:23 -04:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-11-28 14:25:11 -05:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-09-18 17:36:23 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-09-14 14:19:36 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 17:09:52 -04:00
										 |  |  | SocketTopics.enter = function(socket, tid, callback) { | 
					
						
							| 
									
										
										
										
											2014-10-28 23:56:33 -04:00
										 |  |  | 	if (!parseInt(tid, 10) || !socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-08-05 17:09:52 -04:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-09 20:48:35 -05:00
										 |  |  | 	async.parallel({ | 
					
						
							|  |  |  | 		markAsRead: function(next) { | 
					
						
							|  |  |  | 			SocketTopics.markAsRead(socket, [tid], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		users: function(next) { | 
					
						
							|  |  |  | 			websockets.getUsersInRoom(socket.uid, 'topic_' + tid, next); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, function(err, result) { | 
					
						
							|  |  |  | 		callback(err, result ? result.users : null); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | SocketTopics.postcount = function(socket, tid, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	topics.getTopicField(tid, 'postcount', callback); | 
					
						
							| 
									
										
										
										
											2014-06-27 15:35:53 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-10 21:44:19 -04:00
										 |  |  | SocketTopics.markAsRead = function(socket, tids, callback) { | 
					
						
							|  |  |  | 	if(!Array.isArray(tids) || !socket.uid) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-21 10:06:13 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-10 21:44:19 -04:00
										 |  |  | 	if (!tids.length) { | 
					
						
							|  |  |  | 		return callback(); | 
					
						
							| 
									
										
										
										
											2014-03-03 15:26:15 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-19 19:45:16 -04:00
										 |  |  | 	tids = tids.filter(function(tid) { | 
					
						
							|  |  |  | 		return tid && utils.isNumber(tid); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-10-17 17:20:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-10 21:44:19 -04:00
										 |  |  | 	topics.markAsRead(tids, socket.uid, function(err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 			return callback(err); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 17:09:55 -04:00
										 |  |  | 		topics.pushUnreadCount(socket.uid); | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-21 17:48:32 -04:00
										 |  |  | 		for (var i=0; i<tids.length; ++i) { | 
					
						
							|  |  |  | 			topics.markTopicNotificationsRead(tids[i], socket.uid); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-03-03 15:26:15 -05:00
										 |  |  | 		callback(); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-02 19:45:13 -04:00
										 |  |  | SocketTopics.markTopicNotificationsRead = function(socket, tid, callback) { | 
					
						
							|  |  |  | 	if(!tid || !socket.uid) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	topics.markTopicNotificationsRead(tid, socket.uid); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 17:09:55 -04:00
										 |  |  | SocketTopics.markAllRead = function(socket, data, callback) { | 
					
						
							|  |  |  | 	topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-10 21:44:19 -04:00
										 |  |  | 		SocketTopics.markAsRead(socket, tids, callback); | 
					
						
							| 
									
										
										
										
											2014-04-07 17:09:55 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketTopics.markCategoryTopicsRead = function(socket, cid, callback) { | 
					
						
							|  |  |  | 	topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 20:19:10 -05:00
										 |  |  | 		topics.getTopicsFields(tids, ['tid', 'cid'], function(err, topicData) { | 
					
						
							| 
									
										
										
										
											2014-04-07 17:09:55 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			tids = topicData.filter(function(topic) { | 
					
						
							| 
									
										
										
										
											2014-09-10 21:44:19 -04:00
										 |  |  | 				return topic && parseInt(topic.cid, 10) === parseInt(cid, 10); | 
					
						
							| 
									
										
										
										
											2014-04-07 17:09:55 -04:00
										 |  |  | 			}).map(function(topic) { | 
					
						
							|  |  |  | 				return topic.tid; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-10 21:44:19 -04:00
										 |  |  | 			SocketTopics.markAsRead(socket, tids, callback); | 
					
						
							| 
									
										
										
										
											2014-04-07 17:09:55 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | SocketTopics.markAsUnreadForAll = function(socket, tids, callback) { | 
					
						
							| 
									
										
										
										
											2014-08-05 23:47:17 -04:00
										 |  |  | 	if (!Array.isArray(tids)) { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-tid]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 23:47:17 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 23:47:17 -04:00
										 |  |  | 	user.isAdministrator(socket.uid, function(err, isAdmin) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 23:47:17 -04:00
										 |  |  | 		async.each(tids, function(tid, next) { | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					threadTools.exists(tid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(exists, next) { | 
					
						
							|  |  |  | 					if (!exists) { | 
					
						
							|  |  |  | 						return next(new Error('[[error:invalid-tid]]')); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					topics.getTopicField(tid, 'cid', next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(cid, next) { | 
					
						
							|  |  |  | 					user.isModerator(socket.uid, cid, next); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			], function(err, isMod) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-08-05 23:47:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if (!isAdmin && !isMod) { | 
					
						
							|  |  |  | 					return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				topics.markAsUnreadForAll(tid, function(err) { | 
					
						
							|  |  |  | 					if(err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-05 17:39:24 -04:00
										 |  |  | 					topics.updateRecent(tid, Date.now(), function(err) { | 
					
						
							| 
									
										
										
										
											2014-08-05 23:47:17 -04:00
										 |  |  | 						if(err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2014-09-16 11:06:10 -04:00
										 |  |  | 						topics.pushUnreadCount(socket.uid); | 
					
						
							| 
									
										
										
										
											2014-08-05 23:47:17 -04:00
										 |  |  | 						next(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-08-05 23:47:17 -04:00
										 |  |  | 		}, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-03-21 17:59:46 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-17 17:26:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | SocketTopics.delete = function(socket, data, callback) { | 
					
						
							|  |  |  | 	doTopicAction('delete', socket, data, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | SocketTopics.restore = function(socket, data, callback) { | 
					
						
							|  |  |  | 	doTopicAction('restore', socket, data, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | SocketTopics.purge = function(socket, data, callback) { | 
					
						
							|  |  |  | 	doTopicAction('purge', socket, data, function(err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-11-19 23:37:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | 		websockets.in('category_' + data.cid).emit('event:topic_purged', data.tids); | 
					
						
							|  |  |  | 		async.each(data.tids, function(tid, next) { | 
					
						
							|  |  |  | 			websockets.in('topic_' + tid).emit('event:topic_purged', tid); | 
					
						
							|  |  |  | 			next(); | 
					
						
							|  |  |  | 		}, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-06-10 16:56:55 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | SocketTopics.lock = function(socket, data, callback) { | 
					
						
							|  |  |  | 	doTopicAction('lock', socket, data, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | SocketTopics.unlock = function(socket, data, callback) { | 
					
						
							|  |  |  | 	doTopicAction('unlock', socket, data, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | SocketTopics.pin = function(socket, data, callback) { | 
					
						
							|  |  |  | 	doTopicAction('pin', socket, data, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | SocketTopics.unpin = function(socket, data, callback) { | 
					
						
							|  |  |  | 	doTopicAction('unpin', socket, data, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | function doTopicAction(action, socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-10-31 22:04:09 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | 	if(!data || !Array.isArray(data.tids) || !data.cid) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-tid]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | 	async.each(data.tids, function(tid, next) { | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 		privileges.topics.canEdit(tid, socket.uid, function(err, canEdit) { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 			if(!canEdit) { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 				return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 			if(typeof threadTools[action] === 'function') { | 
					
						
							|  |  |  | 				threadTools[action](tid, socket.uid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}, callback); | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | SocketTopics.createTopicFromPosts = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:not-logged-in]]')); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 	if(!data || !data.title || !data.pids || !Array.isArray(data.pids)) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	topics.createTopicFromPosts(socket.uid, data.title, data.pids, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | SocketTopics.movePost = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:not-logged-in]]')); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 	if (!data || !data.pid || !data.tid) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 18:06:59 -04:00
										 |  |  | 	privileges.posts.canMove(data.pid, socket.uid, function(err, canMove) { | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 		if (err || !canMove) { | 
					
						
							|  |  |  | 			return callback(err || new Error('[[error:no-privileges]]')); | 
					
						
							| 
									
										
										
										
											2014-01-19 17:28:08 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | 		topics.movePostToTopic(data.pid, data.tid, function(err) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, 'notifications:moved_your_post'); | 
					
						
							|  |  |  | 			callback(); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-01-19 17:28:08 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | SocketTopics.move = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 	if(!data || !Array.isArray(data.tids) || !data.cid) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | 	async.eachLimit(data.tids, 10, function(tid, next) { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 		var oldCid; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 				privileges.topics.canMove(tid, socket.uid, next); | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 			function(canMove, next) { | 
					
						
							|  |  |  | 				if (!canMove) { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 					return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				topics.getTopicField(tid, 'cid', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(cid, next) { | 
					
						
							|  |  |  | 				oldCid = cid; | 
					
						
							| 
									
										
										
										
											2014-07-24 16:17:38 -04:00
										 |  |  | 				threadTools.move(tid, data.cid, socket.uid, next); | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		], function(err) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:40:47 -05:00
										 |  |  | 			websockets.in('topic_' + tid).emit('event:topic_moved', { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 				tid: tid | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:40:47 -05:00
										 |  |  | 			websockets.in('category_' + oldCid).emit('event:topic_moved', { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 				tid: tid | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | 			SocketTopics.sendNotificationToTopicOwner(tid, socket.uid, 'notifications:moved_your_topic'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 			next(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SocketTopics.sendNotificationToTopicOwner = function(tid, fromuid, notification) { | 
					
						
							|  |  |  | 	if(!tid || !fromuid) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async.parallel({ | 
					
						
							|  |  |  | 		username: async.apply(user.getUserField, fromuid, 'username'), | 
					
						
							|  |  |  | 		topicData: async.apply(topics.getTopicFields, tid, ['uid', 'slug']), | 
					
						
							|  |  |  | 	}, function(err, results) { | 
					
						
							|  |  |  | 		if (err || fromuid === parseInt(results.topicData.uid, 10)) { | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		notifications.create({ | 
					
						
							|  |  |  | 			bodyShort: '[[' + notification + ', ' + results.username + ']]', | 
					
						
							|  |  |  | 			path: nconf.get('relative_path') + '/topic/' + results.topicData.slug, | 
					
						
							| 
									
										
										
										
											2014-09-12 16:35:30 -04:00
										 |  |  | 			nid: 'tid:' + tid + ':uid:' + fromuid, | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | 			from: fromuid | 
					
						
							| 
									
										
										
										
											2014-09-08 23:03:37 -04:00
										 |  |  | 		}, function(err, notification) { | 
					
						
							|  |  |  | 			if (!err && notification) { | 
					
						
							|  |  |  | 				notifications.push(notification, [results.topicData.uid]); | 
					
						
							| 
									
										
										
										
											2014-08-12 13:45:18 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-21 13:29:40 -04:00
										 |  |  | SocketTopics.moveAll = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data || !data.cid || !data.currentCid) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 	privileges.categories.canMoveAllTopics(data.currentCid, data.cid, data.uid, function(err, canMove) { | 
					
						
							|  |  |  | 		if (err || canMove) { | 
					
						
							|  |  |  | 			return callback(err || new Error('[[error:no-privileges]]')); | 
					
						
							| 
									
										
										
										
											2014-04-21 13:29:40 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-07 17:15:01 -05:00
										 |  |  | 		categories.getTopicIds('cid:' + data.currentCid + ':tids', 0, -1, function(err, tids) { | 
					
						
							| 
									
										
										
										
											2014-04-21 13:29:40 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-02 16:48:34 -04:00
										 |  |  | 			async.eachLimit(tids, 10, function(tid, next) { | 
					
						
							| 
									
										
										
										
											2014-07-24 16:17:38 -04:00
										 |  |  | 				threadTools.move(tid, data.cid, socket.uid, next); | 
					
						
							| 
									
										
										
										
											2014-04-21 13:29:40 -04:00
										 |  |  | 			}, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | SocketTopics.follow = function(socket, tid, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 	if(!socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:not-logged-in]]')); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	threadTools.toggleFollow(tid, socket.uid, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | SocketTopics.loadMore = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-10-12 04:01:35 +02:00
										 |  |  | 	if(!data || !data.tid || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0)  { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 23:22:21 -04:00
										 |  |  | 	async.parallel({ | 
					
						
							|  |  |  | 		settings: function(next) { | 
					
						
							|  |  |  | 			user.getSettings(socket.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		privileges: function(next) { | 
					
						
							|  |  |  | 			privileges.topics.get(data.tid, socket.uid, next); | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		postCount: function(next) { | 
					
						
							|  |  |  | 			topics.getPostCount(data.tid, next); | 
					
						
							| 
									
										
										
										
											2014-08-05 23:22:21 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}, function(err, results) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							| 
									
										
										
										
											2014-03-23 15:05:04 -04:00
										 |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-01-24 20:00:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-05 23:22:21 -04:00
										 |  |  | 		if (!results.privileges.read) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 		var set = 'tid:' + data.tid + ':posts', | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 			reverse = false, | 
					
						
							|  |  |  | 			start = Math.max(parseInt(data.after, 10) - 1, 0); | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-27 23:20:40 -04:00
										 |  |  | 		if (results.settings.topicPostSort === 'newest_to_oldest' || results.settings.topicPostSort === 'most_votes') { | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 			reverse = true; | 
					
						
							| 
									
										
										
										
											2014-10-27 23:20:40 -04:00
										 |  |  | 			data.after = results.postCount - 1 - data.after; | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 			start = Math.max(parseInt(data.after, 10), 0); | 
					
						
							| 
									
										
										
										
											2014-10-27 23:20:40 -04:00
										 |  |  | 			if (results.settings.topicPostSort === 'most_votes') { | 
					
						
							|  |  |  | 				set = 'tid:' + data.tid + ':posts:votes'; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 		var end = start + results.settings.postsPerPage - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			posts: function(next) { | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 				topics.getTopicPosts(data.tid, set, start, end, socket.uid, reverse, next); | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			privileges: function(next) { | 
					
						
							| 
									
										
										
										
											2014-08-05 23:22:21 -04:00
										 |  |  | 				next(null, results.privileges); | 
					
						
							| 
									
										
										
										
											2014-07-09 16:59:34 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			'reputation:disabled': function(next) { | 
					
						
							|  |  |  | 				next(null, parseInt(meta.config['reputation:disabled'], 10) === 1); | 
					
						
							| 
									
										
										
										
											2014-09-04 01:48:57 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			'downvote:disabled': function(next) { | 
					
						
							|  |  |  | 				next(null, parseInt(meta.config['downvote:disabled'], 10) === 1); | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-10 00:17:06 -04:00
										 |  |  | 		}, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | SocketTopics.loadMoreUnreadTopics = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-29 15:08:14 -05:00
										 |  |  | 	if(!data || !data.after) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-29 15:08:14 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-26 17:17:34 -05:00
										 |  |  | 	var start = parseInt(data.after, 10), | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 		end = start + 9; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 	topics.getUnreadTopics(socket.uid, start, end, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:46:25 -05:00
										 |  |  | SocketTopics.loadMoreFromSet = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data || !data.after || !data.set) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-30 19:46:25 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var start = parseInt(data.after, 10), | 
					
						
							|  |  |  | 		end = start + 9; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-16 01:33:11 -05:00
										 |  |  | 	topics.getTopicsFromSet(data.set, socket.uid, start, end, callback); | 
					
						
							| 
									
										
										
										
											2014-01-30 19:46:25 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-19 14:31:46 -04:00
										 |  |  | SocketTopics.loadTopics = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data || !data.set || !utils.isNumber(data.start) || !utils.isNumber(data.end)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-16 01:33:11 -05:00
										 |  |  | 	topics.getTopicsFromSet(data.set, socket.uid, data.start, data.end, callback); | 
					
						
							| 
									
										
										
										
											2014-04-19 14:31:46 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-24 20:00:56 -05:00
										 |  |  | SocketTopics.getPageCount = function(socket, tid, callback) { | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 	topics.getPageCount(tid, socket.uid, callback); | 
					
						
							| 
									
										
										
										
											2014-01-30 19:46:25 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-24 20:00:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 23:45:12 -05:00
										 |  |  | SocketTopics.getTidPage = function(socket, tid, callback) { | 
					
						
							|  |  |  | 	topics.getTidPage(tid, socket.uid, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketTopics.getTidIndex = function(socket, tid, callback) { | 
					
						
							|  |  |  | 	categories.getTopicIndex(tid, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-15 21:25:57 -04:00
										 |  |  | SocketTopics.searchTags = function(socket, data, callback) { | 
					
						
							|  |  |  | 	topics.searchTags(data, callback); | 
					
						
							| 
									
										
										
										
											2014-05-21 16:13:46 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-24 17:30:37 -04:00
										 |  |  | SocketTopics.search = function(socket, data, callback) { | 
					
						
							|  |  |  | 	topics.search(data.tid, data.term, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-27 18:03:53 -04:00
										 |  |  | SocketTopics.searchAndLoadTags = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-10-17 19:12:18 -04:00
										 |  |  | 	if (!data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!data.query || !data.query.length) { | 
					
						
							|  |  |  | 		return callback(null, []); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-27 18:03:53 -04:00
										 |  |  | 	topics.searchTags(data, function(err, tags) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			counts: function(next) { | 
					
						
							|  |  |  | 				db.sortedSetScores('tags:topic:count', tags, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			tagData: function(next) { | 
					
						
							|  |  |  | 				tags = tags.map(function(tag) { | 
					
						
							|  |  |  | 					return {value: tag}; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				topics.getTagData(tags, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, function(err, results) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			results.tagData.forEach(function(tag, index) { | 
					
						
							|  |  |  | 				tag.score = results.counts[index]; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			results.tagData.sort(function(a, b) { | 
					
						
							| 
									
										
										
										
											2014-09-07 13:03:06 -04:00
										 |  |  | 				return b.score - a.score; | 
					
						
							| 
									
										
										
										
											2014-08-27 18:03:53 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, results.tagData); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketTopics.loadMoreTags = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data || !data.after) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var start = parseInt(data.after, 10), | 
					
						
							|  |  |  | 		end = start + 99; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	topics.getTags(start, end, function(err, tags) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		callback(null, {tags: tags, nextStart: end + 1}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = SocketTopics; |