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

573 lines
14 KiB
JavaScript
Raw Normal View History

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'),
topics = require('../topics'),
2014-02-27 23:45:12 -05:00
categories = require('../categories'),
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'),
threadTools = require('../threadTools'),
websockets = require('./index'),
2014-02-10 14:15:54 -05:00
user = require('../user'),
2014-08-12 13:45:18 -04:00
db = require('../database'),
meta = require('../meta'),
2014-04-19 14:31:46 -04:00
utils = require('../../public/src/utils'),
2014-08-12 13:45:18 -04:00
SocketPosts = require('./posts'),
2014-01-26 16:22:50 -05:00
SocketTopics = {};
2014-01-16 16:10:23 -05:00
SocketTopics.post = function(socket, data, callback) {
2014-01-16 19:58:57 -05:00
if(!data) {
return callback(new Error('[[error:invalid-data]]'));
2014-01-16 19:58:57 -05:00
}
topics.post({
uid: socket.uid,
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);
}
callback(null, result.topicData);
2014-10-09 18:12:40 -04:00
socket.emit('event:new_post', {posts: [result.postData]});
socket.emit('event:new_topic', result.topicData);
var uids = websockets.getConnectedClients();
privileges.categories.filterUids('read', result.topicData.cid, uids, function(err, uids) {
if (err) {
return;
}
2014-09-16 12:38:27 -04:00
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: data.uid, type: "newTopic"}, function(err, data) {
2014-09-18 17:40:15 -04:00
uids = data.uidsTo;
for(var i=0; i<uids.length; ++i) {
if (parseInt(uids[i], 10) !== socket.uid) {
2014-10-09 18:16:25 -04:00
websockets.in('uid_' + uids[i]).emit('event:new_post', {posts: [result.postData]});
websockets.in('uid_' + uids[i]).emit('event:new_topic', result.topicData);
}
}
});
});
websockets.emitTopicPostStats();
});
};
2014-08-05 17:09:52 -04:00
SocketTopics.enter = function(socket, tid, callback) {
if (!tid || !socket.uid) {
return;
}
SocketTopics.markAsRead(socket, [tid], callback);
2014-08-05 17:09:52 -04:00
topics.increaseViewCount(tid);
2014-09-06 00:19:46 -04:00
websockets.updateRoomBrowsingText('topic_' + tid);
};
2014-01-16 16:10:23 -05:00
SocketTopics.postcount = function(socket, tid, callback) {
topics.getTopicField(tid, 'postcount', callback);
2014-06-27 15:35:53 -04:00
};
SocketTopics.markAsRead = function(socket, tids, callback) {
if(!Array.isArray(tids) || !socket.uid) {
return callback(new Error('[[error:invalid-data]]'));
}
if (!tids.length) {
return callback();
2014-03-03 15:26:15 -05:00
}
2014-09-19 19:45:16 -04:00
tids = tids.filter(function(tid) {
return tid && utils.isNumber(tid);
});
topics.markAsRead(tids, socket.uid, function(err) {
if (err) {
2014-01-16 16:10:23 -05:00
return callback(err);
}
2014-01-16 16:10:23 -05:00
topics.pushUnreadCount(socket.uid);
2014-01-16 16:10:23 -05:00
for (var i=0; i<tids.length; ++i) {
topics.markTopicNotificationsRead(tids[i], socket.uid);
}
2014-03-03 15:26:15 -05:00
callback();
});
};
SocketTopics.markTopicNotificationsRead = function(socket, tid, callback) {
if(!tid || !socket.uid) {
return callback(new Error('[[error:invalid-data]]'));
}
topics.markTopicNotificationsRead(tid, socket.uid);
};
SocketTopics.markAllRead = function(socket, data, callback) {
topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) {
if (err) {
return callback(err);
}
SocketTopics.markAsRead(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 topic && parseInt(topic.cid, 10) === parseInt(cid, 10);
}).map(function(topic) {
return topic.tid;
});
SocketTopics.markAsRead(socket, tids, callback);
});
});
};
SocketTopics.markAsUnreadForAll = function(socket, tids, callback) {
2014-08-05 23:47:17 -04:00
if (!Array.isArray(tids)) {
return callback(new Error('[[error:invalid-tid]]'));
}
2014-04-09 16:51:10 -04:00
2014-08-05 23:47:17 -04:00
if (!socket.uid) {
return callback(new Error('[[error:no-privileges]]'));
}
2014-08-05 23:47:17 -04:00
user.isAdministrator(socket.uid, function(err, isAdmin) {
if (err) {
return callback(err);
}
2014-08-05 23:47:17 -04:00
async.each(tids, function(tid, next) {
async.waterfall([
function(next) {
threadTools.exists(tid, next);
},
function(exists, next) {
if (!exists) {
return next(new Error('[[error:invalid-tid]]'));
}
topics.getTopicField(tid, 'cid', next);
},
function(cid, next) {
user.isModerator(socket.uid, cid, next);
}
], function(err, isMod) {
if (err) {
return next(err);
}
2014-08-05 23:47:17 -04:00
if (!isAdmin && !isMod) {
return next(new Error('[[error:no-privileges]]'));
}
topics.markAsUnreadForAll(tid, function(err) {
if(err) {
return next(err);
}
2014-09-05 17:39:24 -04:00
topics.updateRecent(tid, Date.now(), function(err) {
2014-08-05 23:47:17 -04:00
if(err) {
return next(err);
}
2014-09-16 11:06:10 -04:00
topics.pushUnreadCount(socket.uid);
2014-08-05 23:47:17 -04:00
next();
});
});
});
2014-08-05 23:47:17 -04:00
}, callback);
});
2014-03-21 17:59:46 -04:00
};
2014-01-17 17:26:07 -05: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
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
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
};
SocketTopics.lock = function(socket, data, callback) {
doTopicAction('lock', socket, data, callback);
};
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
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
SocketTopics.unpin = function(socket, data, callback) {
doTopicAction('unpin', socket, data, callback);
};
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]]'));
}
async.each(data.tids, function(tid, next) {
privileges.topics.canEdit(tid, socket.uid, function(err, canEdit) {
if(err) {
return next(err);
}
2014-04-09 16:51:10 -04:00
if(!canEdit) {
return next(new Error('[[error:no-privileges]]'));
}
2014-04-09 16:51:10 -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-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-16 16:10:23 -05:00
SocketTopics.movePost = 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.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
}
privileges.posts.canMove(data.pid, socket.uid, function(err, canMove) {
if (err || !canMove) {
return callback(err || new Error('[[error:no-privileges]]'));
2014-01-19 17:28:08 -05:00
}
2014-08-12 13:45:18 -04:00
topics.movePostToTopic(data.pid, data.tid, function(err) {
if (err) {
return callback(err);
}
SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, 'notifications:moved_your_post');
callback();
});
2014-01-19 17:28:08 -05:00
});
};
2014-01-16 16:10:23 -05:00
SocketTopics.move = function(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-data]]'));
2014-01-16 20:53:32 -05:00
}
2014-08-12 13:45:18 -04:00
async.eachLimit(data.tids, 10, function(tid, next) {
var oldCid;
async.waterfall([
function(next) {
privileges.topics.canMove(tid, socket.uid, next);
},
function(canMove, next) {
if (!canMove) {
return next(new Error('[[error:no-privileges]]'));
}
next();
},
function(next) {
topics.getTopicField(tid, 'cid', next);
},
function(cid, next) {
oldCid = cid;
2014-07-24 16:17:38 -04:00
threadTools.move(tid, data.cid, socket.uid, next);
}
], function(err) {
if(err) {
return next(err);
}
2014-01-16 16:10:23 -05:00
websockets.server.sockets.in('topic_' + tid).emit('event:topic_moved', {
tid: tid
});
2014-01-16 20:53:32 -05:00
websockets.server.sockets.in('category_' + oldCid).emit('event:topic_moved', {
tid: tid
});
2014-08-12 13:45:18 -04:00
SocketTopics.sendNotificationToTopicOwner(tid, socket.uid, 'notifications:moved_your_topic');
next();
});
}, callback);
};
2014-08-12 13:45:18 -04:00
SocketTopics.sendNotificationToTopicOwner = function(tid, fromuid, notification) {
if(!tid || !fromuid) {
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,
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
}
});
});
};
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);
}
2014-08-02 16:48:34 -04:00
async.eachLimit(tids, 10, function(tid, next) {
2014-07-24 16:17:38 -04:00
threadTools.move(tid, data.cid, socket.uid, next);
2014-04-21 13:29:40 -04:00
}, callback);
});
});
};
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-16 20:53:32 -05:00
threadTools.toggleFollow(tid, socket.uid, callback);
};
2014-01-16 16:10:23 -05:00
SocketTopics.loadMore = function(socket, data, callback) {
2014-10-12 04:01:35 +02:00
if(!data || !data.tid || !utils.isNumber(data.after) || 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
}
async.parallel({
settings: function(next) {
user.getSettings(socket.uid, next);
},
privileges: function(next) {
privileges.topics.get(data.tid, socket.uid, next);
2014-08-16 21:33:42 -04:00
},
postCount: function(next) {
topics.getPostCount(data.tid, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
2014-01-24 20:00:56 -05:00
if (!results.privileges.read) {
return callback(new Error('[[error:no-privileges]]'));
}
2014-02-10 14:15:54 -05:00
var set = 'tid:' + data.tid + ':posts',
2014-08-16 21:33:42 -04:00
reverse = false,
start = Math.max(parseInt(data.after, 10) - 1, 0);
if (results.settings.topicPostSort === 'newest_to_oldest') {
reverse = true;
2014-08-16 21:33:42 -04:00
data.after = results.postCount - data.after;
start = Math.max(parseInt(data.after, 10), 0);
} else if (results.settings.topicPostSort === 'most_votes') {
reverse = true;
2014-08-16 21:33:42 -04:00
data.after = results.postCount - data.after;
start = Math.max(parseInt(data.after, 10), 0);
set = 'tid:' + data.tid + ':posts:votes';
}
2014-08-16 21:33:42 -04:00
var end = start + results.settings.postsPerPage - 1;
2014-02-10 14:15:54 -05:00
async.parallel({
posts: function(next) {
topics.getTopicPosts(data.tid, set, start, end, socket.uid, reverse, next);
2014-02-10 14:15:54 -05:00
},
privileges: function(next) {
next(null, results.privileges);
},
'reputation:disabled': function(next) {
next(null, parseInt(meta.config['reputation:disabled'], 10) === 1);
},
'downvote:disabled': function(next) {
next(null, parseInt(meta.config['downvote:disabled'], 10) === 1);
2014-02-10 14:15:54 -05:00
}
2014-03-10 00:17:06 -04:00
}, callback);
});
};
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),
end = start + 9;
2014-01-16 20:53:32 -05:00
topics.getLatestTopics(socket.uid, start, end, data.term, callback);
};
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),
end = start + 9;
2014-01-16 20:53:32 -05:00
topics.getUnreadTopics(socket.uid, start, end, callback);
};
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-07-24 17:30:37 -04:00
SocketTopics.search = function(socket, data, callback) {
topics.search(data.tid, data.term, callback);
};
2014-08-27 18:03:53 -04:00
SocketTopics.searchAndLoadTags = function(socket, data, callback) {
topics.searchTags(data, function(err, tags) {
if (err) {
return callback(err);
}
async.parallel({
counts: function(next) {
db.sortedSetScores('tags:topic:count', tags, next);
},
tagData: function(next) {
tags = tags.map(function(tag) {
return {value: tag};
});
topics.getTagData(tags, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
results.tagData.forEach(function(tag, index) {
tag.score = results.counts[index];
});
results.tagData.sort(function(a, b) {
2014-09-07 13:03:06 -04:00
return b.score - a.score;
2014-08-27 18:03:53 -04:00
});
callback(null, results.tagData);
});
});
};
SocketTopics.loadMoreTags = function(socket, data, callback) {
if(!data || !data.after) {
return callback(new Error('[[error:invalid-data]]'));
}
var start = parseInt(data.after, 10),
end = start + 99;
topics.getTags(start, end, function(err, tags) {
if (err) {
return callback(err);
}
callback(null, {tags: tags, nextStart: end + 1});
});
};
2014-04-10 20:31:57 +01:00
module.exports = SocketTopics;