Files
NodeBB/src/socket.io/topics.js

132 lines
3.5 KiB
JavaScript
Raw Normal View History

2014-03-03 15:26:15 -05:00
'use strict';
2017-03-02 19:03:49 +03:00
var async = require('async');
2016-03-08 11:24:32 +02:00
var topics = require('../topics');
var posts = require('../posts');
2016-03-08 11:24:32 +02:00
var websockets = require('./index');
var user = require('../user');
var apiController = require('../controllers/api');
2016-03-24 20:55:10 +02:00
var socketHelpers = require('./helpers');
2014-01-26 16:22:50 -05:00
2017-03-02 19:03:49 +03:00
var SocketTopics = module.exports;
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]]'));
2014-01-16 19:58:57 -05:00
}
2016-08-27 14:06:14 +03:00
data.uid = socket.uid;
data.req = websockets.reqFromSocket(socket);
data.timestamp = Date.now();
async.waterfall([
function (next) {
posts.shouldQueue(socket.uid, data, next);
},
function (shouldQueue, next) {
if (shouldQueue) {
posts.addToQueue(data, next);
} else {
postTopic(socket, data, next);
}
},
], callback);
};
function postTopic(socket, data, callback) {
2017-03-02 19:03:49 +03:00
async.waterfall([
function (next) {
topics.post(data, next);
},
function (result, next) {
next(null, result.topicData);
2016-03-24 20:55:10 +02:00
2017-03-02 19:03:49 +03:00
socket.emit('event:new_post', { posts: [result.postData] });
socket.emit('event:new_topic', result.topicData);
2017-03-02 19:03:49 +03:00
socketHelpers.notifyNew(socket.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
},
], callback);
}
SocketTopics.postcount = function (socket, tid, callback) {
topics.getTopicField(tid, 'postcount', callback);
2014-06-27 15:35:53 -04:00
};
SocketTopics.bookmark = function (socket, data, callback) {
2015-09-25 00:08:25 -04:00
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) {
2014-04-09 16:51:10 -04:00
return callback(new Error('[[error:not-logged-in]]'));
}
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, data.fromTid, callback);
};
SocketTopics.changeWatching = function (socket, data, callback) {
2017-03-02 19:03:49 +03:00
if (!data || !data.tid || !data.type) {
2016-05-18 19:02:43 +03:00
return callback(new Error('[[error:invalid-data]]'));
}
var commands = ['follow', 'unfollow', 'ignore'];
if (commands.indexOf(data.type) === -1) {
return callback(new Error('[[error:invalid-command]]'));
}
followCommand(topics[data.type], socket, data.tid, callback);
};
SocketTopics.follow = function (socket, tid, callback) {
2015-09-25 15:34:53 -04:00
followCommand(topics.follow, socket, tid, callback);
};
function followCommand(method, socket, tid, callback) {
if (!socket.uid) {
2015-01-25 16:47:47 -05:00
return callback(new Error('[[error:not-logged-in]]'));
}
2015-09-25 20:26:44 -04:00
method(tid, socket.uid, callback);
2015-09-25 15:34:53 -04:00
}
2015-01-25 16:47:47 -05:00
SocketTopics.isFollowed = function (socket, tid, callback) {
topics.isFollowing([tid], socket.uid, function (err, isFollowing) {
2016-05-11 16:40:40 +03:00
callback(err, Array.isArray(isFollowing) && isFollowing.length ? isFollowing[0] : false);
});
};
SocketTopics.search = function (socket, data, callback) {
2017-03-02 19:03:49 +03:00
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
}
2014-07-24 17:30:37 -04:00
topics.search(data.tid, data.term, callback);
};
SocketTopics.isModerator = function (socket, tid, callback) {
2017-03-02 19:03:49 +03:00
async.waterfall([
function (next) {
topics.getTopicField(tid, 'cid', next);
},
function (cid, next) {
user.isModerator(socket.uid, cid, next);
},
], callback);
2015-03-18 17:50:47 -04:00
};
2016-03-08 11:24:32 +02:00
SocketTopics.getTopic = function (socket, tid, callback) {
2016-07-01 13:01:09 +03:00
apiController.getTopicData(tid, socket.uid, callback);
2016-03-08 11:24:32 +02:00
};