Added blocking, unblocking, removing from followers and unfollowing

This commit is contained in:
Jesse Wierzbinski 2023-09-21 21:07:39 -10:00
parent a9688b8178
commit bb2c770b68
8 changed files with 320 additions and 11 deletions

View file

@ -67,6 +67,9 @@ Working endpoints are:
- `/api/v1/accounts/:id` - `/api/v1/accounts/:id`
- `/api/v1/accounts/:id/statuses` - `/api/v1/accounts/:id/statuses`
- `/api/v1/accounts/:id/follow` - `/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/update_credentials`
- `/api/v1/accounts/verify_credentials` - `/api/v1/accounts/verify_credentials`
- `/api/v1/statuses` - `/api/v1/statuses`

View file

@ -26,37 +26,37 @@ export class Relationship extends BaseEntity {
@ManyToOne(() => User) @ManyToOne(() => User)
subject!: User; subject!: User;
@Column("varchar") @Column("boolean")
following!: boolean; following!: boolean;
@Column("varchar") @Column("boolean")
showing_reblogs!: boolean; showing_reblogs!: boolean;
@Column("varchar") @Column("boolean")
notifying!: boolean; notifying!: boolean;
@Column("varchar") @Column("boolean")
followed_by!: boolean; followed_by!: boolean;
@Column("varchar") @Column("boolean")
blocking!: boolean; blocking!: boolean;
@Column("varchar") @Column("boolean")
blocked_by!: boolean; blocked_by!: boolean;
@Column("varchar") @Column("boolean")
muting!: boolean; muting!: boolean;
@Column("varchar") @Column("boolean")
muting_notifications!: boolean; muting_notifications!: boolean;
@Column("varchar") @Column("boolean")
requested!: boolean; requested!: boolean;
@Column("varchar") @Column("boolean")
domain_blocking!: boolean; domain_blocking!: boolean;
@Column("varchar") @Column("boolean")
endorsed!: boolean; endorsed!: boolean;
@Column("jsonb") @Column("jsonb")

View file

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

View file

@ -62,5 +62,7 @@ export default async (
if (languages) { if (languages) {
relationship.languages = languages; relationship.languages = languages;
} }
await relationship.save();
return jsonResponse(await relationship.toAPI()); return jsonResponse(await relationship.toAPI());
}; };

View file

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

View file

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

View file

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

View file

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