mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-26 15:29:21 +00:00
Can determine post access level
This commit is contained in:
parent
cab3954edc
commit
a3bb9685ea
@ -8,7 +8,7 @@ use crate::api_data::survey_api::SurveyAPI;
|
||||
use crate::data::error::ResultBoxError;
|
||||
use crate::data::post::{Post, PostKind};
|
||||
use crate::data::user::UserID;
|
||||
use crate::helpers::{likes_helper, movies_helper, survey_helper};
|
||||
use crate::helpers::{likes_helper, movies_helper, survey_helper, posts_helper};
|
||||
use crate::helpers::likes_helper::LikeType;
|
||||
use crate::utils::user_data_utils::user_data_url;
|
||||
|
||||
@ -22,6 +22,7 @@ pub struct PostAPI {
|
||||
post_time: u64,
|
||||
content: Option<String>,
|
||||
visibility_level: String,
|
||||
user_access: String,
|
||||
kind: String,
|
||||
|
||||
// File specific
|
||||
@ -62,6 +63,7 @@ impl PostAPI {
|
||||
post_time: p.time_create,
|
||||
content: p.content.clone(),
|
||||
visibility_level: p.visibility.to_api(),
|
||||
user_access: posts_helper::get_access_level(&p, user)?.to_api(),
|
||||
kind: p.kind.to_api(),
|
||||
|
||||
// File specific
|
||||
|
@ -5,6 +5,7 @@
|
||||
use crate::data::group_id::GroupID;
|
||||
use crate::data::user::UserID;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum PostVisibilityLevel {
|
||||
//Posts that can be seen by anyone
|
||||
@ -31,6 +32,33 @@ impl PostVisibilityLevel {
|
||||
}
|
||||
}
|
||||
|
||||
/// Post access level (for a given user)
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum PostAccessLevel {
|
||||
//When a user can't access to a post
|
||||
NO_ACCESS = 0,
|
||||
|
||||
//When a user can see a post and perform basic actions such as liking
|
||||
BASIC_ACCESS = 1,
|
||||
|
||||
//When a user has intermediate access to the post (delete post)
|
||||
INTERMEDIATE_ACCESS = 2,
|
||||
|
||||
//When a user has a full access to the post
|
||||
FULL_ACCESS = 3,
|
||||
}
|
||||
|
||||
impl PostAccessLevel {
|
||||
pub fn to_api(&self) -> String {
|
||||
match self {
|
||||
PostAccessLevel::NO_ACCESS => "no-access",
|
||||
PostAccessLevel::BASIC_ACCESS => "basic",
|
||||
PostAccessLevel::INTERMEDIATE_ACCESS => "intermediate",
|
||||
PostAccessLevel::FULL_ACCESS => "full",
|
||||
}.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum PostPageKind {
|
||||
PAGE_KIND_USER(UserID),
|
||||
|
@ -26,6 +26,14 @@ impl UserID {
|
||||
pub fn is_valid(&self) -> bool {
|
||||
self.0 > 0
|
||||
}
|
||||
|
||||
/// Turn this `UserID` object as an instance. An invalid user id will become `None` variant
|
||||
pub fn as_option(&self) -> Option<UserID> {
|
||||
match self.is_valid() {
|
||||
true => Some(self.clone()),
|
||||
false => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
@ -249,6 +249,11 @@ pub fn get_visibility(group_id: &GroupID) -> ResultBoxError<GroupVisibilityLevel
|
||||
Ok(GroupVisibilityLevel::from_db(result))
|
||||
}
|
||||
|
||||
/// Check out whether a group is open or not
|
||||
pub fn is_open(group_id: &GroupID) -> ResultBoxError<bool> {
|
||||
Ok(get_visibility(group_id)? == GroupVisibilityLevel::OPEN_GROUP)
|
||||
}
|
||||
|
||||
/// Get the current access level of a user over a group
|
||||
pub fn get_access_level(group_id: &GroupID, user_id: Option<UserID>) -> ResultBoxError<GroupAccessLevel> {
|
||||
let membership_level = get_membership_level(group_id, user_id)?;
|
||||
|
@ -4,10 +4,11 @@
|
||||
|
||||
use crate::constants::database_tables_names::POSTS_TABLE;
|
||||
use crate::data::error::{ExecError, ResultBoxError};
|
||||
use crate::data::post::{Post, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink};
|
||||
use crate::data::group_member::GroupMembershipLevel;
|
||||
use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink};
|
||||
use crate::data::post::PostKind::{POST_KIND_COUNTDOWN, POST_KIND_IMAGE, POST_KIND_MOVIE, POST_KIND_PDF, POST_KIND_SURVEY, POST_KIND_WEBLINK, POST_KIND_YOUTUBE};
|
||||
use crate::data::user::UserID;
|
||||
use crate::helpers::{database, friends_helper};
|
||||
use crate::helpers::{database, friends_helper, groups_helper, user_helper};
|
||||
use crate::utils::date_utils::time;
|
||||
|
||||
impl PostVisibilityLevel {
|
||||
@ -122,6 +123,60 @@ fn get_user(query: &PostsQuery, target_id: &UserID) -> ResultBoxError<Vec<Post>>
|
||||
.exec(db_to_post)
|
||||
}
|
||||
|
||||
/// Get the access level of a user over a post
|
||||
pub fn get_access_level(p: &Post, user_id: &Option<UserID>) -> ResultBoxError<PostAccessLevel> {
|
||||
if user_id == &p.user_id.as_option() {
|
||||
return Ok(PostAccessLevel::FULL_ACCESS);
|
||||
}
|
||||
|
||||
match &p.target_page {
|
||||
// User page
|
||||
PostPageKind::PAGE_KIND_USER(user_page_id) => {
|
||||
if &user_page_id.as_option() == user_id {
|
||||
return Ok(PostAccessLevel::INTERMEDIATE_ACCESS);
|
||||
}
|
||||
|
||||
return match p.visibility {
|
||||
PostVisibilityLevel::VISIBILITY_PUBLIC => {
|
||||
if user_helper::can_see_user_page(user_id.as_ref().unwrap_or(&UserID::invalid()), user_page_id)? {
|
||||
Ok(PostAccessLevel::BASIC_ACCESS)
|
||||
} else {
|
||||
Ok(PostAccessLevel::NO_ACCESS)
|
||||
}
|
||||
}
|
||||
|
||||
PostVisibilityLevel::VISIBILITY_FRIENDS => {
|
||||
if user_id.is_some() && friends_helper::are_friend(user_id.as_ref().unwrap_or(&UserID::invalid()), user_page_id)? {
|
||||
Ok(PostAccessLevel::BASIC_ACCESS)
|
||||
} else {
|
||||
Ok(PostAccessLevel::NO_ACCESS)
|
||||
}
|
||||
}
|
||||
|
||||
// No access to posts with restricted visibility
|
||||
PostVisibilityLevel::VISIBILITY_USER => Ok(PostAccessLevel::NO_ACCESS),
|
||||
_ => Ok(PostAccessLevel::NO_ACCESS),
|
||||
};
|
||||
}
|
||||
|
||||
// Group page
|
||||
PostPageKind::PAGE_KIND_GROUP(group_id) => {
|
||||
let access_level = groups_helper::get_membership_level(group_id, user_id.clone())?;
|
||||
|
||||
// Moderators & administrators
|
||||
if access_level < GroupMembershipLevel::MEMBER {
|
||||
Ok(PostAccessLevel::INTERMEDIATE_ACCESS)
|
||||
} else if access_level == GroupMembershipLevel::MEMBER {
|
||||
Ok(PostAccessLevel::BASIC_ACCESS)
|
||||
} else if p.visibility != PostVisibilityLevel::VISIBILITY_PUBLIC || !groups_helper::is_open(group_id)? {
|
||||
Ok(PostAccessLevel::NO_ACCESS)
|
||||
} else {
|
||||
Ok(PostAccessLevel::BASIC_ACCESS)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Turn a post into a database entry
|
||||
fn db_to_post(res: &database::RowResult) -> ResultBoxError<Post> {
|
||||
let user_id = if res.get_u64("ID_amis")? == 0 {
|
||||
|
Loading…
Reference in New Issue
Block a user