diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-09-01 10:47:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-01 10:47:00 +0200 |
commit | df1d633c4b56bea97e0b76da34171d80c0c6a3ff (patch) | |
tree | 7e57837ec000a641935a32649ac17b38ef2e29d8 /api/src/routes/users/@me/relationships.ts | |
parent | Merge pull request #287 from EMREOYUN/patch-1 (diff) | |
parent | Merge pull request #299 from AlTech98/typeorm (diff) | |
download | server-df1d633c4b56bea97e0b76da34171d80c0c6a3ff.tar.xz |
Merge pull request #300 from fosscord/typeorm
Diffstat (limited to 'api/src/routes/users/@me/relationships.ts')
-rw-r--r-- | api/src/routes/users/@me/relationships.ts | 89 |
1 files changed, 45 insertions, 44 deletions
diff --git a/api/src/routes/users/@me/relationships.ts b/api/src/routes/users/@me/relationships.ts index 642ee5f9..0b864d88 100644 --- a/api/src/routes/users/@me/relationships.ts +++ b/api/src/routes/users/@me/relationships.ts @@ -1,12 +1,11 @@ import { RelationshipAddEvent, - UserModel, + User, PublicUserProjection, - toObject, RelationshipType, RelationshipRemoveEvent, - UserDocument, - emitEvent + emitEvent, + Relationship } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; @@ -15,40 +14,36 @@ import { check, Length } from "../../../util/instanceOf"; const router = Router(); -const userProjection = { "user_data.relationships": true, ...PublicUserProjection }; +const userProjection: (keyof User)[] = ["relationships", ...PublicUserProjection]; router.get("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }, { user_data: { relationships: true } }) - .populate({ path: "user_data.relationships.id", model: UserModel }) - .exec(); + const user = await User.findOneOrFail({ where: { id: req.user_id }, select: ["relationships"] }); - return res.json(toObject(user.user_data.relationships)); + return res.json(user.relationships); }); -async function addRelationship(req: Request, res: Response, friend: UserDocument, 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"); - const user = await UserModel.findOne({ id: req.user_id }, userProjection).exec(); - const newUserRelationships = [...user.user_data.relationships]; - const newFriendRelationships = [...friend.user_data.relationships]; + const user = await User.findOneOrFail({ id: req.user_id }, { relations: ["relationships"], select: userProjection }); - var relationship = newUserRelationships.find((x) => x.id === id); - const friendRequest = newFriendRelationships.find((x) => x.id === req.user_id); + var relationship = user.relationships.find((x) => x.id === id); + const friendRequest = friend.relationships.find((x) => x.id === req.user_id); if (type === RelationshipType.blocked) { if (relationship) { if (relationship.type === RelationshipType.blocked) throw new HTTPError("You already blocked the user"); relationship.type = RelationshipType.blocked; } else { - relationship = { id, type: RelationshipType.blocked }; - newUserRelationships.push(relationship); + relationship = new Relationship({ id, type: RelationshipType.blocked }); + user.relationships.push(relationship); } if (friendRequest && friendRequest.type !== RelationshipType.blocked) { - newFriendRelationships.remove(friendRequest); + friend.relationships.remove(friendRequest); await Promise.all([ - UserModel.updateOne({ id: friend.id }, { "user_data.relationships": newFriendRelationships }).exec(), + user.save(), emitEvent({ event: "RELATIONSHIP_REMOVE", data: friendRequest, @@ -58,12 +53,12 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument } await Promise.all([ - UserModel.updateOne({ id: req.user_id }, { "user_data.relationships": newUserRelationships }).exec(), + user.save(), emitEvent({ event: "RELATIONSHIP_ADD", data: { - ...toObject(relationship), - user: { ...toObject(friend), user_data: undefined } + ...relationship, + user: { ...friend } }, user_id: req.user_id } as RelationshipAddEvent) @@ -72,41 +67,40 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument return res.sendStatus(204); } - var incoming_relationship = { id: req.user_id, nickname: undefined, type: RelationshipType.incoming }; - var outgoing_relationship = { id, nickname: undefined, type: RelationshipType.outgoing }; + var incoming_relationship = new Relationship({ nickname: undefined, type: RelationshipType.incoming, id: req.user_id }); + var outgoing_relationship = new Relationship({ nickname: undefined, type: RelationshipType.outgoing, id }); if (friendRequest) { if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you"); // accept friend request - // @ts-ignore incoming_relationship = friendRequest; incoming_relationship.type = RelationshipType.friends; outgoing_relationship.type = RelationshipType.friends; - } else newFriendRelationships.push(incoming_relationship); + } else friend.relationships.push(incoming_relationship); 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"); - } else newUserRelationships.push(outgoing_relationship); + } else user.relationships.push(outgoing_relationship); await Promise.all([ - UserModel.updateOne({ id: req.user_id }, { "user_data.relationships": newUserRelationships }).exec(), - UserModel.updateOne({ id: friend.id }, { "user_data.relationships": newFriendRelationships }).exec(), + user.save(), + friend.save(), emitEvent({ event: "RELATIONSHIP_ADD", data: { ...outgoing_relationship, - user: { ...toObject(friend), user_data: undefined } + user: { ...friend } }, user_id: req.user_id } as RelationshipAddEvent), emitEvent({ event: "RELATIONSHIP_ADD", data: { - ...toObject(incoming_relationship), + ...incoming_relationship, should_notify: true, - user: { ...toObject(user), user_data: undefined } + user: { ...user } }, user_id: id } as RelationshipAddEvent) @@ -116,14 +110,23 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument } router.put("/:id", check({ $type: new Length(Number, 1, 4) }), async (req: Request, res: Response) => { - return await addRelationship(req, res, await UserModel.findOne({ id: req.params.id }), req.body.type); + return await updateRelationship( + req, + res, + await User.findOneOrFail({ id: req.params.id }, { relations: ["relationships"], select: userProjection }), + req.body.type + ); }); router.post("/", check({ discriminator: String, username: String }), async (req: Request, res: Response) => { - return await addRelationship( + return await updateRelationship( req, res, - await UserModel.findOne(req.body as { discriminator: string; username: string }).exec(), + await User.findOneOrFail({ + relations: ["relationships"], + select: userProjection, + where: req.body as { discriminator: string; username: string } + }), req.body.type ); }); @@ -132,17 +135,15 @@ router.delete("/:id", async (req: Request, res: Response) => { const { id } = req.params; if (id === req.user_id) throw new HTTPError("You can't remove yourself as a friend"); - const user = await UserModel.findOne({ id: req.user_id }).exec(); - if (!user) throw new HTTPError("Invalid token", 400); + const user = await User.findOneOrFail({ id: req.user_id }, { select: userProjection, relations: ["relationships"] }); + const friend = await User.findOneOrFail({ id: id }, { select: userProjection, relations: ["relationships"] }); - const friend = await UserModel.findOne({ id }, userProjection).exec(); - if (!friend) throw new HTTPError("User not found", 404); + const relationship = user.relationships.find((x) => x.id === id); + const friendRequest = friend.relationships.find((x) => x.id === req.user_id); - const relationship = user.user_data.relationships.find((x) => x.id === id); - const friendRequest = friend.user_data.relationships.find((x) => x.id === req.user_id); if (relationship?.type === RelationshipType.blocked) { // unblock user - user.user_data.relationships.remove(relationship); + user.relationships.remove(relationship); await Promise.all([ user.save(), @@ -153,8 +154,8 @@ router.delete("/:id", async (req: Request, res: Response) => { if (!relationship || !friendRequest) throw new HTTPError("You are not friends with the user", 404); if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you"); - user.user_data.relationships.remove(relationship); - friend.user_data.relationships.remove(friendRequest); + user.relationships.remove(relationship); + friend.relationships.remove(friendRequest); await Promise.all([ user.save(), |