@@ -39,6 +39,7 @@ use crate::mimeparser::SystemMessage;
39
39
use crate :: param:: { Param , Params } ;
40
40
use crate :: peerstate:: Peerstate ;
41
41
use crate :: receive_imf:: ReceivedMsg ;
42
+ use crate :: rusqlite:: OptionalExtension ;
42
43
use crate :: securejoin:: BobState ;
43
44
use crate :: smtp:: send_msg_to_smtp;
44
45
use crate :: sql;
@@ -3243,15 +3244,16 @@ pub async fn get_chat_msgs_ex(
3243
3244
/// Marks all messages in the chat as noticed.
3244
3245
/// If the given chat-id is the archive-link, marks all messages in all archived chats as noticed.
3245
3246
pub async fn marknoticed_chat ( context : & Context , chat_id : ChatId ) -> Result < ( ) > {
3246
- // "WHERE" below uses the index `(state, hidden, chat_id)`, see get_fresh_msg_cnt() for reasoning
3247
- // the additional SELECT statement may speed up things as no write-blocking is needed.
3247
+ // `WHERE` statements below use the index `(state, hidden, chat_id)`, that's why we enumerate
3248
+ // `hidden` values, see `get_fresh_msg_cnt()` for reasoning.
3249
+ // The additional `SELECT` statement may speed up things as no write-blocking is needed.
3248
3250
if chat_id. is_archived_link ( ) {
3249
3251
let chat_ids_in_archive = context
3250
3252
. sql
3251
3253
. query_map (
3252
3254
"SELECT DISTINCT(m.chat_id) FROM msgs m
3253
3255
LEFT JOIN chats c ON m.chat_id=c.id
3254
- WHERE m.state=10 AND m.hidden=0 AND m.chat_id>9 AND c.archived=1" ,
3256
+ WHERE m.state=10 AND m.hidden IN (0,1) AND m.chat_id>9 AND c.archived=1" ,
3255
3257
( ) ,
3256
3258
|row| row. get :: < _ , ChatId > ( 0 ) ,
3257
3259
|ids| ids. collect :: < Result < Vec < _ > , _ > > ( ) . map_err ( Into :: into) ,
@@ -3265,7 +3267,7 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
3265
3267
. sql
3266
3268
. transaction ( |transaction| {
3267
3269
let mut stmt = transaction. prepare (
3268
- "UPDATE msgs SET state=13 WHERE state=10 AND hidden=0 AND chat_id = ?" ,
3270
+ "UPDATE msgs SET state=13 WHERE state=10 AND hidden IN (0,1) AND chat_id= ?" ,
3269
3271
) ?;
3270
3272
for chat_id_in_archive in & chat_ids_in_archive {
3271
3273
stmt. execute ( ( chat_id_in_archive, ) ) ?;
@@ -3281,22 +3283,56 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
3281
3283
}
3282
3284
} else {
3283
3285
start_chat_ephemeral_timers ( context, chat_id) . await ?;
3284
-
3285
- if context
3286
- . sql
3287
- . execute (
3286
+ let conn_fn = |conn : & mut rusqlite:: Connection | {
3287
+ // This is to trigger emitting `MsgsNoticed` on other devices when reactions are noticed
3288
+ // locally. We filter out `InNoticed` messages because they are normally a result of
3289
+ // `mark_old_messages_as_noticed()` which happens on all devices anyway. Also we limit
3290
+ // this to one message because the effect is the same anyway.
3291
+ //
3292
+ // Even if `message::markseen_msgs()` fails then, in the worst case other devices won't
3293
+ // emit `MsgsNoticed` and app notifications won't be removed. The bigger problem is that
3294
+ // another device may have more reactions received and not yet seen notifications are
3295
+ // removed from it, but the same problem already exists for "usual" messages, so let's
3296
+ // not solve it for now.
3297
+ let mut stmt = conn. prepare (
3298
+ "SELECT id, state FROM msgs
3299
+ WHERE (state=? OR state=? OR state=?)
3300
+ AND hidden=1
3301
+ AND chat_id=?
3302
+ ORDER BY id DESC LIMIT 1" ,
3303
+ ) ?;
3304
+ let id_to_markseen = stmt
3305
+ . query_row (
3306
+ (
3307
+ MessageState :: InFresh ,
3308
+ MessageState :: InNoticed ,
3309
+ MessageState :: InSeen ,
3310
+ chat_id,
3311
+ ) ,
3312
+ |row| {
3313
+ let id: MsgId = row. get ( 0 ) ?;
3314
+ let state: MessageState = row. get ( 1 ) ?;
3315
+ Ok ( ( id, state) )
3316
+ } ,
3317
+ )
3318
+ . optional ( ) ?
3319
+ . filter ( |& ( _, state) | state == MessageState :: InFresh )
3320
+ . map ( |( id, _) | id) ;
3321
+ let nr_msgs_noticed = conn. execute (
3288
3322
"UPDATE msgs
3289
- SET state=?
3290
- WHERE state=?
3291
- AND hidden=0
3292
- AND chat_id=?;" ,
3323
+ SET state=?
3324
+ WHERE state=? AND hidden IN (0,1) AND chat_id=?" ,
3293
3325
( MessageState :: InNoticed , MessageState :: InFresh , chat_id) ,
3294
- )
3295
- . await ?
3296
- == 0
3297
- {
3326
+ ) ?;
3327
+ Ok ( ( nr_msgs_noticed, id_to_markseen) )
3328
+ } ;
3329
+ let ( nr_msgs_noticed, id_to_markseen) = context. sql . call_write ( conn_fn) . await ?;
3330
+ if nr_msgs_noticed == 0 {
3298
3331
return Ok ( ( ) ) ;
3299
3332
}
3333
+ if let Some ( id) = id_to_markseen {
3334
+ message:: markseen_msgs ( context, vec ! [ id] ) . await ?;
3335
+ }
3300
3336
}
3301
3337
3302
3338
context. emit_event ( EventType :: MsgsNoticed ( chat_id) ) ;
@@ -3337,11 +3373,12 @@ pub(crate) async fn mark_old_messages_as_noticed(
3337
3373
. transaction ( |transaction| {
3338
3374
let mut changed_chats = Vec :: new ( ) ;
3339
3375
for ( _, msg) in msgs_by_chat {
3376
+ // NB: Enumerate `hidden` values to employ the index `(state, hidden, chat_id)`.
3340
3377
let changed_rows = transaction. execute (
3341
3378
"UPDATE msgs
3342
3379
SET state=?
3343
3380
WHERE state=?
3344
- AND hidden=0
3381
+ AND hidden IN (0,1)
3345
3382
AND chat_id=?
3346
3383
AND timestamp<=?;" ,
3347
3384
(
0 commit comments