Implement account relationship endpoints

This commit is contained in:
Jesse Wierzbinski 2023-09-22 16:28:00 -10:00
parent ee3d4a386f
commit 50ab0155a5
3 changed files with 96 additions and 0 deletions

View file

@ -75,6 +75,7 @@ Working endpoints are:
- `/api/v1/accounts/:id/pin` - `/api/v1/accounts/:id/pin`
- `/api/v1/accounts/:id/unpin` - `/api/v1/accounts/:id/unpin`
- `/api/v1/accounts/:id/note` - `/api/v1/accounts/:id/note`
- `/api/v1/accounts/relationships`
- `/api/v1/accounts/update_credentials` - `/api/v1/accounts/update_credentials`
- `/api/v1/accounts/verify_credentials` - `/api/v1/accounts/verify_credentials`
- `/api/v1/statuses` - `/api/v1/statuses`

View file

@ -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<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);
}
// 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()))
);
};

View file

@ -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 () => { afterAll(async () => {
const activities = await RawActivity.createQueryBuilder("activity") const activities = await RawActivity.createQueryBuilder("activity")
.where("activity.data->>'actor' = :actor", { .where("activity.data->>'actor' = :actor", {