Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the expire timer #173

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>"]
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 @@ -1006,13 +1026,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