-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip. ability to report level, comment, or review
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); |