| 
									
										
										
										
											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-05-15 10:38:02 -04:00
										 |  |  | 	privileges = require('../privileges'), | 
					
						
							| 
									
										
										
										
											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-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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-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-27 00:47:08 -04:00
										 |  |  | 			websockets.server.sockets.in('category_' + data.category_id).emit('event:new_topic', result.topicData); | 
					
						
							|  |  |  | 			websockets.server.sockets.in('recent_posts').emit('event:new_topic', result.topicData); | 
					
						
							|  |  |  | 			websockets.server.sockets.in('home').emit('event:new_topic', result.topicData); | 
					
						
							|  |  |  | 			websockets.server.sockets.in('home').emit('event:new_post', { | 
					
						
							| 
									
										
										
										
											2014-03-11 18:46:16 -04:00
										 |  |  | 				posts: result.postData | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-04-27 00:47:08 -04:00
										 |  |  | 			websockets.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-07-17 18:50:03 -04:00
										 |  |  | 			topics.pushUnreadCount(); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-06-27 15:35:53 -04:00
										 |  |  | SocketTopics.lastPostIndex = function(socket, tid, callback) { | 
					
						
							|  |  |  | 	db.sortedSetCard('tid:' + tid + ':posts', callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-28 18:20:02 -04:00
										 |  |  | SocketTopics.increaseViewCount = function(socket, tid) { | 
					
						
							|  |  |  | 	topics.increaseViewCount(tid); | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-05-28 18:20:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-28 18:23:59 -04:00
										 |  |  | SocketTopics.markAsRead = function(socket, tid) { | 
					
						
							|  |  |  | 	if(!tid || !socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-01-21 10:06:13 -05:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-28 18:22:34 -04:00
										 |  |  | 	topics.markAsRead(tid, socket.uid, function(err) { | 
					
						
							| 
									
										
										
										
											2014-05-28 18:21:15 -04:00
										 |  |  | 		topics.pushUnreadCount(socket.uid); | 
					
						
							| 
									
										
										
										
											2014-05-28 18:22:34 -04:00
										 |  |  | 		topics.markTopicNotificationsRead(tid, socket.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-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); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		websockets.emitTopicPostStats(); | 
					
						
							|  |  |  | 		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) { | 
					
						
							|  |  |  | 	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
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 				threadTools.move(tid, data.cid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], function(err) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-16 16:10:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-27 00:47:08 -04:00
										 |  |  | 			websockets.server.sockets.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-04-27 00:47:08 -04:00
										 |  |  | 			websockets.server.sockets.in('category_' + oldCid).emit('event:topic_moved', { | 
					
						
							| 
									
										
										
										
											2014-04-17 20:07:23 -04:00
										 |  |  | 				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]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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-04-24 20:05:05 -04:00
										 |  |  | 	topics.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-06-06 22:12:14 -04:00
										 |  |  | 		var start = Math.max(parseInt(data.after, 10) - 1, 0), | 
					
						
							| 
									
										
										
										
											2014-02-10 14:15:54 -05:00
										 |  |  | 			end = start + settings.postsPerPage - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 		var set = 'tid:' + data.tid + ':posts', | 
					
						
							|  |  |  | 			reverse = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (settings.topicPostSort === 'newest_to_oldest') { | 
					
						
							|  |  |  | 			reverse = true; | 
					
						
							|  |  |  | 		} else if (settings.topicPostSort === 'most_votes') { | 
					
						
							|  |  |  | 			reverse = true; | 
					
						
							|  |  |  | 			set = 'tid:' + data.tid + ':posts:votes'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-15 10:38:02 -04:00
										 |  |  | 				privileges.topics.get(data.tid, socket.uid, next); | 
					
						
							| 
									
										
										
										
											2014-07-09 16:59:34 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			'reputation:disabled': function(next) { | 
					
						
							|  |  |  | 				next(null, parseInt(meta.config['reputation: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.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-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-04-10 20:31:57 +01:00
										 |  |  | module.exports = SocketTopics; |