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