From d1ea274281317cf57937156a36bfa939c27bd0c5 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 28 Dec 2019 14:09:20 +0100 Subject: [PATCH] Return the number of friends of the user --- src/controllers/UserController.ts | 2 ++ src/helpers/FriendsHelper.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 532fe0c..4a15713 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -4,6 +4,7 @@ import { User, UserPageStatus } from "../entities/User"; import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage"; import { BackgroundImageHelper } from "../helpers/BackgroundImageHelper"; import { LikesHelper, LikesType } from "../helpers/LikesHelper"; +import { FriendsHelper } from "../helpers/FriendsHelper"; /** * User information controller @@ -87,6 +88,7 @@ export class UserController { info["account_creation_time"] = user.timeCreate; info["backgroundImage"] = BackgroundImageHelper.GetURL(user.id); info["pageLikes"] = await LikesHelper.Count(user.id, LikesType.USER); + info["number_friends"] = user.friendsListPublic ? await FriendsHelper.CountForUser(user.id) : 0 } return info; diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index 4a3bcf2..616625f 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -26,6 +26,21 @@ export class FriendsHelper { }); } + /** + * Count the number of friends of a specific user + * + * @param userID Target user ID + */ + public static async CountForUser(userID: number) : Promise { + return await DatabaseHelper.Count({ + table: FRIENDS_TABLE, + where: { + ID_amis: userID, + actif: 1 + } + }); + } + /** * Check out whether two users are friend or not *