| 
									
										
										
										
											2014-03-11 14:48:35 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-30 17:41:54 -05:00
										 |  |  | var	async = require('async'), | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-30 17:41:54 -05:00
										 |  |  | 	posts = require('../posts'), | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 	privileges = require('../privileges'), | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 	meta = require('../meta'), | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							| 
									
										
										
										
											2014-01-22 21:08:43 +01:00
										 |  |  | 	user = require('../user'), | 
					
						
							| 
									
										
										
										
											2014-04-09 20:55:49 -04:00
										 |  |  | 	websockets = require('./index'), | 
					
						
							| 
									
										
										
										
											2015-03-09 13:16:14 -04:00
										 |  |  | 	socketTopics = require('./topics'), | 
					
						
							| 
									
										
										
										
											2015-10-02 18:55:23 -04:00
										 |  |  | 	socketHelpers = require('./helpers'), | 
					
						
							| 
									
										
										
										
											2014-11-17 23:57:31 -05:00
										 |  |  | 	utils = require('../../public/src/utils'), | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-27 00:47:08 -04:00
										 |  |  | 	SocketPosts = {}; | 
					
						
							| 
									
										
										
										
											2014-04-26 03:00:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | require('./posts/edit')(SocketPosts); | 
					
						
							|  |  |  | require('./posts/move')(SocketPosts); | 
					
						
							|  |  |  | require('./posts/favourites')(SocketPosts); | 
					
						
							|  |  |  | require('./posts/tools')(SocketPosts); | 
					
						
							|  |  |  | require('./posts/flag')(SocketPosts); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:34:43 -05:00
										 |  |  | SocketPosts.reply = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-10-02 18:55:23 -04:00
										 |  |  | 	if (!data || !data.tid || !data.content) { | 
					
						
							| 
									
										
										
										
											2014-04-09 14:12:46 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-16 22:06:23 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-22 17:56:13 -05:00
										 |  |  | 	data.uid = socket.uid; | 
					
						
							| 
									
										
										
										
											2014-04-27 00:47:08 -04:00
										 |  |  | 	data.req = websockets.reqFromSocket(socket); | 
					
						
							| 
									
										
										
										
											2014-02-22 17:56:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	topics.reply(data, function(err, postData) { | 
					
						
							| 
									
										
										
										
											2014-09-14 14:19:36 -04:00
										 |  |  | 		if (err) { | 
					
						
							| 
									
										
										
										
											2014-01-16 22:06:23 -05:00
										 |  |  | 			return callback(err); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-14 14:19:36 -04:00
										 |  |  | 		var result = { | 
					
						
							|  |  |  | 			posts: [postData], | 
					
						
							|  |  |  | 			privileges: { | 
					
						
							| 
									
										
										
										
											2014-05-22 18:24:58 -04:00
										 |  |  | 				'topics:reply': true | 
					
						
							| 
									
										
										
										
											2014-09-14 14:19:36 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			'reputation:disabled': parseInt(meta.config['reputation:disabled'], 10) === 1, | 
					
						
							|  |  |  | 			'downvote:disabled': parseInt(meta.config['downvote:disabled'], 10) === 1, | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 14:37:46 -04:00
										 |  |  | 		callback(null, postData); | 
					
						
							| 
									
										
										
										
											2014-09-14 14:19:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		socket.emit('event:new_post', result); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-25 15:51:11 -04:00
										 |  |  | 		user.updateOnlineUsers(socket.uid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 18:55:23 -04:00
										 |  |  | 		socketHelpers.notifyOnlineUsers(socket.uid, result); | 
					
						
							| 
									
										
										
										
											2015-03-18 17:50:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (data.lock) { | 
					
						
							|  |  |  | 			socketTopics.doTopicAction('lock', 'event:topic_locked', socket, {tids: [postData.topic.tid], cid: postData.topic.cid}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-31 15:42:10 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-09-18 17:09:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 22:06:23 -05:00
										 |  |  | SocketPosts.getRawPost = function(socket, pid, callback) { | 
					
						
							| 
									
										
										
										
											2014-05-08 13:24:34 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							| 
									
										
										
										
											2014-05-17 18:59:34 -04:00
										 |  |  | 			privileges.posts.can('read', pid, socket.uid, next); | 
					
						
							| 
									
										
										
										
											2014-05-08 13:24:34 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 		function(canRead, next) { | 
					
						
							|  |  |  | 			if (!canRead) { | 
					
						
							| 
									
										
										
										
											2014-05-08 13:24:34 -04:00
										 |  |  | 				return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			posts.getPostFields(pid, ['content', 'deleted'], next); | 
					
						
							| 
									
										
										
										
											2014-08-02 16:41:44 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function(postData, next) { | 
					
						
							|  |  |  | 			if (parseInt(postData.deleted, 10) === 1) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-post]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			next(null, postData.content); | 
					
						
							| 
									
										
										
										
											2014-05-08 13:24:34 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-08-02 16:41:44 -04:00
										 |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | SocketPosts.loadMoreFavourites = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 	loadMorePosts('uid:' + data.uid + ':favourites', socket.uid, data, callback); | 
					
						
							| 
									
										
										
										
											2014-02-03 19:24:27 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-04 17:31:05 -05:00
										 |  |  | SocketPosts.loadMoreUserPosts = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 	loadMorePosts('uid:' + data.uid + ':posts', socket.uid, data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function loadMorePosts(set, uid, data, callback) { | 
					
						
							|  |  |  | 	if (!data || !utils.isNumber(data.uid) || !utils.isNumber(data.after)) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:40:39 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-02-04 17:31:05 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 23:57:31 -05:00
										 |  |  | 	var start = Math.max(0, parseInt(data.after, 10)), | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 		stop = start + 9; | 
					
						
							| 
									
										
										
										
											2014-02-04 17:31:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 15:43:57 -04:00
										 |  |  | 	posts.getPostSummariesFromSet(set, uid, start, stop, callback); | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-03-11 21:52:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 20:24:04 -04:00
										 |  |  | SocketPosts.getCategory = function(socket, pid, callback) { | 
					
						
							|  |  |  | 	posts.getCidByPid(pid, callback); | 
					
						
							| 
									
										
										
										
											2014-03-24 14:30:11 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-03-11 18:46:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-14 21:04:56 -04:00
										 |  |  | SocketPosts.getPidIndex = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if (!data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback); | 
					
						
							| 
									
										
										
										
											2014-08-27 15:03:36 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = SocketPosts; |