From 89608a2ff397792b97169e869d04b7b0b93ccbf5 Mon Sep 17 00:00:00 2001 From: nick-funk Date: Mon, 30 Oct 2023 09:31:04 -0600 Subject: [PATCH] add resolvers for DSA deports associated to a notification also adds loaders for DSA reports --- .../core/server/graph/loaders/DSAReports.ts | 22 +++++++++++++++ server/src/core/server/graph/loaders/index.ts | 2 ++ .../server/graph/resolvers/Notification.ts | 7 +++++ .../resolvers/NotificationDSAReportDetails.ts | 27 +++++++++++++++++++ .../src/core/server/graph/resolvers/index.ts | 2 ++ .../core/server/graph/schema/schema.graphql | 26 +++++++++++++++++- .../core/server/models/dsaReport/report.ts | 24 ++++++++++++++++- 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 server/src/core/server/graph/loaders/DSAReports.ts create mode 100644 server/src/core/server/graph/resolvers/NotificationDSAReportDetails.ts diff --git a/server/src/core/server/graph/loaders/DSAReports.ts b/server/src/core/server/graph/loaders/DSAReports.ts new file mode 100644 index 0000000000..17c5159b57 --- /dev/null +++ b/server/src/core/server/graph/loaders/DSAReports.ts @@ -0,0 +1,22 @@ +import DataLoader from "dataloader"; + +import { find } from "coral-server/models/dsaReport/report"; + +import GraphContext from "../context"; +import { createManyBatchLoadFn } from "./util"; + +export interface FindDSAReportInput { + id: string; +} + +export default (ctx: GraphContext) => ({ + find: new DataLoader( + createManyBatchLoadFn((input: FindDSAReportInput) => + find(ctx.mongo, ctx.tenant, input) + ), + { + cacheKeyFn: (input: FindDSAReportInput) => `${input.id}`, + cache: !ctx.disableCaching, + } + ), +}); diff --git a/server/src/core/server/graph/loaders/index.ts b/server/src/core/server/graph/loaders/index.ts index 64a77c41bd..906ed2d51a 100644 --- a/server/src/core/server/graph/loaders/index.ts +++ b/server/src/core/server/graph/loaders/index.ts @@ -4,6 +4,7 @@ import Auth from "./Auth"; import CommentActions from "./CommentActions"; import CommentModerationActions from "./CommentModerationActions"; import Comments from "./Comments"; +import DSAReports from "./DSAReports"; import Notifications from "./Notifications"; import SeenComments from "./SeenComments"; import Sites from "./Sites"; @@ -20,4 +21,5 @@ export default (ctx: Context) => ({ Sites: Sites(ctx), SeenComments: SeenComments(ctx), Notifications: Notifications(ctx), + DSAReports: DSAReports(ctx), }); diff --git a/server/src/core/server/graph/resolvers/Notification.ts b/server/src/core/server/graph/resolvers/Notification.ts index 9742b23d4b..56e86def8a 100644 --- a/server/src/core/server/graph/resolvers/Notification.ts +++ b/server/src/core/server/graph/resolvers/Notification.ts @@ -23,4 +23,11 @@ export const NotificationResolver: Required< return comment; }, + dsaReport: async ({ reportID }, input, ctx) => { + if (!reportID) { + return null; + } + + return await ctx.loaders.DSAReports.find.load({ id: reportID }); + }, }; diff --git a/server/src/core/server/graph/resolvers/NotificationDSAReportDetails.ts b/server/src/core/server/graph/resolvers/NotificationDSAReportDetails.ts new file mode 100644 index 0000000000..77ecb0abf4 --- /dev/null +++ b/server/src/core/server/graph/resolvers/NotificationDSAReportDetails.ts @@ -0,0 +1,27 @@ +import { DSAReport } from "coral-server/models/dsaReport/report"; + +import { GQLNotificationDSAReportDetailsTypeResolver } from "coral-server/graph/schema/__generated__/types"; + +export const NotificationDSAReportDetailsResolver: Required< + GQLNotificationDSAReportDetailsTypeResolver +> = { + id: ({ id }) => id, + publicID: ({ publicID }) => publicID, + comment: async ({ commentID }, input, ctx) => { + if (!commentID) { + return null; + } + + return await ctx.loaders.Comments.comment.load(commentID); + }, + user: async ({ userID }, input, ctx) => { + if (!userID) { + return null; + } + + return await ctx.loaders.Users.user.load(userID); + }, + lawBrokenDescription: ({ lawBrokenDescription }) => lawBrokenDescription, + additionalInformation: ({ additionalInformation }) => additionalInformation, + submissionID: ({ submissionID }) => submissionID, +}; diff --git a/server/src/core/server/graph/resolvers/index.ts b/server/src/core/server/graph/resolvers/index.ts index fc90bced55..58df174566 100644 --- a/server/src/core/server/graph/resolvers/index.ts +++ b/server/src/core/server/graph/resolvers/index.ts @@ -46,6 +46,7 @@ import { ModMessageStatusHistory } from "./ModMessageStatusHistory"; import { Mutation } from "./Mutation"; import { NewCommentersConfiguration } from "./NewCommentersConfiguration"; import { NotificationResolver as Notification } from "./Notification"; +import { NotificationDSAReportDetailsResolver as NotificationDSAReportDetails } from "./NotificationDSAReportDetails"; import { OIDCAuthIntegration } from "./OIDCAuthIntegration"; import { PremodStatus } from "./PremodStatus"; import { PremodStatusHistory } from "./PremodStatusHistory"; @@ -167,6 +168,7 @@ const Resolvers: GQLResolver = { LocalAuthIntegration, AuthenticationTargetFilter, Notification, + NotificationDSAReportDetails, }; export default Resolvers; diff --git a/server/src/core/server/graph/schema/schema.graphql b/server/src/core/server/graph/schema/schema.graphql index 3cb9af4668..3c03c2c84e 100644 --- a/server/src/core/server/graph/schema/schema.graphql +++ b/server/src/core/server/graph/schema/schema.graphql @@ -4514,6 +4514,13 @@ type Notification { comment is the optional comment that is linked to this notification. """ comment: Comment + + """ + dsaReports are details of the DSA Reports related to the notification. + This is usually in reference to the comment that is also related to + the notification. + """ + dsaReport: NotificationDSAReportDetails } type NotificationEdge { @@ -4545,6 +4552,22 @@ type NotificationsConnection { pageInfo: PageInfo! } +type NotificationDSAReportDetails { + id: ID! + + publicID: String! + + comment: Comment + + user: User + + lawBrokenDescription: String @auth(roles: [ADMIN, MODERATOR]) + + additionalInformation: String @auth(roles: [ADMIN, MODERATOR]) + + submissionID: ID @auth(roles: [ADMIN, MODERATOR]) +} + ################################################################################ ## Query ################################################################################ @@ -4718,7 +4741,8 @@ type Query { notifications( ownerID: ID! first: Int = 10 @constraint(max: 50) - after: Cursor): NotificationsConnection! + after: Cursor + ): NotificationsConnection! } ################################################################################ diff --git a/server/src/core/server/models/dsaReport/report.ts b/server/src/core/server/models/dsaReport/report.ts index 1bbbf94935..968cbfd0ec 100644 --- a/server/src/core/server/models/dsaReport/report.ts +++ b/server/src/core/server/models/dsaReport/report.ts @@ -2,8 +2,9 @@ import { v4 as uuid } from "uuid"; import { Sub } from "coral-common/common/lib/types"; import { MongoContext } from "coral-server/data/context"; +import { FindDSAReportInput } from "coral-server/graph/loaders/DSAReports"; import { FilterQuery } from "coral-server/models/helpers"; -import { TenantResource } from "coral-server/models/tenant"; +import { Tenant, TenantResource } from "coral-server/models/tenant"; import { GQLDSAReportStatus } from "coral-server/graph/schema/__generated__/types"; @@ -98,3 +99,24 @@ export async function createDSAReport( dsaReport: report, }; } + +export async function find( + mongo: MongoContext, + tenant: Tenant, + input: FindDSAReportInput +) { + return findDSAReport(mongo, tenant.id, input.id); +} + +export async function findDSAReport( + mongo: MongoContext, + tenantID: string, + id: string +): Promise { + const result = await mongo.dsaReports().findOne({ + tenantID, + id, + }); + + return result ?? null; +}