Skip to content

Node.js based forum software built for the modern web

fix: handle start=0 stop=0 for topics.getTopicPosts

Barış Soner Uşaklı906dc5675e5e
diff --git a/public/src/client/topic/posts.js b/public/src/client/topic/posts.js
index 3b03b950cb..124ccdd3e5 100644
--- a/public/src/client/topic/posts.js
+++ b/public/src/client/topic/posts.js
@@ -261,7 +261,7 @@ define('forum/topic/posts', [
infinitescroll.loadMore('topics.loadMore', {
tid: tid,
- after: after,
+ after: after + (direction > 0 ? 1 : 0),
count: config.postsPerPage,
direction: direction,
topicPostSort: config.topicPostSort,
diff --git a/src/socket.io/topics/infinitescroll.js b/src/socket.io/topics/infinitescroll.js
index d2cb0132a7..2e9e95948e 100644
--- a/src/socket.io/topics/infinitescroll.js
+++ b/src/socket.io/topics/infinitescroll.js
@@ -30,9 +30,7 @@ module.exports = function (SocketTopics) {
parseInt(data.count, 10) || meta.config.postsPerPage || 20
));
- if (data.direction === 1) {
- start += 1;
- } else if (data.direction === -1) {
+ if (data.direction === -1) {
start -= infScrollPostsPerPage;
}
diff --git a/src/topics/posts.js b/src/topics/posts.js
index a561c1339c..53ca58dfbe 100644
--- a/src/topics/posts.js
+++ b/src/topics/posts.js
@@ -39,7 +39,10 @@ module.exports = function (Topics) {
repliesStart -= 1;
}
}
- const pids = await posts.getPidsFromSet(set, repliesStart, repliesStop, reverse);
+ let pids = [];
+ if (start !== 0 || stop !== 0) {
+ pids = await posts.getPidsFromSet(set, repliesStart, repliesStop, reverse);
+ }
if (!pids.length && !topicOrTid.mainPid) {
return [];
}
diff --git a/test/topics.js b/test/topics.js
index 9f9cec2dd9..48b9469365 100644
--- a/test/topics.js
+++ b/test/topics.js
@@ -507,6 +507,42 @@ describe('Topic\'s', () => {
const posts = await topics.getTopicPosts(null, `tid:${tid}:posts`, 0, 9, topic.userId, true);
assert.deepStrictEqual(posts, []);
});
+
+ it('should only return main post', async () => {
+ const topicData = await topics.getTopicData(tid);
+ const postsData = await topics.getTopicPosts(topicData, `tid:${tid}:posts`, 0, 0, topic.userId, false);
+ assert.strictEqual(postsData.length, 1);
+ assert.strictEqual(postsData[0].content, 'main post');
+ });
+
+ it('should only return first reply', async () => {
+ const topicData = await topics.getTopicData(tid);
+ const postsData = await topics.getTopicPosts(topicData, `tid:${tid}:posts`, 1, 1, topic.userId, false);
+ assert.strictEqual(postsData.length, 1);
+ assert.strictEqual(postsData[0].content, 'topic reply 1');
+ });
+
+ it('should return main post and first reply', async () => {
+ const topicData = await topics.getTopicData(tid);
+ const postsData = await topics.getTopicPosts(topicData, `tid:${tid}:posts`, 0, 1, topic.userId, false);
+ assert.strictEqual(postsData.length, 2);
+ assert.strictEqual(postsData[0].content, 'main post');
+ assert.strictEqual(postsData[1].content, 'topic reply 1');
+ });
+
+ it('should return posts in correct order', async () => {
+ const data = await socketTopics.loadMore({ uid: topic.userId }, { tid: tid, after: 20, direction: 1 });
+ assert.strictEqual(data.posts.length, 11);
+ assert.strictEqual(data.posts[0].content, 'topic reply 20');
+ assert.strictEqual(data.posts[1].content, 'topic reply 21');
+ });
+
+ it('should return posts in correct order in reverse direction', async () => {
+ const data = await socketTopics.loadMore({ uid: topic.userId }, { tid: tid, after: 25, direction: -1 });
+ assert.strictEqual(data.posts.length, 20);
+ assert.strictEqual(data.posts[0].content, 'topic reply 5');
+ assert.strictEqual(data.posts[1].content, 'topic reply 6');
+ });
});
});