Skip to content

Commit

Permalink
🚸 Ability to silence/ban/warn etc at lower & higher timescales
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasGruber committed Oct 19, 2024
1 parent f47f0a2 commit bc05bd1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 25 deletions.
3 changes: 3 additions & 0 deletions app/drizzle/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ export const BanStates = [
"OFFICIAL_WARNING",
] as const;

export const TimeUnits = ["minutes", "hours", "days", "weeks", "months"] as const;
export type TimeUnit = (typeof TimeUnits)[number];

export const WeaponTypes = [
"STAFF",
"AXE",
Expand Down
58 changes: 44 additions & 14 deletions app/src/app/reports/[reportid]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ import SliderField from "@/layout/SliderField";
import Post from "@/layout/Post";
import ParsedReportJson from "@/layout/ReportReason";
import Loader from "@/layout/Loader";

import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Button } from "@/components/ui/button";
import { CommentOnReport } from "@/layout/Comment";
import { api } from "@/utils/api";
import { type ReportCommentSchema } from "@/validators/reports";
import { reportCommentSchema } from "@/validators/reports";
import { useInfinitePagination } from "@/libs/pagination";
import { useRequiredUserData } from "@/utils/UserContext";
Expand All @@ -30,6 +35,9 @@ import { canPostReportComment } from "@/validators/reports";
import { canModerateReports } from "@/validators/reports";
import { canEscalateBan } from "@/validators/reports";
import { canClearReport } from "@/validators/reports";
import { TimeUnits } from "@/drizzle/constants";
import type { ReportCommentSchema } from "@/validators/reports";
import type { TimeUnit } from "@/drizzle/constants";
import type { BaseServerResponse } from "@/server/api/trpc";

export default function Report({ params }: { params: { reportid: string } }) {
Expand Down Expand Up @@ -76,11 +84,13 @@ export default function Report({ params }: { params: { reportid: string } }) {
} = useForm<ReportCommentSchema>({
defaultValues: {
banTime: 0,
banTimeUnit: "days",
},
resolver: zodResolver(reportCommentSchema),
});

const watchedLength = watch("banTime", 0);
const watchedUnit = watch("banTimeUnit", "days");

// Get utils
const utils = api.useUtils();
Expand Down Expand Up @@ -191,18 +201,38 @@ export default function Report({ params }: { params: { reportid: string } }) {
<form>
<div className="mb-3">
{canBan && (
<SliderField
id="banTime"
default={0}
min={0}
max={365}
unit="days"
label="Select ban duration in days"
register={register}
setValue={setValue}
watchedValue={watchedLength}
error={errors.banTime?.message}
/>
<div className="flex flex-row items-end">
<div className="grow">
<SliderField
id="banTime"
default={0}
min={0}
max={100}
unit={watchedUnit}
label={`Select duration in ${watchedUnit}`}
register={register}
setValue={setValue}
watchedValue={watchedLength}
error={errors.banTime?.message}
/>
</div>
<Select
onValueChange={(e) => setValue("banTimeUnit", e as TimeUnit)}
defaultValue={watchedUnit}
value={watchedUnit}
>
<SelectTrigger className="basis-1/4 m-1">
<SelectValue placeholder={`None`} />
</SelectTrigger>
<SelectContent>
{TimeUnits.map((unit) => (
<SelectItem key={unit} value={unit}>
{unit}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)}
{canWrite && (
<RichInput
Expand Down
23 changes: 14 additions & 9 deletions app/src/server/api/routers/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import { fetchUser } from "./profile";
import { fetchImage } from "./conceptart";
import { canSeeSecretData } from "@/utils/permissions";
import { getServerPusher } from "@/libs/pusher";
import type { DrizzleClient } from "../../db";
import { getMillisecondsFromTimeUnit } from "@/utils/time";
import sanitize from "@/utils/sanitize";
import type { ReportCommentSchema } from "@/validators/reports";
import type { DrizzleClient } from "../../db";

const pusher = getServerPusher();

Expand Down Expand Up @@ -289,10 +291,7 @@ export const reportsRouter = createTRPCRouter({
status: "BAN_ACTIVATED",
adminResolved: user.role === "ADMIN" ? 1 : 0,
updatedAt: new Date(),
banEnd:
input.banTime !== undefined
? new Date(new Date().getTime() + input.banTime * 24 * 60 * 60 * 1000)
: null,
banEnd: getBanEndDate(input),
})
.where(eq(userReport.id, input.object_id)),
ctx.drizzle.insert(userReportComment).values({
Expand Down Expand Up @@ -337,10 +336,7 @@ export const reportsRouter = createTRPCRouter({
status: "SILENCE_ACTIVATED",
adminResolved: user.role === "ADMIN" ? 1 : 0,
updatedAt: new Date(),
banEnd:
input.banTime !== undefined
? new Date(new Date().getTime() + input.banTime * 24 * 60 * 60 * 1000)
: null,
banEnd: getBanEndDate(input),
})
.where(eq(userReport.id, input.object_id)),
ctx.drizzle.insert(userReportComment).values({
Expand Down Expand Up @@ -582,3 +578,12 @@ export const fetchUserReport = async (
}
return entry;
};

export const getBanEndDate = (input: ReportCommentSchema) => {
return input.banTime !== undefined && input.banTimeUnit !== undefined
? new Date(
new Date().getTime() +
input.banTime * getMillisecondsFromTimeUnit(input.banTimeUnit),
)
: null;
};
19 changes: 19 additions & 0 deletions app/src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { TimeUnit } from "@/drizzle/constants";

/**
* Get game time which is the UTC HH:MM:SS timestring
*/
Expand Down Expand Up @@ -123,3 +125,20 @@ export const getCurrentSeason = () => {
return "summer";
}
};

export const getMillisecondsFromTimeUnit = (timeUnit: TimeUnit) => {
switch (timeUnit) {
case "minutes":
return 1000 * 60;
case "hours":
return 1000 * 60 * 60;
case "days":
return 1000 * 60 * 60 * 24;
case "weeks":
return 1000 * 60 * 60 * 24 * 7;
case "months":
return 1000 * 60 * 60 * 24 * 30;
default:
return 1000;
}
};
6 changes: 4 additions & 2 deletions app/src/validators/reports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "zod";
import type { UserData, UserReport } from "../../drizzle/schema";
import { TimeUnits } from "@/drizzle/constants";
import type { UserData, UserReport } from "@/drizzle/schema";

export const systems = [
"forum_comment",
Expand All @@ -21,7 +22,8 @@ export type UserReportSchema = z.infer<typeof userReportSchema>;
export const reportCommentSchema = z.object({
comment: z.string().min(10).max(5000),
object_id: z.string(),
banTime: z.number().min(0).max(365),
banTime: z.number().min(0).max(100),
banTimeUnit: z.enum(TimeUnits),
});

export type ReportCommentSchema = z.infer<typeof reportCommentSchema>;
Expand Down

0 comments on commit bc05bd1

Please sign in to comment.