From ca2e106852426c3a29c54aa932640d8d3f1b7505 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 31 Dec 2019 10:33:38 +0100 Subject: [PATCH] Can get a friendship status --- src/controllers/FriendsController.ts | 28 ++++++++++++++++++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/FriendsHelper.ts | 17 +++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts index e1789a2..ea801f3 100644 --- a/src/controllers/FriendsController.ts +++ b/src/controllers/FriendsController.ts @@ -60,6 +60,34 @@ export class FriendsController { h.send(this.FriendToAPI(info, true)); } + /** + * Get a friendship status + * + * @param h Request handler + */ + public static async GetStatus(h: RequestHandler) { + const friendID = await h.postUserId("friendID"); + + let response = { + are_friend: false, + sent_request: false, + received_request: false, + following: false + } + + response.are_friend = await FriendsHelper.AreFriend(h.getUserId(), friendID); + + if(!response.are_friend) { + response.sent_request = await FriendsHelper.SentRequest(h.getUserId(), friendID); + response.received_request = await FriendsHelper.SentRequest(friendID, h.getUserId()); + } + else + response.following = await FriendsHelper.IsFollowing(h.getUserId(), friendID); + + + h.send(response); + } + /** * Turn a friend object into an API entry * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index ca8f791..4d31b55 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -81,6 +81,8 @@ export const Routes : Route[] = [ {path: "/friends/get_single_infos", cb: (h) => FriendsController.GetSingleFrienshipInfo(h)}, + {path: "/friends/getStatus", cb: (h) => FriendsController.GetStatus(h)}, + // Conversations controller {path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)}, diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index 32a4ff5..be7138d 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -80,6 +80,23 @@ export class FriendsHelper { return this.DbToFriend(result); } + /** + * Check out whether a user sent a request to another friend + * + * @param userID The ID of the user supposed to have sent a request + * @param targetUser The ID of the target user + */ + public static async SentRequest(userID: number, targetUser: number) : Promise { + return await DatabaseHelper.Count({ + table: FRIENDS_TABLE, + where: { + ID_personne: targetUser, + ID_amis: userID, + actif: 0 + } + }) > 0; + } + /** * Count the number of friendship requests a user * received