2014-03-03 15:26:15 -05:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2014-08-12 13:45:18 -04:00
|
|
|
var nconf = require('nconf'),
|
|
|
|
|
async = require('async'),
|
2014-12-22 00:12:47 -05:00
|
|
|
winston = require('winston'),
|
2014-08-12 13:45:18 -04:00
|
|
|
|
|
|
|
|
topics = require('../topics'),
|
2014-05-15 10:38:02 -04:00
|
|
|
privileges = require('../privileges'),
|
2014-09-18 17:44:24 -04:00
|
|
|
plugins = require('../plugins'),
|
2014-08-12 13:45:18 -04:00
|
|
|
notifications = require('../notifications'),
|
2014-04-27 00:47:08 -04:00
|
|
|
websockets = require('./index'),
|
2014-02-10 14:15:54 -05:00
|
|
|
user = require('../user'),
|
2014-01-26 16:22:50 -05:00
|
|
|
|
2014-01-10 10:46:26 -05:00
|
|
|
SocketTopics = {};
|
|
|
|
|
|
2015-09-25 15:09:25 -04:00
|
|
|
require('./topics/unread')(SocketTopics);
|
|
|
|
|
require('./topics/move')(SocketTopics);
|
|
|
|
|
require('./topics/tools')(SocketTopics);
|
|
|
|
|
require('./topics/infinitescroll')(SocketTopics);
|
|
|
|
|
require('./topics/tags')(SocketTopics);
|
2015-02-01 21:38:10 -05:00
|
|
|
|
2014-01-16 16:10:23 -05:00
|
|
|
SocketTopics.post = function(socket, data, callback) {
|
2015-09-25 15:09:25 -04: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,
|
2014-12-31 16:27:35 -05:00
|
|
|
handle: data.handle,
|
2014-04-27 00:47:08 -04:00
|
|
|
title: data.title,
|
|
|
|
|
content: data.content,
|
|
|
|
|
cid: data.category_id,
|
|
|
|
|
thumb: data.topic_thumb,
|
2014-05-21 16:13:46 -04:00
|
|
|
tags: data.tags,
|
2014-04-27 00:47:08 -04:00
|
|
|
req: websockets.reqFromSocket(socket)
|
|
|
|
|
}, function(err, result) {
|
2014-09-14 14:19:36 -04:00
|
|
|
if (err) {
|
2014-01-23 15:46:39 -05:00
|
|
|
return callback(err);
|
2014-01-10 10:46:26 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-18 17:50:47 -04:00
|
|
|
if (data.lock) {
|
|
|
|
|
SocketTopics.doTopicAction('lock', 'event:topic_locked', socket, {tids: [result.topicData.tid], cid: result.topicData.cid});
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-14 14:19:36 -04:00
|
|
|
callback(null, result.topicData);
|
2014-10-09 18:12:40 -04:00
|
|
|
socket.emit('event:new_post', {posts: [result.postData]});
|
2014-09-14 14:19:36 -04:00
|
|
|
socket.emit('event:new_topic', result.topicData);
|
|
|
|
|
|
2014-12-22 00:12:47 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
|
|
|
|
user.getUidsFromSet('users:online', 0, -1, next);
|
|
|
|
|
},
|
|
|
|
|
function(uids, next) {
|
|
|
|
|
privileges.categories.filterUids('read', result.topicData.cid, uids, next);
|
|
|
|
|
},
|
|
|
|
|
function(uids, next) {
|
|
|
|
|
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: data.uid, type: 'newTopic'}, next);
|
|
|
|
|
}
|
|
|
|
|
], function(err, data) {
|
2014-09-14 14:19:36 -04:00
|
|
|
if (err) {
|
2014-12-22 00:12:47 -05:00
|
|
|
return winston.error(err.stack);
|
2014-09-14 14:19:36 -04:00
|
|
|
}
|
2014-09-16 12:38:27 -04:00
|
|
|
|
2014-12-22 00:12:47 -05:00
|
|
|
var uids = data.uidsTo;
|
2014-09-18 17:36:23 -04:00
|
|
|
|
2014-12-22 00:12:47 -05:00
|
|
|
for(var i=0; i<uids.length; ++i) {
|
|
|
|
|
if (parseInt(uids[i], 10) !== socket.uid) {
|
|
|
|
|
websockets.in('uid_' + uids[i]).emit('event:new_post', {posts: [result.postData]});
|
|
|
|
|
websockets.in('uid_' + uids[i]).emit('event:new_topic', result.topicData);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-14 14:19:36 -04:00
|
|
|
});
|
2014-01-10 10:46:26 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-05 17:09:52 -04:00
|
|
|
SocketTopics.enter = function(socket, tid, callback) {
|
2014-10-28 23:56:33 -04:00
|
|
|
if (!parseInt(tid, 10) || !socket.uid) {
|
2014-08-05 17:09:52 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2014-12-09 20:48:35 -05:00
|
|
|
async.parallel({
|
|
|
|
|
markAsRead: function(next) {
|
|
|
|
|
SocketTopics.markAsRead(socket, [tid], next);
|
|
|
|
|
},
|
|
|
|
|
users: function(next) {
|
2015-07-14 17:03:28 -04:00
|
|
|
websockets.getUsersInRoom(socket.uid, 'topic_' + tid, 0, 9, next);
|
2014-12-09 20:48:35 -05:00
|
|
|
}
|
|
|
|
|
}, function(err, result) {
|
|
|
|
|
callback(err, result ? result.users : null);
|
|
|
|
|
});
|
2014-01-10 10:46:26 -05:00
|
|
|
};
|
|
|
|
|
|
2014-01-16 16:10:23 -05:00
|
|
|
SocketTopics.postcount = function(socket, tid, callback) {
|
2014-01-10 10:46:26 -05:00
|
|
|
topics.getTopicField(tid, 'postcount', callback);
|
2014-06-27 15:35:53 -04:00
|
|
|
};
|
|
|
|
|
|
2015-09-25 00:08:25 -04:00
|
|
|
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);
|
2015-07-30 23:13:34 -07:00
|
|
|
};
|
2015-07-21 21:20:13 +08:00
|
|
|
|
2014-01-10 10:46:26 -05:00
|
|
|
|
2015-09-25 15:09:25 -04:00
|
|
|
SocketTopics.emitToTopicAndCategory = function(event, data) {
|
2015-02-01 21:38:10 -05:00
|
|
|
websockets.in('topic_' + data.tid).emit(event, data);
|
|
|
|
|
websockets.in('category_' + data.cid).emit(event, data);
|
2015-09-25 15:09:25 -04:00
|
|
|
};
|
2015-02-01 21:38:10 -05:00
|
|
|
|
2014-01-16 16:10:23 -05:00
|
|
|
SocketTopics.createTopicFromPosts = function(socket, data, callback) {
|
2015-09-01 12:38:26 -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
|
|
|
}
|
|
|
|
|
|
2015-09-01 12:38:26 -04: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-08-12 13:45:18 -04:00
|
|
|
SocketTopics.sendNotificationToTopicOwner = function(tid, fromuid, notification) {
|
2015-09-25 15:09:25 -04:00
|
|
|
if (!tid || !fromuid) {
|
2014-08-12 13:45:18 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
username: async.apply(user.getUserField, fromuid, 'username'),
|
|
|
|
|
topicData: async.apply(topics.getTopicFields, tid, ['uid', 'slug']),
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
if (err || fromuid === parseInt(results.topicData.uid, 10)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notifications.create({
|
|
|
|
|
bodyShort: '[[' + notification + ', ' + results.username + ']]',
|
|
|
|
|
path: nconf.get('relative_path') + '/topic/' + results.topicData.slug,
|
2014-09-12 16:35:30 -04:00
|
|
|
nid: 'tid:' + tid + ':uid:' + fromuid,
|
2014-08-12 13:45:18 -04:00
|
|
|
from: fromuid
|
2014-09-08 23:03:37 -04:00
|
|
|
}, function(err, notification) {
|
|
|
|
|
if (!err && notification) {
|
|
|
|
|
notifications.push(notification, [results.topicData.uid]);
|
2014-08-12 13:45:18 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-01-25 16:47:47 -05:00
|
|
|
SocketTopics.toggleFollow = function(socket, tid, callback) {
|
2015-09-25 15:09:25 -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-01-16 20:53:32 -05:00
|
|
|
|
2015-01-13 14:54:13 -05:00
|
|
|
topics.toggleFollow(tid, socket.uid, callback);
|
2014-01-10 10:46:26 -05:00
|
|
|
};
|
|
|
|
|
|
2015-01-25 16:47:47 -05:00
|
|
|
SocketTopics.follow = function(socket, tid, callback) {
|
2015-09-25 15:09:25 -04:00
|
|
|
if (!socket.uid) {
|
2015-01-25 16:47:47 -05:00
|
|
|
return callback(new Error('[[error:not-logged-in]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topics.follow(tid, socket.uid, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-24 17:30:37 -04:00
|
|
|
SocketTopics.search = function(socket, data, callback) {
|
|
|
|
|
topics.search(data.tid, data.term, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-18 17:50:47 -04:00
|
|
|
SocketTopics.isModerator = function(socket, tid, callback) {
|
|
|
|
|
topics.getTopicField(tid, 'cid', function(err, cid) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
user.isModerator(socket.uid, cid, callback);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
module.exports = SocketTopics;
|