277 lines
6.6 KiB
TypeScript
277 lines
6.6 KiB
TypeScript
import Elysia, { t } from "elysia";
|
|
import session from "../session";
|
|
import { generateIdFromEntropySize } from "lucia";
|
|
import { client } from "../db";
|
|
import { disconnectID, findID } from "./ws";
|
|
|
|
export default new Elysia({
|
|
prefix: "/api/staff",
|
|
})
|
|
.use(session)
|
|
.post(
|
|
"/invalidatePunishment",
|
|
async (context) => {
|
|
if (!context.user) {
|
|
return new Response("Not authenicated", {
|
|
status: 401,
|
|
});
|
|
}
|
|
if (!context.user.staff) {
|
|
return new Response("You are not staff.", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const punishment = await client.punishment.findFirst({
|
|
where: {
|
|
id: context.body.punishmentId,
|
|
},
|
|
});
|
|
if (!punishment) {
|
|
return new Response("No punishment found!", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
await client.punishment.delete({
|
|
where: {
|
|
id: context.body.punishmentId,
|
|
},
|
|
});
|
|
},
|
|
{
|
|
body: t.Object({
|
|
punishmentId: t.String(),
|
|
}),
|
|
detail: {
|
|
tags: ["Staff"],
|
|
description:
|
|
"Invalidates a request. Invalidate currently means delete, but it might in the future mean that the punishment is invalid.",
|
|
},
|
|
}
|
|
)
|
|
.post(
|
|
"/punishments",
|
|
async (context) => {
|
|
if (!context.user) {
|
|
return new Response("Not authenicated", {
|
|
status: 401,
|
|
});
|
|
}
|
|
if (!context.user.staff) {
|
|
return new Response("You are not staff.", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const user = await client.user.findFirst({
|
|
where: {
|
|
id: context.body.userId,
|
|
},
|
|
});
|
|
|
|
if (!user) {
|
|
return new Response("User does not exist!", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const punishments = await client.punishment.findMany({
|
|
where: {
|
|
punishedUserId: user.id,
|
|
},
|
|
});
|
|
return punishments.map((z) => {
|
|
return {
|
|
id: z.id,
|
|
type: z.type,
|
|
reason: z.reason,
|
|
staffId: z.staffId,
|
|
time: z.time,
|
|
at: z.at,
|
|
};
|
|
});
|
|
},
|
|
{
|
|
body: t.Object({
|
|
userId: t.String(),
|
|
}),
|
|
response: t.MaybeEmpty(
|
|
t.Array(
|
|
t.Object({
|
|
id: t.String(),
|
|
type: t.String(),
|
|
reason: t.String(),
|
|
staffId: t.String(),
|
|
time: t.Number(),
|
|
at: t.Date(),
|
|
})
|
|
)
|
|
),
|
|
detail: {
|
|
tags: ["Staff"],
|
|
description:
|
|
"View an user's punishments. Will show all punishments by user, and who banned them and for what reason. Can be invalidated.",
|
|
},
|
|
}
|
|
)
|
|
.guard(
|
|
{
|
|
body: t.Object({
|
|
id: t.String(),
|
|
reason: t.String(),
|
|
ms: t.Number(),
|
|
isPermanent: t.MaybeEmpty(t.Boolean()),
|
|
}),
|
|
detail: {
|
|
tags: ["Staff"],
|
|
description:
|
|
"Ban and Mute. These either ban, or mute a person. Mutes prohibit the user from chatting, and bans prohibit the user from joining.",
|
|
},
|
|
},
|
|
(a) =>
|
|
a
|
|
.post("/ban", async (context) => {
|
|
if (!context.user) {
|
|
return new Response("Not authenicated", {
|
|
status: 401,
|
|
});
|
|
}
|
|
if (!context.user.staff) {
|
|
return new Response("You are not staff.", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const user = await client.user.findFirst({
|
|
where: {
|
|
id: context.body.id,
|
|
},
|
|
});
|
|
|
|
if (!user) {
|
|
return new Response("User does not exist!", {
|
|
status: 401,
|
|
});
|
|
}
|
|
if (user.staff) {
|
|
return new Response("Cannot ban staff!", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
disconnectID(user.id);
|
|
|
|
await client.punishment.create({
|
|
data: {
|
|
type: "ban",
|
|
id: generateIdFromEntropySize(10),
|
|
reason: context.body.reason,
|
|
punishedUserId: user.id,
|
|
staffId: context.user.id,
|
|
at: new Date(),
|
|
time: context.body.isPermanent ? -1 : context.body.ms,
|
|
},
|
|
});
|
|
})
|
|
.post("/mute", async (context) => {
|
|
if (!context.user) {
|
|
return new Response("Not authenicated", {
|
|
status: 401,
|
|
});
|
|
}
|
|
if (!context.user.staff) {
|
|
return new Response("You are not staff.", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const user = await client.user.findFirst({
|
|
where: {
|
|
id: context.body.id,
|
|
},
|
|
});
|
|
|
|
if (!user) {
|
|
return new Response("User does not exist!", {
|
|
status: 401,
|
|
});
|
|
}
|
|
if (user.staff) {
|
|
return new Response("Cannot mute staff!", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
await client.punishment.create({
|
|
data: {
|
|
type: "mute",
|
|
id: generateIdFromEntropySize(10),
|
|
reason: context.body.reason,
|
|
punishedUserId: user.id,
|
|
staffId: context.user.id,
|
|
at: new Date(),
|
|
time: context.body.isPermanent ? -1 : context.body.ms,
|
|
},
|
|
});
|
|
})
|
|
)
|
|
.post(
|
|
"/isUserOnline",
|
|
async (context) => {
|
|
if (!context.user) {
|
|
return new Response("Not authenicated", {
|
|
status: 401,
|
|
});
|
|
}
|
|
if (!context.user.staff) {
|
|
return new Response("You are not staff.", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const user = await client.user.findFirst({
|
|
where: {
|
|
id: context.body.userId,
|
|
},
|
|
});
|
|
|
|
if (!user) {
|
|
return new Response("User does not exist!", {
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const participiants = findID(user.id);
|
|
|
|
if (participiants.length == 0) {
|
|
return {
|
|
isOnline: false,
|
|
name: user.username,
|
|
};
|
|
}
|
|
|
|
return {
|
|
isOnline: true,
|
|
rooms: participiants.map((z) => z.room),
|
|
name: user.username,
|
|
};
|
|
},
|
|
{
|
|
response: t.MaybeEmpty(
|
|
t.Object({
|
|
isOnline: t.Boolean(),
|
|
rooms: t.MaybeEmpty(t.Array(t.String())),
|
|
name: t.String(),
|
|
})
|
|
),
|
|
body: t.Object({
|
|
userId: t.String(),
|
|
}),
|
|
detail: {
|
|
tags: ["Staff"],
|
|
description:
|
|
"Returns username from ID, and checks if user is online. If is online, then also returns all rooms they're in.",
|
|
},
|
|
}
|
|
);
|