Skip to content

Commit

Permalink
Fix sending note to self as primary without secondary devices
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed Oct 22, 2024
1 parent 468fa6f commit 0a8964f
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/sender.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::time;
use std::{collections::HashSet, time::SystemTime};

use chrono::prelude::*;
Expand Down Expand Up @@ -142,6 +141,15 @@ pub enum ThreadIdentifier {
Group(GroupV2Id),
}

#[derive(Debug)]
pub enum EncryptedMessages {
None,
Some {
messages: Vec<OutgoingPushMessage>,
used_identity_key: IdentityKey,
},
}

impl<S, R> MessageSender<S, R>
where
S: ProtocolStore + SenderKeyStore + SessionStoreExt + Sync + Clone,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -839,8 +854,7 @@ where
recipient: &ServiceAddress,
unidentified_access: Option<&SenderCertificate>,
content: &[u8],
) -> Result<(Vec<OutgoingPushMessage>, IdentityKey), MessageSenderError>
{
) -> Result<EncryptedMessages, MessageSenderError> {
let mut messages = vec![];

let mut devices: HashSet<DeviceId> = self
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 0a8964f

Please sign in to comment.