From 998614b923ffcabb446fadae64e14e378347928f Mon Sep 17 00:00:00 2001 From: link2xt Date: Wed, 29 Nov 2023 02:57:32 +0000 Subject: [PATCH] api: make Contact.is_verified() return bool --- deltachat-ffi/src/lib.rs | 11 +++- deltachat-jsonrpc/src/api/types/contact.rs | 3 +- deltachat-repl/src/cmdline.rs | 9 +--- src/chat.rs | 8 ++- src/contact.rs | 28 ++-------- src/securejoin.rs | 61 +++++----------------- src/tests/verified_chats.rs | 18 ++----- 7 files changed, 36 insertions(+), 102 deletions(-) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 336654894..06c55bc84 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -4112,10 +4112,17 @@ pub unsafe extern "C" fn dc_contact_is_verified(contact: *mut dc_contact_t) -> l let ffi_contact = &*contact; let ctx = &*ffi_contact.context; - block_on(ffi_contact.contact.is_verified(ctx)) + if block_on(ffi_contact.contact.is_verified(ctx)) .context("is_verified failed") .log_err(ctx) - .unwrap_or_default() as libc::c_int + .unwrap_or_default() + { + // Return value is essentially a boolean, + // but we return 2 for true for backwards compatibility. + 2 + } else { + 0 + } } #[no_mangle] diff --git a/deltachat-jsonrpc/src/api/types/contact.rs b/deltachat-jsonrpc/src/api/types/contact.rs index c5676aba7..60acd6b19 100644 --- a/deltachat-jsonrpc/src/api/types/contact.rs +++ b/deltachat-jsonrpc/src/api/types/contact.rs @@ -1,5 +1,4 @@ use anyhow::Result; -use deltachat::contact::VerifiedStatus; use deltachat::context::Context; use serde::Serialize; use typescript_type_def::TypeDef; @@ -57,7 +56,7 @@ impl ContactObject { Some(path_buf) => path_buf.to_str().map(|s| s.to_owned()), None => None, }; - let is_verified = contact.is_verified(context).await? == VerifiedStatus::BidirectVerified; + let is_verified = contact.is_verified(context).await?; let is_profile_verified = contact.is_profile_verified(context).await?; let verifier_id = contact diff --git a/deltachat-repl/src/cmdline.rs b/deltachat-repl/src/cmdline.rs index 40fa149fa..30f4c9185 100644 --- a/deltachat-repl/src/cmdline.rs +++ b/deltachat-repl/src/cmdline.rs @@ -284,13 +284,8 @@ async fn log_contactlist(context: &Context, contacts: &[ContactId]) -> Result<() let contact = Contact::get_by_id(context, *contact_id).await?; let name = contact.get_display_name(); let addr = contact.get_addr(); - let verified_state = contact.is_verified(context).await?; - let verified_str = if VerifiedStatus::Unverified != verified_state { - if verified_state == VerifiedStatus::BidirectVerified { - " √√" - } else { - " √" - } + let verified_str = if contact.is_verified(context).await? { + " √" } else { "" }; diff --git a/src/chat.rs b/src/chat.rs index d7163419e..18db1f05c 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -21,7 +21,7 @@ use crate::constants::{ Blocked, Chattype, DC_CHAT_ID_ALLDONE_HINT, DC_CHAT_ID_ARCHIVED_LINK, DC_CHAT_ID_LAST_SPECIAL, DC_CHAT_ID_TRASH, DC_RESEND_USER_AVATAR_DAYS, }; -use crate::contact::{self, Contact, ContactAddress, ContactId, Origin, VerifiedStatus}; +use crate::contact::{self, Contact, ContactAddress, ContactId, Origin}; use crate::context::Context; use crate::debug_logging::maybe_set_logging_xdc; use crate::download::DownloadState; @@ -503,7 +503,7 @@ impl ChatId { let contact_ids = get_chat_contacts(context, self).await?; for contact_id in contact_ids { let contact = Contact::get_by_id(context, contact_id).await?; - if contact.is_verified(context).await? != VerifiedStatus::BidirectVerified { + if !contact.is_verified(context).await? { bail!("{} is not verified.", contact.get_display_name()); } } @@ -3444,9 +3444,7 @@ pub(crate) async fn add_contact_to_chat_ex( } } else { // else continue and send status mail - if chat.is_protected() - && contact.is_verified(context).await? != VerifiedStatus::BidirectVerified - { + if chat.is_protected() && !contact.is_verified(context).await? { error!( context, "Only bidirectional verified contacts can be added to protected chats." diff --git a/src/contact.rs b/src/contact.rs index eba291654..91b11fe55 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -348,24 +348,6 @@ pub(crate) enum Modifier { Created, } -/// Verification status of the contact. -#[derive(Debug, PartialEq, Eq, Clone, Copy, FromPrimitive)] -#[repr(u8)] -pub enum VerifiedStatus { - /// Contact is not verified. - Unverified = 0, - /// SELF has verified the fingerprint of a contact. Currently unused. - Verified = 1, - /// SELF and contact have verified their fingerprints in both directions; in the UI typically checkmarks are shown. - BidirectVerified = 2, -} - -impl Default for VerifiedStatus { - fn default() -> Self { - Self::Unverified - } -} - impl Contact { /// Loads a single contact object from the database. /// @@ -1281,20 +1263,20 @@ impl Contact { /// otherwise use is_chat_protected(). /// Use [Self::get_verifier_id] to display the verifier contact /// in the info section of the contact profile. - pub async fn is_verified(&self, context: &Context) -> Result { + pub async fn is_verified(&self, context: &Context) -> Result { // We're always sort of secured-verified as we could verify the key on this device any time with the key // on this device if self.id == ContactId::SELF { - return Ok(VerifiedStatus::BidirectVerified); + return Ok(true); } if let Some(peerstate) = Peerstate::from_addr(context, &self.addr).await? { if peerstate.is_using_verified_key() { - return Ok(VerifiedStatus::BidirectVerified); + return Ok(true); } } - Ok(VerifiedStatus::Unverified) + Ok(false) } /// Returns the `ContactId` that verified the contact. @@ -1349,7 +1331,7 @@ impl Contact { Ok(chat_id.is_protected(context).await? == ProtectionStatus::Protected) } else { // 1:1 chat does not exist. - Ok(self.is_verified(context).await? == VerifiedStatus::BidirectVerified) + Ok(self.is_verified(context).await?) } } diff --git a/src/securejoin.rs b/src/securejoin.rs index 04cd0caa9..39a14901b 100644 --- a/src/securejoin.rs +++ b/src/securejoin.rs @@ -774,7 +774,6 @@ mod tests { use crate::chatlist::Chatlist; use crate::constants::Chattype; use crate::contact::ContactAddress; - use crate::contact::VerifiedStatus; use crate::peerstate::Peerstate; use crate::receive_imf::receive_imf; use crate::stock_str::chat_protection_enabled; @@ -893,17 +892,11 @@ mod tests { let contact_bob = Contact::get_by_id(&alice.ctx, contact_bob_id) .await .unwrap(); - assert_eq!( - contact_bob.is_verified(&alice.ctx).await.unwrap(), - VerifiedStatus::Unverified - ); + assert_eq!(contact_bob.is_verified(&alice.ctx).await.unwrap(), false); // Step 5+6: Alice receives vc-request-with-auth, sends vc-contact-confirm alice.recv_msg(&sent).await; - assert_eq!( - contact_bob.is_verified(&alice.ctx).await.unwrap(), - VerifiedStatus::BidirectVerified - ); + assert_eq!(contact_bob.is_verified(&alice.ctx).await.unwrap(), true); // exactly one one-to-one chat should be visible for both now // (check this before calling alice.create_chat() explicitly below) @@ -946,17 +939,11 @@ mod tests { let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id) .await .unwrap(); - assert_eq!( - contact_bob.is_verified(&bob.ctx).await.unwrap(), - VerifiedStatus::Unverified - ); + assert_eq!(contact_bob.is_verified(&bob.ctx).await.unwrap(), false); // Step 7: Bob receives vc-contact-confirm, sends vc-contact-confirm-received bob.recv_msg(&sent).await; - assert_eq!( - contact_alice.is_verified(&bob.ctx).await.unwrap(), - VerifiedStatus::BidirectVerified - ); + assert_eq!(contact_alice.is_verified(&bob.ctx).await.unwrap(), true); // Check Bob got the verified message in his 1:1 chat. { @@ -1063,17 +1050,11 @@ mod tests { ) .await?; let contact_bob = Contact::get_by_id(&alice.ctx, contact_bob_id).await?; - assert_eq!( - contact_bob.is_verified(&alice.ctx).await?, - VerifiedStatus::Unverified - ); + assert_eq!(contact_bob.is_verified(&alice.ctx).await?, false); // Step 5+6: Alice receives vc-request-with-auth, sends vc-contact-confirm alice.recv_msg(&sent).await; - assert_eq!( - contact_bob.is_verified(&alice.ctx).await?, - VerifiedStatus::BidirectVerified - ); + assert_eq!(contact_bob.is_verified(&alice.ctx).await?, true); let sent = alice.pop_sent_msg().await; let msg = bob.parse_msg(&sent).await; @@ -1090,17 +1071,11 @@ mod tests { .expect("Error looking up contact") .expect("Contact not found"); let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id).await?; - assert_eq!( - contact_bob.is_verified(&bob.ctx).await?, - VerifiedStatus::Unverified - ); + assert_eq!(contact_bob.is_verified(&bob.ctx).await?, false); // Step 7: Bob receives vc-contact-confirm, sends vc-contact-confirm-received bob.recv_msg(&sent).await; - assert_eq!( - contact_alice.is_verified(&bob.ctx).await?, - VerifiedStatus::BidirectVerified - ); + assert_eq!(contact_alice.is_verified(&bob.ctx).await?, true); let sent = bob.pop_sent_msg().await; let msg = alice.parse_msg(&sent).await; @@ -1231,17 +1206,11 @@ mod tests { .await? .expect("Contact not found"); let contact_bob = Contact::get_by_id(&alice.ctx, contact_bob_id).await?; - assert_eq!( - contact_bob.is_verified(&alice.ctx).await?, - VerifiedStatus::Unverified - ); + assert_eq!(contact_bob.is_verified(&alice.ctx).await?, false); // Step 5+6: Alice receives vg-request-with-auth, sends vg-member-added alice.recv_msg(&sent).await; - assert_eq!( - contact_bob.is_verified(&alice.ctx).await?, - VerifiedStatus::BidirectVerified - ); + assert_eq!(contact_bob.is_verified(&alice.ctx).await?, true); let sent = alice.pop_sent_msg().await; let msg = bob.parse_msg(&sent).await; @@ -1277,19 +1246,13 @@ mod tests { .expect("Error looking up contact") .expect("Contact not found"); let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id).await?; - assert_eq!( - contact_bob.is_verified(&bob.ctx).await?, - VerifiedStatus::Unverified - ); + assert_eq!(contact_bob.is_verified(&bob.ctx).await?, false); // Step 7: Bob receives vg-member-added, sends vg-member-added-received bob.recv_msg(&sent).await; { // Bob has Alice verified, message shows up in the group chat. - assert_eq!( - contact_alice.is_verified(&bob.ctx).await?, - VerifiedStatus::BidirectVerified - ); + assert_eq!(contact_alice.is_verified(&bob.ctx).await?, true); let chat = bob.get_chat(&alice).await; assert_eq!( chat.blocked, diff --git a/src/tests/verified_chats.rs b/src/tests/verified_chats.rs index ca75a11e9..aacf79d9f 100644 --- a/src/tests/verified_chats.rs +++ b/src/tests/verified_chats.rs @@ -5,7 +5,6 @@ use crate::chat::ProtectionStatus; use crate::chatlist::Chatlist; use crate::config::Config; use crate::constants::DC_GCL_FOR_FORWARDING; -use crate::contact::VerifiedStatus; use crate::contact::{Contact, Origin}; use crate::message::{Message, Viewtype}; use crate::mimefactory::MimeFactory; @@ -70,10 +69,7 @@ async fn check_verified_oneonone_chat(broken_by_classical_email: bool) { tcm.send_recv(&bob, &alice, "Using DC again").await; let contact = alice.add_or_lookup_contact(&bob).await; - assert_eq!( - contact.is_verified(&alice.ctx).await.unwrap(), - VerifiedStatus::BidirectVerified - ); + assert_eq!(contact.is_verified(&alice.ctx).await.unwrap(), true); // Bob's chat is marked as verified again assert_verified(&alice, &bob, ProtectionStatus::Protected).await; @@ -121,10 +117,7 @@ async fn test_create_verified_oneonone_chat() -> Result<()> { // Alice and Fiona should now be verified because of gossip let alice_fiona_contact = alice.add_or_lookup_contact(&fiona).await; - assert_eq!( - alice_fiona_contact.is_verified(&alice).await.unwrap(), - VerifiedStatus::BidirectVerified - ); + assert!(alice_fiona_contact.is_verified(&alice).await.unwrap(),); // Alice should have a hidden protected chat with Fiona { @@ -684,7 +677,7 @@ async fn test_break_protection_then_verify_again() -> Result<()> { // Bob sent a message with a new key, so he most likely doesn't have // the old key anymore. This means that Alice's device should show // him as unverified: - VerifiedStatus::Unverified + false ); let chat = alice.get_chat(&bob_new).await; assert_eq!(chat.is_protected(), false); @@ -786,10 +779,7 @@ async fn test_create_oneonone_chat_with_former_verified_contact() -> Result<()> async fn assert_verified(this: &TestContext, other: &TestContext, protected: ProtectionStatus) { let contact = this.add_or_lookup_contact(other).await; - assert_eq!( - contact.is_verified(this).await.unwrap(), - VerifiedStatus::BidirectVerified - ); + assert_eq!(contact.is_verified(this).await.unwrap(), true); let chat = this.get_chat(other).await; let (expect_protected, expect_broken) = match protected {