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

112 lines
3.7 KiB
JavaScript
Raw Normal View History

'use strict';
const winston = require('winston');
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.getTopicFields(data.tid, ['postcount', 'deleted', 'scheduled', 'uid']),
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]]');
}
2021-02-03 23:59:08 -07: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
if (data.direction === -1) {
start -= (infScrollPostsPerPage + 1);
}
let stop = start + infScrollPostsPerPage - 1;
start = Math.max(0, start);
stop = Math.max(0, stop);
const [mainPost, posts, postSharing] = await Promise.all([
start > 0 ? null : topics.getMainPost(data.tid, socket.uid),
topics.getTopicPosts(data.tid, set, start, stop, socket.uid, reverse),
social.getActivePostSharing(),
]);
if (mainPost) {
topicData.mainPost = mainPost;
topicData.posts = [mainPost].concat(posts);
} else {
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;
};
2019-09-12 23:27:36 -04:00
SocketTopics.loadMoreSortedTopics = async function (socket, data) {
winston.warn('[deprecated] SocketTopics.loadMoreSortedTopics use infinitescroll.loadMoreXhr'); // TODO: remove in 1.19.0
2017-12-08 20:10:37 -05:00
if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:invalid-data]]');
2017-12-08 20:10:37 -05:00
}
const { start, stop } = calculateStartStop(data);
2018-10-22 13:07:02 -04:00
const params = {
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,
};
if (data.sort === 'unread') {
2018-10-22 13:07:02 -04:00
params.cid = data.cid;
2019-09-12 23:27:36 -04:00
return await topics.getUnreadTopics(params);
2018-10-22 13:07:02 -04:00
}
params.cids = data.cid;
params.tags = data.tags;
params.sort = data.sort;
2018-10-22 13:07:02 -04:00
params.term = data.term;
2019-09-12 23:27:36 -04:00
return await topics.getSortedTopics(params);
};
2017-12-08 20:10:37 -05:00
2019-09-12 23:27:36 -04:00
SocketTopics.loadMoreFromSet = async function (socket, data) {
winston.warn('[deprecated] SocketTopics.loadMoreFromSet use infinitescroll.loadMoreXhr'); // TODO: remove in 1.19.0
2016-03-14 10:40:24 -04:00
if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0 || !data.set) {
2019-09-12 23:27:36 -04:00
throw new Error('[[error:invalid-data]]');
}
const { start, stop } = calculateStartStop(data);
2019-09-12 23:27:36 -04:00
return await topics.getTopicsFromSet(data.set, socket.uid, start, stop);
};
function calculateStartStop(data) {
2021-02-04 02:07:29 -07:00
const itemsPerPage = Math.min(
meta.config.topicsPerPage || 20,
parseInt(data.count, 10) || meta.config.topicsPerPage || 20
);
2019-09-12 23:27:36 -04:00
let start = Math.max(0, parseInt(data.after, 10));
if (data.direction === -1) {
start -= itemsPerPage;
}
2019-09-12 23:27:36 -04:00
const 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
};