From 92723b487a2708209ecf429ba4afa96dfbb20315 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 28 Dec 2019 14:29:00 +0100 Subject: [PATCH] Completed advanced info --- src/controllers/UserController.ts | 10 ++++--- src/helpers/FriendsHelper.ts | 23 ++++++++++++++++ src/helpers/UserHelper.ts | 44 +++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 4a15713..0b3a371 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -68,7 +68,7 @@ export class UserController { } - private static async UserToAPI(user : User, handler: RequestHandler, advanced: boolean = false) : Promise { + private static async UserToAPI(user : User, h: RequestHandler, advanced: boolean = false) : Promise { const info = { "userID": user.id, "firstName": user.firstName, @@ -76,7 +76,7 @@ export class UserController { "publicPage": user.pageStatus == UserPageStatus.PUBLIC, "openPage": user.pageStatus == UserPageStatus.OPEN, "virtualDirectory": user.virtualDirectory, - "accountImage": this.GetAccountImageURL(user.accountImage, handler) + "accountImage": this.GetAccountImageURL(user.accountImage, h) }; if(advanced) { @@ -87,8 +87,12 @@ export class UserController { info["allowPostFromFriendOnHisPage"] = user.allowPostsFromFriends; 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 + + info["pageLikes"] = await LikesHelper.Count(user.id, LikesType.USER); + info["user_like_page"] = h.signedIn ? await LikesHelper.IsLiking(h.getUserId(), user.id, LikesType.USER) : false; + + info["can_post_texts"] = await UserHelper.CanCreatePosts(h.getUserId(), user.id); } return info; diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index 616625f..ccbf866 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -57,4 +57,27 @@ export class FriendsHelper { } }) > 0; } + + /** + * Check out whether friendship allows to create posts or not + * + * @param userID User wishing to create a post + * @param targetID Target user ID + */ + public static async CanPostTexts(userID: number, targetID: number) : Promise { + const result = await DatabaseHelper.QueryRow({ + table: FRIENDS_TABLE, + where: { + ID_personne: targetID, + ID_amis: userID, + actif: 1 + }, + fields: ["autoriser_post_page"] + }); + + if(result == null) + return false; + + return result["autoriser_post_page"] == 1; + } } \ No newline at end of file diff --git a/src/helpers/UserHelper.ts b/src/helpers/UserHelper.ts index 6e4ba16..d36e71d 100644 --- a/src/helpers/UserHelper.ts +++ b/src/helpers/UserHelper.ts @@ -125,6 +125,50 @@ export class UserHelper { return (await this.GetUserInfo(userID)).pageStatus; } + /** + * Check out whether a user allows posts from his + * friends on his page or not + * + * This is a convenience method + * + * @param userID Target user ID + */ + private static async AllowPostsOnHisPage(userID: number) : Promise { + return (await this.GetUserInfo(userID)).allowPostsFromFriends; + } + + /** + * Check out whether a user can create a post on another user + * page + * + * @param userID The user who wish to create a post + * @param targetID The target page + */ + public static async CanCreatePosts(userID: number, targetID: number) { + + // User MUST be signed in + if(userID < 1) + return false; + + // A user can always create posts on his page + if(userID == targetID) return true; + + // User must be able to see the page + if(!await this.CanSeeUserPage(userID, targetID)) + return false; + + //Check if the user allow posts on his page + if(!await this.AllowPostsOnHisPage(targetID)) + return false; + + //Check if the friendship of the user allows him to create posts + if(!await FriendsHelper.CanPostTexts(userID, targetID)) + return false; + + // yes by default + return true; + } + private static async DbToUser(row: any) : Promise { return new User({