1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 00:25:17 +00:00

Start to determine post access level

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

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
*