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

58 lines
1.9 KiB
JavaScript
Raw Normal View History

'use strict';
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');
module.exports = function (SocketTopics) {
2019-09-12 23:27:36 -04:00
SocketTopics.loadMore = async function (socket, data) {
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),
topics.getTopicData(data.tid),
2019-09-12 23:27:36 -04:00
]);
if (!userPrivileges['topics:read'] || !privileges.topics.canViewDeletedScheduled(topicData, userPrivileges)) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:no-privileges]]');
}
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
if (parseInt(data.direction, 10) === -1) {
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);
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(),
]);
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;
};
2017-02-18 02:30:48 -07:00
};