Add new familiar followers endpoint

This commit is contained in:
Jesse Wierzbinski 2023-09-22 17:16:24 -10:00
parent 41e70d00e8
commit d0c07d804b
4 changed files with 123 additions and 1 deletions

View file

@ -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`

View file

@ -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<Response> => {
// 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);
};

View file

@ -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<Response> => {
// Check auth token

View file

@ -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", {