From 50ab0155a54f270e1c175f63980cfe5557fc7586 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Fri, 22 Sep 2023 16:28:00 -1000 Subject: [PATCH] Implement account relationship endpoints --- README.md | 1 + .../api/v1/accounts/relationships/index.ts | 60 +++++++++++++++++++ tests/api.test.ts | 35 +++++++++++ 3 files changed, 96 insertions(+) create mode 100644 server/api/api/v1/accounts/relationships/index.ts diff --git a/README.md b/README.md index ad48a899..715f1549 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ Working endpoints are: - `/api/v1/accounts/:id/pin` - `/api/v1/accounts/:id/unpin` - `/api/v1/accounts/:id/note` +- `/api/v1/accounts/relationships` - `/api/v1/accounts/update_credentials` - `/api/v1/accounts/verify_credentials` - `/api/v1/statuses` diff --git a/server/api/api/v1/accounts/relationships/index.ts b/server/api/api/v1/accounts/relationships/index.ts new file mode 100644 index 00000000..2c95d339 --- /dev/null +++ b/server/api/api/v1/accounts/relationships/index.ts @@ -0,0 +1,60 @@ +import { getUserByToken } from "@auth"; +import { parseRequest } from "@request"; +import { errorResponse, jsonResponse } from "@response"; +import { Relationship } from "~database/entities/Relationship"; +import { User } from "~database/entities/User"; + +/** + * Sets a user note + */ +export default async (req: Request): Promise => { + // Check auth token + const token = req.headers.get("Authorization")?.split(" ")[1] || null; + + if (!token) + return errorResponse("This method requires an authenticated user", 422); + + const self = await getUserByToken(token); + + if (!self) return errorResponse("Unauthorized", 401); + + const { "id[]": ids } = await parseRequest<{ + "id[]": string[]; + }>(req); + + // Minimum id count 1, maximum 10 + if (!ids || ids.length < 1 || ids.length > 10) { + return errorResponse("Number of ids must be between 1 and 10", 422); + } + + // Check if already following + // TODO: Limit ID amount + const relationships = ( + await Promise.all( + ids.map(async id => { + const user = await User.findOneBy({ id }); + if (!user) return null; + let relationship = await self.getRelationshipToOtherUser(user); + + if (!relationship) { + // Create new relationship + + const newRelationship = await Relationship.createNew( + self, + user + ); + + self.relationships.push(newRelationship); + await self.save(); + + relationship = newRelationship; + } + return relationship; + }) + ) + ).filter(relationship => relationship !== null) as Relationship[]; + + return jsonResponse( + await Promise.all(relationships.map(async r => await r.toAPI())) + ); +}; diff --git a/tests/api.test.ts b/tests/api.test.ts index af14fb7c..285cdbb9 100644 --- a/tests/api.test.ts +++ b/tests/api.test.ts @@ -483,6 +483,41 @@ describe("POST /api/v1/accounts/:id/note", () => { }); }); +describe("GET /api/v1/accounts/relationships", () => { + test("should return an array of APIRelationship objects for the authenticated user's relationships", async () => { + const response = await fetch( + `${config.http.base_url}/api/v1/accounts/relationships`, + { + method: "POST", + headers: { + Authorization: `Bearer ${token.access_token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + "id[]": [user2.id], + }), + } + ); + + expect(response.status).toBe(200); + expect(response.headers.get("content-type")).toBe("application/json"); + + const relationships: APIRelationship[] = await response.json(); + + expect(Array.isArray(relationships)).toBe(true); + expect(relationships.length).toBeGreaterThan(0); + expect(relationships[0].id).toBeDefined(); + expect(relationships[0].following).toBeDefined(); + expect(relationships[0].followed_by).toBeDefined(); + expect(relationships[0].blocking).toBeDefined(); + expect(relationships[0].muting).toBeDefined(); + expect(relationships[0].muting_notifications).toBeDefined(); + expect(relationships[0].requested).toBeDefined(); + expect(relationships[0].domain_blocking).toBeDefined(); + expect(relationships[0].notifying).toBeDefined(); + }); +}); + afterAll(async () => { const activities = await RawActivity.createQueryBuilder("activity") .where("activity.data->>'actor' = :actor", {