Skip to content

Commit

Permalink
Merge pull request #4691 from coralproject/fix/ignored-user-replies
Browse files Browse the repository at this point in the history
add helper function for checking if user is ignored
  • Loading branch information
tessalt authored Nov 8, 2024
2 parents cae99e9 + eb8fdec commit b751a91
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
15 changes: 14 additions & 1 deletion server/src/core/server/models/user/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GQLUSER_ROLE } from "coral-server/graph/schema/__generated__/types";
import { isSiteModerationScoped } from "coral-common/common/lib/permissions";

import { MODERATOR_ROLES, STAFF_ROLES } from "./constants";
import { LocalProfile, Profile, SSOProfile, User } from "./user";
import { IgnoredUser, LocalProfile, Profile, SSOProfile, User } from "./user";

export function roleIsStaff(role: GQLUSER_ROLE) {
if (STAFF_ROLES.includes(role)) {
Expand Down Expand Up @@ -189,3 +189,16 @@ export function hasSSOProfile(user: Pick<User, "profiles">): boolean {
const profile = getUserProfile(user, "sso") as SSOProfile | null;
return profile ? true : false;
}

/**
* authorIsIgnored will return true if the author is ignored by the viewer
* @param authorID id of the author of the comment
* @param viewer the User who attempting to access the comment
*/

export function authorIsIgnored(authorID: string, viewer: User): boolean {
return (
viewer.ignoredUsers &&
viewer.ignoredUsers.some((user: IgnoredUser) => user.id === authorID)
);
}
13 changes: 7 additions & 6 deletions server/src/core/server/models/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ import {
createEmptyCommentStatusCounts,
updateRelatedCommentCounts,
} from "../comment";
import { getLocalProfile, getSSOProfile, hasLocalProfile } from "./helpers";
import {
authorIsIgnored,
getLocalProfile,
getSSOProfile,
hasLocalProfile,
} from "./helpers";

export interface LocalProfile {
type: "local";
Expand Down Expand Up @@ -3154,11 +3159,7 @@ export async function ignoreUser(
throw new UserNotFoundError(id);
}

// TODO: extract function
if (
user.ignoredUsers &&
user.ignoredUsers.some((u) => u.id === ignoreUserID)
) {
if (authorIsIgnored(ignoreUserID, user)) {
// TODO: improve error
throw new Error("user already ignored");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { mapErrorsToNull } from "coral-server/helpers/dataloader";
import { hasPublishedStatus } from "coral-server/models/comment";
import { PUBLISHED_STATUSES } from "coral-server/models/comment/constants";
import { getStoryTitle, getURLWithCommentID } from "coral-server/models/story";
import { IgnoredUser, User } from "coral-server/models/user";
import { User } from "coral-server/models/user";
import { authorIsIgnored } from "coral-server/models/user/helpers";

import { NotificationCategory } from "./category";

Expand Down Expand Up @@ -70,11 +71,7 @@ export const reply: NotificationCategory<Payloads> = {

// Check to see if this user is ignoring the user who replied to their
// comment.
if (
parentAuthor.ignoredUsers.some(
(ignoredUser: IgnoredUser) => ignoredUser.id === author.id
)
) {
if (authorIsIgnored(author.id, parentAuthor)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
retrieveUser,
User,
} from "coral-server/models/user";
import { authorIsIgnored } from "coral-server/models/user/helpers";
import { I18n } from "coral-server/services/i18n";

import {
Expand Down Expand Up @@ -66,7 +67,7 @@ const shouldSendReplyNotification = (
return false;
}
// don't notify when ignored users reply
return !targetUser.ignoredUsers.some((user) => user.id === replyAuthorID);
return !authorIsIgnored(replyAuthorID, targetUser);
}

return false;
Expand Down
4 changes: 2 additions & 2 deletions server/src/core/server/services/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import {
warnUser,
} from "coral-server/models/user";
import {
authorIsIgnored,
getLocalProfile,
hasLocalProfile,
hasStaffRole,
Expand Down Expand Up @@ -2299,8 +2300,7 @@ export async function ignore(
throw new UserCannotBeIgnoredError(userID);
}

// TODO: extract function
if (user.ignoredUsers && user.ignoredUsers.some((u) => u.id === userID)) {
if (authorIsIgnored(userID, user)) {
// TODO: improve error
throw new Error("user already ignored");
}
Expand Down

0 comments on commit b751a91

Please sign in to comment.