Skip to content

Commit

Permalink
Set the expire timer
Browse files Browse the repository at this point in the history
This sets the expire timer of a message such that the current expiration
timer of a conversation stays the same (except in cases where it was
explicitly set to another value).

It may be a little counterproductive when sending a message to (e.g.) a
contact to query the timer for the thread which unwraps it back to
querying the contact, but this method may potentially also be used to
display the expiration timer.

Note that due to #171, I would guess that the expiration timer always
stays the same for contacts directly after linking, e.g. changing it
on an official client and then sending a message from presage, presage
will set the expiration timer back to before the change.

This fixes #93.
  • Loading branch information
Schmiddiii committed May 19, 2023
1 parent 9935bb0 commit dcaca32
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
4 changes: 2 additions & 2 deletions presage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ authors = ["Gabriel Féron <g@leirbag.net>"]
edition = "2021"

[dependencies]
libsignal-service = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "c2f70ef" }
libsignal-service-hyper = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "c2f70ef" }
libsignal-service = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "a2e871a" }
libsignal-service-hyper = { git = "https://github.com/whisperfish/libsignal-service-rs", rev = "a2e871a" }

base64 = "0.12"
futures = "0.3"
Expand Down
49 changes: 46 additions & 3 deletions presage/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,9 @@ impl<C: Store> Manager<C, Registered> {
/// Sends a messages to the provided [ServiceAddress].
/// The timestamp should be set to now and is used by Signal mobile apps
/// to order messages later, and apply reactions.
///
/// This method will automatically update the [DataMessage::expiration_timer] if it is set to
/// [None] such that the chat will keep the current expire timer.
pub async fn send_message(
&mut self,
recipient_addr: impl Into<ServiceAddress>,
Expand All @@ -951,7 +954,24 @@ impl<C: Store> Manager<C, Registered> {

let online_only = false;
let recipient = recipient_addr.into();
let content_body: ContentBody = message.into();
let mut content_body: ContentBody = message.into();

// Only update the expiration timer if it is not set.
match content_body {
ContentBody::DataMessage(DataMessage {
expire_timer: ref mut timer,
..
}) if timer.is_none() => {
// Set the expire timer to None for errors.
let store_expire_timer = self
.config_store
.expire_timer(&Thread::Contact(recipient.uuid))
.unwrap_or_default();

*timer = store_expire_timer;
}
_ => {}
}

let sender_certificate = self.sender_certificate().await?;
let unidentified_access =
Expand Down Expand Up @@ -1003,13 +1023,36 @@ impl<C: Store> Manager<C, Registered> {
Ok(upload.await)
}

/// Sends one message in a group (v2).
/// Sends one message in a group (v2). The `master_key_bytes` is required to have 32 elements.
///
/// This method will automatically update the [DataMessage::expiration_timer] if it is set to
/// [None] such that the chat will keep the current expire timer.
pub async fn send_message_to_group(
&mut self,
master_key_bytes: &[u8],
message: DataMessage,
mut message: DataMessage,
timestamp: u64,
) -> Result<(), Error<C::Error>> {
// Only update the expiration timer if it is not set.
match message {
DataMessage {
expire_timer: ref mut timer,
..
} if timer.is_none() => {
// Set the expire timer to None for errors.
let store_expire_timer = self
.config_store
.expire_timer(&Thread::Group(
master_key_bytes
.try_into()
.expect("Master key bytes to be of size 32."),
))
.unwrap_or_default();

*timer = store_expire_timer;
}
_ => {}
}
let mut sender = self.new_message_sender().await?;

let mut groups_manager = self.groups_manager()?;
Expand Down
12 changes: 12 additions & 0 deletions presage/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ pub trait Store: ProtocolStore + SenderKeyStore + SessionStoreExt + Sync + Clone
range: impl RangeBounds<u64>,
) -> Result<Self::MessagesIter, Self::Error>;

/// Get the expire timer from a [Thread], which corresponds to either [Contact::expire_timer]
/// or [Group::disappearing_messages_timer].
fn expire_timer(&self, thread: &Thread) -> Result<Option<u32>, Self::Error> {
match thread {
Thread::Contact(uuid) => Ok(self.contact_by_id(*uuid)?.map(|c| c.expire_timer)),
Thread::Group(key) => Ok(self
.group(*key)?
.and_then(|g| g.disappearing_messages_timer)
.map(|t| t.duration)),
}
}

/// Contacts

/// Clear all saved synchronized contact data
Expand Down

0 comments on commit dcaca32

Please sign in to comment.