From fa4177c3bc0b0a4b4c81632d406b653963922cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 18 Nov 2020 14:25:39 -0500 Subject: [PATCH] fix: #6407, fix feeds display latest posts instead of oldest in topic rss feed fix missing await that was causing rss_tokens to not function fix feed test more tests for getTopicWithPosts --- src/routes/feeds.js | 8 +-- src/topics/index.js | 12 ++-- test/feeds.js | 2 +- test/topics.js | 135 ++++++++++++++++++++++++++++++++++++++------ 4 files changed, 130 insertions(+), 27 deletions(-) diff --git a/src/routes/feeds.js b/src/routes/feeds.js index be23cafed6..0c12af5334 100644 --- a/src/routes/feeds.js +++ b/src/routes/feeds.js @@ -53,7 +53,7 @@ async function validateTokenIfRequiresLogin(requiresLogin, cid, req, res) { await user.auth.logAttempt(uid, req.ip); return helpers.notAllowed(req, res); } - const userPrivileges = privileges.categories.get(cid, uid); + const userPrivileges = await privileges.categories.get(cid, uid); if (!userPrivileges.read) { return helpers.notAllowed(req, res); } @@ -77,7 +77,7 @@ async function generateForTopic(req, res) { } if (await validateTokenIfRequiresLogin(!userPrivileges['topics:read'], topic.cid, req, res)) { - const topicData = await topics.getTopicWithPosts(topic, 'tid:' + tid + ':posts', req.uid || req.query.uid || 0, 0, 25, false); + const topicData = await topics.getTopicWithPosts(topic, 'tid:' + tid + ':posts', req.uid || req.query.uid || 0, 0, 24, true); topics.modifyPostsByPrivilege(topicData, userPrivileges); @@ -94,8 +94,8 @@ async function generateForTopic(req, res) { if (topicData.posts.length > 0) { feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString(); } - - topicData.posts.forEach(function (postData) { + const replies = topicData.posts.slice(1); + replies.forEach(function (postData) { if (!postData.deleted) { const dateStamp = new Date(parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)).toUTCString(); diff --git a/src/topics/index.js b/src/topics/index.js index 943930e375..372141dda5 100644 --- a/src/topics/index.js +++ b/src/topics/index.js @@ -189,18 +189,20 @@ Topics.getTopicWithPosts = async function (topicData, set, uid, start, stop, rev }; async function getMainPostAndReplies(topic, set, uid, start, stop, reverse) { + let repliesStart = start; + let repliesStop = stop; if (stop > 0) { - stop -= 1; + repliesStop -= 1; if (start > 0) { - start -= 1; + repliesStart -= 1; } } - const pids = await posts.getPidsFromSet(set, start, stop, reverse); + const pids = await posts.getPidsFromSet(set, repliesStart, repliesStop, reverse); if (!pids.length && !topic.mainPid) { return []; } - if (parseInt(topic.mainPid, 10) && start === 0) { + if (topic.mainPid && start === 0) { pids.unshift(topic.mainPid); } const postData = await posts.getPostsByPids(pids, uid); @@ -213,7 +215,7 @@ async function getMainPostAndReplies(topic, set, uid, start, stop, reverse) { replies = postData.slice(1); } - Topics.calculatePostIndices(replies, start); + Topics.calculatePostIndices(replies, repliesStart); return await Topics.addPostData(postData, uid); } diff --git a/test/feeds.js b/test/feeds.js index 34bf9d3905..6f540c1c71 100644 --- a/test/feeds.js +++ b/test/feeds.js @@ -182,7 +182,7 @@ describe('feeds', function () { request(nconf.get('url') + '/category/' + cid + '.rss?uid=' + fooUid + '&token=' + rssToken, { }, function (err, res, body) { assert.ifError(err); assert.equal(res.statusCode, 200); - assert(body); + assert(body.startsWith(' { + assert.strictEqual(post.index, index); + }); + }); + + it('should return 3 posts from 1 to 3 excluding main post', async function () { + const topicData = await topics.getTopicData(tid); + const start = 1; + const data = await topics.getTopicWithPosts(topicData, 'tid:' + tid + ':posts', topic.userId, start, 3, false); + assert.strictEqual(data.posts.length, 3); + assert.strictEqual(data.posts[0].content, 'topic reply 1'); + assert.strictEqual(data.posts[1].content, 'topic reply 2'); + assert.strictEqual(data.posts[2].content, 'topic reply 3'); + data.posts.forEach((post, index) => { + assert.strictEqual(post.index, index + start); + }); + }); + + it('should return main post and last 2 posts', async function () { + const topicData = await topics.getTopicData(tid); + const data = await topics.getTopicWithPosts(topicData, 'tid:' + tid + ':posts', topic.userId, 0, 2, true); + assert.strictEqual(data.posts.length, 3); + assert.strictEqual(data.posts[0].content, 'main post'); + assert.strictEqual(data.posts[1].content, 'topic reply 30'); + assert.strictEqual(data.posts[2].content, 'topic reply 29'); + data.posts.forEach((post, index) => { + assert.strictEqual(post.index, index); + }); + }); + + it('should return last 3 posts and not main post', async function () { + const topicData = await topics.getTopicData(tid); + const start = 1; + const data = await topics.getTopicWithPosts(topicData, 'tid:' + tid + ':posts', topic.userId, start, 3, true); + assert.strictEqual(data.posts.length, 3); + assert.strictEqual(data.posts[0].content, 'topic reply 30'); + assert.strictEqual(data.posts[1].content, 'topic reply 29'); + assert.strictEqual(data.posts[2].content, 'topic reply 28'); + data.posts.forEach((post, index) => { + assert.strictEqual(post.index, index + start); + }); + }); + + it('should return posts 29 to 27 posts and not main post', async function () { + const topicData = await topics.getTopicData(tid); + const start = 2; + const data = await topics.getTopicWithPosts(topicData, 'tid:' + tid + ':posts', topic.userId, start, 4, true); + assert.strictEqual(data.posts.length, 3); + assert.strictEqual(data.posts[0].content, 'topic reply 29'); + assert.strictEqual(data.posts[1].content, 'topic reply 28'); + assert.strictEqual(data.posts[2].content, 'topic reply 27'); + data.posts.forEach((post, index) => { + assert.strictEqual(post.index, index + start); + }); + }); + + it('should return 3 posts in reverse', async function () { + const topicData = await topics.getTopicData(tid); + const start = 28; + const data = await topics.getTopicWithPosts(topicData, 'tid:' + tid + ':posts', topic.userId, start, 30, true); + assert.strictEqual(data.posts.length, 3); + assert.strictEqual(data.posts[0].content, 'topic reply 3'); + assert.strictEqual(data.posts[1].content, 'topic reply 2'); + assert.strictEqual(data.posts[2].content, 'topic reply 1'); + data.posts.forEach((post, index) => { + assert.strictEqual(post.index, index + start); + }); + }); + + it('should get all posts with main post at the start', async function () { + const topicData = await topics.getTopicData(tid); + const data = await topics.getTopicWithPosts(topicData, 'tid:' + tid + ':posts', topic.userId, 0, -1, false); + assert.strictEqual(data.posts.length, 31); + assert.strictEqual(data.posts[0].content, 'main post'); + assert.strictEqual(data.posts[1].content, 'topic reply 1'); + assert.strictEqual(data.posts[data.posts.length - 1].content, 'topic reply 30'); + data.posts.forEach((post, index) => { + assert.strictEqual(post.index, index); + }); + }); + + it('should get all posts in reverse with main post at the start followed by reply 30', async function () { + const topicData = await topics.getTopicData(tid); + const data = await topics.getTopicWithPosts(topicData, 'tid:' + tid + ':posts', topic.userId, 0, -1, true); + assert.strictEqual(data.posts.length, 31); + assert.strictEqual(data.posts[0].content, 'main post'); + assert.strictEqual(data.posts[1].content, 'topic reply 30'); + assert.strictEqual(data.posts[data.posts.length - 1].content, 'topic reply 1'); + data.posts.forEach((post, index) => { + assert.strictEqual(post.index, index); }); }); });