Skip to content

Commit

Permalink
fix: start ephemeral timer when chat is archived
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Dec 24, 2024
1 parent 64a1b8e commit 0cc8026
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,10 @@ impl ChatId {
})
.await?;

if visibility == ChatVisibility::Archived {
start_chat_ephemeral_timers(context, self).await?;
}

context.emit_msgs_changed_without_ids();
chatlist_events::emit_chatlist_changed(context);
chatlist_events::emit_chatlist_item_changed(context, self);
Expand Down Expand Up @@ -3242,10 +3246,10 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
.query_map(
"SELECT DISTINCT(m.chat_id) FROM msgs m
LEFT JOIN chats c ON m.chat_id=c.id
WHERE m.state=10 AND m.hidden=0 AND m.chat_id>9 AND c.blocked=0 AND c.archived=1",
(),
WHERE m.state=10 AND m.hidden=0 AND m.chat_id>9 AND c.archived=1",
(),
|row| row.get::<_, ChatId>(0),
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into)
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
)
.await?;
if chat_ids_in_archive.is_empty() {
Expand All @@ -3266,6 +3270,7 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
.await?;

for chat_id_in_archive in chat_ids_in_archive {
start_chat_ephemeral_timers(context, chat_id_in_archive).await?;
context.emit_event(EventType::MsgsNoticed(chat_id_in_archive));
chatlist_events::emit_chatlist_item_changed(context, chat_id_in_archive);
}
Expand Down
51 changes: 50 additions & 1 deletion src/ephemeral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,9 @@ pub(crate) async fn start_ephemeral_timers(context: &Context) -> Result<()> {
#[cfg(test)]
mod tests {
use super::*;
use crate::chat::marknoticed_chat;
use crate::chat::{marknoticed_chat, set_muted, ChatVisibility, MuteDuration};
use crate::config::Config;
use crate::constants::DC_CHAT_ID_ARCHIVED_LINK;
use crate::download::DownloadState;
use crate::location;
use crate::message::markseen_msgs;
Expand Down Expand Up @@ -1468,4 +1469,52 @@ mod tests {
.is_none());
Ok(())
}

/// Tests that archiving the chat starts ephemeral timer.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_archived_ephemeral_timer() -> Result<()> {
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;

let chat = alice.create_chat(bob).await;
let duration = 60;
chat.id
.set_ephemeral_timer(alice, Timer::Enabled { duration })
.await?;
let bob_received_message = tcm.send_recv(alice, bob, "Hello!").await;

bob_received_message
.chat_id
.set_visibility(bob, ChatVisibility::Archived)
.await?;
SystemTime::shift(Duration::from_secs(100));

delete_expired_messages(bob, time()).await?;

assert!(Message::load_from_db_optional(bob, bob_received_message.id)
.await?
.is_none());

// Bob mutes the chat so it is not unarchived.
set_muted(bob, bob_received_message.chat_id, MuteDuration::Forever).await?;

// Now test that for already archived chat
// timer is started if all archived chats are marked as noticed.
let bob_received_message_2 = tcm.send_recv(alice, bob, "Hello again!").await;
assert_eq!(bob_received_message_2.state, MessageState::InFresh);

marknoticed_chat(bob, DC_CHAT_ID_ARCHIVED_LINK).await?;
SystemTime::shift(Duration::from_secs(100));

delete_expired_messages(bob, time()).await?;

assert!(
Message::load_from_db_optional(bob, bob_received_message_2.id)
.await?
.is_none()
);

Ok(())
}
}

0 comments on commit 0cc8026

Please sign in to comment.