mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 13:59:16 +01:00
Add 2 more endpoints (mute and unmute)
This commit is contained in:
parent
bb2c770b68
commit
012f4b6f5b
6 changed files with 201 additions and 1 deletions
64
server/api/api/v1/accounts/[id]/mute.ts
Normal file
64
server/api/api/v1/accounts/[id]/mute.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import { getUserByToken } from "@auth";
|
||||
import { parseRequest } from "@request";
|
||||
import { errorResponse, jsonResponse } from "@response";
|
||||
import { MatchedRoute } from "bun";
|
||||
import { Relationship } from "~database/entities/Relationship";
|
||||
import { User } from "~database/entities/User";
|
||||
|
||||
/**
|
||||
* Follow 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);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { notifications, duration } = await parseRequest<{
|
||||
notifications: boolean;
|
||||
duration: number;
|
||||
}>(req);
|
||||
|
||||
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.muting) {
|
||||
relationship.muting = true;
|
||||
}
|
||||
if (notifications ?? true) {
|
||||
relationship.muting_notifications = true;
|
||||
}
|
||||
|
||||
// TODO: Implement duration
|
||||
|
||||
await relationship.save();
|
||||
return jsonResponse(await relationship.toAPI());
|
||||
};
|
||||
54
server/api/api/v1/accounts/[id]/unmute.ts
Normal file
54
server/api/api/v1/accounts/[id]/unmute.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
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";
|
||||
|
||||
/**
|
||||
* Follow 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.muting) {
|
||||
relationship.muting = false;
|
||||
}
|
||||
|
||||
// TODO: Implement duration
|
||||
|
||||
await relationship.save();
|
||||
return jsonResponse(await relationship.toAPI());
|
||||
};
|
||||
|
|
@ -102,7 +102,13 @@ export default async (req: Request): Promise<Response> => {
|
|||
account: user,
|
||||
application,
|
||||
content: status,
|
||||
visibility: visibility || "public",
|
||||
visibility:
|
||||
visibility ||
|
||||
(config.defaults.visibility as
|
||||
| "public"
|
||||
| "unlisted"
|
||||
| "private"
|
||||
| "direct"),
|
||||
sensitive: sensitive || false,
|
||||
spoiler_text: spoiler_text || "",
|
||||
emojis: [],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue