Skip to content

Commit

Permalink
feat: start ephemeral timers when the chat is noticed
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Dec 23, 2024
1 parent 3d5e442 commit 5fd04fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::contact::{self, Contact, ContactId, Origin};
use crate::context::Context;
use crate::debug_logging::maybe_set_logging_xdc;
use crate::download::DownloadState;
use crate::ephemeral::Timer as EphemeralTimer;
use crate::ephemeral::{start_chat_ephemeral_timers, Timer as EphemeralTimer};
use crate::events::EventType;
use crate::html::new_html_mimepart;
use crate::location;
Expand Down Expand Up @@ -3282,20 +3282,24 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
context.emit_event(EventType::MsgsNoticed(chat_id_in_archive));
chatlist_events::emit_chatlist_item_changed(context, chat_id_in_archive);
}
} else if context
.sql
.execute(
"UPDATE msgs
} else {
start_chat_ephemeral_timers(context, chat_id).await?;

if context
.sql
.execute(
"UPDATE msgs
SET state=?
WHERE state=?
AND hidden=0
AND chat_id=?;",
(MessageState::InNoticed, MessageState::InFresh, chat_id),
)
.await?
== 0
{
return Ok(());
(MessageState::InNoticed, MessageState::InFresh, chat_id),
)
.await?
== 0
{
return Ok(());
}
}

context.emit_event(EventType::MsgsNoticed(chat_id));
Expand Down
22 changes: 22 additions & 0 deletions src/ephemeral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,28 @@ pub(crate) async fn start_ephemeral_timers_msgids(
Ok(())
}

/// Starts ephemeral timer for all messages in the chat.
///
/// This should be called when chat is marked as noticed.
pub(crate) async fn start_chat_ephemeral_timers(context: &Context, chat_id: ChatId) -> Result<()> {
let now = time();
let should_interrupt = context
.sql
.execute(
"UPDATE msgs SET ephemeral_timestamp = ?1 + ephemeral_timer
WHERE chat_id = ?2
AND ephemeral_timer > 0
AND (ephemeral_timestamp == 0 OR ephemeral_timestamp > ?1 + ephemeral_timer)",
(now, chat_id),
)
.await?
> 0;
if should_interrupt {
context.scheduler.interrupt_ephemeral_task().await;
}
Ok(())
}

/// Selects messages which are expired according to
/// `delete_device_after` setting or `ephemeral_timestamp` column.
///
Expand Down

0 comments on commit 5fd04fe

Please sign in to comment.