Skip to content

Commit

Permalink
feat: add reply count to getPostSummaryByPids
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Aug 27, 2020
1 parent a7f03b6 commit de0f4aa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion public/openapi/components/schemas/PostsObject.yaml
Expand Up @@ -112,4 +112,6 @@ PostsObject:
nullable: true
type: string
isMainPost:
type: boolean
type: boolean
replies:
type: number
5 changes: 5 additions & 0 deletions src/posts/data.js
Expand Up @@ -53,6 +53,11 @@ module.exports = function (Posts) {
data.pid = pid;
plugins.fireHook('action:post.setFields', { data: data });
};

Posts.getReplyCount = async function (pid) {
// const keys = pids.map(pid => 'pid:' +pid + ':replies');
return await db.sortedSetCard(`pid:${pid}:replies`);
};
};

function modifyPost(post, fields) {
Expand Down
7 changes: 5 additions & 2 deletions src/posts/summary.js
Expand Up @@ -38,7 +38,7 @@ module.exports = function (Posts) {
const tidToTopic = toObject('tid', topicsAndCategories.topics);
const cidToCategory = toObject('cid', topicsAndCategories.categories);

posts.forEach(function (post) {
Promise.all(posts.map(async (post) => {
// If the post author isn't represented in the retrieved users' data, then it means they were deleted, assume guest.
if (!uidToUser.hasOwnProperty(post.uid)) {
post.uid = 0;
Expand All @@ -47,9 +47,12 @@ module.exports = function (Posts) {
post.topic = tidToTopic[post.tid];
post.category = post.topic && cidToCategory[post.topic.cid];
post.isMainPost = post.topic && post.pid === post.topic.mainPid;
post.replies = await Posts.getReplyCount(post.pid);
post.deleted = post.deleted === 1;
post.timestampISO = utils.toISOString(post.timestamp);
});

return post;
}));

posts = posts.filter(post => tidToTopic[post.tid]);

Expand Down

0 comments on commit de0f4aa

Please sign in to comment.