cleanup search_chat_ids_by_contact_ids

This commit is contained in:
holger krekel
2019-09-06 01:29:24 +02:00
parent 0a6e540394
commit b32f3f71ad

View File

@@ -1676,24 +1676,33 @@ fn hex_hash(s: impl AsRef<str>) -> String {
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
unsafe fn search_chat_ids_by_contact_ids( fn search_chat_ids_by_contact_ids(
context: &Context, context: &Context,
unsorted_contact_ids: &Vec<u32>, unsorted_contact_ids: &Vec<u32>,
) -> Vec<u32> { ) -> Vec<u32> {
/* searches chat_id's by the given contact IDs, may return zero, one or more chat_id's */ /* searches chat_id's by the given contact IDs, may return zero, one or more chat_id's */
let mut contact_ids = Vec::with_capacity(23); let mut contact_ids = Vec::with_capacity(23);
let mut chat_ids = Vec::with_capacity(23); let mut chat_ids = Vec::with_capacity(23);
if unsorted_contact_ids.is_empty() {
return chat_ids;
}
/* copy array, remove duplicates and SELF, sort by ID */ /* copy array, remove duplicates and SELF, sort by ID */
if !unsorted_contact_ids.is_empty() {
for &curr_id in unsorted_contact_ids { for &curr_id in unsorted_contact_ids {
if curr_id != 1 && !contact_ids.contains(&curr_id) { if curr_id != DC_CONTACT_ID_SELF && !contact_ids.contains(&curr_id) {
contact_ids.push(curr_id); contact_ids.push(curr_id);
} }
} }
if !contact_ids.is_empty() { if contact_ids.is_empty() {
return chat_ids;
}
/* collect all possible chats with the contact count as the data (as contact_ids have no doubles, this is sufficient) */
contact_ids.sort(); contact_ids.sort();
let contact_ids_str = join(contact_ids.iter().map(|x| x.to_string()), ","); let contact_ids_str = join(contact_ids.iter().map(|x| x.to_string()), ",");
context.sql.query_map( context.sql.query_map(
format!( format!(
"SELECT DISTINCT cc.chat_id, cc.contact_id FROM chats_contacts cc LEFT JOIN chats c ON c.id=cc.chat_id WHERE cc.chat_id IN(SELECT chat_id FROM chats_contacts WHERE contact_id IN({})) AND c.type=120 AND cc.contact_id!=1 ORDER BY cc.chat_id, cc.contact_id;", "SELECT DISTINCT cc.chat_id, cc.contact_id FROM chats_contacts cc LEFT JOIN chats c ON c.id=cc.chat_id WHERE cc.chat_id IN(SELECT chat_id FROM chats_contacts WHERE contact_id IN({})) AND c.type=120 AND cc.contact_id!=1 ORDER BY cc.chat_id, cc.contact_id;",
@@ -1708,11 +1717,11 @@ unsafe fn search_chat_ids_by_contact_ids(
for row in rows { for row in rows {
let (chat_id, contact_id) = row?; let (chat_id, contact_id) = row?;
if chat_id as u32 != last_chat_id { if chat_id != last_chat_id {
if matches == contact_ids.len() && mismatches == 0 { if matches == contact_ids.len() && mismatches == 0 {
chat_ids.push(last_chat_id); chat_ids.push(last_chat_id);
} }
last_chat_id = chat_id as u32; last_chat_id = chat_id;
matches = 0; matches = 0;
mismatches = 0; mismatches = 0;
} }
@@ -1729,8 +1738,6 @@ unsafe fn search_chat_ids_by_contact_ids(
Ok(()) Ok(())
} }
).unwrap(); // TODO: better error handling ).unwrap(); // TODO: better error handling
}
}
chat_ids chat_ids
} }