1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Can make global search

This commit is contained in:
Pierre HUBERT 2020-06-23 18:55:23 +02:00
parent c4eec8150c
commit 2cf2146f17
11 changed files with 132 additions and 7 deletions

1
Cargo.lock generated
View File

@ -547,6 +547,7 @@ dependencies = [
"futures",
"image",
"kamadak-exif",
"lazy_static",
"mailchecker",
"mysql",
"percent-encoding",

View File

@ -24,3 +24,4 @@ chrono = "0.4.11"
bytes = "0.5.4"
image = "0.23.5"
kamadak-exif = "0.5.1"
lazy_static = "1.4.0"

View 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()
}
}

View File

@ -22,3 +22,4 @@ pub mod conversation_message_api;
pub mod conversations_refresh_api;
pub mod res_count_unread_conversations;
pub mod list_unread_conversations_api;
pub mod global_search_result_api;

View File

@ -2,9 +2,11 @@
//!
//! @author Pierre Hubert
use crate::api_data::global_search_result_api::GlobalSearchResultAPI;
use crate::controllers::routes::RequestResult;
use crate::data::global_search_result::GlobalSearchResult;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::user_helper;
use crate::helpers::{groups_helper, user_helper};
/// Search for user
pub fn search_user(r: &mut HttpRequestHandler) -> RequestResult {
@ -18,5 +20,19 @@ pub fn search_user(r: &mut HttpRequestHandler) -> RequestResult {
/// Perform a global search
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))
}

View 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
View 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
View 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
}
}

View File

@ -12,3 +12,6 @@ pub mod conversation;
pub mod conversation_message;
pub mod new_conversation_message;
pub mod unread_conversation;
pub mod group_id;
pub mod group;
pub mod global_search_result;

View File

@ -11,6 +11,7 @@ use mysql::prelude::Queryable;
use crate::data::config::DatabaseConfig;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::user::UserID;
use crate::data::group_id::GroupID;
/// Database access helper
///
@ -271,6 +272,11 @@ impl<'a> RowResult<'a> {
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
pub fn get_str(&self, name: &str) -> Result<String, Box<dyn Error>> {
let value = self.row.get_opt(self.find_col(name)?);

View File

@ -2,9 +2,21 @@
//!
//! @author Pierre Hubert
use crate::data::error::ResultBoxError;
use crate::helpers::database;
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
pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError<u64> {
@ -13,3 +25,14 @@ pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError<u64> {
.add_field("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"))
}