mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-14 17:56:16 +01:00
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
This commit is contained in:
@@ -53,7 +53,7 @@ async function validateTokenIfRequiresLogin(requiresLogin, cid, req, res) {
|
|||||||
await user.auth.logAttempt(uid, req.ip);
|
await user.auth.logAttempt(uid, req.ip);
|
||||||
return helpers.notAllowed(req, res);
|
return helpers.notAllowed(req, res);
|
||||||
}
|
}
|
||||||
const userPrivileges = privileges.categories.get(cid, uid);
|
const userPrivileges = await privileges.categories.get(cid, uid);
|
||||||
if (!userPrivileges.read) {
|
if (!userPrivileges.read) {
|
||||||
return helpers.notAllowed(req, res);
|
return helpers.notAllowed(req, res);
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ async function generateForTopic(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (await validateTokenIfRequiresLogin(!userPrivileges['topics:read'], topic.cid, 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);
|
topics.modifyPostsByPrivilege(topicData, userPrivileges);
|
||||||
|
|
||||||
@@ -94,8 +94,8 @@ async function generateForTopic(req, res) {
|
|||||||
if (topicData.posts.length > 0) {
|
if (topicData.posts.length > 0) {
|
||||||
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
|
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
|
||||||
}
|
}
|
||||||
|
const replies = topicData.posts.slice(1);
|
||||||
topicData.posts.forEach(function (postData) {
|
replies.forEach(function (postData) {
|
||||||
if (!postData.deleted) {
|
if (!postData.deleted) {
|
||||||
const dateStamp = new Date(parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)).toUTCString();
|
const dateStamp = new Date(parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)).toUTCString();
|
||||||
|
|
||||||
|
|||||||
@@ -189,18 +189,20 @@ Topics.getTopicWithPosts = async function (topicData, set, uid, start, stop, rev
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function getMainPostAndReplies(topic, set, uid, start, stop, reverse) {
|
async function getMainPostAndReplies(topic, set, uid, start, stop, reverse) {
|
||||||
|
let repliesStart = start;
|
||||||
|
let repliesStop = stop;
|
||||||
if (stop > 0) {
|
if (stop > 0) {
|
||||||
stop -= 1;
|
repliesStop -= 1;
|
||||||
if (start > 0) {
|
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) {
|
if (!pids.length && !topic.mainPid) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parseInt(topic.mainPid, 10) && start === 0) {
|
if (topic.mainPid && start === 0) {
|
||||||
pids.unshift(topic.mainPid);
|
pids.unshift(topic.mainPid);
|
||||||
}
|
}
|
||||||
const postData = await posts.getPostsByPids(pids, uid);
|
const postData = await posts.getPostsByPids(pids, uid);
|
||||||
@@ -213,7 +215,7 @@ async function getMainPostAndReplies(topic, set, uid, start, stop, reverse) {
|
|||||||
replies = postData.slice(1);
|
replies = postData.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Topics.calculatePostIndices(replies, start);
|
Topics.calculatePostIndices(replies, repliesStart);
|
||||||
|
|
||||||
return await Topics.addPostData(postData, uid);
|
return await Topics.addPostData(postData, uid);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ describe('feeds', function () {
|
|||||||
request(nconf.get('url') + '/category/' + cid + '.rss?uid=' + fooUid + '&token=' + rssToken, { }, function (err, res, body) {
|
request(nconf.get('url') + '/category/' + cid + '.rss?uid=' + fooUid + '&token=' + rssToken, { }, function (err, res, body) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
assert.equal(res.statusCode, 200);
|
assert.equal(res.statusCode, 200);
|
||||||
assert(body);
|
assert(body.startsWith('<?xml version="1.0"'));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
121
test/topics.js
121
test/topics.js
@@ -256,23 +256,124 @@ describe('Topic\'s', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('.getTopicWithPosts', function () {
|
describe('.getTopicWithPosts', function () {
|
||||||
it('should get a topic with posts and other data', function (done) {
|
let tid;
|
||||||
topics.getTopicData(newTopic.tid, function (err, topicData) {
|
before(async function () {
|
||||||
if (err) {
|
const result = await topics.post({ uid: topic.userId, title: 'page test', content: 'main post', cid: topic.categoryId });
|
||||||
return done(err);
|
tid = result.topicData.tid;
|
||||||
}
|
for (let i = 0; i < 30; i++) {
|
||||||
topics.getTopicWithPosts(topicData, 'tid:' + newTopic.tid + ':posts', topic.userId, 0, -1, false, function (err, data) {
|
// eslint-disable-next-line no-await-in-loop
|
||||||
if (err) {
|
await topics.reply({ uid: adminUid, content: 'topic reply ' + (i + 1), tid: tid });
|
||||||
return done(err);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get a topic with posts and other data', async function () {
|
||||||
|
const topicData = await topics.getTopicData(tid);
|
||||||
|
const data = await topics.getTopicWithPosts(topicData, 'tid:' + tid + ':posts', topic.userId, 0, -1, false);
|
||||||
assert(data);
|
assert(data);
|
||||||
assert.equal(data.category.cid, topic.categoryId);
|
assert.equal(data.category.cid, topic.categoryId);
|
||||||
assert.equal(data.unreplied, true);
|
assert.equal(data.unreplied, false);
|
||||||
assert.equal(data.deleted, false);
|
assert.equal(data.deleted, false);
|
||||||
assert.equal(data.locked, false);
|
assert.equal(data.locked, false);
|
||||||
assert.equal(data.pinned, false);
|
assert.equal(data.pinned, false);
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return first 3 posts including main post', async function () {
|
||||||
|
const topicData = await topics.getTopicData(tid);
|
||||||
|
const data = await topics.getTopicWithPosts(topicData, 'tid:' + tid + ':posts', topic.userId, 0, 2, false);
|
||||||
|
assert.strictEqual(data.posts.length, 3);
|
||||||
|
assert.strictEqual(data.posts[0].content, 'main post');
|
||||||
|
assert.strictEqual(data.posts[1].content, 'topic reply 1');
|
||||||
|
assert.strictEqual(data.posts[2].content, 'topic reply 2');
|
||||||
|
data.posts.forEach((post, index) => {
|
||||||
|
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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user