From d0c07d804b2f1df4c4801b8fc2aa5ee1db021fcd Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Fri, 22 Sep 2023 17:16:24 -1000 Subject: [PATCH] Add new familiar followers endpoint --- README.md | 1 + .../v1/accounts/familiar_followers/index.ts | 73 +++++++++++++++++++ .../api/v1/accounts/relationships/index.ts | 2 +- tests/api.test.ts | 48 ++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 server/api/api/v1/accounts/familiar_followers/index.ts diff --git a/README.md b/README.md index 715f1549..26b8e699 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Working endpoints are: - `/api/v1/accounts/relationships` - `/api/v1/accounts/update_credentials` - `/api/v1/accounts/verify_credentials` +- `/api/v1/accounts/familiar_followers` - `/api/v1/statuses` - `/api/v1/apps` - `/api/v1/apps/verify_credentials` diff --git a/server/api/api/v1/accounts/familiar_followers/index.ts b/server/api/api/v1/accounts/familiar_followers/index.ts new file mode 100644 index 00000000..651aa9d3 --- /dev/null +++ b/server/api/api/v1/accounts/familiar_followers/index.ts @@ -0,0 +1,73 @@ +import { getUserByToken } from "@auth"; +import { parseRequest } from "@request"; +import { errorResponse, jsonResponse } from "@response"; +import { User } from "~database/entities/User"; +import { APIAccount } from "~types/entities/account"; + +/** + * Find familiar followers (followers of a user that you also follow) + */ +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); + } + + const response = ( + await Promise.all( + ids.map(async id => { + // Find followers of user that you also follow + + // Get user + const user = await User.findOne({ + where: { id }, + relations: { + relationships: { + subject: { + relationships: true, + }, + }, + }, + }); + + if (!user) return null; + + // Map to user response + const response = user.relationships + .filter(r => r.following) + .map(r => r.subject) + .filter(u => + u.relationships.some( + r => r.following && r.subject.id === self.id + ) + ); + + return { + id: id, + accounts: await Promise.all( + response.map(async u => await u.toAPI()) + ), + }; + }) + ) + ).filter(r => r !== null) as { + id: string; + accounts: APIAccount[]; + }[]; + + return jsonResponse(response); +}; diff --git a/server/api/api/v1/accounts/relationships/index.ts b/server/api/api/v1/accounts/relationships/index.ts index 2c95d339..94d88b72 100644 --- a/server/api/api/v1/accounts/relationships/index.ts +++ b/server/api/api/v1/accounts/relationships/index.ts @@ -5,7 +5,7 @@ import { Relationship } from "~database/entities/Relationship"; import { User } from "~database/entities/User"; /** - * Sets a user note + * Find relationships */ export default async (req: Request): Promise => { // Check auth token diff --git a/tests/api.test.ts b/tests/api.test.ts index 35965dab..8aa481de 100644 --- a/tests/api.test.ts +++ b/tests/api.test.ts @@ -514,6 +514,54 @@ describe("GET /api/v1/accounts/relationships", () => { }); }); +describe("GET /api/v1/accounts/familiar_followers", () => { + test("should return an array of objects with id and accounts properties, where id is a string and accounts is an array of APIAccount objects", async () => { + const response = await fetch( + `${config.http.base_url}/api/v1/accounts/familiar_followers?id[]=${user2.id}`, + { + method: "GET", + headers: { + Authorization: `Bearer ${token.access_token}`, + }, + } + ); + + expect(response.status).toBe(200); + expect(response.headers.get("content-type")).toBe("application/json"); + + const familiarFollowers: { id: string; accounts: APIAccount[] }[] = + await response.json(); + + expect(Array.isArray(familiarFollowers)).toBe(true); + expect(familiarFollowers.length).toBeGreaterThan(0); + expect(typeof familiarFollowers[0].id).toBe("string"); + expect(Array.isArray(familiarFollowers[0].accounts)).toBe(true); + expect(familiarFollowers[0].accounts.length).toBeGreaterThanOrEqual(0); + + if (familiarFollowers[0].accounts.length === 0) return; + expect(familiarFollowers[0].accounts[0].id).toBeDefined(); + expect(familiarFollowers[0].accounts[0].username).toBeDefined(); + expect(familiarFollowers[0].accounts[0].acct).toBeDefined(); + expect(familiarFollowers[0].accounts[0].display_name).toBeDefined(); + expect(familiarFollowers[0].accounts[0].locked).toBeDefined(); + expect(familiarFollowers[0].accounts[0].bot).toBeDefined(); + expect(familiarFollowers[0].accounts[0].discoverable).toBeDefined(); + expect(familiarFollowers[0].accounts[0].group).toBeDefined(); + expect(familiarFollowers[0].accounts[0].created_at).toBeDefined(); + expect(familiarFollowers[0].accounts[0].note).toBeDefined(); + expect(familiarFollowers[0].accounts[0].url).toBeDefined(); + expect(familiarFollowers[0].accounts[0].avatar).toBeDefined(); + expect(familiarFollowers[0].accounts[0].avatar_static).toBeDefined(); + expect(familiarFollowers[0].accounts[0].header).toBeDefined(); + expect(familiarFollowers[0].accounts[0].header_static).toBeDefined(); + expect(familiarFollowers[0].accounts[0].followers_count).toBeDefined(); + expect(familiarFollowers[0].accounts[0].following_count).toBeDefined(); + expect(familiarFollowers[0].accounts[0].statuses_count).toBeDefined(); + expect(familiarFollowers[0].accounts[0].emojis).toBeDefined(); + expect(familiarFollowers[0].accounts[0].fields).toBeDefined(); + }); +}); + afterAll(async () => { const activities = await RawActivity.createQueryBuilder("activity") .where("activity.data->>'actor' = :actor", {