mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Can make global search
This commit is contained in:
		
							
								
								
									
										1
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							@@ -547,6 +547,7 @@ dependencies = [
 | 
				
			|||||||
 "futures",
 | 
					 "futures",
 | 
				
			||||||
 "image",
 | 
					 "image",
 | 
				
			||||||
 "kamadak-exif",
 | 
					 "kamadak-exif",
 | 
				
			||||||
 | 
					 "lazy_static",
 | 
				
			||||||
 "mailchecker",
 | 
					 "mailchecker",
 | 
				
			||||||
 "mysql",
 | 
					 "mysql",
 | 
				
			||||||
 "percent-encoding",
 | 
					 "percent-encoding",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,4 +23,5 @@ rand = "0.7.3"
 | 
				
			|||||||
chrono = "0.4.11"
 | 
					chrono = "0.4.11"
 | 
				
			||||||
bytes = "0.5.4"
 | 
					bytes = "0.5.4"
 | 
				
			||||||
image = "0.23.5"
 | 
					image = "0.23.5"
 | 
				
			||||||
kamadak-exif = "0.5.1"
 | 
					kamadak-exif = "0.5.1"
 | 
				
			||||||
 | 
					lazy_static = "1.4.0"
 | 
				
			||||||
							
								
								
									
										36
									
								
								src/api_data/global_search_result_api.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/api_data/global_search_result_api.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
				
			|||||||
 | 
					//! # Global search result API
 | 
				
			||||||
 | 
					//!
 | 
				
			||||||
 | 
					//! @author Pierre
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use serde::Serialize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::data::global_search_result::GlobalSearchResult;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Serialize)]
 | 
				
			||||||
 | 
					pub struct GlobalSearchResultAPI {
 | 
				
			||||||
 | 
					    kind: String,
 | 
				
			||||||
 | 
					    id: u64,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl GlobalSearchResultAPI {
 | 
				
			||||||
 | 
					    /// Construct new entry
 | 
				
			||||||
 | 
					    pub fn new(res: &GlobalSearchResult) -> GlobalSearchResultAPI {
 | 
				
			||||||
 | 
					        match res {
 | 
				
			||||||
 | 
					            GlobalSearchResult::User(user_id) => GlobalSearchResultAPI {
 | 
				
			||||||
 | 
					                kind: "user".to_string(),
 | 
				
			||||||
 | 
					                id: user_id.clone() as u64,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            GlobalSearchResult::Group(group_id) => GlobalSearchResultAPI {
 | 
				
			||||||
 | 
					                kind: "group".to_string(),
 | 
				
			||||||
 | 
					                id: group_id.id(),
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Construct a list of results
 | 
				
			||||||
 | 
					    pub fn for_list(l: &Vec<GlobalSearchResult>) -> Vec<GlobalSearchResultAPI> {
 | 
				
			||||||
 | 
					        l.iter()
 | 
				
			||||||
 | 
					            .map(|f| Self::new(f))
 | 
				
			||||||
 | 
					            .collect()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -21,4 +21,5 @@ pub mod res_find_private_conversations;
 | 
				
			|||||||
pub mod conversation_message_api;
 | 
					pub mod conversation_message_api;
 | 
				
			||||||
pub mod conversations_refresh_api;
 | 
					pub mod conversations_refresh_api;
 | 
				
			||||||
pub mod res_count_unread_conversations;
 | 
					pub mod res_count_unread_conversations;
 | 
				
			||||||
pub mod list_unread_conversations_api;
 | 
					pub mod list_unread_conversations_api;
 | 
				
			||||||
 | 
					pub mod global_search_result_api;
 | 
				
			||||||
@@ -2,9 +2,11 @@
 | 
				
			|||||||
//!
 | 
					//!
 | 
				
			||||||
//! @author Pierre Hubert
 | 
					//! @author Pierre Hubert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::api_data::global_search_result_api::GlobalSearchResultAPI;
 | 
				
			||||||
use crate::controllers::routes::RequestResult;
 | 
					use crate::controllers::routes::RequestResult;
 | 
				
			||||||
 | 
					use crate::data::global_search_result::GlobalSearchResult;
 | 
				
			||||||
use crate::data::http_request_handler::HttpRequestHandler;
 | 
					use crate::data::http_request_handler::HttpRequestHandler;
 | 
				
			||||||
use crate::helpers::user_helper;
 | 
					use crate::helpers::{groups_helper, user_helper};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Search for user
 | 
					/// Search for user
 | 
				
			||||||
pub fn search_user(r: &mut HttpRequestHandler) -> RequestResult {
 | 
					pub fn search_user(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			||||||
@@ -18,5 +20,19 @@ pub fn search_user(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/// Perform a global search
 | 
					/// Perform a global search
 | 
				
			||||||
pub fn search_global(r: &mut HttpRequestHandler) -> RequestResult {
 | 
					pub fn search_global(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			||||||
    r.success("implement me")
 | 
					    let query = r.post_string("query")?;
 | 
				
			||||||
 | 
					    let limit = 10;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut list = user_helper::search_user(&query, limit)?
 | 
				
			||||||
 | 
					        .iter()
 | 
				
			||||||
 | 
					        .map(|f| GlobalSearchResult::User(f.clone()))
 | 
				
			||||||
 | 
					        .collect::<Vec<GlobalSearchResult>>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    list.append(&mut groups_helper::search_group(&query, limit)?
 | 
				
			||||||
 | 
					        .iter()
 | 
				
			||||||
 | 
					        .map(|f| GlobalSearchResult::Group(f.clone()))
 | 
				
			||||||
 | 
					        .collect::<Vec<GlobalSearchResult>>());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    r.set_response(GlobalSearchResultAPI::for_list(&list))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										9
									
								
								src/data/global_search_result.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/data/global_search_result.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					//! # Global search result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::data::group_id::GroupID;
 | 
				
			||||||
 | 
					use crate::data::user::UserID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub enum GlobalSearchResult {
 | 
				
			||||||
 | 
					    User(UserID),
 | 
				
			||||||
 | 
					    Group(GroupID),
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										11
									
								
								src/data/group.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/data/group.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					//! # Group information
 | 
				
			||||||
 | 
					//!
 | 
				
			||||||
 | 
					//! Group visibility level
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[allow(non_camel_case_types)]
 | 
				
			||||||
 | 
					#[derive(Eq, PartialEq, Hash)]
 | 
				
			||||||
 | 
					pub enum GroupVisibilityLevel {
 | 
				
			||||||
 | 
					    OPEN_GROUP,
 | 
				
			||||||
 | 
					    PRIVATE_GROUP,
 | 
				
			||||||
 | 
					    SECRETE_GROUP,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										18
									
								
								src/data/group_id.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/data/group_id.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					//! # Group ID
 | 
				
			||||||
 | 
					//!
 | 
				
			||||||
 | 
					//! @author Pierre Hubert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Clone, PartialEq, Eq)]
 | 
				
			||||||
 | 
					pub struct GroupID(u64);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl GroupID {
 | 
				
			||||||
 | 
					    /// Initialize a new group ID object
 | 
				
			||||||
 | 
					    pub fn new(id: u64) -> GroupID {
 | 
				
			||||||
 | 
					        GroupID(id)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Get the ID current stored in this structure
 | 
				
			||||||
 | 
					    pub fn id(&self) -> u64 {
 | 
				
			||||||
 | 
					        self.0
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -11,4 +11,7 @@ pub mod new_conversation;
 | 
				
			|||||||
pub mod conversation;
 | 
					pub mod conversation;
 | 
				
			||||||
pub mod conversation_message;
 | 
					pub mod conversation_message;
 | 
				
			||||||
pub mod new_conversation_message;
 | 
					pub mod new_conversation_message;
 | 
				
			||||||
pub mod unread_conversation;
 | 
					pub mod unread_conversation;
 | 
				
			||||||
 | 
					pub mod group_id;
 | 
				
			||||||
 | 
					pub mod group;
 | 
				
			||||||
 | 
					pub mod global_search_result;
 | 
				
			||||||
@@ -11,6 +11,7 @@ use mysql::prelude::Queryable;
 | 
				
			|||||||
use crate::data::config::DatabaseConfig;
 | 
					use crate::data::config::DatabaseConfig;
 | 
				
			||||||
use crate::data::error::{ExecError, ResultBoxError};
 | 
					use crate::data::error::{ExecError, ResultBoxError};
 | 
				
			||||||
use crate::data::user::UserID;
 | 
					use crate::data::user::UserID;
 | 
				
			||||||
 | 
					use crate::data::group_id::GroupID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Database access helper
 | 
					/// Database access helper
 | 
				
			||||||
///
 | 
					///
 | 
				
			||||||
@@ -271,6 +272,11 @@ impl<'a> RowResult<'a> {
 | 
				
			|||||||
        self.get_int64(name)
 | 
					        self.get_int64(name)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Get the ID of a group included in the response
 | 
				
			||||||
 | 
					    pub fn get_group_id(&self, name: &str) -> ResultBoxError<GroupID> {
 | 
				
			||||||
 | 
					        Ok(GroupID::new(self.get_u64(name)?))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Find a string included in the request
 | 
					    /// Find a string included in the request
 | 
				
			||||||
    pub fn get_str(&self, name: &str) -> Result<String, Box<dyn Error>> {
 | 
					    pub fn get_str(&self, name: &str) -> Result<String, Box<dyn Error>> {
 | 
				
			||||||
        let value = self.row.get_opt(self.find_col(name)?);
 | 
					        let value = self.row.get_opt(self.find_col(name)?);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,9 +2,21 @@
 | 
				
			|||||||
//!
 | 
					//!
 | 
				
			||||||
//! @author Pierre Hubert
 | 
					//! @author Pierre Hubert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::data::error::ResultBoxError;
 | 
					 | 
				
			||||||
use crate::helpers::database;
 | 
					 | 
				
			||||||
use crate::constants::database_tables_names::GROUPS_LIST_TABLE;
 | 
					use crate::constants::database_tables_names::GROUPS_LIST_TABLE;
 | 
				
			||||||
 | 
					use crate::data::error::ResultBoxError;
 | 
				
			||||||
 | 
					use crate::data::group::GroupVisibilityLevel;
 | 
				
			||||||
 | 
					use crate::data::group_id::GroupID;
 | 
				
			||||||
 | 
					use crate::helpers::database;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl GroupVisibilityLevel {
 | 
				
			||||||
 | 
					    pub fn to_db(&self) -> u64 {
 | 
				
			||||||
 | 
					        match self {
 | 
				
			||||||
 | 
					            GroupVisibilityLevel::OPEN_GROUP => 0,
 | 
				
			||||||
 | 
					            GroupVisibilityLevel::PRIVATE_GROUP => 1,
 | 
				
			||||||
 | 
					            GroupVisibilityLevel::SECRETE_GROUP => 2,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Find a group id by virtual directory
 | 
					/// Find a group id by virtual directory
 | 
				
			||||||
pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError<u64> {
 | 
					pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError<u64> {
 | 
				
			||||||
@@ -12,4 +24,15 @@ pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError<u64> {
 | 
				
			|||||||
        .cond("virtual_directory", dir)
 | 
					        .cond("virtual_directory", dir)
 | 
				
			||||||
        .add_field("id")
 | 
					        .add_field("id")
 | 
				
			||||||
        .query_row(|res| res.get_u64("id"))
 | 
					        .query_row(|res| res.get_u64("id"))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Search for group
 | 
				
			||||||
 | 
					pub fn search_group(query: &str, limit: u64) -> ResultBoxError<Vec<GroupID>> {
 | 
				
			||||||
 | 
					    database::QueryInfo::new(GROUPS_LIST_TABLE)
 | 
				
			||||||
 | 
					        .set_custom_where("name LIKE ? AND visibility != ?")
 | 
				
			||||||
 | 
					        .add_custom_where_argument_str(format!("%{}%", query).as_str())
 | 
				
			||||||
 | 
					        .add_custom_where_argument_u64(GroupVisibilityLevel::SECRETE_GROUP.to_db())
 | 
				
			||||||
 | 
					        .set_limit(limit)
 | 
				
			||||||
 | 
					        .add_field("id")
 | 
				
			||||||
 | 
					        .exec(|row| row.get_group_id("id"))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user