From d8bc8022c04dbedcb99d6e8910204283e4eba5da Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 1 Jan 2020 11:52:34 +0100 Subject: [PATCH] Can cancel a frienship request --- src/controllers/FriendsController.ts | 18 ++++++++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/FriendsHelper.ts | 17 +++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts index 88687cf..d4bc7a0 100644 --- a/src/controllers/FriendsController.ts +++ b/src/controllers/FriendsController.ts @@ -115,6 +115,24 @@ export class FriendsController { h.success("Send (create) the friendship request"); } + /** + * Cancel (remove) a friendship request + * + * @param h Request handler + */ + public static async CancelRequest(h: RequestHandler) { + const friendID = await h.postUserId("friendID"); + + if(!await FriendsHelper.SentRequest(h.getUserId(), friendID)) + h.error(401, "No friendship request was sent to this user!"); + + await FriendsHelper.RemoveRequest(h.getUserId(), friendID); + + // TODO : delete any related notification + + h.success("Friendship request removed!"); + } + /** * Turn a friend object into an API entry * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index c5c92bb..040f4b4 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -85,6 +85,8 @@ export const Routes : Route[] = [ {path: "/friends/sendRequest", cb: (h) => FriendsController.SendRequest(h)}, + {path: "/friends/removeRequest", cb: (h) => FriendsController.CancelRequest(h)}, + // Conversations controller {path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)}, diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index 1e10b05..dfcdad5 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -114,6 +114,23 @@ export class FriendsHelper { }) > 0; } + /** + * Delete a previously sent friendship request + * + * @param userID The ID of the user who have sent the request + * @param targetUser The ID of the target user + */ + public static async RemoveRequest(userID: number, targetUser: number) { + await DatabaseHelper.DeleteRows( + FRIENDS_TABLE, + { + ID_personne: targetUser, + ID_amis: userID, + actif: 0 + } + ); + } + /** * Count the number of friendship requests a user * received