2015-09-25 15:09:25 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
2016-08-31 22:50:48 +03:00
|
|
|
|
2015-09-25 15:09:25 -04:00
|
|
|
var topics = require('../../topics');
|
|
|
|
|
var privileges = require('../../privileges');
|
|
|
|
|
var meta = require('../../meta');
|
2017-04-08 20:22:21 -06:00
|
|
|
var utils = require('../../utils');
|
2016-03-21 17:49:44 +02:00
|
|
|
var social = require('../../social');
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (SocketTopics) {
|
|
|
|
|
SocketTopics.loadMore = function (socket, data, callback) {
|
2017-02-18 01:52:56 -07:00
|
|
|
if (!data || !data.tid || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) {
|
2015-09-25 15:09:25 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
2016-11-08 18:14:30 +03:00
|
|
|
var userPrivileges;
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
async.parallel({
|
|
|
|
|
privileges: function (next) {
|
|
|
|
|
privileges.topics.get(data.tid, socket.uid, next);
|
|
|
|
|
},
|
|
|
|
|
topic: function (next) {
|
|
|
|
|
topics.getTopicFields(data.tid, ['postcount', 'deleted'], next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-08 18:14:30 +03:00
|
|
|
}, next);
|
2015-09-25 15:09:25 -04:00
|
|
|
},
|
2016-11-08 18:14:30 +03:00
|
|
|
function (results, next) {
|
2017-05-19 19:35:10 -04:00
|
|
|
if (!results.privileges['topics:read'] || (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted)) {
|
2016-11-08 18:14:30 +03:00
|
|
|
return callback(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2016-11-08 18:14:30 +03:00
|
|
|
userPrivileges = results.privileges;
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2016-11-08 18:14:30 +03:00
|
|
|
var set = 'tid:' + data.tid + ':posts';
|
|
|
|
|
if (data.topicPostSort === 'most_votes') {
|
|
|
|
|
set = 'tid:' + data.tid + ':posts:votes';
|
|
|
|
|
}
|
|
|
|
|
var reverse = data.topicPostSort === 'newest_to_oldest' || data.topicPostSort === 'most_votes';
|
|
|
|
|
var start = Math.max(0, parseInt(data.after, 10));
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2017-05-12 14:58:40 -04:00
|
|
|
var infScrollPostsPerPage = Math.max(0, Math.min(meta.config.postsPerPage || 20, parseInt(data.count, 10) || meta.config.postsPerPage || 20) - 1);
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2016-11-08 18:14:30 +03:00
|
|
|
if (data.direction > 0) {
|
|
|
|
|
if (reverse) {
|
|
|
|
|
start = results.topic.postcount - start;
|
|
|
|
|
}
|
2017-02-18 14:32:35 -07:00
|
|
|
} else if (reverse) {
|
2017-03-10 12:50:19 -05:00
|
|
|
start = results.topic.postcount - start - infScrollPostsPerPage;
|
2015-09-26 18:29:27 -04:00
|
|
|
} else {
|
2017-03-10 15:56:45 -05:00
|
|
|
start -= infScrollPostsPerPage;
|
2015-09-25 15:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
2017-03-10 12:50:19 -05:00
|
|
|
var stop = start + (infScrollPostsPerPage);
|
2016-11-08 18:14:30 +03:00
|
|
|
|
|
|
|
|
start = Math.max(0, start);
|
|
|
|
|
stop = Math.max(0, stop);
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
mainPost: function (next) {
|
|
|
|
|
if (start > 0) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
topics.getMainPost(data.tid, socket.uid, next);
|
|
|
|
|
},
|
|
|
|
|
posts: function (next) {
|
|
|
|
|
topics.getTopicPosts(data.tid, set, start, stop, socket.uid, reverse, next);
|
|
|
|
|
},
|
|
|
|
|
postSharing: function (next) {
|
|
|
|
|
social.getActivePostSharing(next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-08 18:14:30 +03:00
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function (topicData, next) {
|
2015-10-06 18:36:03 -04:00
|
|
|
if (topicData.mainPost) {
|
|
|
|
|
topicData.posts = [topicData.mainPost].concat(topicData.posts);
|
|
|
|
|
}
|
2015-10-06 18:41:39 -04:00
|
|
|
|
2016-11-08 18:14:30 +03:00
|
|
|
topicData.privileges = userPrivileges;
|
2018-10-21 16:47:51 -04:00
|
|
|
topicData['reputation:disabled'] = meta.config['reputation:disabled'] === 1;
|
|
|
|
|
topicData['downvote:disabled'] = meta.config['downvote:disabled'] === 1;
|
2015-10-06 18:41:39 -04:00
|
|
|
|
2016-11-08 18:14:30 +03:00
|
|
|
topics.modifyPostsByPrivilege(topicData, userPrivileges);
|
|
|
|
|
next(null, topicData);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-08 18:14:30 +03:00
|
|
|
], callback);
|
2015-09-25 15:09:25 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketTopics.loadMoreUnreadTopics = function (socket, data, callback) {
|
2018-06-18 14:37:32 -04:00
|
|
|
loadData(socket.uid, data, 'unread', callback);
|
2015-09-25 15:09:25 -04:00
|
|
|
};
|
|
|
|
|
|
2016-11-03 18:02:10 +03:00
|
|
|
SocketTopics.loadMoreRecentTopics = function (socket, data, callback) {
|
2018-06-18 14:37:32 -04:00
|
|
|
loadData(socket.uid, data, 'recent', callback);
|
2018-01-29 11:56:06 -05:00
|
|
|
};
|
2016-11-03 18:02:10 +03:00
|
|
|
|
2018-01-29 11:56:06 -05:00
|
|
|
SocketTopics.loadMorePopularTopics = function (socket, data, callback) {
|
2018-06-18 14:37:32 -04:00
|
|
|
loadData(socket.uid, data, 'posts', callback);
|
2016-11-03 18:02:10 +03:00
|
|
|
};
|
|
|
|
|
|
2017-12-08 20:10:37 -05:00
|
|
|
SocketTopics.loadMoreTopTopics = function (socket, data, callback) {
|
2018-06-18 14:37:32 -04:00
|
|
|
loadData(socket.uid, data, 'votes', callback);
|
2018-01-29 11:56:06 -05:00
|
|
|
};
|
|
|
|
|
|
2018-06-18 14:37:32 -04:00
|
|
|
function loadData(uid, data, sort, callback) {
|
2017-12-08 20:10:37 -05:00
|
|
|
if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) {
|
|
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
2018-05-31 00:16:52 -04:00
|
|
|
var itemsPerPage = Math.min(meta.config.topicsPerPage || 20, parseInt(data.count, 10) || meta.config.topicsPerPage || 20);
|
|
|
|
|
var start = Math.max(0, parseInt(data.after, 10));
|
|
|
|
|
if (data.direction === -1) {
|
|
|
|
|
start -= itemsPerPage;
|
|
|
|
|
}
|
|
|
|
|
var stop = start + Math.max(0, itemsPerPage - 1);
|
|
|
|
|
start = Math.max(0, start);
|
|
|
|
|
stop = Math.max(0, stop);
|
2018-06-18 14:37:32 -04:00
|
|
|
if (sort === 'unread') {
|
|
|
|
|
return topics.getUnreadTopics({ cid: data.cid, uid: uid, start: start, stop: stop, filter: data.filter }, callback);
|
|
|
|
|
}
|
|
|
|
|
topics.getSortedTopics({
|
|
|
|
|
cids: data.cid,
|
|
|
|
|
uid: uid,
|
|
|
|
|
start: start,
|
|
|
|
|
stop: stop,
|
|
|
|
|
filter: data.filter,
|
|
|
|
|
sort: sort,
|
|
|
|
|
term: data.term,
|
|
|
|
|
}, callback);
|
2018-01-29 11:56:06 -05:00
|
|
|
}
|
2017-12-08 20:10:37 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
SocketTopics.loadMoreFromSet = function (socket, data, callback) {
|
2016-03-14 10:40:24 -04:00
|
|
|
if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0 || !data.set) {
|
2015-09-25 15:09:25 -04:00
|
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 18:02:10 +03:00
|
|
|
var start = parseInt(data.after, 10);
|
2017-05-12 14:58:40 -04:00
|
|
|
var stop = start + Math.max(0, Math.min(meta.config.topicsPerPage || 20, parseInt(data.count, 10) || meta.config.topicsPerPage || 20) - 1);
|
2015-09-25 15:09:25 -04:00
|
|
|
|
|
|
|
|
topics.getTopicsFromSet(data.set, socket.uid, start, stop, callback);
|
|
|
|
|
};
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|