@@ -2065,39 +2065,39 @@ async fn create_group(
2065
2065
context
2066
2066
. sql
2067
2067
. transaction ( |transaction| {
2068
+ let mut add_statement = transaction. prepare (
2069
+ "INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2070
+ VALUES (?1, ?2, ?3)
2071
+ ON CONFLICT (chat_id, contact_id)
2072
+ DO UPDATE SET add_timestamp=MAX(add_timestamp, ?3)" ,
2073
+ ) ?;
2074
+
2068
2075
for ( contact_id, ts) in iter:: zip (
2069
2076
to_ids. iter ( ) ,
2070
2077
chat_group_member_timestamps. iter ( ) . take ( to_ids. len ( ) ) ,
2071
2078
) {
2072
- transaction. execute (
2073
- "INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2074
- VALUES (?1, ?2, ?3)
2075
- ON CONFLICT (chat_id, contact_id)
2076
- DO UPDATE SET add_timestamp=MAX(add_timestamp, ?3)" ,
2077
- ( chat_id, contact_id, ts) ,
2078
- ) ?;
2079
+ add_statement. execute ( ( chat_id, contact_id, ts) ) ?;
2079
2080
}
2080
2081
2082
+ let mut remove_statement = transaction. prepare (
2083
+ "UPDATE chats_contacts
2084
+ SET remove_timestamp=MAX(remove_timestamp, ?)
2085
+ WHERE chat_id=? AND contact_id=?" ,
2086
+ ) ?;
2087
+
2081
2088
for ( contact_id, ts) in iter:: zip (
2082
2089
past_ids. iter ( ) ,
2083
2090
chat_group_member_timestamps. iter ( ) . skip ( to_ids. len ( ) ) ,
2084
2091
) {
2085
- transaction. execute (
2086
- "UPDATE chats_contacts
2087
- SET remove_timestamp=MAX(remove_timestamp, ?)
2088
- WHERE chat_id=? AND contact_id=?" ,
2089
- ( ts, chat_id, contact_id) ,
2090
- ) ?;
2092
+ remove_statement. execute ( ( ts, chat_id, contact_id) ) ?;
2091
2093
}
2092
2094
2093
2095
if !to_ids. contains ( & from_id) {
2094
- transaction. execute (
2095
- "INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2096
- VALUES (?1, ?2, ?3)
2097
- ON CONFLICT (chat_id, contact_id)
2098
- DO UPDATE SET add_timestamp=MAX(add_timestamp, ?3)" ,
2099
- ( chat_id, from_id, mime_parser. timestamp_sent ) ,
2100
- ) ?;
2096
+ add_statement. execute ( (
2097
+ chat_id,
2098
+ from_id,
2099
+ mime_parser. timestamp_sent ,
2100
+ ) ) ?;
2101
2101
}
2102
2102
2103
2103
Ok ( ( ) )
@@ -2291,20 +2291,21 @@ async fn apply_group_changes(
2291
2291
context
2292
2292
. sql
2293
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
+
2294
2303
for ( contact_id, ts) in iter:: zip (
2295
2304
to_ids. iter ( ) ,
2296
2305
chat_group_member_timestamps. iter ( ) . take ( to_ids. len ( ) ) ,
2297
2306
) {
2298
2307
if * contact_id != from_id {
2299
- let modified = transaction. execute (
2300
- "INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
2301
- VALUES (?1, ?2, ?3)
2302
- ON CONFLICT (chat_id, contact_id)
2303
- DO
2304
- UPDATE SET add_timestamp=?3
2305
- WHERE ?3>add_timestamp AND ?3>=remove_timestamp" ,
2306
- ( chat_id, contact_id, ts) ,
2307
- ) ? > 0 ;
2308
+ let modified = add_statement. execute ( ( chat_id, contact_id, ts) ) ? > 0 ;
2308
2309
2309
2310
if modified {
2310
2311
// It could be that member was already added,
@@ -2315,17 +2316,18 @@ async fn apply_group_changes(
2315
2316
}
2316
2317
}
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
+
2318
2326
for ( contact_id, ts) in iter:: zip (
2319
2327
past_ids. iter ( ) ,
2320
2328
chat_group_member_timestamps. iter ( ) . skip ( to_ids. len ( ) ) ,
2321
2329
) {
2322
- let modified = transaction. execute (
2323
- "UPDATE chats_contacts
2324
- SET remove_timestamp=?1
2325
- WHERE chat_id=?2 AND contact_id=?3
2326
- AND ?1>remove_timestamp AND ?1>add_timestamp" ,
2327
- ( ts, chat_id, contact_id) ,
2328
- ) ? > 0 ;
2330
+ let modified = remove_statement. execute ( ( ts, chat_id, contact_id) ) ? > 0 ;
2329
2331
2330
2332
if modified {
2331
2333
// It could be that member was already removed,
0 commit comments