From eb8fdec4224685beba3ce0d946dfefc77bbab3d5 Mon Sep 17 00:00:00 2001 From: Tessa Thornton Date: Thu, 7 Nov 2024 14:10:42 -0500 Subject: [PATCH] add helper function for checking if user is ignored --- server/src/core/server/models/user/helpers.ts | 15 ++++++++++++++- server/src/core/server/models/user/user.ts | 13 +++++++------ .../notifications/email/categories/reply.ts | 9 +++------ .../services/notifications/internal/context.ts | 3 ++- server/src/core/server/services/users/users.ts | 4 ++-- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/server/src/core/server/models/user/helpers.ts b/server/src/core/server/models/user/helpers.ts index 1368203046..7b57e8cacd 100644 --- a/server/src/core/server/models/user/helpers.ts +++ b/server/src/core/server/models/user/helpers.ts @@ -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)) { @@ -189,3 +189,16 @@ export function hasSSOProfile(user: Pick): 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) + ); +} diff --git a/server/src/core/server/models/user/user.ts b/server/src/core/server/models/user/user.ts index 2e177c80a1..801d5d6f13 100644 --- a/server/src/core/server/models/user/user.ts +++ b/server/src/core/server/models/user/user.ts @@ -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"; @@ -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"); } diff --git a/server/src/core/server/services/notifications/email/categories/reply.ts b/server/src/core/server/services/notifications/email/categories/reply.ts index ba44bb7af9..8ed90807bf 100644 --- a/server/src/core/server/services/notifications/email/categories/reply.ts +++ b/server/src/core/server/services/notifications/email/categories/reply.ts @@ -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"; @@ -70,11 +71,7 @@ export const reply: NotificationCategory = { // 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; } diff --git a/server/src/core/server/services/notifications/internal/context.ts b/server/src/core/server/services/notifications/internal/context.ts index 2beee71df0..6581c3f92e 100644 --- a/server/src/core/server/services/notifications/internal/context.ts +++ b/server/src/core/server/services/notifications/internal/context.ts @@ -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 { @@ -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; diff --git a/server/src/core/server/services/users/users.ts b/server/src/core/server/services/users/users.ts index 0980b67a6e..6d071f3a56 100644 --- a/server/src/core/server/services/users/users.ts +++ b/server/src/core/server/services/users/users.ts @@ -106,6 +106,7 @@ import { warnUser, } from "coral-server/models/user"; import { + authorIsIgnored, getLocalProfile, hasLocalProfile, hasStaffRole, @@ -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"); }