mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 16:35:17 +00:00
Can determine post access level
This commit is contained in:
@ -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 {
|
||||
|
Reference in New Issue
Block a user