diff --git a/README.md b/README.md index 353039da..7190c906 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,9 @@ Working endpoints are: - `/api/v1/accounts/:id` - `/api/v1/accounts/:id/statuses` - `/api/v1/accounts/:id/follow` +- `/api/v1/accounts/:id/unfollow` +- `/api/v1/accounts/:id/block` +- `/api/v1/accounts/:id/unblock` - `/api/v1/accounts/update_credentials` - `/api/v1/accounts/verify_credentials` - `/api/v1/statuses` diff --git a/database/entities/Relationship.ts b/database/entities/Relationship.ts index 9f366ea4..8fcb5ce5 100644 --- a/database/entities/Relationship.ts +++ b/database/entities/Relationship.ts @@ -26,37 +26,37 @@ export class Relationship extends BaseEntity { @ManyToOne(() => User) subject!: User; - @Column("varchar") + @Column("boolean") following!: boolean; - @Column("varchar") + @Column("boolean") showing_reblogs!: boolean; - @Column("varchar") + @Column("boolean") notifying!: boolean; - @Column("varchar") + @Column("boolean") followed_by!: boolean; - @Column("varchar") + @Column("boolean") blocking!: boolean; - @Column("varchar") + @Column("boolean") blocked_by!: boolean; - @Column("varchar") + @Column("boolean") muting!: boolean; - @Column("varchar") + @Column("boolean") muting_notifications!: boolean; - @Column("varchar") + @Column("boolean") requested!: boolean; - @Column("varchar") + @Column("boolean") domain_blocking!: boolean; - @Column("varchar") + @Column("boolean") endorsed!: boolean; @Column("jsonb") diff --git a/server/api/api/v1/accounts/[id]/block.ts b/server/api/api/v1/accounts/[id]/block.ts new file mode 100644 index 00000000..bf09cb46 --- /dev/null +++ b/server/api/api/v1/accounts/[id]/block.ts @@ -0,0 +1,52 @@ +import { getUserByToken } from "@auth"; +import { errorResponse, jsonResponse } from "@response"; +import { MatchedRoute } from "bun"; +import { Relationship } from "~database/entities/Relationship"; +import { User } from "~database/entities/User"; + +/** + * Blocks a user + */ +export default async ( + req: Request, + matchedRoute: MatchedRoute +): Promise => { + const id = matchedRoute.params.id; + + // 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 user = await User.findOneBy({ + id, + }); + + if (!user) return errorResponse("User not found", 404); + + // Check if already following + 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; + } + + if (!relationship.blocking) { + relationship.blocking = true; + } + + await relationship.save(); + return jsonResponse(await relationship.toAPI()); +}; diff --git a/server/api/api/v1/accounts/[id]/follow.ts b/server/api/api/v1/accounts/[id]/follow.ts index bf1caf69..2cceeb2e 100644 --- a/server/api/api/v1/accounts/[id]/follow.ts +++ b/server/api/api/v1/accounts/[id]/follow.ts @@ -62,5 +62,7 @@ export default async ( if (languages) { relationship.languages = languages; } + + await relationship.save(); return jsonResponse(await relationship.toAPI()); }; diff --git a/server/api/api/v1/accounts/[id]/remove_from_followers.ts b/server/api/api/v1/accounts/[id]/remove_from_followers.ts new file mode 100644 index 00000000..f636e5da --- /dev/null +++ b/server/api/api/v1/accounts/[id]/remove_from_followers.ts @@ -0,0 +1,52 @@ +import { getUserByToken } from "@auth"; +import { errorResponse, jsonResponse } from "@response"; +import { MatchedRoute } from "bun"; +import { Relationship } from "~database/entities/Relationship"; +import { User } from "~database/entities/User"; + +/** + * Removes an account from your followers list + */ +export default async ( + req: Request, + matchedRoute: MatchedRoute +): Promise => { + const id = matchedRoute.params.id; + + // 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 user = await User.findOneBy({ + id, + }); + + if (!user) return errorResponse("User not found", 404); + + // Check if already following + 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; + } + + if (relationship.followed_by) { + relationship.followed_by = false; + } + + await relationship.save(); + return jsonResponse(await relationship.toAPI()); +}; diff --git a/server/api/api/v1/accounts/[id]/unblock.ts b/server/api/api/v1/accounts/[id]/unblock.ts new file mode 100644 index 00000000..7f9fa22d --- /dev/null +++ b/server/api/api/v1/accounts/[id]/unblock.ts @@ -0,0 +1,52 @@ +import { getUserByToken } from "@auth"; +import { errorResponse, jsonResponse } from "@response"; +import { MatchedRoute } from "bun"; +import { Relationship } from "~database/entities/Relationship"; +import { User } from "~database/entities/User"; + +/** + * Blocks a user + */ +export default async ( + req: Request, + matchedRoute: MatchedRoute +): Promise => { + const id = matchedRoute.params.id; + + // 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 user = await User.findOneBy({ + id, + }); + + if (!user) return errorResponse("User not found", 404); + + // Check if already following + 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; + } + + if (relationship.blocking) { + relationship.blocking = false; + } + + await relationship.save(); + return jsonResponse(await relationship.toAPI()); +}; diff --git a/server/api/api/v1/accounts/[id]/unfollow.ts b/server/api/api/v1/accounts/[id]/unfollow.ts new file mode 100644 index 00000000..a7f02ce2 --- /dev/null +++ b/server/api/api/v1/accounts/[id]/unfollow.ts @@ -0,0 +1,52 @@ +import { getUserByToken } from "@auth"; +import { errorResponse, jsonResponse } from "@response"; +import { MatchedRoute } from "bun"; +import { Relationship } from "~database/entities/Relationship"; +import { User } from "~database/entities/User"; + +/** + * Unfollows a user + */ +export default async ( + req: Request, + matchedRoute: MatchedRoute +): Promise => { + const id = matchedRoute.params.id; + + // 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 user = await User.findOneBy({ + id, + }); + + if (!user) return errorResponse("User not found", 404); + + // Check if already following + 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; + } + + if (relationship.following) { + relationship.following = false; + } + + await relationship.save(); + return jsonResponse(await relationship.toAPI()); +}; diff --git a/tests/api.test.ts b/tests/api.test.ts index 8e8c8a70..5d289146 100644 --- a/tests/api.test.ts +++ b/tests/api.test.ts @@ -243,6 +243,102 @@ describe("POST /api/v1/accounts/:id/follow", () => { }); }); +describe("POST /api/v1/accounts/:id/unfollow", () => { + test("should unfollow the specified user and return an APIRelationship object", async () => { + const response = await fetch( + `${config.http.base_url}/api/v1/accounts/${user2.id}/unfollow`, + { + method: "POST", + headers: { + Authorization: `Bearer ${token.access_token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({}), + } + ); + + expect(response.status).toBe(200); + expect(response.headers.get("content-type")).toBe("application/json"); + + const account: APIRelationship = await response.json(); + + expect(account.id).toBe(user2.id); + expect(account.following).toBe(false); + }); +}); + +describe("POST /api/v1/accounts/:id/remove_from_followers", () => { + test("should remove the specified user from the authenticated user's followers and return an APIRelationship object", async () => { + const response = await fetch( + `${config.http.base_url}/api/v1/accounts/${user2.id}/remove_from_followers`, + { + method: "POST", + headers: { + Authorization: `Bearer ${token.access_token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({}), + } + ); + + expect(response.status).toBe(200); + expect(response.headers.get("content-type")).toBe("application/json"); + + const account: APIRelationship = await response.json(); + + expect(account.id).toBe(user2.id); + expect(account.followed_by).toBe(false); + }); +}); + +describe("POST /api/v1/accounts/:id/block", () => { + test("should block the specified user and return an APIRelationship object", async () => { + const response = await fetch( + `${config.http.base_url}/api/v1/accounts/${user2.id}/block`, + { + method: "POST", + headers: { + Authorization: `Bearer ${token.access_token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({}), + } + ); + + expect(response.status).toBe(200); + expect(response.headers.get("content-type")).toBe("application/json"); + + const account: APIRelationship = await response.json(); + + expect(account.id).toBe(user2.id); + expect(account.blocking).toBe(true); + }); +}); + +describe("POST /api/v1/accounts/:id/unblock", () => { + test("should unblock the specified user and return an APIRelationship object", async () => { + const response = await fetch( + `${config.http.base_url}/api/v1/accounts/${user2.id}/unblock`, + { + method: "POST", + headers: { + Authorization: `Bearer ${token.access_token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({}), + } + ); + + expect(response.status).toBe(200); + expect(response.headers.get("content-type")).toBe("application/json"); + + const account: APIRelationship = await response.json(); + + expect(account.id).toBe(user2.id); + expect(account.blocking).toBe(false); + }); +}); + afterAll(async () => { const activities = await RawActivity.createQueryBuilder("activity") .where("activity.data->>'actor' = :actor", {