2015-09-25 15:09:25 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2024-01-19 11:31:04 -05:00
|
|
|
const validator = require('validator');
|
|
|
|
|
|
2019-09-12 23:27:36 -04:00
|
|
|
const topics = require('../../topics');
|
|
|
|
|
const privileges = require('../../privileges');
|
|
|
|
|
const meta = require('../../meta');
|
|
|
|
|
const utils = require('../../utils');
|
|
|
|
|
const social = require('../../social');
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (SocketTopics) {
|
2019-09-12 23:27:36 -04:00
|
|
|
SocketTopics.loadMore = async function (socket, data) {
|
2017-02-18 01:52:56 -07:00
|
|
|
if (!data || !data.tid || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) {
|
2019-09-12 23:27:36 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [userPrivileges, topicData] = await Promise.all([
|
|
|
|
|
privileges.topics.get(data.tid, socket.uid),
|
2021-11-15 18:08:09 -05:00
|
|
|
topics.getTopicData(data.tid),
|
2019-09-12 23:27:36 -04:00
|
|
|
]);
|
|
|
|
|
|
2021-03-24 21:28:02 +03:00
|
|
|
if (!userPrivileges['topics:read'] || !privileges.topics.canViewDeletedScheduled(topicData, userPrivileges)) {
|
2019-09-12 23:27:36 -04:00
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-07 12:28:16 -05:00
|
|
|
const set = data.topicPostSort === 'most_votes' ? `tid:${data.tid}:posts:votes` : `tid:${data.tid}:posts`;
|
2019-09-12 23:27:36 -04:00
|
|
|
const reverse = data.topicPostSort === 'newest_to_oldest' || data.topicPostSort === 'most_votes';
|
|
|
|
|
let start = Math.max(0, parseInt(data.after, 10));
|
|
|
|
|
|
2021-02-04 02:07:29 -07:00
|
|
|
const infScrollPostsPerPage = Math.max(0, Math.min(
|
|
|
|
|
meta.config.postsPerPage || 20,
|
|
|
|
|
parseInt(data.count, 10) || meta.config.postsPerPage || 20
|
|
|
|
|
));
|
2019-09-12 23:27:36 -04:00
|
|
|
|
2023-12-27 17:43:56 -05:00
|
|
|
if (parseInt(data.direction, 10) === -1) {
|
2021-11-15 18:08:09 -05:00
|
|
|
start -= infScrollPostsPerPage;
|
2019-09-12 23:27:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let stop = start + infScrollPostsPerPage - 1;
|
|
|
|
|
|
|
|
|
|
start = Math.max(0, start);
|
|
|
|
|
stop = Math.max(0, stop);
|
2021-11-15 18:08:09 -05:00
|
|
|
const [posts, postSharing] = await Promise.all([
|
|
|
|
|
topics.getTopicPosts(topicData, set, start, stop, socket.uid, reverse),
|
2019-09-12 23:27:36 -04:00
|
|
|
social.getActivePostSharing(),
|
|
|
|
|
]);
|
|
|
|
|
|
2021-11-15 18:08:09 -05:00
|
|
|
topicData.posts = posts;
|
2019-09-12 23:27:36 -04:00
|
|
|
topicData.privileges = userPrivileges;
|
|
|
|
|
topicData.postSharing = postSharing;
|
|
|
|
|
topicData['reputation:disabled'] = meta.config['reputation:disabled'] === 1;
|
|
|
|
|
topicData['downvote:disabled'] = meta.config['downvote:disabled'] === 1;
|
|
|
|
|
|
|
|
|
|
topics.modifyPostsByPrivilege(topicData, userPrivileges);
|
|
|
|
|
return topicData;
|
2015-09-25 15:09:25 -04:00
|
|
|
};
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|