mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
|
import { errorResponse, jsonResponse } from "@/response";
|
|
import { zValidator } from "@hono/zod-validator";
|
|
import { z } from "zod";
|
|
import { RolePermissions } from "~/drizzle/schema";
|
|
import { Relationship } from "~/packages/database-interface/relationship";
|
|
import { User } from "~/packages/database-interface/user";
|
|
|
|
export const meta = applyConfig({
|
|
allowedMethods: ["POST"],
|
|
ratelimits: {
|
|
max: 30,
|
|
duration: 60,
|
|
},
|
|
route: "/api/v1/accounts/:id/mute",
|
|
auth: {
|
|
required: true,
|
|
oauthPermissions: ["write:mutes"],
|
|
},
|
|
permissions: {
|
|
required: [
|
|
RolePermissions.ManageOwnMutes,
|
|
RolePermissions.ViewAccounts,
|
|
],
|
|
},
|
|
});
|
|
|
|
export const schemas = {
|
|
param: z.object({
|
|
id: z.string().uuid(),
|
|
}),
|
|
json: z.object({
|
|
notifications: z.boolean().optional(),
|
|
duration: z
|
|
.number()
|
|
.int()
|
|
.min(60)
|
|
.max(60 * 60 * 24 * 365 * 5)
|
|
.optional(),
|
|
}),
|
|
};
|
|
|
|
export default apiRoute((app) =>
|
|
app.on(
|
|
meta.allowedMethods,
|
|
meta.route,
|
|
zValidator("param", schemas.param, handleZodError),
|
|
zValidator("json", schemas.json, handleZodError),
|
|
auth(meta.auth, meta.permissions),
|
|
async (context) => {
|
|
const { id } = context.req.valid("param");
|
|
const { user } = context.req.valid("header");
|
|
// TODO: Add duration support
|
|
const { notifications } = context.req.valid("json");
|
|
|
|
if (!user) {
|
|
return errorResponse("Unauthorized", 401);
|
|
}
|
|
|
|
const otherUser = await User.fromId(id);
|
|
|
|
if (!otherUser) {
|
|
return errorResponse("User not found", 404);
|
|
}
|
|
|
|
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
|
user,
|
|
otherUser,
|
|
);
|
|
|
|
// TODO: Implement duration
|
|
await foundRelationship.update({
|
|
muting: true,
|
|
mutingNotifications: notifications ?? true,
|
|
});
|
|
|
|
return jsonResponse(foundRelationship.toApi());
|
|
},
|
|
),
|
|
);
|