-
Notifications
You must be signed in to change notification settings - Fork 49
feat: Report tag #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
feat: Report tag #419
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { connectToDatabase } from "@/lib/database/mongoose"; | ||
| import TagReport from "@/db/tagReport"; | ||
| import { Ratelimit } from "@upstash/ratelimit"; | ||
| import { redis } from "@/lib/utils/redis"; | ||
|
|
||
| const ALLOWED_EXAMS = ["CAT-1", "CAT-2", "FAT"]; | ||
| const ALLOWED_FIELDS = ["subject", "courseCode", "exam", "slot", "year"]; | ||
|
|
||
| const ratelimit = new Ratelimit({ | ||
| redis, | ||
| limiter: Ratelimit.slidingWindow(3, "1 h"),//per id - 3 request - per hour | ||
| analytics: true, | ||
| }); | ||
|
|
||
| function getClientIp(req: any): string { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use appropriate type |
||
| return req.ip || "127.0.0.1"; | ||
| } | ||
|
|
||
| export async function POST(req: Request & { ip?: string }) { | ||
| try { | ||
| await connectToDatabase(); | ||
|
|
||
| const body = await req.json(); | ||
| const { paperId } = body; | ||
|
|
||
| if (!paperId) { | ||
| return NextResponse.json( | ||
| { error: "paperId is required" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
| const ip = getClientIp(req); | ||
| const key = `${ip}::${paperId}`; | ||
| const { success } = await ratelimit.limit(key); | ||
|
|
||
| if (!success) { | ||
| return NextResponse.json( | ||
| { error: "Rate limit exceeded for reporting." }, | ||
| { status: 429 } | ||
| ); | ||
| } | ||
| const MAX_REPORTS_PER_PAPER = 5; | ||
| const count = await TagReport.countDocuments({ paperId }); | ||
|
|
||
| if (count >= MAX_REPORTS_PER_PAPER) { | ||
| return NextResponse.json( | ||
| { error: "Received many reports; we are currently working on it." }, | ||
| { status: 429 } | ||
| ); | ||
| } | ||
| const reportedFields = (body.reportedFields ?? []) | ||
| .map((r: any) => ({ | ||
| field: String(r.field).trim(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typecast it properly. avoid using |
||
| value: r.value?.trim(), | ||
| })) | ||
| .filter((r: any) => r.field); | ||
|
|
||
| for (const rf of reportedFields) { | ||
| if (!ALLOWED_FIELDS.includes(rf.field)) { | ||
| return NextResponse.json( | ||
| { error: `Invalid field: ${rf.field}` }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
| if (rf.field === "exam" && rf.value) { | ||
| if (!ALLOWED_EXAMS.includes(rf.value)) { | ||
| return NextResponse.json( | ||
| { error: `Invalid exam value: ${rf.value}` }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const newReport = await TagReport.create({ | ||
| paperId, | ||
| reportedFields, | ||
| comment: body.comment, | ||
| reporterEmail: body.reporterEmail, | ||
| reporterId: body.reporterId, | ||
| }); | ||
|
|
||
| return NextResponse.json( | ||
| { message: "Report submitted.", report: newReport }, | ||
| { status: 201 } | ||
| ); | ||
| } catch (err) { | ||
| console.error(err); | ||
| return NextResponse.json( | ||
| { error: "Failed to submit tag report." }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||
| "use client"; | ||||||
|
|
||||||
| import { useState } from "react"; | ||||||
| import { FaFlag } from "react-icons/fa6"; | ||||||
| import { Button } from "./ui/button"; | ||||||
| import ReportTagModal from "./ReportTagModal"; | ||||||
|
|
||||||
| export default function ReportButton({ | ||||||
| paperId, subject, exam, slot, year | ||||||
| }: { | ||||||
| paperId: string; | ||||||
| subject?: string; | ||||||
| exam?: string; | ||||||
| slot?: string; | ||||||
| year?: string; | ||||||
| }) { | ||||||
| const [open, setOpen] = useState(false); | ||||||
|
|
||||||
| return ( | ||||||
| <> | ||||||
| <Button | ||||||
| onClick={() => setOpen(true)} | ||||||
| className="h-10 w-10 rounded p-0 text-white transition hover:bg-red-600 bg-red-500" | ||||||
|
||||||
| className="h-10 w-10 rounded p-0 text-white transition hover:bg-red-600 bg-red-500" | |
| className="h-10 w-10 p-0 rounded bg-red-500 text-white hover:bg-red-600 transition" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we must already have allowed exams defined elsewhere. re-use that definition to reduce duplication and improve consistency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure