| 
									
										
										
										
											2014-03-03 15:26:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | var topics = require('../topics'), | 
					
						
							| 
									
										
										
										
											2014-02-27 23:45:12 -05:00
										 |  |  | 	categories = require('../categories'), | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	threadTools = require('../threadTools'), | 
					
						
							| 
									
										
										
										
											2014-04-21 13:29:40 -04:00
										 |  |  | 	categoryTools = require('../categoryTools'), | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 	index = require('./index'), | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 	user = require('../user'), | 
					
						
							| 
									
										
										
										
											2014-02-21 22:13:13 -05:00
										 |  |  | 	db = require('./../database'), | 
					
						
							| 
									
										
										
										
											2014-03-21 17:59:46 -04:00
										 |  |  | 	meta = require('./../meta'), | 
					
						
							| 
									
										
										
										
											2014-04-19 14:31:46 -04:00
										 |  |  | 	utils = require('../../public/src/utils'), | 
					
						
							| 
									
										
										
										
											2014-01-26 16:22:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-14 07:39:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!socket.uid && !parseInt(meta.config.allowGuestPosting, 10)) { | 
					
						
							| 
									
										
										
										
											2014-04-09 14:12:46 -04:00
										 |  |  | 		return callback(new Error('[[error:not-logged-in]]')); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-20 02:05:49 -05:00
										 |  |  | 	topics.post({uid: socket.uid, title: data.title, content: data.content, cid: data.category_id, thumb: data.topic_thumb}, function(err, result) { | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 		if(err) { | 
					
						
							| 
									
										
										
										
											2014-01-23 15:46:39 -05:00
										 |  |  | 			return callback(err); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (result) { | 
					
						
							| 
									
										
										
										
											2014-03-11 15:18:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-24 15:53:41 -04:00
										 |  |  | 			index.server.sockets.in('category_' + data.category_id).emit('event:new_topic', result.topicData); | 
					
						
							|  |  |  | 			index.server.sockets.in('recent_posts').emit('event:new_topic', result.topicData); | 
					
						
							|  |  |  | 			index.server.sockets.in('home').emit('event:new_topic', result.topicData); | 
					
						
							| 
									
										
										
										
											2014-03-11 18:46:16 -04:00
										 |  |  | 			index.server.sockets.in('home').emit('event:new_post', { | 
					
						
							|  |  |  | 				posts: result.postData | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 			index.server.sockets.in('user/' + socket.uid).emit('event:new_post', { | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 				posts: result.postData | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			module.parent.exports.emitTopicPostStats(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-24 15:53:41 -04:00
										 |  |  | 			callback(null, result.topicData); | 
					
						
							| 
									
										
										
										
											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-01-21 10:06:13 -05:00
										 |  |  | SocketTopics.markAsRead = function(socket, data) { | 
					
						
							|  |  |  | 	if(!data || !data.tid || !data.uid) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	topics.markAsRead(data.tid, data.uid, function(err) { | 
					
						
							|  |  |  | 		topics.pushUnreadCount(data.uid); | 
					
						
							| 
									
										
										
										
											2014-03-21 17:48:32 -04:00
										 |  |  | 		topics.markTopicNotificationsRead(data.tid, data.uid); | 
					
						
							| 
									
										
										
										
											2014-01-21 10:06:13 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-02-21 22:13:13 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-21 10:06:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 17:09:55 -04:00
										 |  |  | SocketTopics.markTidsRead = function(socket, tids, callback) { | 
					
						
							| 
									
										
										
										
											2014-03-03 15:26:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-21 17:48:32 -04:00
										 |  |  | 	if (!Array.isArray(tids)) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-03-03 15:26:15 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 17:09:55 -04:00
										 |  |  | 	topics.markTidsRead(socket.uid, tids, function(err) { | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 		if(err) { | 
					
						
							|  |  |  | 			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-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); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		SocketTopics.markTidsRead(socket, tids, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketTopics.markCategoryTopicsRead = function(socket, cid, callback) { | 
					
						
							|  |  |  | 	topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var keys = tids.map(function(tid) { | 
					
						
							|  |  |  | 			return 'topic:' + tid; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		db.getObjectsFields(keys, ['tid', 'cid'], function(err, topicData) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			tids = topicData.filter(function(topic) { | 
					
						
							|  |  |  | 				return parseInt(topic.cid, 10) === parseInt(cid, 10); | 
					
						
							|  |  |  | 			}).map(function(topic) { | 
					
						
							|  |  |  | 				return topic.tid; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			SocketTopics.markTidsRead(socket, tids, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | SocketTopics.markAsUnreadForAll = function(socket, tids, callback) { | 
					
						
							|  |  |  | 	if(!Array.isArray(tids)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-tid]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 	async.each(tids, function(tid, next) { | 
					
						
							|  |  |  | 		topics.markAsUnreadForAll(tid, function(err) { | 
					
						
							| 
									
										
										
										
											2014-02-21 22:13:13 -05:00
										 |  |  | 			if(err) { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 				return next(err); | 
					
						
							| 
									
										
										
										
											2014-02-21 22:13:13 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			db.sortedSetAdd('topics:recent', Date.now(), tid, function(err) { | 
					
						
							|  |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				topics.pushUnreadCount(); | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-02-21 22:13:13 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 	}, callback); | 
					
						
							| 
									
										
										
										
											2014-03-21 17:59:46 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-17 17:26:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | SocketTopics.delete = function(socket, tids, callback) { | 
					
						
							|  |  |  | 	doTopicAction('delete', socket, tids, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | SocketTopics.restore = function(socket, tids, callback) { | 
					
						
							|  |  |  | 	doTopicAction('restore', socket, tids, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | SocketTopics.lock = function(socket, tids, callback) { | 
					
						
							|  |  |  | 	doTopicAction('lock', socket, tids, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | SocketTopics.unlock = function(socket, tids, callback) { | 
					
						
							|  |  |  | 	doTopicAction('unlock', socket, tids, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | SocketTopics.pin = function(socket, tids, callback) { | 
					
						
							|  |  |  | 	doTopicAction('pin', socket, tids, callback); | 
					
						
							| 
									
										
										
										
											2014-01-16 19:58:57 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | SocketTopics.unpin = function(socket, tids, callback) { | 
					
						
							|  |  |  | 	doTopicAction('unpin', socket, tids, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | function doTopicAction(action, socket, tids, callback) { | 
					
						
							|  |  |  | 	if(!tids) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-tid]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 	async.each(tids, function(tid, next) { | 
					
						
							|  |  |  | 		threadTools.privileges(tid, socket.uid, function(err, privileges) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 			if(!privileges || !privileges.editable) { | 
					
						
							|  |  |  | 				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) { | 
					
						
							|  |  |  | 	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.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-01-19 17:28:08 -05:00
										 |  |  | 	threadTools.privileges(data.tid, socket.uid, function(err, privileges) { | 
					
						
							|  |  |  | 		if(err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(!(privileges.admin || privileges.moderator)) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 			return callback(new Error('[[error:no-privileges]]')); | 
					
						
							| 
									
										
										
										
											2014-01-19 17:28:08 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		topics.movePostToTopic(data.pid, data.tid, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											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-04-17 20:07:23 -04:00
										 |  |  | 	async.each(data.tids, function(tid, next) { | 
					
						
							|  |  |  | 		var oldCid; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				threadTools.privileges(tid, socket.uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(privileges, next) { | 
					
						
							|  |  |  | 				if(!(privileges.admin || privileges.moderator)) { | 
					
						
							|  |  |  | 					return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				topics.getTopicField(tid, 'cid', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(cid, next) { | 
					
						
							|  |  |  | 				oldCid = cid; | 
					
						
							|  |  |  | 				threadTools.move(tid, data.cid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], function(err) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 			index.server.sockets.in('topic_' + tid).emit('event:topic_moved', { | 
					
						
							|  |  |  | 				tid: tid | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 			index.server.sockets.in('category_' + oldCid).emit('event:topic_moved', { | 
					
						
							|  |  |  | 				tid: tid | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			next(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05: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]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async.parallel({ | 
					
						
							|  |  |  | 		from: function(next) { | 
					
						
							|  |  |  | 			categoryTools.privileges(data.currentCid, socket.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		to: function(next) { | 
					
						
							|  |  |  | 			categoryTools.privileges(data.cid, socket.uid, next); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, function(err, results) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!results.from.admin && (!results.from.moderator || !results.to.moderator)) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		categories.getTopicIds(data.currentCid, 0, -1, function(err, tids) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.each(tids, function(tid, next) { | 
					
						
							|  |  |  | 				threadTools.move(tid, data.cid, next); | 
					
						
							|  |  |  | 			}, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | SocketTopics.followCheck = function(socket, tid, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -05:00
										 |  |  | 	threadTools.isFollowing(tid, socket.uid, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-03-23 15:05:04 -04:00
										 |  |  | 	if(!data || !data.tid || !(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-02-10 14:15:54 -05:00
										 |  |  | 	user.getSettings(socket.uid, function(err, settings) { | 
					
						
							| 
									
										
										
										
											2014-03-23 15:05:04 -04:00
										 |  |  | 		if(err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-01-24 20:00:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 		var start = parseInt(data.after, 10), | 
					
						
							|  |  |  | 			end = start + settings.postsPerPage - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			posts: function(next) { | 
					
						
							| 
									
										
										
										
											2014-02-26 16:43:21 -05:00
										 |  |  | 				topics.getTopicPosts(data.tid, start, end, socket.uid, false, next); | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			privileges: function(next) { | 
					
						
							|  |  |  | 				threadTools.privileges(data.tid, socket.uid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											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.loadMoreRecentTopics = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-29 15:08:14 -05:00
										 |  |  | 	if(!data || !data.term || !data.after) { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:51:10 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-16 20:53:32 -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.getLatestTopics(socket.uid, start, end, data.term, 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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	topics.getTopicsFromSet(socket.uid, data.set, start, end, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	topics.getTopicsFromSet(socket.uid, data.set, data.start, data.end, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-04-10 20:31:57 +01:00
										 |  |  | module.exports = SocketTopics; |