From 5a45096fa687634e622becdb259b91755735e415 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 Jan 2020 17:01:09 +0100 Subject: [PATCH] Ready to implement comments --- src/controllers/PostsController.ts | 3 ++- src/entities/Post.ts | 6 +++++- src/helpers/PostsHelper.ts | 9 +++++++++ src/helpers/UserHelper.ts | 10 ++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index 05bd7ba..b467c37 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -105,7 +105,8 @@ export class PostsController { // Determine user access level user_access: ACCESS_LEVELS_API[await PostsHelper.GetAccessLevel(h.optionnalUserID, p)], - // TODO : add comments + // Load comments (if possible) + comments: await PostsHelper.AllowCommentsOnPost(p) ? "to come" : null, }; return data; diff --git a/src/entities/Post.ts b/src/entities/Post.ts index 82d040c..451ecdb 100644 --- a/src/entities/Post.ts +++ b/src/entities/Post.ts @@ -133,8 +133,12 @@ export class Post implements PostBuilder { } } + get isUserPage() : boolean { + return this.kindPage == PostPageKind.PAGE_KIND_USER; + } + get userPageID() : number { - return this.kindPage == PostPageKind.PAGE_KIND_USER ? this.pageID : 0 + return this.isUserPage ? this.pageID : 0 } get groupID() : number { diff --git a/src/helpers/PostsHelper.ts b/src/helpers/PostsHelper.ts index e52f22d..5aa1ab8 100644 --- a/src/helpers/PostsHelper.ts +++ b/src/helpers/PostsHelper.ts @@ -181,6 +181,15 @@ export class PostsHelper { throw Error("GetAccessLevel reached an unimplemented status!"); } + /** + * Check out whether comments are allowed on a post or not + * + * @param post The post to check + */ + public static async AllowCommentsOnPost(post: Post) : Promise { + return !post.isUserPage || await UserHelper.AllowComments(post.userPageID); + } + /** * Turn a database entry into a row object * diff --git a/src/helpers/UserHelper.ts b/src/helpers/UserHelper.ts index 517a708..31f31bd 100644 --- a/src/helpers/UserHelper.ts +++ b/src/helpers/UserHelper.ts @@ -147,6 +147,16 @@ export class UserHelper { return (await this.GetUserInfo(userID)).friendsListPublic; } + /** + * Convenience function to check out whether comments are + * allowed on a page or not + * + * @param userID Target user ID + */ + public static async AllowComments(userID: number) : Promise { + return !(await this.GetUserInfo(userID)).blockComments; + } + /** * Check out whether a user can create a post on another user * page