import { GraphQLResolveInfo } from "graphql"; import GraphContext from "coral-server/graph/context"; import { hasFeatureFlag } from "coral-server/models/tenant"; import * as user from "coral-server/models/user"; import { roleIsStaff } from "coral-server/models/user/helpers"; import { GQLFEATURE_FLAG, GQLUser, GQLUSER_ROLE, GQLUserTypeResolver, } from "coral-server/graph/schema/__generated__/types"; import { RecentCommentHistoryInput } from "./RecentCommentHistory"; import { UserStatusInput } from "./UserStatus"; import { getRequestedFields } from "./util"; const maybeLoadOnlyIgnoredUserID = ( ctx: GraphContext, info: GraphQLResolveInfo, users?: user.IgnoredUser[] ) => { // If there isn't any ids, then return nothing! if (!users || users.length <= 0) { return []; } // Get the field names of the fields being requested, if it's only the ID, // we have that, so no need to make a database request. const fields = getRequestedFields(info); if (fields.length === 1 && fields[0] === "id") { return users.map(({ id }) => ({ id })); } // We want more than the ID! Get the user! return Promise.all(users.map(({ id }) => ctx.loaders.Users.user.load(id))); }; export const User: GQLUserTypeResolver = { comments: ({ id }, input, ctx) => ctx.loaders.Comments.forUser(id, input), allComments: ({ id }, input, ctx) => ctx.loaders.Comments.forUserAll(id, input), rejectedComments: ({ id }, input, ctx) => ctx.loaders.Comments.forUserRejected(id, input), commentModerationActionHistory: ({ id }, input, ctx) => ctx.loaders.CommentModerationActions.forModerator(input, id), status: ({ id, status }): UserStatusInput => ({ ...status, userID: id, }), moderationScopes: ({ role, moderationScopes }, input, ctx) => { // If the feature flag for site moderators is not turned on return null // always. if (!hasFeatureFlag(ctx.tenant, GQLFEATURE_FLAG.SITE_MODERATOR)) { return null; } // Moderation scopes only apply to users that have the moderator role. if (role !== GQLUSER_ROLE.MODERATOR) { return null; } // For all other users return null for moderation scopes. return moderationScopes; }, ignoredUsers: ({ ignoredUsers }, input, ctx, info) => maybeLoadOnlyIgnoredUserID(ctx, info, ignoredUsers), ignoreable: ({ role }) => !roleIsStaff(role), recentCommentHistory: ({ id }): RecentCommentHistoryInput => ({ userID: id }), profiles: ({ profiles = [] }) => profiles, };