diff --git a/src/sender.rs b/src/sender.rs index 56f31d0fb..041949315 100644 --- a/src/sender.rs +++ b/src/sender.rs @@ -1,4 +1,3 @@ -use core::time; use std::{collections::HashSet, time::SystemTime}; use chrono::prelude::*; @@ -142,6 +141,15 @@ pub enum ThreadIdentifier { Group(GroupV2Id), } +#[derive(Debug)] +pub enum EncryptedMessages { + None, + Some { + messages: Vec, + used_identity_key: IdentityKey, + }, +} + impl MessageSender where S: ProtocolStore + SenderKeyStore + SessionStoreExt + Sync + Clone, @@ -533,13 +541,20 @@ where let content_bytes = content.encode_to_vec(); for _ in 0..4u8 { - let (messages, used_identity_key) = self + let EncryptedMessages::Some { + messages, + used_identity_key, + } = self .create_encrypted_messages( &recipient, unidentified_access.map(|x| &x.certificate), &content_bytes, ) - .await?; + .await? + else { + debug!("no messages were encrypted: this should only happen when device is primary without any secondaries"); + break; + }; let messages = OutgoingPushMessages { destination: recipient.uuid, @@ -839,8 +854,7 @@ where recipient: &ServiceAddress, unidentified_access: Option<&SenderCertificate>, content: &[u8], - ) -> Result<(Vec, IdentityKey), MessageSenderError> - { + ) -> Result { let mut messages = vec![]; let mut devices: HashSet = self @@ -925,15 +939,22 @@ where } } - let identity_key = self - .protocol_store - .get_identity(&recipient.to_protocol_address(DEFAULT_DEVICE_ID)) - .await? - .ok_or(MessageSenderError::UntrustedIdentity { - address: *recipient, - })?; - - Ok((messages, identity_key)) + if messages.is_empty() { + Ok(EncryptedMessages::None) + } else { + Ok(EncryptedMessages::Some { + messages, + used_identity_key: self + .protocol_store + .get_identity( + &recipient.to_protocol_address(DEFAULT_DEVICE_ID), + ) + .await? + .ok_or(MessageSenderError::UntrustedIdentity { + address: *recipient, + })?, + }) + } } /// Equivalent to `getEncryptedMessage`