1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 00:45:18 +00:00

Check if a user is allow to create posts on another user's page

This commit is contained in:
2020-06-01 16:57:14 +02:00
parent 637408c626
commit f9a8cd4e82
4 changed files with 65 additions and 4 deletions

View File

@ -10,6 +10,7 @@ use crate::data::config::DatabaseConfig;
use crate::data::error::{ExecError, ResultBoxError};
use std::collections::HashMap;
use chrono::{Utc, TimeZone};
use crate::data::user::UserID;
/// Database access helper
///
@ -97,6 +98,11 @@ impl QueryInfo {
self
}
pub fn cond_user_id(mut self, key: &str, val: UserID) -> QueryInfo {
self.conditions.insert(key.to_string(), val.to_string());
self
}
/// Append a field to the list of selected fields
pub fn add_field(mut self, key: &str) -> QueryInfo {
self.fields.push(key.to_string());
@ -109,6 +115,12 @@ impl QueryInfo {
query(self, process_function)
}
/// Query just a row
pub fn query_row<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(self, process_function: F)
-> Result<E, Box<dyn Error>> {
query_row(self, process_function)
}
/// Execute count query
pub fn exec_count(self) -> ResultBoxError<usize> {
count(self)

View File

@ -23,4 +23,14 @@ pub fn count_friends(user_id: UserID) -> ResultBoxError<usize> {
.cond_i64("ID_amis", user_id)
.cond_u32("actif", 1)
.exec_count()
}
/// Check if a user can create posts on another friend's page
pub fn can_post_texts(user_id: UserID, target_user: UserID) -> ResultBoxError<bool> {
QueryInfo::new(FRIENDS_TABLE)
.cond_user_id("ID_personne", target_user)
.cond_user_id("ID_amis", user_id)
.add_field("autoriser_post_page")
.query_row(|res| res.get_legacy_bool("autoriser_post_page"))
.or(Ok(false))
}

View File

@ -1,6 +1,6 @@
use crate::data::error::ResultBoxError;
use crate::data::user::{User, UserID, UserPageStatus, AccountImageVisibility};
use crate::helpers::database;
use crate::helpers::{database, friends_helper};
use crate::constants::database_tables_names::USERS_TABLE;
use crate::data::user::UserPageStatus::PUBLIC;
use crate::helpers::friends_helper::are_friend;
@ -98,4 +98,40 @@ pub fn can_see_user_page(user_id: UserID, target_user: UserID) -> ResultBoxError
}
return Ok(true);
}
/// Check out whether a user allow posts on his page or not
pub fn allow_posts_on_his_page(user_id: UserID) -> ResultBoxError<bool> {
Ok(find_user_by_id(user_id)?.allow_posts_from_friends)
}
/// Check out if a user can create posts on another user page
pub fn can_create_posts(user_id: UserID, target_id: UserID) -> ResultBoxError<bool> {
// Login required
if user_id <= 0 {
return Ok(false);
}
// A user can always create posts on his page
if user_id == target_id {
return Ok(true);
}
// User must be able to see the page
if !can_see_user_page(user_id, target_id)? {
return Ok(false);
}
// Check if user allow posts on his page
if !allow_posts_on_his_page(target_id)? {
return Ok(false);
}
// Check if the friendship of the user allows him to create posts
if !friends_helper::can_post_texts(user_id, target_id)? {
return Ok(false);
}
Ok(true)
}