From ab10bc2d3651ac1ba2856c735d352b3a44803160 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 18 Jun 2020 09:26:25 +0200 Subject: [PATCH] Get the list of private conversations between two users --- src/controllers/conversations_controller.rs | 8 ++++++++ src/helpers/conversations_helper.rs | 16 ++++++++++++++++ src/helpers/database.rs | 15 +++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 7060ba1..d33f52b 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -130,5 +130,13 @@ pub fn update_settings(r: &mut HttpRequestHandler) -> RequestResult { /// Find a private conversation pub fn find_private(r: &mut HttpRequestHandler) -> RequestResult { + let other_user = r.post_user_id("otherUser")?; + let allow_create = r.post_bool_opt("allowCreate", false); + + // Query the database + let list = conversations_helper::find_private(r.user_id()?, other_user)?; + + println!("{:#?}", list); + r.success("implement it") } \ No newline at end of file diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index ffcb1d1..f535c65 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -183,6 +183,22 @@ pub fn set_can_everyone_add_members(conv_id: u64, allow: bool) -> ResultBoxError .exec() } +/// Search for private conversation between two users +pub fn find_private(user_1: UserID, user_2: UserID) -> ResultBoxError> { + database::QueryInfo::new(CONV_USERS_TABLE) + .alias("t1") + + // Join + .join(CONV_USERS_TABLE, "t2", "t1.conv_id = t2.conv_id") + + // Conditions + .cond_user_id("t1.user_id", user_1) + .cond_user_id("t2.user_id", user_2) + .set_custom_where(format!("(SELECT COUNT(*) FROM {} WHERE conv_id = t1.conv_id) = 2", CONV_USERS_TABLE).as_ref()) + .add_field("t1.conv_id AS conv_id") + .exec(|f|f.get_u64("conv_id")) +} + /// Turn a database entry into a ConversationInfo object fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError { let conv_id = row.get_u64("id")?; diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 8fe3ed5..c67f896 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -68,6 +68,9 @@ pub struct QueryInfo { /// Query limits pub conditions: collections::HashMap, + /// Custom WHERE condition + pub custom_where: Option, + /// Limit of the query (0 = no limit) pub limit: u64, @@ -88,6 +91,7 @@ impl QueryInfo { table_alias: None, joins: Vec::new(), conditions: collections::HashMap::new(), + custom_where: None, limit: 0, order: None, fields: Vec::new(), @@ -134,6 +138,12 @@ impl QueryInfo { self } + /// Set custom where + pub fn set_custom_where(mut self, custom_where: &str) -> QueryInfo { + self.custom_where = Some(custom_where.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()); @@ -340,6 +350,11 @@ pub fn query ProcessRowResult>(info: QueryInfo, proce query = query.add(&where_args); } + // Custom WHERE clause + if let Some(custom_where) = info.custom_where { + query = query.add(format!(" AND ({})", custom_where).as_str()); + } + // ORDER clause if let Some(order) = info.order { query = query.add(format!(" ORDER BY {} ", order).as_str());