server/server/api/api/v1/accounts/[id]/pin.ts

56 lines
1.6 KiB
TypeScript

import { apiRoute, applyConfig, idValidator } from "@api";
import { errorResponse, jsonResponse } from "@response";
import { eq } from "drizzle-orm";
import { relationshipToAPI } from "~database/entities/Relationship";
import { getRelationshipToOtherUser } from "~database/entities/User";
import { db } from "~drizzle/db";
import { Relationships } from "~drizzle/schema";
import { User } from "~packages/database-interface/user";
export const meta = applyConfig({
allowedMethods: ["POST"],
ratelimits: {
max: 30,
duration: 60,
},
route: "/api/v1/accounts/:id/pin",
auth: {
required: true,
oauthPermissions: ["write:accounts"],
},
});
/**
* Pin a user
*/
export default apiRoute(async (req, matchedRoute, extraData) => {
const id = matchedRoute.params.id;
if (!id.match(idValidator)) {
return errorResponse("Invalid ID, must be of type UUIDv7", 404);
}
const { user: self } = extraData.auth;
if (!self) return errorResponse("Unauthorized", 401);
const otherUser = await User.fromId(id);
if (!otherUser) return errorResponse("User not found", 404);
// Check if already following
const foundRelationship = await getRelationshipToOtherUser(self, otherUser);
if (!foundRelationship.endorsed) {
foundRelationship.endorsed = true;
await db
.update(Relationships)
.set({
endorsed: true,
})
.where(eq(Relationships.id, foundRelationship.id));
}
return jsonResponse(relationshipToAPI(foundRelationship));
});