2025-03-24 14:42:09 +01:00
|
|
|
import { apiRoute, auth, qsQuery } from "@/api";
|
2025-02-05 21:49:39 +01:00
|
|
|
import { createRoute, z } from "@hono/zod-openapi";
|
2025-03-22 02:34:03 +01:00
|
|
|
import {
|
|
|
|
|
Account as AccountSchema,
|
|
|
|
|
FamiliarFollowers as FamiliarFollowersSchema,
|
2025-03-22 18:04:47 +01:00
|
|
|
} from "@versia/client/schemas";
|
|
|
|
|
import { RolePermission } from "@versia/client/schemas";
|
2024-11-01 21:05:54 +01:00
|
|
|
import { User, db } from "@versia/kit/db";
|
2025-03-22 18:04:47 +01:00
|
|
|
import type { Users } from "@versia/kit/tables";
|
2024-11-03 17:45:21 +01:00
|
|
|
import { type InferSelectModel, sql } from "drizzle-orm";
|
2025-03-24 14:42:09 +01:00
|
|
|
import { ApiError } from "~/classes/errors/api-error";
|
2025-03-27 20:12:00 +01:00
|
|
|
import { rateLimit } from "~/middlewares/rate-limit";
|
2023-09-23 05:16:24 +02:00
|
|
|
|
2024-08-27 20:14:10 +02:00
|
|
|
const route = createRoute({
|
|
|
|
|
method: "get",
|
|
|
|
|
path: "/api/v1/accounts/familiar_followers",
|
|
|
|
|
summary: "Get familiar followers",
|
|
|
|
|
description:
|
|
|
|
|
"Obtain a list of all accounts that follow a given account, filtered for accounts you follow.",
|
2025-02-13 01:31:15 +01:00
|
|
|
externalDocs: {
|
|
|
|
|
url: "https://docs.joinmastodon.org/methods/accounts/#familiar_followers",
|
|
|
|
|
},
|
|
|
|
|
tags: ["Accounts"],
|
2024-12-30 19:18:31 +01:00
|
|
|
middleware: [
|
|
|
|
|
auth({
|
|
|
|
|
auth: true,
|
|
|
|
|
scopes: ["read:follows"],
|
2025-03-22 18:04:47 +01:00
|
|
|
permissions: [RolePermission.ManageOwnFollows],
|
2024-12-30 19:18:31 +01:00
|
|
|
}),
|
2025-03-27 20:12:00 +01:00
|
|
|
rateLimit(5),
|
2024-12-30 19:18:31 +01:00
|
|
|
qsQuery(),
|
|
|
|
|
] as const,
|
2024-08-27 20:14:10 +02:00
|
|
|
request: {
|
2025-02-13 01:31:15 +01:00
|
|
|
query: z.object({
|
|
|
|
|
id: z
|
|
|
|
|
.array(AccountSchema.shape.id)
|
|
|
|
|
.min(1)
|
|
|
|
|
.max(10)
|
|
|
|
|
.or(AccountSchema.shape.id.transform((v) => [v]))
|
|
|
|
|
.openapi({
|
|
|
|
|
description:
|
|
|
|
|
"Find familiar followers for the provided account IDs.",
|
|
|
|
|
example: [
|
|
|
|
|
"f137ce6f-ff5e-4998-b20f-0361ba9be007",
|
|
|
|
|
"8424c654-5d03-4a1b-bec8-4e87db811b5d",
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
}),
|
2024-08-27 20:14:10 +02:00
|
|
|
},
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "Familiar followers",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
2025-02-13 01:31:15 +01:00
|
|
|
schema: z.array(FamiliarFollowersSchema),
|
2024-08-27 20:14:10 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-03-24 14:42:09 +01:00
|
|
|
401: ApiError.missingAuthentication().schema,
|
|
|
|
|
422: ApiError.validationFailed().schema,
|
2024-08-27 20:14:10 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-19 20:06:38 +02:00
|
|
|
export default apiRoute((app) =>
|
2024-08-27 20:14:10 +02:00
|
|
|
app.openapi(route, async (context) => {
|
2024-12-30 19:38:41 +01:00
|
|
|
const { user } = context.get("auth");
|
2024-08-27 20:14:10 +02:00
|
|
|
const { id: ids } = context.req.valid("query");
|
2023-09-23 05:16:24 +02:00
|
|
|
|
2024-11-03 17:45:21 +01:00
|
|
|
// Find followers of the accounts in "ids", that you also follow
|
|
|
|
|
const finalUsers = await Promise.all(
|
|
|
|
|
ids.map(async (id) => ({
|
|
|
|
|
id,
|
|
|
|
|
accounts: await User.fromIds(
|
|
|
|
|
(
|
|
|
|
|
await db.execute(sql<InferSelectModel<typeof Users>>`
|
|
|
|
|
SELECT "Users"."id" FROM "Users"
|
2024-12-30 18:00:23 +01:00
|
|
|
INNER JOIN "Relationships" AS "SelfFollowing"
|
2024-11-03 17:45:21 +01:00
|
|
|
ON "SelfFollowing"."subjectId" = "Users"."id"
|
2024-12-30 19:38:41 +01:00
|
|
|
WHERE "SelfFollowing"."ownerId" = ${user.id}
|
2024-11-03 17:45:21 +01:00
|
|
|
AND "SelfFollowing"."following" = true
|
|
|
|
|
AND EXISTS (
|
|
|
|
|
SELECT 1 FROM "Relationships" AS "IdsFollowers"
|
|
|
|
|
WHERE "IdsFollowers"."subjectId" = ${id}
|
|
|
|
|
AND "IdsFollowers"."ownerId" = "Users"."id"
|
|
|
|
|
AND "IdsFollowers"."following" = true
|
|
|
|
|
)
|
|
|
|
|
`)
|
|
|
|
|
).rows.map((u) => u.id as string),
|
2024-08-27 20:14:10 +02:00
|
|
|
),
|
2024-11-03 17:45:21 +01:00
|
|
|
})),
|
2024-08-27 20:14:10 +02:00
|
|
|
);
|
2024-04-13 14:20:12 +02:00
|
|
|
|
2024-08-27 20:14:10 +02:00
|
|
|
return context.json(
|
2024-11-03 17:45:21 +01:00
|
|
|
finalUsers.map((u) => ({
|
|
|
|
|
...u,
|
|
|
|
|
accounts: u.accounts.map((a) => a.toApi()),
|
|
|
|
|
})),
|
2024-08-27 20:14:10 +02:00
|
|
|
200,
|
|
|
|
|
);
|
|
|
|
|
}),
|
2024-08-19 20:06:38 +02:00
|
|
|
);
|