all files / src/socket.io/ topics.js

39.66% Statements 23/58
0% Branches 0/24
0% Functions 0/14
39.66% Lines 23/58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                                                                                                                                                               
 
'use strict';
 
var async = require('async');
 
var topics = require('../topics');
var websockets = require('./index');
var user = require('../user');
var apiController = require('../controllers/api');
var socketHelpers = require('./helpers');
 
var SocketTopics = {};
 
require('./topics/unread')(SocketTopics);
require('./topics/move')(SocketTopics);
require('./topics/tools')(SocketTopics);
require('./topics/infinitescroll')(SocketTopics);
require('./topics/tags')(SocketTopics);
 
SocketTopics.post = function(socket, data, callback) {
	if (!data) {
		return callback(new Error('[[error:invalid-data]]'));
	}
 
	topics.post({
		uid: socket.uid,
		handle: data.handle,
		title: data.title,
		content: data.content,
		cid: data.category_id,
		thumb: data.topic_thumb,
		tags: data.tags,
		req: websockets.reqFromSocket(socket)
	}, function(err, result) {
		if (err) {
			return callback(err);
		}
 
		if (data.lock) {
			SocketTopics.doTopicAction('lock', 'event:topic_locked', socket, {tids: [result.topicData.tid], cid: result.topicData.cid});
		}
 
		callback(null, result.topicData);
 
		socket.emit('event:new_post', {posts: [result.postData]});
		socket.emit('event:new_topic', result.topicData);
 
		socketHelpers.notifyNew(socket.uid, 'newTopic', {posts: [result.postData], topic: result.topicData});
	});
};
 
SocketTopics.postcount = function(socket, tid, callback) {
	topics.getTopicField(tid, 'postcount', callback);
};
 
SocketTopics.bookmark = function(socket, data, callback) {
	if (!socket.uid || !data) {
		return callback(new Error('[[error:invalid-data]]'));
	}
	topics.setUserBookmark(data.tid, socket.uid, data.index, callback);
};
 
SocketTopics.createTopicFromPosts = function(socket, data, callback) {
	if (!socket.uid) {
		return callback(new Error('[[error:not-logged-in]]'));
	}
 
	if (!data || !data.title || !data.pids || !Array.isArray(data.pids)) {
		return callback(new Error('[[error:invalid-data]]'));
	}
 
	topics.createTopicFromPosts(socket.uid, data.title, data.pids, data.fromTid, callback);
};
 
SocketTopics.toggleFollow = function(socket, tid, callback) {
	followCommand(topics.toggleFollow, socket, tid, callback);
};
 
SocketTopics.follow = function(socket, tid, callback) {
	followCommand(topics.follow, socket, tid, callback);
};
 
function followCommand(method, socket, tid, callback) {
	if (!socket.uid) {
		return callback(new Error('[[error:not-logged-in]]'));
	}
 
	method(tid, socket.uid, callback);
}
 
SocketTopics.search = function(socket, data, callback) {
	topics.search(data.tid, data.term, callback);
};
 
SocketTopics.isModerator = function(socket, tid, callback) {
	topics.getTopicField(tid, 'cid', function(err, cid) {
		if (err) {
			return callback(err);
		}
		user.isModerator(socket.uid, cid, callback);
	});
};
 
SocketTopics.getTopic = function (socket, tid, callback) {
	async.waterfall([
		function (next) {
			apiController.getObjectByType(socket.uid, 'topic', tid, next);
		},
		function (topicData, next) {
			if (parseInt(topicData.deleted, 10) === 1) {
				return next(new Error('[[error:no-topic]]'));
			}
			next(null, topicData);
		}
	], callback);
};
 
module.exports = SocketTopics;