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-05-06 09:16:33 +02:00
|
|
|
import { zValidator } from "@hono/zod-validator";
|
|
|
|
|
import type { Hono } from "hono";
|
2024-04-14 12:36:25 +02:00
|
|
|
import { z } from "zod";
|
2023-11-12 02:37:14 +01:00
|
|
|
import {
|
2024-04-07 07:30:49 +02:00
|
|
|
createNewRelationship,
|
2024-06-13 04:26:43 +02:00
|
|
|
relationshipToApi,
|
2024-06-29 05:50:56 +02:00
|
|
|
} from "~/classes/functions/relationship";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { db } from "~/drizzle/db";
|
2024-06-12 01:42:36 +02:00
|
|
|
import { RolePermissions } from "~/drizzle/schema";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { User } from "~/packages/database-interface/user";
|
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-05-06 09:16:33 +02:00
|
|
|
const relationships = await db.query.Relationships.findMany({
|
|
|
|
|
where: (relationship, { inArray, and, eq }) =>
|
|
|
|
|
and(
|
|
|
|
|
inArray(relationship.subjectId, ids),
|
|
|
|
|
eq(relationship.ownerId, self.id),
|
|
|
|
|
),
|
|
|
|
|
});
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
const missingIds = ids.filter(
|
|
|
|
|
(id) => !relationships.some((r) => r.subjectId === id),
|
|
|
|
|
);
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
for (const id of missingIds) {
|
|
|
|
|
const user = await User.fromId(id);
|
2024-06-13 04:26:43 +02:00
|
|
|
if (!user) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-05-06 09:16:33 +02:00
|
|
|
const relationship = await createNewRelationship(self, user);
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
relationships.push(relationship);
|
|
|
|
|
}
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-05-06 09:16:33 +02:00
|
|
|
relationships.sort(
|
|
|
|
|
(a, b) => ids.indexOf(a.subjectId) - ids.indexOf(b.subjectId),
|
|
|
|
|
);
|
2023-09-23 04:28:00 +02:00
|
|
|
|
2024-06-13 04:26:43 +02:00
|
|
|
return jsonResponse(relationships.map((r) => relationshipToApi(r)));
|
2024-05-06 09:16:33 +02:00
|
|
|
},
|
|
|
|
|
);
|