Skip to content

Commit

Permalink
fix: Look up ContactId::DEVICE by ContactId::DEVICE_ADDR (#6323)
Browse files Browse the repository at this point in the history
At least this fixes the bug when the device chat is e.g. archived by the user on one device, but
this user action is synchronized as archiving a chat with a new contact having the address
"device@localhost" on another device. Not sure if we want to sinchronize actions on the device chats
at all because they are chats with different devices, but maybe that simplifies UX, so let's discuss
it first and maybe disable synchronization in a separate commit.
  • Loading branch information
iequidoo committed Dec 15, 2024
1 parent b74ff27 commit ab59788
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7546,6 +7546,27 @@ mod tests {
Ok(())
}

/// Tests that user actions on device chats are synced despite they are different chats.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_sync_device_chats() -> Result<()> {
let alice0 = &TestContext::new_alice().await;
let alice1 = &TestContext::new_alice().await;
for a in [alice0, alice1] {
a.set_config_bool(Config::SyncMsgs, true).await?;
}
let a0dev_chat_id = ChatId::get_for_contact(alice0, ContactId::DEVICE).await?;
let a1dev_chat_id = ChatId::get_for_contact(alice1, ContactId::DEVICE).await?;
let a1dev_chat = Chat::load_from_db(alice1, a1dev_chat_id).await?;
assert_eq!(a1dev_chat.get_visibility(), ChatVisibility::Normal);
a0dev_chat_id
.set_visibility(alice0, ChatVisibility::Pinned)
.await?;
sync(alice0, alice1).await;
let a1dev_chat = Chat::load_from_db(alice1, a1dev_chat_id).await?;
assert_eq!(a1dev_chat.get_visibility(), ChatVisibility::Pinned);
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_sync_muted() -> Result<()> {
let alice0 = &TestContext::new_alice().await;
Expand Down
17 changes: 16 additions & 1 deletion src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,9 @@ impl Contact {

let addr_normalized = addr_normalize(addr);

if addr_normalized == ContactId::DEVICE_ADDR {
return Ok(Some(ContactId::DEVICE));
}
if context.is_self_addr(&addr_normalized).await? {
return Ok(Some(ContactId::SELF));
}
Expand Down Expand Up @@ -800,8 +803,11 @@ impl Contact {
ensure!(!addr.is_empty(), "Can not add_or_lookup empty address");
ensure!(origin != Origin::Unknown, "Missing valid origin");

if addr.as_ref() == ContactId::DEVICE_ADDR {
return Ok((ContactId::DEVICE, Modifier::None));
}
if context.is_self_addr(addr).await? {
return Ok((ContactId::SELF, sth_modified));
return Ok((ContactId::SELF, Modifier::None));
}

let mut name = sanitize_name(name);
Expand Down Expand Up @@ -2228,6 +2234,10 @@ mod tests {
assert_eq!(contact.get_name(), stock_str::self_msg(&t).await);
assert_eq!(contact.get_addr(), ""); // we're not configured
assert!(!contact.is_blocked());

let contact = Contact::get_by_id(&t, ContactId::DEVICE).await.unwrap();
assert_eq!(contact.get_addr(), "device@localhost");
assert!(!contact.is_blocked());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
Expand Down Expand Up @@ -2684,6 +2694,11 @@ mod tests {
.await
.unwrap();
assert_eq!(id, Some(ContactId::SELF));

let id = Contact::lookup_id_by_addr(&alice.ctx, ContactId::DEVICE_ADDR, Origin::Unknown)
.await
.unwrap();
assert_eq!(id, Some(ContactId::DEVICE));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
Expand Down

0 comments on commit ab59788

Please sign in to comment.