mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Get the list of private conversations between two users
This commit is contained in:
		@@ -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")?;
 | 
			
		||||
 
 | 
			
		||||
@@ -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());
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user