From 2152511843e98e29f73a62456285fd5f428e122b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 31 Dec 2019 10:08:04 +0100 Subject: [PATCH] Get information about a single friendship --- src/controllers/FriendsController.ts | 16 ++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/FriendsHelper.ts | 38 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts index 502d6a0..e1789a2 100644 --- a/src/controllers/FriendsController.ts +++ b/src/controllers/FriendsController.ts @@ -44,6 +44,22 @@ export class FriendsController { h.send(friends.map((f) => f.friendID)); } + /** + * Get single friendship information + * + * @param h Request handler + */ + public static async GetSingleFrienshipInfo(h: RequestHandler) { + const friendID = await h.postUserId("friendID"); + + const info = await FriendsHelper.GetSingleInfo(h.getUserId(), friendID); + + if(info == null) + h.error(404, "Requested frienship not found!"); + + h.send(this.FriendToAPI(info, true)); + } + /** * Turn a friend object into an API entry * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 9a06ed6..ca8f791 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -79,6 +79,8 @@ export const Routes : Route[] = [ {path: "/friends/get_user_list", cb: (h) => FriendsController.GetOtherUserList(h), needLogin: false}, + {path: "/friends/get_single_infos", cb: (h) => FriendsController.GetSingleFrienshipInfo(h)}, + // Conversations controller {path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)}, diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index 3e2cd0f..b29eff7 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -42,6 +42,44 @@ export class FriendsHelper { return results.map((r) => this.DbToFriend(r)); } + /** + * Get information about a single membership + * + * @param userID Target user ID (ID of the user making the request) + * @param friendID Target friend ID + * @returns Information about the membership / null if no membership was found + */ + public static async GetSingleInfo(userID: number, friendID: number) : Promise { + + // Same request has above, but just for a single user + const result = await DatabaseHelper.QueryRow({ + table: FRIENDS_TABLE + " f", + where: { + ID_personne: userID, + ID_amis: friendID + }, + joins: [ + { + table: USER_TABLE + " u", + condition: "f.ID_amis = u.ID" + } + ], + fields: [ + "u.last_activity", + "f.ID_amis", + "f.actif", + "f.abonnement", + "f.autoriser_post_page" + ], + order: "u.last_activity DESC" + }); + + if(result == null) + return null; + + return this.DbToFriend(result); + } + /** * Count the number of friendship requests a user * received