summary refs log tree commit diff
path: root/src/api/routes/users/@me/relationships.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/routes/users/@me/relationships.ts')
-rw-r--r--src/api/routes/users/@me/relationships.ts67
1 files changed, 16 insertions, 51 deletions
diff --git a/src/api/routes/users/@me/relationships.ts b/src/api/routes/users/@me/relationships.ts

index a83c24de..d3df9eb0 100644 --- a/src/api/routes/users/@me/relationships.ts +++ b/src/api/routes/users/@me/relationships.ts
@@ -17,25 +17,14 @@ */ import { route } from "@spacebar/api"; -import { - Config, - DiscordApiErrors, - Relationship, - RelationshipAddEvent, - RelationshipRemoveEvent, - User, - emitEvent, -} from "@spacebar/util"; +import { Config, DiscordApiErrors, Relationship, RelationshipAddEvent, RelationshipRemoveEvent, User, emitEvent } from "@spacebar/util"; import { Request, Response, Router } from "express"; import { HTTPError } from "lambert-server"; import { PublicUserProjection, RelationshipType } from "@spacebar/schemas"; const router = Router({ mergeParams: true }); -const userProjection: (keyof User)[] = [ - "relationships", - ...PublicUserProjection, -]; +const userProjection: (keyof User)[] = ["relationships", ...PublicUserProjection]; router.get( "/", @@ -111,10 +100,7 @@ router.post( relations: ["relationships", "relationships.to"], select: userProjection, where: { - discriminator: String(req.body.discriminator).padStart( - 4, - "0", - ), //Discord send the discriminator as integer, we need to add leading zeroes + discriminator: String(req.body.discriminator).padStart(4, "0"), //Discord send the discriminator as integer, we need to add leading zeroes username: req.body.username, }, }), @@ -138,8 +124,7 @@ router.delete( }), async (req: Request, res: Response) => { const { user_id } = req.params; - if (user_id === req.user_id) - throw new HTTPError("You can't remove yourself as a friend"); + if (user_id === req.user_id) throw new HTTPError("You can't remove yourself as a friend"); const user = await User.findOneOrFail({ where: { id: req.user_id }, @@ -153,12 +138,9 @@ router.delete( }); const relationship = user.relationships.find((x) => x.to_id === user_id); - const friendRequest = friend.relationships.find( - (x) => x.to_id === req.user_id, - ); + const friendRequest = friend.relationships.find((x) => x.to_id === req.user_id); - if (!relationship) - throw new HTTPError("You are not friends with the user", 404); + if (!relationship) throw new HTTPError("You are not friends with the user", 404); if (relationship?.type === RelationshipType.blocked) { // unblock user @@ -198,15 +180,9 @@ router.delete( export default router; -async function updateRelationship( - req: Request, - res: Response, - friend: User, - type: RelationshipType, -) { +async function updateRelationship(req: Request, res: Response, friend: User, type: RelationshipType) { const id = friend.id; - if (id === req.user_id) - throw new HTTPError("You can't add yourself as a friend"); + if (id === req.user_id) throw new HTTPError("You can't add yourself as a friend"); const user = await User.findOneOrFail({ where: { id: req.user_id }, @@ -215,15 +191,12 @@ async function updateRelationship( }); let relationship = user.relationships.find((x) => x.to_id === id); - const friendRequest = friend.relationships.find( - (x) => x.to_id === req.user_id, - ); + const friendRequest = friend.relationships.find((x) => x.to_id === req.user_id); // TODO: you can add infinitely many blocked users (should this be prevented?) if (type === RelationshipType.blocked) { if (relationship) { - if (relationship.type === RelationshipType.blocked) - throw new HTTPError("You already blocked the user"); + if (relationship.type === RelationshipType.blocked) throw new HTTPError("You already blocked the user"); relationship.type = RelationshipType.blocked; await relationship.save(); } else { @@ -255,8 +228,7 @@ async function updateRelationship( } const { maxFriends } = Config.get().limits.user; - if (user.relationships.length >= maxFriends) - throw DiscordApiErrors.MAXIMUM_FRIENDS.withParams(maxFriends); + if (user.relationships.length >= maxFriends) throw DiscordApiErrors.MAXIMUM_FRIENDS.withParams(maxFriends); let incoming_relationship = Relationship.create({ nickname: undefined, @@ -272,24 +244,17 @@ async function updateRelationship( }); if (friendRequest) { - if (friendRequest.type === RelationshipType.blocked) - throw new HTTPError("The user blocked you"); - if (friendRequest.type === RelationshipType.friends) - throw new HTTPError("You are already friends with the user"); + if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you"); + if (friendRequest.type === RelationshipType.friends) throw new HTTPError("You are already friends with the user"); // accept friend request incoming_relationship = friendRequest; incoming_relationship.type = RelationshipType.friends; } if (relationship) { - if (relationship.type === RelationshipType.outgoing) - throw new HTTPError("You already sent a friend request"); - if (relationship.type === RelationshipType.blocked) - throw new HTTPError( - "Unblock the user before sending a friend request", - ); - if (relationship.type === RelationshipType.friends) - throw new HTTPError("You are already friends with the user"); + if (relationship.type === RelationshipType.outgoing) throw new HTTPError("You already sent a friend request"); + if (relationship.type === RelationshipType.blocked) throw new HTTPError("Unblock the user before sending a friend request"); + if (relationship.type === RelationshipType.friends) throw new HTTPError("You are already friends with the user"); outgoing_relationship = relationship; outgoing_relationship.type = RelationshipType.friends; }