From 90d81c1d7a9993280bf449cd2714b7b3bb1abb4a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 Jan 2020 16:49:11 +0100 Subject: [PATCH] Add access level for group pages --- src/helpers/GroupsHelper.ts | 9 +++++++++ src/helpers/PostsHelper.ts | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index 797ee1a..143a9d5 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -158,6 +158,15 @@ export class GroupsHelper { return result.visibility; } + /** + * Convenience function to check whether a group is open or not + * + * @param groupID Target group ID + */ + public static async IsOpen(groupID: number) : Promise { + return (await this.GetVisibility(groupID)) == GroupVisibilityLevel.OPEN_GROUP; + } + /** * Invite a user to join a group * diff --git a/src/helpers/PostsHelper.ts b/src/helpers/PostsHelper.ts index 28db429..e52f22d 100644 --- a/src/helpers/PostsHelper.ts +++ b/src/helpers/PostsHelper.ts @@ -2,6 +2,8 @@ import { PostKind, PostVisibilityLevel, Post, PostPageKind, PostFile, PostLink, import { FriendsHelper } from "./FriendsHelper"; import { DatabaseHelper } from "./DatabaseHelper"; import { UserHelper } from "./UserHelper"; +import { GroupsHelper } from "./GroupsHelper"; +import { GroupMembershipLevels } from "../entities/GroupMember"; /** * Posts helper @@ -155,7 +157,25 @@ export class PostsHelper { // Group page else if(post.kindPage == PostPageKind.PAGE_KIND_GROUP) { + // TODO : check this code + + const accessLevel = await GroupsHelper.GetMembershipLevel(userID, post.groupID); + // Moderator & admin = intermediate access + if(accessLevel < GroupMembershipLevels.MEMBER) + return PostAccessLevel.INTERMEDIATE_ACCESS; + + // Members can see all the posts of a group + if(accessLevel == GroupMembershipLevels.MEMBER) + return PostAccessLevel.BASIC_ACCESS; + + // Now we check if the post is public & the group is open + if(post.visibilityLevel != PostVisibilityLevel.VISIBILITY_PUBLIC + || !await GroupsHelper.IsOpen(post.groupID)) + return PostAccessLevel.NO_ACCESS; + + // Public post + open group = basic access + return PostAccessLevel.BASIC_ACCESS; } throw Error("GetAccessLevel reached an unimplemented status!");