Skip to content

Commit

Permalink
update errors and comments for dsareport creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kabeaty committed Oct 11, 2023
1 parent 6948fda commit dc0d5f3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
6 changes: 6 additions & 0 deletions common/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export enum ERROR_CODES {
*/
DUPLICATE_STORY_URL = "DUPLICATE_STORY_URL",

/**
* DUPLICATE_DSA_REPORT is used when trying to create a DSA report for a user who has already
* reported a comment for illegal content.
*/
DUPLICATE_DSA_REPORT = "DUPLICATE_DSA_REPORT",

/**
* DUPLICATE_FLAIR_BADGE is used when trying to create a flair badge for a
* tenant that already includes that flair badge.
Expand Down
9 changes: 9 additions & 0 deletions server/src/core/server/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ export class DuplicateEmailError extends CoralError {
}
}

export class DuplicateDSAReportError extends CoralError {
constructor(reportID: string) {
super({
code: ERROR_CODES.DUPLICATE_DSA_REPORT,
context: { pub: { reportID } },
});
}
}

export class DuplicateEmailDomainError extends CoralError {
constructor(emailDomain: string) {
super({
Expand Down
1 change: 1 addition & 0 deletions server/src/core/server/errors/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const ERROR_TRANSLATIONS: Record<ERROR_CODES, string> = {
COMMENTING_DISABLED: "error-commentingDisabled",
DUPLICATE_EMAIL: "error-duplicateEmail",
DUPLICATE_EMAIL_DOMAIN: "error-duplicateEmailDomain",
DUPLICATE_DSA_REPORT: "error-duplicateDSAReport",
DUPLICATE_STORY_ID: "error-duplicateStoryID",
DUPLICATE_STORY_URL: "error-duplicateStoryURL",
DUPLICATE_FLAIR_BADGE: "error-duplicateFlairBadge",
Expand Down
1 change: 1 addition & 0 deletions server/src/core/server/locales/en-US/errors.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ error-duplicateUser =
Specified user already exists with a different login method.
error-duplicateEmail = Specified email address is already in use.
error-duplicateEmailDomain = Specified email domain is already configured.
error-duplicateDSAReport = User has already reported this comment for illegal content.
error-localProfileAlreadySet =
Specified account already has a password set.
error-localProfileNotSet =
Expand Down
52 changes: 46 additions & 6 deletions server/src/core/server/models/dsaReport/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,62 @@ import { v4 as uuid } from "uuid";

import { Sub } from "coral-common/common/lib/types";
import { MongoContext } from "coral-server/data/context";
import {
CommentNotFoundError,
DuplicateDSAReportError,
} from "coral-server/errors";
import { FilterQuery } from "coral-server/models/helpers";
import { TenantResource } from "coral-server/models/tenant";

import { GQLDSAReportStatus } from "coral-server/graph/schema/__generated__/types";

export interface DSAReport extends TenantResource {
/**
* id identifies this DSA Report specifically.
*/
readonly id: string;

/**
* userID is the id of the user who reported this comment for illegal content.
*/
userID: string;

/**
* createdAt is the date that this DSAReport was created
*/
createdAt: Date;

/**
* lawBrokenDescription is the description of the law this comment is being
* reported for breaking.
*/
lawBrokenDescription: string;

/**
* additionalInformation is more explanation of how this comment being reported
* breaks the law.
*/
additionalInformation: string;

/**
* commentID is the id of the comment being reported.
*/
commentID: string;

/**
* submissionID is the id that keeps track of all comments that are submitted together
* as part of one illegal content report form by a user.
*/
submissionID?: string;

/**
* publicID is a user-friendly id used to reference the DSA Report.
*/
publicID: string;

/**
* status keeps track of the current status of the DSA Report
*/
status: GQLDSAReportStatus;
}

Expand Down Expand Up @@ -81,15 +115,21 @@ export async function createDSAReport(
submissionID: submissionIDToUse,
};

// check that a comment for the comment ID exists and throw an error if not
const commentExists = await mongo
.comments()
.findOne({ tenantID, id: commentID });

if (!commentExists) {
throw new CommentNotFoundError(commentID);
}

// check if there's already a dsareport submitted by this user for this comment
// and return a duplicate error if so
const alreadyExisting = await mongo.dsaReports().findOne(filter);
const alreadyExistingReport = await mongo.dsaReports().findOne(filter);

if (alreadyExisting) {
// TODO: update error thrown
throw new Error(
"dsa report submitted by user for this comment already exists"
);
if (alreadyExistingReport) {
throw new DuplicateDSAReportError(alreadyExistingReport.id);
}

await mongo.dsaReports().insertOne(report);
Expand Down

0 comments on commit dc0d5f3

Please sign in to comment.