Skip to content

Commit

Permalink
Fix determining whether we need to send multi-device SyncMessage or not
Browse files Browse the repository at this point in the history
SyncMessage were never sent in the configuration where there's a single linked device.
  • Loading branch information
gferon committed Nov 18, 2023
1 parent 3949d27 commit 6ceccea
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions libsignal-service/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ where
self.upload_attachment(spec, out).await
}

/// Return whether we have to prepare sync messages for other devices
///
/// - If we are the main registered device, and there are established sub-device sessions (linked clients), return true
/// - If we are a secondary linked device, return true
async fn is_multi_device(&self) -> bool {
if self.device_id == DEFAULT_DEVICE_ID.into() {
self.protocol_store
.get_sub_device_sessions(&self.local_address)
.await
.map_or(false, |s| s.len() > 0)

Check warning on line 280 in libsignal-service/src/sender.rs

View workflow job for this annotation

GitHub Actions / clippy

length comparison to zero

warning: length comparison to zero --> libsignal-service/src/sender.rs:280:36 | 280 | .map_or(false, |s| s.len() > 0) | ^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!s.is_empty()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_zero = note: `#[warn(clippy::len_zero)]` on by default
} else {
true
}
}

/// Send a message `content` to a single `recipient`.
pub async fn send_message(
&mut self,
Expand Down Expand Up @@ -302,17 +317,12 @@ where
.await,
];

let sub_device_count = self
.protocol_store
.get_sub_device_sessions(&self.local_address)
.await?
.len();
match (&content_body, &results[0]) {
// if we sent a data message and we have linked devices, we need to send a sync message
(
ContentBody::DataMessage(message),
Ok(SentMessage { needs_sync, .. }),
) if *needs_sync || sub_device_count > 0 => {
) if *needs_sync || self.is_multi_device().await => {
log::debug!("sending multi-device sync message");
let sync_message = self
.create_multi_device_sent_transcript_content(
Expand Down Expand Up @@ -387,19 +397,7 @@ where

// we only need to send a synchronization message once
if let Some(message) = message {
let is_multi_device = self
.protocol_store
.get_sub_device_sessions(&self.local_address)
.await
.map(|sessions| !sessions.is_empty())
.unwrap_or_else(|e| {
log::error!(
"Attempt to count local subdevices failed; \
assuming zero: {e}"
);
false
});
if needs_sync_in_results || is_multi_device {
if needs_sync_in_results || self.is_multi_device().await {
let sync_message = self
.create_multi_device_sent_transcript_content(
None,
Expand Down

0 comments on commit 6ceccea

Please sign in to comment.