Skip to content

Commit

Permalink
wip. ability to report level, comment, or review
Browse files Browse the repository at this point in the history
  • Loading branch information
k2xl committed Jun 15, 2024
1 parent 531ade4 commit edc7e17
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
13 changes: 13 additions & 0 deletions models/db/report.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ReportReason, ReportType } from '@root/pages/api/report';

interface Report {
createdAt: Date;
updatedAt: Date;
reporter: User;
reportedUser: User;
reportedEntity: Collection | Level | Comment | Review;
reportedEntityModel: ReportType;
entityType: ReportEntityType;
reasonType: ReportReason;
message: string;
}
3 changes: 3 additions & 0 deletions models/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Notification from './db/notification';
import PlayAttempt from './db/playAttempt';
import QueueMessage from './db/queueMessage';
import Record from './db/record';
import { Report } from './db/report';
import Review from './db/review';
import Stat from './db/stat';
import { StripeEvent } from './db/stripeEvent';
Expand All @@ -36,6 +37,7 @@ import NotificationSchema from './schemas/notificationSchema';
import PlayAttemptSchema from './schemas/playAttemptSchema';
import QueueMessageSchema from './schemas/queueMessageSchema';
import RecordSchema from './schemas/recordSchema';
import ReportSchema from './schemas/reportSchema';
import ReviewSchema from './schemas/reviewSchema';
import StatSchema from './schemas/statSchema';
import StripeEventSchema from './schemas/stripeEventSchema';
Expand Down Expand Up @@ -65,3 +67,4 @@ export const CampaignModel = mongoose.models.Campaign || mongoose.model<Campaign
export const EmailLogModel = mongoose.models.EmailLog || mongoose.model<EmailLog>('EmailLog', EmailLogSchema);
export const AchievementModel = mongoose.models.Achievement || mongoose.model<Achievement>('Achievement', AchievementSchema);
export const StripeEventModel = mongoose.models.StripeEvent || mongoose.model<StripeEvent>('StripeEvent', StripeEventSchema);
export const ReportModel = mongoose.models.Report || mongoose.model<Report>('Report', ReportSchema);
36 changes: 36 additions & 0 deletions models/schemas/reportSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import mongoose from 'mongoose';
import { Report } from '../db/report';

const ReportSchema = new mongoose.Schema<Report>({
reporter: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
reportedUser: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
reportedEntity: {
type: mongoose.Schema.Types.ObjectId,
required: true,
},
reportedEntityModel: {
type: String,
required: true,
},
reasonType: {
type: String,
required: true,
},

message: {
type: String,
required: true,
},
});

ReportSchema.index({ reporter: 1, reportedUser: 1, reportedEntity: 1 }, { unique: true });

export default ReportSchema;
86 changes: 86 additions & 0 deletions pages/api/report/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import DiscordChannel from '@root/constants/discordChannel';
import queueDiscordWebhook from '@root/helpers/discordWebhook';
import Comment from '@root/models/db/comment';
import Level from '@root/models/db/level';
import Review from '@root/models/db/review';
import { CommentModel, LevelModel, ReportModel, ReviewModel, UserModel } from '@root/models/mongoose';
import type { NextApiResponse } from 'next';
import { ValidEnum, ValidObjectId, ValidType } from '../../../helpers/apiWrapper';
import withAuth, { NextApiRequestWithAuth } from '../../../lib/withAuth';

export enum ReportType {
LEVEL = 'LevelModel',
COMMENT = 'CommentModel',
REVIEW = 'ReviewModel',
}
export enum ReportReason {
HARASSMENT = 'HARASSMENT',
SPAM = 'SPAM',
REVIEW_BOMBING = 'REVIEW_BOMBING',
OTHER = 'OTHER',
}
export default withAuth({
POST: {
body: {
targetId: ValidObjectId(true),
reportReason: ValidEnum(Object.values(ReportReason), true),
reportType: ValidEnum(Object.values(ReportType), true),
message: ValidType('string', true),
},
},
}, async (req: NextApiRequestWithAuth, res: NextApiResponse) => {
const { targetId, reportReason, reportType, message } = req.body;

let url = '';
const userReporting = req.user;

let userBeingReported, reportEntityObj = null;

switch (reportType) {
case ReportType.LEVEL:
url = `/levels/${targetId}`;
reportEntityObj = await LevelModel.findById(targetId) as Level;
userBeingReported = reportEntityObj.userId;
break;
case ReportType.COMMENT:
// report comment
url = `/comments/${targetId}`;
reportEntityObj = await CommentModel.findById(targetId) as Comment;
userBeingReported = reportEntityObj.author;
break;
case ReportType.REVIEW:
// report review
reportEntityObj = await ReviewModel.findById(targetId) as Review;
userBeingReported = reportEntityObj.userId;
break;
}

if (!reportEntityObj) {
return res.status(404).json({ error: 'Could not find entity to report. It may have been deleted.' });
}

if (!userBeingReported) {
return res.status(404).json({ error: 'Could not find user to report. They may be been deleted.' });
}

await ReportModel.updateOne({
reporter: userReporting._id,
reported: userBeingReported,
reportedEntity: targetId,
}, {
reporter: userReporting._id,
reported: userBeingReported,
reportedEntity: targetId,
reportedEntityModel: reportType,
reasonType: reportReason,
message,
}, {
upsert: true,
});

const content = `User ${userReporting.name} reported a ${reportType} by user ${userBeingReported.name} for reason ${reportReason} with message: ${message}. [Link](${url})`;

await queueDiscordWebhook( DiscordChannel.DevPriv, content);

return res.status(200).json({ success: true });
});

0 comments on commit edc7e17

Please sign in to comment.