Skip to content

Commit beb6a21

Browse files
committed
feat: start ephemeral timers when the chat is noticed
1 parent 22bc756 commit beb6a21

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

src/chat.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::contact::{self, Contact, ContactId, Origin};
2828
use crate::context::Context;
2929
use crate::debug_logging::maybe_set_logging_xdc;
3030
use crate::download::DownloadState;
31-
use crate::ephemeral::Timer as EphemeralTimer;
31+
use crate::ephemeral::{start_chat_ephemeral_timers, Timer as EphemeralTimer};
3232
use crate::events::EventType;
3333
use crate::html::new_html_mimepart;
3434
use crate::location;
@@ -3269,20 +3269,24 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
32693269
context.emit_event(EventType::MsgsNoticed(chat_id_in_archive));
32703270
chatlist_events::emit_chatlist_item_changed(context, chat_id_in_archive);
32713271
}
3272-
} else if context
3273-
.sql
3274-
.execute(
3275-
"UPDATE msgs
3272+
} else {
3273+
start_chat_ephemeral_timers(context, chat_id).await?;
3274+
3275+
if context
3276+
.sql
3277+
.execute(
3278+
"UPDATE msgs
32763279
SET state=?
32773280
WHERE state=?
32783281
AND hidden=0
32793282
AND chat_id=?;",
3280-
(MessageState::InNoticed, MessageState::InFresh, chat_id),
3281-
)
3282-
.await?
3283-
== 0
3284-
{
3285-
return Ok(());
3283+
(MessageState::InNoticed, MessageState::InFresh, chat_id),
3284+
)
3285+
.await?
3286+
== 0
3287+
{
3288+
return Ok(());
3289+
}
32863290
}
32873291

32883292
context.emit_event(EventType::MsgsNoticed(chat_id));
@@ -3354,6 +3358,7 @@ pub(crate) async fn mark_old_messages_as_noticed(
33543358
}
33553359

33563360
for c in changed_chats {
3361+
start_chat_ephemeral_timers(context, c).await?;
33573362
context.emit_event(EventType::MsgsNoticed(c));
33583363
chatlist_events::emit_chatlist_item_changed(context, c);
33593364
}

src/ephemeral.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,28 @@ pub(crate) async fn start_ephemeral_timers_msgids(
349349
Ok(())
350350
}
351351

352+
/// Starts ephemeral timer for all messages in the chat.
353+
///
354+
/// This should be called when chat is marked as noticed.
355+
pub(crate) async fn start_chat_ephemeral_timers(context: &Context, chat_id: ChatId) -> Result<()> {
356+
let now = time();
357+
let should_interrupt = context
358+
.sql
359+
.execute(
360+
"UPDATE msgs SET ephemeral_timestamp = ?1 + ephemeral_timer
361+
WHERE chat_id = ?2
362+
AND ephemeral_timer > 0
363+
AND (ephemeral_timestamp == 0 OR ephemeral_timestamp > ?1 + ephemeral_timer)",
364+
(now, chat_id),
365+
)
366+
.await?
367+
> 0;
368+
if should_interrupt {
369+
context.scheduler.interrupt_ephemeral_task().await;
370+
}
371+
Ok(())
372+
}
373+
352374
/// Selects messages which are expired according to
353375
/// `delete_device_after` setting or `ephemeral_timestamp` column.
354376
///
@@ -693,6 +715,7 @@ pub(crate) async fn start_ephemeral_timers(context: &Context) -> Result<()> {
693715
#[cfg(test)]
694716
mod tests {
695717
use super::*;
718+
use crate::chat::marknoticed_chat;
696719
use crate::config::Config;
697720
use crate::download::DownloadState;
698721
use crate::location;
@@ -1420,4 +1443,29 @@ mod tests {
14201443

14211444
Ok(())
14221445
}
1446+
1447+
/// Tests that ephemeral timer is started when the chat is noticed.
1448+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1449+
async fn test_noticed_ephemeral_timer() -> Result<()> {
1450+
let mut tcm = TestContextManager::new();
1451+
let alice = &tcm.alice().await;
1452+
let bob = &tcm.bob().await;
1453+
1454+
let chat = alice.create_chat(bob).await;
1455+
let duration = 60;
1456+
chat.id
1457+
.set_ephemeral_timer(alice, Timer::Enabled { duration })
1458+
.await?;
1459+
let bob_received_message = tcm.send_recv(alice, bob, "Hello!").await;
1460+
1461+
marknoticed_chat(bob, bob_received_message.chat_id).await?;
1462+
SystemTime::shift(Duration::from_secs(100));
1463+
1464+
delete_expired_messages(bob, time()).await?;
1465+
1466+
assert!(Message::load_from_db_optional(bob, bob_received_message.id)
1467+
.await?
1468+
.is_none());
1469+
Ok(())
1470+
}
14231471
}

0 commit comments

Comments
 (0)