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

Get the list of private conversations between two users

This commit is contained in:
Pierre HUBERT 2020-06-18 09:26:25 +02:00
parent 2f571787a3
commit ab10bc2d36
3 changed files with 39 additions and 0 deletions

View File

@ -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")
}

View File

@ -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<Vec<u64>> {
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<Conversation> {
let conv_id = row.get_u64("id")?;

View File

@ -68,6 +68,9 @@ pub struct QueryInfo {
/// Query limits
pub conditions: collections::HashMap<String, String>,
/// Custom WHERE condition
pub custom_where: Option<String>,
/// 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<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(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());