47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import type { APIRoute } from "astro";
|
|
|
|
export const POST: APIRoute = async ({ request, locals }) => {
|
|
if (!locals.pb.authStore.isValid) {
|
|
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
|
status: 401,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
|
|
try {
|
|
const { friendshipId } = await request.json();
|
|
|
|
if (!friendshipId) {
|
|
return new Response(JSON.stringify({ error: "Missing friendship id" }), {
|
|
status: 400,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
|
|
const friendship = await locals.pb.collection("friendships").getOne(friendshipId);
|
|
|
|
// Only the addressee can accept
|
|
if (friendship.addressee !== locals.pb.authStore.record!.id) {
|
|
return new Response(JSON.stringify({ error: "Forbidden" }), {
|
|
status: 403,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
|
|
await locals.pb.collection("friendships").update(friendshipId, {
|
|
status: "accepted",
|
|
});
|
|
|
|
return new Response(JSON.stringify({ success: true }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
} catch (err) {
|
|
console.error("Accept friend failed:", err);
|
|
return new Response(JSON.stringify({ error: "Failed to accept" }), {
|
|
status: 500,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
};
|