1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19:22 +00:00

Start to determine post access level

This commit is contained in:
Pierre HUBERT 2020-01-03 16:38:44 +01:00
parent 8bcbcfb2b3
commit 015cf1e0d7
2 changed files with 71 additions and 2 deletions

View File

@ -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;

View File

@ -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<PostAccessLevel> {
// 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
*