From 015cf1e0d796cdbab64030b7e9d09ab989737116 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 Jan 2020 16:38:44 +0100 Subject: [PATCH] Start to determine post access level --- src/controllers/PostsController.ts | 15 +++++++- src/helpers/PostsHelper.ts | 58 +++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/controllers/PostsController.ts b/src/controllers/PostsController.ts index 2ee9c06..05bd7ba 100644 --- a/src/controllers/PostsController.ts +++ b/src/controllers/PostsController.ts @@ -1,7 +1,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { UserHelper } from "../helpers/UserHelper"; import { PostsHelper } from "../helpers/PostsHelper"; -import { Post, PostVisibilityLevel, PostKind } from "../entities/Post"; +import { Post, PostVisibilityLevel, PostKind, PostAccessLevel } from "../entities/Post"; import { MoviesController } from "./MoviesController"; import { MoviesHelper } from "../helpers/MoviesHelper"; import { SurveyHelper } from "../helpers/SurveyHelper"; @@ -20,6 +20,14 @@ VISIBILITY_LEVELS_API[PostVisibilityLevel.VISIBILITY_FRIENDS] = "friends"; VISIBILITY_LEVELS_API[PostVisibilityLevel.VISIBILITY_USER] = "private"; VISIBILITY_LEVELS_API[PostVisibilityLevel.VISIBILITY_GROUP_MEMBERS] = "members"; + +const ACCESS_LEVELS_API = {}; +ACCESS_LEVELS_API[PostAccessLevel.NO_ACCESS] = "no-access"; +ACCESS_LEVELS_API[PostAccessLevel.BASIC_ACCESS] = "basic"; +ACCESS_LEVELS_API[PostAccessLevel.INTERMEDIATE_ACCESS] = "intermediate"; +ACCESS_LEVELS_API[PostAccessLevel.FULL_ACCESS] = "full"; + + export class PostsController { /** @@ -93,6 +101,11 @@ export class PostsController { // Likes information likes: await LikesHelper.Count(p.id, LikesType.POST), userlike: h.signedIn ? await LikesHelper.IsLiking(h.getUserId(), p.id, LikesType.POST) : false, + + // Determine user access level + user_access: ACCESS_LEVELS_API[await PostsHelper.GetAccessLevel(h.optionnalUserID, p)], + + // TODO : add comments }; return data; diff --git a/src/helpers/PostsHelper.ts b/src/helpers/PostsHelper.ts index 148e2ca..28db429 100644 --- a/src/helpers/PostsHelper.ts +++ b/src/helpers/PostsHelper.ts @@ -1,6 +1,7 @@ -import { PostKind, PostVisibilityLevel, Post, PostPageKind, PostFile, PostLink } from "../entities/Post"; +import { PostKind, PostVisibilityLevel, Post, PostPageKind, PostFile, PostLink, PostAccessLevel } from "../entities/Post"; import { FriendsHelper } from "./FriendsHelper"; import { DatabaseHelper } from "./DatabaseHelper"; +import { UserHelper } from "./UserHelper"; /** * Posts helper @@ -105,6 +106,61 @@ export class PostsHelper { return entries.map((r) => this.DBToPost(r)); } + /** + * Get the access level of a user over a post + * + * @param userID Target user ID + * @param post Target post + */ + public static async GetAccessLevel(userID: number, post: Post) : Promise { + + // User is the owner of the post + if(userID == post.userID) + return PostAccessLevel.FULL_ACCESS; + + + // User page + if(post.kindPage == PostPageKind.PAGE_KIND_USER) { + + // Post made on user page + if(post.pageID == userID) + return PostAccessLevel.INTERMEDIATE_ACCESS; + + // Check if the post is private + if(post.visibilityLevel == PostVisibilityLevel.VISIBILITY_USER) + return PostAccessLevel.NO_ACCESS; + + // In case the post is only for friends + else if(post.visibilityLevel == PostVisibilityLevel.VISIBILITY_FRIENDS) { + + if(userID < 1 /* user not signed in */ + || !await FriendsHelper.AreFriend(userID, post.pageID) /* not a friend */) + return PostAccessLevel.NO_ACCESS; + + else + return PostAccessLevel.BASIC_ACCESS; + } + + // In case of public post + else if(post.visibilityLevel == PostVisibilityLevel.VISIBILITY_PUBLIC) { + + // Check if the user can see the page + if(await UserHelper.CanSeeUserPage(userID, post.userPageID)) + return PostAccessLevel.BASIC_ACCESS; + + // Else no access to the user + return PostAccessLevel.NO_ACCESS; + } + } + + // Group page + else if(post.kindPage == PostPageKind.PAGE_KIND_GROUP) { + + } + + throw Error("GetAccessLevel reached an unimplemented status!"); + } + /** * Turn a database entry into a row object *