mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
Added blocking, unblocking, removing from followers and unfollowing
This commit is contained in:
parent
a9688b8178
commit
bb2c770b68
|
|
@ -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`
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
52
server/api/api/v1/accounts/[id]/block.ts
Normal file
52
server/api/api/v1/accounts/[id]/block.ts
Normal 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());
|
||||
};
|
||||
|
|
@ -62,5 +62,7 @@ export default async (
|
|||
if (languages) {
|
||||
relationship.languages = languages;
|
||||
}
|
||||
|
||||
await relationship.save();
|
||||
return jsonResponse(await relationship.toAPI());
|
||||
};
|
||||
|
|
|
|||
52
server/api/api/v1/accounts/[id]/remove_from_followers.ts
Normal file
52
server/api/api/v1/accounts/[id]/remove_from_followers.ts
Normal 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());
|
||||
};
|
||||
52
server/api/api/v1/accounts/[id]/unblock.ts
Normal file
52
server/api/api/v1/accounts/[id]/unblock.ts
Normal 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());
|
||||
};
|
||||
52
server/api/api/v1/accounts/[id]/unfollow.ts
Normal file
52
server/api/api/v1/accounts/[id]/unfollow.ts
Normal 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());
|
||||
};
|
||||
|
|
@ -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", {
|
||||
|
|
|
|||
Loading…
Reference in a new issue