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

add helper function for checking if user is ignored #4691

Merged
merged 1 commit into from
Nov 8, 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
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
Loading