2015-09-25 15:09:25 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2021-10-02 13:35:58 -04:00
|
|
|
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');
|
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-03-24 21:28:02 +03:00
|
|
|
topics.getTopicFields(data.tid, ['postcount', 'deleted', 'scheduled', 'uid']),
|
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]]');
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2015-09-25 15:09:25 -04:00
|
|
|
}
|
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
|
|
|
};
|
|
|
|
|
|
2019-09-12 23:27:36 -04:00
|
|
|
SocketTopics.loadMoreSortedTopics = async function (socket, data) {
|
2021-10-21 15:40:42 -04:00
|
|
|
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
|
|
|
}
|
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-09-12 23:27:36 -04:00
|
|
|
return await topics.getUnreadTopics(params);
|
2018-10-22 13:07:02 -04:00
|
|
|
}
|
|
|
|
|
params.cids = data.cid;
|
2021-02-16 23:12:10 -05:00
|
|
|
params.tags = data.tags;
|
2018-10-27 06:26:50 -04:00
|
|
|
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);
|
2018-10-27 06:26:50 -04:00
|
|
|
};
|
2017-12-08 20:10:37 -05:00
|
|
|
|
2019-09-12 23:27:36 -04:00
|
|
|
SocketTopics.loadMoreFromSet = async function (socket, data) {
|
2021-10-21 15:40:42 -04:00
|
|
|
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]]');
|
2015-09-25 15:09:25 -04:00
|
|
|
}
|
2018-10-27 06:26:50 -04:00
|
|
|
const { start, stop } = calculateStartStop(data);
|
2019-09-12 23:27:36 -04:00
|
|
|
return await topics.getTopicsFromSet(data.set, socket.uid, start, stop);
|
2015-09-25 15:09:25 -04:00
|
|
|
};
|
2018-10-27 06:26:50 -04:00
|
|
|
|
|
|
|
|
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));
|
2018-10-27 06:26:50 -04:00
|
|
|
if (data.direction === -1) {
|
|
|
|
|
start -= itemsPerPage;
|
|
|
|
|
}
|
2019-09-12 23:27:36 -04:00
|
|
|
const stop = start + Math.max(0, itemsPerPage - 1);
|
2018-10-27 06:26:50 -04:00
|
|
|
return { start: Math.max(0, start), stop: Math.max(0, stop) };
|
|
|
|
|
}
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|