Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORL-3042]: Fix feature/unfeature counts bug #4502

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading