Skip to content

Commit 6ceac71

Browse files
committed
factor out update_chats_contacts_timestamps
1 parent 1811b6e commit 6ceac71

File tree

1 file changed

+72
-62
lines changed

1 file changed

+72
-62
lines changed

src/receive_imf.rs

Lines changed: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,76 @@ async fn create_group(
21502150
}
21512151
}
21522152

2153+
async fn update_chats_contacts_timestamps(
2154+
context: &Context,
2155+
chat_id: ChatId,
2156+
from_id: ContactId,
2157+
to_ids: &[ContactId],
2158+
past_ids: &[ContactId],
2159+
chat_group_member_timestamps: &[i64],
2160+
) -> Result<bool> {
2161+
let expected_timestamps_count = to_ids.len() + past_ids.len();
2162+
2163+
if chat_group_member_timestamps.len() != expected_timestamps_count {
2164+
warn!(
2165+
context,
2166+
"Chat-Group-Member-Timestamps has wrong number of timestamps, got {}, expected {}.",
2167+
chat_group_member_timestamps.len(),
2168+
expected_timestamps_count
2169+
);
2170+
return Ok(false);
2171+
}
2172+
2173+
let mut modified = false;
2174+
2175+
context
2176+
.sql
2177+
.transaction(|transaction| {
2178+
let mut add_statement = transaction.prepare(
2179+
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2180+
VALUES (?1, ?2, ?3)
2181+
ON CONFLICT (chat_id, contact_id)
2182+
DO
2183+
UPDATE SET add_timestamp=?3
2184+
WHERE ?3>add_timestamp AND ?3>=remove_timestamp",
2185+
)?;
2186+
2187+
for (contact_id, ts) in iter::zip(
2188+
to_ids.iter(),
2189+
chat_group_member_timestamps.iter().take(to_ids.len()),
2190+
) {
2191+
if *contact_id != from_id {
2192+
// It could be that member was already added,
2193+
// but updated addition timestamp
2194+
// is also a modification worth notifying about.
2195+
modified |= add_statement.execute((chat_id, contact_id, ts))? > 0;
2196+
}
2197+
}
2198+
2199+
let mut remove_statement = transaction.prepare(
2200+
"UPDATE chats_contacts
2201+
SET remove_timestamp=?1
2202+
WHERE chat_id=?2 AND contact_id=?3
2203+
AND ?1>remove_timestamp AND ?1>add_timestamp",
2204+
)?;
2205+
2206+
for (contact_id, ts) in iter::zip(
2207+
past_ids.iter(),
2208+
chat_group_member_timestamps.iter().skip(to_ids.len()),
2209+
) {
2210+
// It could be that member was already removed,
2211+
// but updated removal timestamp
2212+
// is also a modification worth notifying about.
2213+
modified |= remove_statement.execute((ts, chat_id, contact_id))? > 0;
2214+
}
2215+
2216+
Ok(())
2217+
})
2218+
.await?;
2219+
2220+
Ok(modified)
2221+
}
2222+
21532223
/// Apply group member list, name, avatar and protection status changes from the MIME message.
21542224
///
21552225
/// Returns `Vec` of group changes messages and, optionally, a better message to replace the
@@ -2286,69 +2356,9 @@ async fn apply_group_changes(
22862356
let mut removed_ids = HashSet::<ContactId>::new();
22872357

22882358
if let Some(ref chat_group_member_timestamps) = mime_parser.chat_group_member_timestamps() {
2289-
let expected_timestamps_count = to_ids.len() + past_ids.len();
2290-
if chat_group_member_timestamps.len() == expected_timestamps_count {
2291-
context
2292-
.sql
2293-
.transaction(|transaction| {
2294-
let mut add_statement = transaction.prepare(
2295-
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2296-
VALUES (?1, ?2, ?3)
2297-
ON CONFLICT (chat_id, contact_id)
2298-
DO
2299-
UPDATE SET add_timestamp=?3
2300-
WHERE ?3>add_timestamp AND ?3>=remove_timestamp",
2301-
)?;
2302-
2303-
for (contact_id, ts) in iter::zip(
2304-
to_ids.iter(),
2305-
chat_group_member_timestamps.iter().take(to_ids.len()),
2306-
) {
2307-
if *contact_id != from_id {
2308-
let modified = add_statement.execute((chat_id, contact_id, ts))? > 0;
2309-
2310-
if modified {
2311-
// It could be that member was already added,
2312-
// but updated addition timestamp
2313-
// is also a modification worth notifying about.
2314-
send_event_chat_modified = true;
2315-
}
2316-
}
2317-
}
2318-
2319-
let mut remove_statement = transaction.prepare(
2320-
"UPDATE chats_contacts
2321-
SET remove_timestamp=?1
2322-
WHERE chat_id=?2 AND contact_id=?3
2323-
AND ?1>remove_timestamp AND ?1>add_timestamp",
2324-
)?;
2325-
2326-
for (contact_id, ts) in iter::zip(
2327-
past_ids.iter(),
2328-
chat_group_member_timestamps.iter().skip(to_ids.len()),
2329-
) {
2330-
let modified = remove_statement.execute((ts, chat_id, contact_id))? > 0;
2331-
2332-
if modified {
2333-
// It could be that member was already removed,
2334-
// but updated removal timestamp
2335-
// is also a modification worth notifying about.
2336-
send_event_chat_modified = true;
2337-
}
2338-
}
2339-
2340-
Ok(())
2341-
})
2359+
send_event_chat_modified |=
2360+
update_chats_contacts_timestamps(context, chat_id, from_id, to_ids, past_ids, &chat_group_member_timestamps)
23422361
.await?;
2343-
} else {
2344-
warn!(
2345-
context,
2346-
"Chat-Group-Member-Timestamps has wrong number of timestamps, got {}, expected {}.",
2347-
chat_group_member_timestamps.len(),
2348-
expected_timestamps_count
2349-
);
2350-
}
2351-
23522362
let new_chat_contacts = HashSet::<ContactId>::from_iter(
23532363
chat::get_chat_contacts(context, chat_id)
23542364
.await?

0 commit comments

Comments
 (0)