mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-03 17:14:03 +00:00 
			
		
		
		
	Can get the status of a friendship
This commit is contained in:
		
							
								
								
									
										27
									
								
								src/api_data/friendship_status_api.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/api_data/friendship_status_api.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					//! # Friendship status API
 | 
				
			||||||
 | 
					//!
 | 
				
			||||||
 | 
					//! @author Pierre Hubert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use serde::Serialize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::data::friendship_status::FriendshipStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Serialize)]
 | 
				
			||||||
 | 
					pub struct FriendshipStatusAPI {
 | 
				
			||||||
 | 
					    are_friend: bool,
 | 
				
			||||||
 | 
					    sent_request: bool,
 | 
				
			||||||
 | 
					    received_request: bool,
 | 
				
			||||||
 | 
					    following: bool,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl FriendshipStatusAPI {
 | 
				
			||||||
 | 
					    /// Create a new instance
 | 
				
			||||||
 | 
					    pub fn new(status: &FriendshipStatus) -> FriendshipStatusAPI {
 | 
				
			||||||
 | 
					        FriendshipStatusAPI {
 | 
				
			||||||
 | 
					            are_friend: status.are_friend,
 | 
				
			||||||
 | 
					            sent_request: status.sent_request,
 | 
				
			||||||
 | 
					            received_request: status.received_request,
 | 
				
			||||||
 | 
					            following: status.following,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -28,4 +28,5 @@ pub mod group_api;
 | 
				
			|||||||
pub mod advanced_group_api;
 | 
					pub mod advanced_group_api;
 | 
				
			||||||
pub mod res_change_group_logo;
 | 
					pub mod res_change_group_logo;
 | 
				
			||||||
pub mod group_member_api;
 | 
					pub mod group_member_api;
 | 
				
			||||||
pub mod friend_api;
 | 
					pub mod friend_api;
 | 
				
			||||||
 | 
					pub mod friendship_status_api;
 | 
				
			||||||
@@ -3,6 +3,7 @@
 | 
				
			|||||||
//! @author Pierre Hubert
 | 
					//! @author Pierre Hubert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::api_data::friend_api::FriendAPI;
 | 
					use crate::api_data::friend_api::FriendAPI;
 | 
				
			||||||
 | 
					use crate::api_data::friendship_status_api::FriendshipStatusAPI;
 | 
				
			||||||
use crate::controllers::routes::RequestResult;
 | 
					use crate::controllers::routes::RequestResult;
 | 
				
			||||||
use crate::data::http_request_handler::HttpRequestHandler;
 | 
					use crate::data::http_request_handler::HttpRequestHandler;
 | 
				
			||||||
use crate::helpers::{account_helper, friends_helper, user_helper};
 | 
					use crate::helpers::{account_helper, friends_helper, user_helper};
 | 
				
			||||||
@@ -49,4 +50,14 @@ pub fn get_other_user_list(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			|||||||
    let friends = friends_helper::GetFriendsQuery::new(&user_id).exec()?;
 | 
					    let friends = friends_helper::GetFriendsQuery::new(&user_id).exec()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    r.set_response(friends.iter().map(|f| f.friend_id.id()).collect::<Vec<u64>>())
 | 
					    r.set_response(friends.iter().map(|f| f.friend_id.id()).collect::<Vec<u64>>())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Get the status of a friendship
 | 
				
			||||||
 | 
					pub fn get_status(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			||||||
 | 
					    let friend_id = r.post_user_id("friendID")?;
 | 
				
			||||||
 | 
					    let curr_user_id = r.user_id()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let status = friends_helper::get_status(&curr_user_id, &friend_id)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    r.set_response(FriendshipStatusAPI::new(&status))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -95,6 +95,8 @@ pub fn get_routes() -> Vec<Route> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        Route::post("/friends/get_user_list", Box::new(friends_controller::get_other_user_list)),
 | 
					        Route::post("/friends/get_user_list", Box::new(friends_controller::get_other_user_list)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Route::post("/friends/getStatus", Box::new(friends_controller::get_status)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Conversations controller
 | 
					        // Conversations controller
 | 
				
			||||||
        Route::post("/conversations/create", Box::new(conversations_controller::create)),
 | 
					        Route::post("/conversations/create", Box::new(conversations_controller::create)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								src/data/friendship_status.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/data/friendship_status.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					//! # Friendship status
 | 
				
			||||||
 | 
					//!
 | 
				
			||||||
 | 
					//! @author Pierre Hubert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub struct FriendshipStatus {
 | 
				
			||||||
 | 
					    pub are_friend: bool,
 | 
				
			||||||
 | 
					    pub sent_request: bool,
 | 
				
			||||||
 | 
					    pub received_request: bool,
 | 
				
			||||||
 | 
					    pub following: bool,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -17,4 +17,5 @@ pub mod group;
 | 
				
			|||||||
pub mod new_group;
 | 
					pub mod new_group;
 | 
				
			||||||
pub mod group_member;
 | 
					pub mod group_member;
 | 
				
			||||||
pub mod global_search_result;
 | 
					pub mod global_search_result;
 | 
				
			||||||
pub mod friend;
 | 
					pub mod friend;
 | 
				
			||||||
 | 
					pub mod friendship_status;
 | 
				
			||||||
@@ -3,9 +3,11 @@
 | 
				
			|||||||
//! @author Pierre Hubert
 | 
					//! @author Pierre Hubert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::constants::database_tables_names::{FRIENDS_TABLE, USERS_TABLE};
 | 
					use crate::constants::database_tables_names::{FRIENDS_TABLE, USERS_TABLE};
 | 
				
			||||||
use crate::data::error::{ExecError, ResultBoxError};
 | 
					use crate::data::error::{ExecError, ResultBoxError};
 | 
				
			||||||
use crate::data::friend::Friend;
 | 
					use crate::data::friend::Friend;
 | 
				
			||||||
 | 
					use crate::data::friendship_status::FriendshipStatus;
 | 
				
			||||||
use crate::data::user::UserID;
 | 
					use crate::data::user::UserID;
 | 
				
			||||||
use crate::helpers::database;
 | 
					use crate::helpers::database;
 | 
				
			||||||
use crate::helpers::database::QueryInfo;
 | 
					use crate::helpers::database::QueryInfo;
 | 
				
			||||||
@@ -59,6 +61,16 @@ fn get_list(friend_query: &GetFriendsQuery) -> ResultBoxError<Vec<Friend>> {
 | 
				
			|||||||
    query.exec(db_to_friend)
 | 
					    query.exec(db_to_friend)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Check out wheterher a user has sent a request to another user
 | 
				
			||||||
 | 
					pub fn sent_request(user_id: &UserID, target_user: &UserID) -> ResultBoxError<bool> {
 | 
				
			||||||
 | 
					    database::QueryInfo::new(FRIENDS_TABLE)
 | 
				
			||||||
 | 
					        .cond_user_id("ID_personne", target_user)
 | 
				
			||||||
 | 
					        .cond_user_id("ID_amis", user_id)
 | 
				
			||||||
 | 
					        .cond_legacy_bool("actif", false)
 | 
				
			||||||
 | 
					        .exec_count()
 | 
				
			||||||
 | 
					        .map(|f| f > 0)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Check out whether two users are friend or not
 | 
					/// Check out whether two users are friend or not
 | 
				
			||||||
pub fn are_friend(user_one: &UserID, user_two: &UserID) -> ResultBoxError<bool> {
 | 
					pub fn are_friend(user_one: &UserID, user_two: &UserID) -> ResultBoxError<bool> {
 | 
				
			||||||
    Ok(database::count(QueryInfo::new(FRIENDS_TABLE)
 | 
					    Ok(database::count(QueryInfo::new(FRIENDS_TABLE)
 | 
				
			||||||
@@ -85,6 +97,42 @@ pub fn can_post_texts(user_id: &UserID, target_user: &UserID) -> ResultBoxError<
 | 
				
			|||||||
        .or(Ok(false))
 | 
					        .or(Ok(false))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Check out whether a user is following another user or not
 | 
				
			||||||
 | 
					pub fn is_following(user_id: &UserID, friend_id: &UserID) -> ResultBoxError<bool> {
 | 
				
			||||||
 | 
					    database::QueryInfo::new(FRIENDS_TABLE)
 | 
				
			||||||
 | 
					        .cond_user_id("ID_personne", user_id)
 | 
				
			||||||
 | 
					        .cond_user_id("ID_amis", friend_id)
 | 
				
			||||||
 | 
					        .cond_legacy_bool("actif", true)
 | 
				
			||||||
 | 
					        .cond_legacy_bool("abonnement", true)
 | 
				
			||||||
 | 
					        .exec_count()
 | 
				
			||||||
 | 
					        .map(|f| f > 0)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Get the status of a friendship
 | 
				
			||||||
 | 
					pub fn get_status(user_id: &UserID, friend_id: &UserID) -> ResultBoxError<FriendshipStatus> {
 | 
				
			||||||
 | 
					    let mut status = FriendshipStatus {
 | 
				
			||||||
 | 
					        are_friend: false,
 | 
				
			||||||
 | 
					        sent_request: false,
 | 
				
			||||||
 | 
					        received_request: false,
 | 
				
			||||||
 | 
					        following: false,
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    status.are_friend = are_friend(user_id, friend_id)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    match status.are_friend {
 | 
				
			||||||
 | 
					        false => {
 | 
				
			||||||
 | 
					            status.sent_request = sent_request(user_id, friend_id)?;
 | 
				
			||||||
 | 
					            status.received_request = sent_request(friend_id, user_id)?;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        true => {
 | 
				
			||||||
 | 
					            status.following = is_following(user_id, friend_id)?;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Ok(status)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Turn a database entry into a Friend structure
 | 
					/// Turn a database entry into a Friend structure
 | 
				
			||||||
fn db_to_friend(row: &database::RowResult) -> ResultBoxError<Friend> {
 | 
					fn db_to_friend(row: &database::RowResult) -> ResultBoxError<Friend> {
 | 
				
			||||||
    Ok(Friend {
 | 
					    Ok(Friend {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user