diff --git a/src/chat.rs b/src/chat.rs index 9fcafe206..e0fd01a1f 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1745,11 +1745,12 @@ pub async fn get_chat_msgs( } pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<(), Error> { + // "WHERE" below uses the index `(state, hidden, chat_id)`, see get_fresh_msg_cnt() for reasoning if !context .sql .exists( - "SELECT id FROM msgs WHERE chat_id=? AND state=?;", - paramsv![chat_id, MessageState::InFresh], + "SELECT id FROM msgs WHERE state=? AND hidden=0 AND chat_id=?;", + paramsv![MessageState::InFresh, chat_id], ) .await? { @@ -1760,10 +1761,11 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<(), .sql .execute( "UPDATE msgs - SET state=13 - WHERE chat_id=? - AND state=10;", - paramsv![chat_id], + SET state=? + WHERE state=? + AND hidden=0 + AND chat_id=?;", + paramsv![MessageState::InNoticed, MessageState::InFresh, chat_id], ) .await?; diff --git a/src/sql.rs b/src/sql.rs index 8a3f47b9a..98fc7d21e 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -1360,7 +1360,7 @@ CREATE INDEX devmsglabels_index1 ON devmsglabels (label); } if dbversion < 68 { info!(context, "[migration] v68"); - // the index is used to speed up get_fresh_msg_cnt(), see comment there for more details + // the index is used to speed up get_fresh_msg_cnt() (see comment there for more details) and marknoticed_chat() sql.execute( "CREATE INDEX IF NOT EXISTS msgs_index7 ON msgs (state, hidden, chat_id);", paramsv![],