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');
|
2019-06-24 15:21:43 -04:00
|
|
|
const categories = require('../../categories');
|
2015-09-25 15:09:25 -04:00
|
|
|
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) {
|
2018-10-25 17:02:59 -04:00
|
|
|
if (!results.privileges['topics:read'] || (results.topic.deleted && !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
|
|
|
|
2019-07-03 12:48:26 -04:00
|
|
|
var set = data.topicPostSort === 'most_votes' ? 'tid:' + data.tid + ':posts:votes' : 'tid:' + data.tid + ':posts';
|
2016-11-08 18:14:30 +03:00
|
|
|
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
|
|
|
|
2019-07-03 12:48:26 -04:00
|
|
|
var infScrollPostsPerPage = Math.max(0, Math.min(meta.config.postsPerPage || 20, parseInt(data.count, 10) || meta.config.postsPerPage || 20));
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2019-07-03 12:48:26 -04:00
|
|
|
if (data.direction === -1) {
|
|
|
|
|
start -= (infScrollPostsPerPage + 1);
|
2015-09-25 15:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-03 12:48:26 -04:00
|
|
|
var stop = start + infScrollPostsPerPage - 1;
|
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
|
|
|
};
|
|
|
|
|
|
2018-10-27 06:26:50 -04:00
|
|
|
SocketTopics.loadMoreSortedTopics = function (socket, data, 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-10-27 06:26:50 -04:00
|
|
|
const { start, stop } = calculateStartStop(data);
|
2018-10-22 13:07:02 -04:00
|
|
|
const params = {
|
2018-10-27 06:26:50 -04:00
|
|
|
uid: socket.uid,
|
2018-06-18 14:37:32 -04:00
|
|
|
start: start,
|
|
|
|
|
stop: stop,
|
|
|
|
|
filter: data.filter,
|
2018-10-22 13:07:02 -04:00
|
|
|
query: data.query,
|
|
|
|
|
};
|
2018-10-27 06:26:50 -04:00
|
|
|
if (data.sort === 'unread') {
|
2018-10-22 13:07:02 -04:00
|
|
|
params.cid = data.cid;
|
2019-07-19 17:02:25 -04:00
|
|
|
topics.getUnreadTopics(params, callback);
|
|
|
|
|
return;
|
2018-10-22 13:07:02 -04:00
|
|
|
}
|
|
|
|
|
params.cids = data.cid;
|
2018-10-27 06:26:50 -04:00
|
|
|
params.sort = data.sort;
|
2018-10-22 13:07:02 -04:00
|
|
|
params.term = data.term;
|
|
|
|
|
topics.getSortedTopics(params, callback);
|
2018-10-27 06:26:50 -04: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]]'));
|
|
|
|
|
}
|
2018-10-27 06:26:50 -04:00
|
|
|
const { start, stop } = calculateStartStop(data);
|
2015-09-25 15:09:25 -04:00
|
|
|
topics.getTopicsFromSet(data.set, socket.uid, start, stop, callback);
|
|
|
|
|
};
|
2018-10-27 06:26:50 -04:00
|
|
|
|
2019-06-24 15:21:43 -04:00
|
|
|
SocketTopics.loadMoreUserTopics = function (socket, data, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
categories.getCidsByPrivilege('categories:cid', socket.uid, 'topics:read', next);
|
|
|
|
|
},
|
|
|
|
|
function (cids, next) {
|
|
|
|
|
data.set = cids.map(c => 'cid:' + c + ':uid:' + data.uid + ':tids');
|
|
|
|
|
SocketTopics.loadMoreFromSet(socket, data, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-27 06:26:50 -04:00
|
|
|
function calculateStartStop(data) {
|
|
|
|
|
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);
|
|
|
|
|
return { start: Math.max(0, start), stop: Math.max(0, stop) };
|
|
|
|
|
}
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|