mirror of
https://github.com/chatmail/core.git
synced 2026-04-05 23:22:11 +03:00
Compare commits
3 Commits
1.0.0-beta
...
cleanup_se
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c359bcd88 | ||
|
|
87c8c5a363 | ||
|
|
b32f3f71ad |
@@ -1,6 +1,8 @@
|
||||
use std::ptr;
|
||||
|
||||
use itertools::join;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use mmime::mailimf::*;
|
||||
use mmime::mailimf_types::*;
|
||||
use mmime::mailmime::*;
|
||||
@@ -1676,61 +1678,60 @@ fn hex_hash(s: impl AsRef<str>) -> String {
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
unsafe fn search_chat_ids_by_contact_ids(
|
||||
fn search_chat_ids_by_contact_ids(
|
||||
context: &Context,
|
||||
unsorted_contact_ids: &Vec<u32>,
|
||||
) -> Vec<u32> {
|
||||
/* 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 match_contact_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 */
|
||||
if !unsorted_contact_ids.is_empty() {
|
||||
for &curr_id in unsorted_contact_ids {
|
||||
if curr_id != 1 && !contact_ids.contains(&curr_id) {
|
||||
contact_ids.push(curr_id);
|
||||
}
|
||||
}
|
||||
if !contact_ids.is_empty() {
|
||||
contact_ids.sort();
|
||||
let contact_ids_str = join(contact_ids.iter().map(|x| x.to_string()), ",");
|
||||
context.sql.query_map(
|
||||
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;",
|
||||
contact_ids_str
|
||||
),
|
||||
params![],
|
||||
|row| Ok((row.get::<_, u32>(0)?, row.get::<_, u32>(1)?)),
|
||||
|rows| {
|
||||
let mut last_chat_id = 0;
|
||||
let mut matches = 0;
|
||||
let mut mismatches = 0;
|
||||
|
||||
for row in rows {
|
||||
let (chat_id, contact_id) = row?;
|
||||
if chat_id as u32 != last_chat_id {
|
||||
if matches == contact_ids.len() && mismatches == 0 {
|
||||
chat_ids.push(last_chat_id);
|
||||
}
|
||||
last_chat_id = chat_id as u32;
|
||||
matches = 0;
|
||||
mismatches = 0;
|
||||
}
|
||||
if contact_id == contact_ids[matches] {
|
||||
matches += 1;
|
||||
} else {
|
||||
mismatches += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if matches == contact_ids.len() && mismatches == 0 {
|
||||
chat_ids.push(last_chat_id);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
).unwrap(); // TODO: better error handling
|
||||
for &curr_id in unsorted_contact_ids {
|
||||
if curr_id != DC_CONTACT_ID_SELF && !match_contact_ids.contains(&curr_id) {
|
||||
match_contact_ids.push(curr_id);
|
||||
}
|
||||
}
|
||||
if match_contact_ids.is_empty() {
|
||||
return chat_ids;
|
||||
}
|
||||
|
||||
/* collect all possible chats which contain at least one contact */
|
||||
|
||||
context.sql.query_map(
|
||||
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;",
|
||||
join(match_contact_ids.iter().map(|x| x.to_string()), ",")
|
||||
),
|
||||
params![],
|
||||
|row| Ok((row.get::<_, u32>(0)?, row.get::<_, u32>(1)?)),
|
||||
|rows| {
|
||||
// per-chat count all matching contacts and add chat-id
|
||||
// if all matching contacts are seen
|
||||
let mut chat2matches = HashMap::new();
|
||||
for row in rows {
|
||||
let (chat_id, contact_id) = row?;
|
||||
if match_contact_ids.contains(&contact_id) {
|
||||
let count = if let Some(x) = chat2matches.get_mut(&chat_id) {
|
||||
*x = *x + 1;
|
||||
*x
|
||||
} else {
|
||||
chat2matches.insert(chat_id, 1);
|
||||
1
|
||||
};
|
||||
if count == match_contact_ids.len() {
|
||||
chat_ids.push(chat_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
).unwrap(); // TODO: better error handling
|
||||
|
||||
chat_ids
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user