mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 13:59:16 +01:00
Added blocking, unblocking, removing from followers and unfollowing
This commit is contained in:
parent
a9688b8178
commit
bb2c770b68
8 changed files with 320 additions and 11 deletions
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());
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue