Skip to content

Commit 0cc8026

Browse files
committed
fix: start ephemeral timer when chat is archived
1 parent 64a1b8e commit 0cc8026

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/chat.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@ impl ChatId {
688688
})
689689
.await?;
690690

691+
if visibility == ChatVisibility::Archived {
692+
start_chat_ephemeral_timers(context, self).await?;
693+
}
694+
691695
context.emit_msgs_changed_without_ids();
692696
chatlist_events::emit_chatlist_changed(context);
693697
chatlist_events::emit_chatlist_item_changed(context, self);
@@ -3242,10 +3246,10 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
32423246
.query_map(
32433247
"SELECT DISTINCT(m.chat_id) FROM msgs m
32443248
LEFT JOIN chats c ON m.chat_id=c.id
3245-
WHERE m.state=10 AND m.hidden=0 AND m.chat_id>9 AND c.blocked=0 AND c.archived=1",
3246-
(),
3249+
WHERE m.state=10 AND m.hidden=0 AND m.chat_id>9 AND c.archived=1",
3250+
(),
32473251
|row| row.get::<_, ChatId>(0),
3248-
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into)
3252+
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
32493253
)
32503254
.await?;
32513255
if chat_ids_in_archive.is_empty() {
@@ -3266,6 +3270,7 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
32663270
.await?;
32673271

32683272
for chat_id_in_archive in chat_ids_in_archive {
3273+
start_chat_ephemeral_timers(context, chat_id_in_archive).await?;
32693274
context.emit_event(EventType::MsgsNoticed(chat_id_in_archive));
32703275
chatlist_events::emit_chatlist_item_changed(context, chat_id_in_archive);
32713276
}

src/ephemeral.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,9 @@ pub(crate) async fn start_ephemeral_timers(context: &Context) -> Result<()> {
715715
#[cfg(test)]
716716
mod tests {
717717
use super::*;
718-
use crate::chat::marknoticed_chat;
718+
use crate::chat::{marknoticed_chat, set_muted, ChatVisibility, MuteDuration};
719719
use crate::config::Config;
720+
use crate::constants::DC_CHAT_ID_ARCHIVED_LINK;
720721
use crate::download::DownloadState;
721722
use crate::location;
722723
use crate::message::markseen_msgs;
@@ -1468,4 +1469,52 @@ mod tests {
14681469
.is_none());
14691470
Ok(())
14701471
}
1472+
1473+
/// Tests that archiving the chat starts ephemeral timer.
1474+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1475+
async fn test_archived_ephemeral_timer() -> Result<()> {
1476+
let mut tcm = TestContextManager::new();
1477+
let alice = &tcm.alice().await;
1478+
let bob = &tcm.bob().await;
1479+
1480+
let chat = alice.create_chat(bob).await;
1481+
let duration = 60;
1482+
chat.id
1483+
.set_ephemeral_timer(alice, Timer::Enabled { duration })
1484+
.await?;
1485+
let bob_received_message = tcm.send_recv(alice, bob, "Hello!").await;
1486+
1487+
bob_received_message
1488+
.chat_id
1489+
.set_visibility(bob, ChatVisibility::Archived)
1490+
.await?;
1491+
SystemTime::shift(Duration::from_secs(100));
1492+
1493+
delete_expired_messages(bob, time()).await?;
1494+
1495+
assert!(Message::load_from_db_optional(bob, bob_received_message.id)
1496+
.await?
1497+
.is_none());
1498+
1499+
// Bob mutes the chat so it is not unarchived.
1500+
set_muted(bob, bob_received_message.chat_id, MuteDuration::Forever).await?;
1501+
1502+
// Now test that for already archived chat
1503+
// timer is started if all archived chats are marked as noticed.
1504+
let bob_received_message_2 = tcm.send_recv(alice, bob, "Hello again!").await;
1505+
assert_eq!(bob_received_message_2.state, MessageState::InFresh);
1506+
1507+
marknoticed_chat(bob, DC_CHAT_ID_ARCHIVED_LINK).await?;
1508+
SystemTime::shift(Duration::from_secs(100));
1509+
1510+
delete_expired_messages(bob, time()).await?;
1511+
1512+
assert!(
1513+
Message::load_from_db_optional(bob, bob_received_message_2.id)
1514+
.await?
1515+
.is_none()
1516+
);
1517+
1518+
Ok(())
1519+
}
14711520
}

0 commit comments

Comments
 (0)