From 906dc5675e5e1e96a7ff693f07f20987cd08c87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 6 Dec 2021 16:36:30 -0500 Subject: [PATCH] fix: handle start=0 stop=0 for topics.getTopicPosts add more tests --- public/src/client/topic/posts.js | 2 +- src/socket.io/topics/infinitescroll.js | 4 +-- src/topics/posts.js | 5 +++- test/topics.js | 36 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) 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'); + }); }); });