2024-05-29 03:14:24 +02:00
|
|
|
import { applyConfig, auth, handleZodError, qsQuery } from "@/api";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { errorResponse, jsonResponse } from "@/response";
|
2024-07-11 12:56:28 +02:00
|
|
|
import type { Hono } from "@hono/hono";
|
2024-05-06 09:16:33 +02:00
|
|
|
import { zValidator } from "@hono/zod-validator";
|
2024-04-14 12:36:25 +02:00
|
|
|
import { z } from "zod";
|
2024-06-12 01:42:36 +02:00
|
|
|
import { RolePermissions } from "~/drizzle/schema";
|
2024-07-27 20:46:19 +02:00
|
|
|
import { Relationship } from "~/packages/database-interface/relationship";
|
2023-10-16 05:51:29 +02:00
|
|
|
|
|
|
|
|
export const meta = applyConfig({
|
2024-04-07 07:30:49 +02:00
|
|
|
allowedMethods: ["GET"],
|
|
|
|
|
route: "/api/v1/accounts/relationships",
|
|
|
|
|
ratelimits: {
|
|
|
|
|
max: 30,
|
|
|
|
|
duration: 60,
|
|
|
|
|
},
|
|
|
|
|
auth: {
|
|
|
|
|
required: true,
|
|
|
|
|
oauthPermissions: ["read:follows"],
|
|
|
|
|
},
|
2024-06-08 06:57:29 +02:00
|
|
|
permissions: {
|
2024-06-13 04:26:43 +02:00
|
|
|
required: [RolePermissions.ManageOwnFollows],
|
2024-06-08 06:57:29 +02:00
|
|
|
},
|
2023-10-16 05:51:29 +02:00
|
|
|
});
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
export const schemas = {
|
|
|
|
|
query: z.object({
|
2024-05-06 10:19:42 +02:00
|
|
|
id: z.array(z.string().uuid()).min(1).max(10).or(z.string().uuid()),
|
2024-05-06 09:16:33 +02:00
|
|
|
}),
|
|
|
|
|
};
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
export default (app: Hono) =>
|
|
|
|
|
app.on(
|
|
|
|
|
meta.allowedMethods,
|
|
|
|
|
meta.route,
|
2024-05-06 10:19:42 +02:00
|
|
|
qsQuery(),
|
2024-05-06 09:16:33 +02:00
|
|
|
zValidator("query", schemas.query, handleZodError),
|
2024-06-08 06:57:29 +02:00
|
|
|
auth(meta.auth, meta.permissions),
|
2024-05-06 09:16:33 +02:00
|
|
|
async (context) => {
|
|
|
|
|
const { user: self } = context.req.valid("header");
|
2024-05-06 10:19:42 +02:00
|
|
|
const { id } = context.req.valid("query");
|
|
|
|
|
|
|
|
|
|
const ids = Array.isArray(id) ? id : [id];
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-06-13 04:26:43 +02:00
|
|
|
if (!self) {
|
|
|
|
|
return errorResponse("Unauthorized", 401);
|
|
|
|
|
}
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-07-27 20:46:19 +02:00
|
|
|
const relationships = await Relationship.fromOwnerAndSubjects(
|
|
|
|
|
self,
|
|
|
|
|
ids,
|
2024-05-06 09:16:33 +02:00
|
|
|
);
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
relationships.sort(
|
2024-07-27 20:46:19 +02:00
|
|
|
(a, b) =>
|
|
|
|
|
ids.indexOf(a.data.subjectId) -
|
|
|
|
|
ids.indexOf(b.data.subjectId),
|
2024-05-06 09:16:33 +02:00
|
|
|
);
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-07-27 20:46:19 +02:00
|
|
|
return jsonResponse(relationships.map((r) => r.toApi()));
|
2024-05-06 09:16:33 +02:00
|
|
|
},
|
|
|
|
|
);
|