mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-09-19 03:18:46 +00:00
Validate that a user can see another user's page
This commit is contained in:
@@ -2,6 +2,8 @@ use crate::data::error::ResultBoxError;
|
||||
use crate::data::user::{User, UserID, UserPageStatus, AccountImageVisibility};
|
||||
use crate::helpers::database;
|
||||
use crate::constants::database_tables_names::USERS_TABLE;
|
||||
use crate::data::user::UserPageStatus::PUBLIC;
|
||||
use crate::helpers::friends_helper::are_friend;
|
||||
|
||||
/// User helper
|
||||
///
|
||||
@@ -59,4 +61,35 @@ pub fn exists(id: UserID) -> ResultBoxError<bool> {
|
||||
Ok(database::QueryInfo::new(USERS_TABLE)
|
||||
.cond_i64("ID", id)
|
||||
.exec_count()? > 0)
|
||||
}
|
||||
|
||||
/// Check if a given user can see another user's page
|
||||
pub fn can_see_user_page(user_id: UserID, target_user: UserID) -> ResultBoxError<bool> {
|
||||
if user_id == target_user {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
let visibility = find_user_by_id(target_user)?.status;
|
||||
|
||||
// Open page = OK
|
||||
if visibility == UserPageStatus::OPEN {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
// The user need to be signed in
|
||||
if user_id <= 0 {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// Public Page = OK for signed in users
|
||||
if visibility == PUBLIC {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
// Check if the users are friends
|
||||
if !are_friend(user_id, target_user)? {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
return Ok(true);
|
||||
}
|
Reference in New Issue
Block a user