Skip to content

Commit

Permalink
Merge pull request #4502 from coralproject/bug/CORL-3042-featured-com…
Browse files Browse the repository at this point in the history
…ments-counts

[CORL-3042]: Fix feature/unfeature counts bug
  • Loading branch information
kabeaty authored Feb 12, 2024
2 parents de0022a + eb9a2c4 commit 2a290f1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
14 changes: 12 additions & 2 deletions server/src/core/server/graph/mutators/Comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export const Comments = (ctx: GraphContext) => ({
// Validate that this user is allowed to moderate this comment
await validateUserModerationScopes(ctx, ctx.user!, { commentID });

const comment = await addTag(
const { comment, alreadyFeatured } = await addTag(
ctx.mongo,
ctx.tenant,
commentID,
Expand All @@ -258,6 +258,11 @@ export const Comments = (ctx: GraphContext) => ({
ctx.now
);

// The comment is already featured; we don't need to feature again
if (alreadyFeatured) {
return comment;
}

if (comment.status !== GQLCOMMENT_STATUS.APPROVED) {
await approveComment(
ctx.mongo,
Expand Down Expand Up @@ -319,13 +324,18 @@ export const Comments = (ctx: GraphContext) => ({
// Validate that this user is allowed to moderate this comment
await validateUserModerationScopes(ctx, ctx.user!, { commentID });

const comment = await removeTag(
const { comment, alreadyUnfeatured } = await removeTag(
ctx.mongo,
ctx.tenant,
commentID,
GQLTAG.FEATURED
);

// The comment is already unfeatured; we don't need to unfeature again
if (alreadyUnfeatured) {
return comment;
}

// If the tag is sucessfully removed (the tag is
// no longer present on the comment) then we can
// update the tag story counts.
Expand Down
22 changes: 14 additions & 8 deletions server/src/core/server/services/comments/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,17 @@ export async function addTag(

// Check to see if this tag is already on this comment.
if (comment.tags.some(({ type }) => type === tagType)) {
return comment;
return { comment, alreadyFeatured: true };
}

return addCommentTag(mongo, tenant.id, commentID, {
type: tagType,
createdBy: user.id,
createdAt: now,
});
return {
comment: await addCommentTag(mongo, tenant.id, commentID, {
type: tagType,
createdBy: user.id,
createdAt: now,
}),
alreadyFeatured: false,
};
}

/**
Expand Down Expand Up @@ -126,10 +129,13 @@ export async function removeTag(

// Check to see if this tag is even on this comment.
if (comment.tags.every(({ type }) => type !== tagType)) {
return comment;
return { comment, alreadyUnfeatured: true };
}

return removeCommentTag(mongo, tenant.id, commentID, tagType);
return {
comment: await removeCommentTag(mongo, tenant.id, commentID, tagType),
alreadyUnfeatured: false,
};
}

/**
Expand Down
7 changes: 6 additions & 1 deletion server/src/core/server/stacks/rejectComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ const stripTag = async (
return comment;
}

const tagResult = await removeTag(mongo, tenant, comment.id, tag);
const { comment: tagResult } = await removeTag(
mongo,
tenant,
comment.id,
tag
);

await updateTagCommentCounts(
tenant.id,
Expand Down

0 comments on commit 2a290f1

Please sign in to comment.