mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
Add pin and unpin endpoints for account
This commit is contained in:
parent
012f4b6f5b
commit
d2d2e576a9
|
|
@ -72,6 +72,8 @@ Working endpoints are:
|
|||
- `/api/v1/accounts/:id/unblock`
|
||||
- `/api/v1/accounts/:id/mute`
|
||||
- `/api/v1/accounts/:id/unmute`
|
||||
- `/api/v1/accounts/:id/pin`
|
||||
- `/api/v1/accounts/:id/unpin`
|
||||
- `/api/v1/accounts/update_credentials`
|
||||
- `/api/v1/accounts/verify_credentials`
|
||||
- `/api/v1/statuses`
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import { Relationship } from "~database/entities/Relationship";
|
|||
import { User } from "~database/entities/User";
|
||||
|
||||
/**
|
||||
* Follow a user
|
||||
* Mute a user
|
||||
*/
|
||||
export default async (
|
||||
req: Request,
|
||||
|
|
|
|||
52
server/api/api/v1/accounts/[id]/pin.ts
Normal file
52
server/api/api/v1/accounts/[id]/pin.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";
|
||||
|
||||
/**
|
||||
* Pin 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.endorsed) {
|
||||
relationship.endorsed = true;
|
||||
}
|
||||
|
||||
await relationship.save();
|
||||
return jsonResponse(await relationship.toAPI());
|
||||
};
|
||||
|
|
@ -5,7 +5,7 @@ import { Relationship } from "~database/entities/Relationship";
|
|||
import { User } from "~database/entities/User";
|
||||
|
||||
/**
|
||||
* Follow a user
|
||||
* Unmute a user
|
||||
*/
|
||||
export default async (
|
||||
req: Request,
|
||||
|
|
|
|||
52
server/api/api/v1/accounts/[id]/unpin.ts
Normal file
52
server/api/api/v1/accounts/[id]/unpin.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";
|
||||
|
||||
/**
|
||||
* Unpin 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.endorsed) {
|
||||
relationship.endorsed = false;
|
||||
}
|
||||
|
||||
await relationship.save();
|
||||
return jsonResponse(await relationship.toAPI());
|
||||
};
|
||||
|
|
@ -411,6 +411,54 @@ describe("POST /api/v1/accounts/:id/unmute", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("POST /api/v1/accounts/:id/pin", () => {
|
||||
test("should pin the specified user and return an APIRelationship object", async () => {
|
||||
const response = await fetch(
|
||||
`${config.http.base_url}/api/v1/accounts/${user2.id}/pin`,
|
||||
{
|
||||
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.endorsed).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/v1/accounts/:id/unpin", () => {
|
||||
test("should unpin the specified user and return an APIRelationship object", async () => {
|
||||
const response = await fetch(
|
||||
`${config.http.base_url}/api/v1/accounts/${user2.id}/unpin`,
|
||||
{
|
||||
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.endorsed).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
const activities = await RawActivity.createQueryBuilder("activity")
|
||||
.where("activity.data->>'actor' = :actor", {
|
||||
|
|
|
|||
Loading…
Reference in a new issue