api: make Contact.is_verified() return bool

This commit is contained in:
link2xt
2023-11-29 02:57:32 +00:00
parent 5b346397b8
commit 998614b923
7 changed files with 36 additions and 102 deletions

View File

@@ -4112,10 +4112,17 @@ pub unsafe extern "C" fn dc_contact_is_verified(contact: *mut dc_contact_t) -> l
let ffi_contact = &*contact; let ffi_contact = &*contact;
let ctx = &*ffi_contact.context; 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") .context("is_verified failed")
.log_err(ctx) .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] #[no_mangle]

View File

@@ -1,5 +1,4 @@
use anyhow::Result; use anyhow::Result;
use deltachat::contact::VerifiedStatus;
use deltachat::context::Context; use deltachat::context::Context;
use serde::Serialize; use serde::Serialize;
use typescript_type_def::TypeDef; use typescript_type_def::TypeDef;
@@ -57,7 +56,7 @@ impl ContactObject {
Some(path_buf) => path_buf.to_str().map(|s| s.to_owned()), Some(path_buf) => path_buf.to_str().map(|s| s.to_owned()),
None => None, 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 is_profile_verified = contact.is_profile_verified(context).await?;
let verifier_id = contact let verifier_id = contact

View File

@@ -284,13 +284,8 @@ async fn log_contactlist(context: &Context, contacts: &[ContactId]) -> Result<()
let contact = Contact::get_by_id(context, *contact_id).await?; let contact = Contact::get_by_id(context, *contact_id).await?;
let name = contact.get_display_name(); let name = contact.get_display_name();
let addr = contact.get_addr(); let addr = contact.get_addr();
let verified_state = contact.is_verified(context).await?; let verified_str = if contact.is_verified(context).await? {
let verified_str = if VerifiedStatus::Unverified != verified_state { ""
if verified_state == VerifiedStatus::BidirectVerified {
" √√"
} else {
""
}
} else { } else {
"" ""
}; };

View File

@@ -21,7 +21,7 @@ use crate::constants::{
Blocked, Chattype, DC_CHAT_ID_ALLDONE_HINT, DC_CHAT_ID_ARCHIVED_LINK, DC_CHAT_ID_LAST_SPECIAL, 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, 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::context::Context;
use crate::debug_logging::maybe_set_logging_xdc; use crate::debug_logging::maybe_set_logging_xdc;
use crate::download::DownloadState; use crate::download::DownloadState;
@@ -503,7 +503,7 @@ impl ChatId {
let contact_ids = get_chat_contacts(context, self).await?; let contact_ids = get_chat_contacts(context, self).await?;
for contact_id in contact_ids { for contact_id in contact_ids {
let contact = Contact::get_by_id(context, contact_id).await?; 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()); bail!("{} is not verified.", contact.get_display_name());
} }
} }
@@ -3444,9 +3444,7 @@ pub(crate) async fn add_contact_to_chat_ex(
} }
} else { } else {
// else continue and send status mail // else continue and send status mail
if chat.is_protected() if chat.is_protected() && !contact.is_verified(context).await? {
&& contact.is_verified(context).await? != VerifiedStatus::BidirectVerified
{
error!( error!(
context, context,
"Only bidirectional verified contacts can be added to protected chats." "Only bidirectional verified contacts can be added to protected chats."

View File

@@ -348,24 +348,6 @@ pub(crate) enum Modifier {
Created, 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 { impl Contact {
/// Loads a single contact object from the database. /// Loads a single contact object from the database.
/// ///
@@ -1281,20 +1263,20 @@ impl Contact {
/// otherwise use is_chat_protected(). /// otherwise use is_chat_protected().
/// Use [Self::get_verifier_id] to display the verifier contact /// Use [Self::get_verifier_id] to display the verifier contact
/// in the info section of the contact profile. /// in the info section of the contact profile.
pub async fn is_verified(&self, context: &Context) -> Result<VerifiedStatus> { pub async fn is_verified(&self, context: &Context) -> Result<bool> {
// We're always sort of secured-verified as we could verify the key on this device any time with the key // We're always sort of secured-verified as we could verify the key on this device any time with the key
// on this device // on this device
if self.id == ContactId::SELF { if self.id == ContactId::SELF {
return Ok(VerifiedStatus::BidirectVerified); return Ok(true);
} }
if let Some(peerstate) = Peerstate::from_addr(context, &self.addr).await? { if let Some(peerstate) = Peerstate::from_addr(context, &self.addr).await? {
if peerstate.is_using_verified_key() { 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. /// Returns the `ContactId` that verified the contact.
@@ -1349,7 +1331,7 @@ impl Contact {
Ok(chat_id.is_protected(context).await? == ProtectionStatus::Protected) Ok(chat_id.is_protected(context).await? == ProtectionStatus::Protected)
} else { } else {
// 1:1 chat does not exist. // 1:1 chat does not exist.
Ok(self.is_verified(context).await? == VerifiedStatus::BidirectVerified) Ok(self.is_verified(context).await?)
} }
} }

View File

@@ -774,7 +774,6 @@ mod tests {
use crate::chatlist::Chatlist; use crate::chatlist::Chatlist;
use crate::constants::Chattype; use crate::constants::Chattype;
use crate::contact::ContactAddress; use crate::contact::ContactAddress;
use crate::contact::VerifiedStatus;
use crate::peerstate::Peerstate; use crate::peerstate::Peerstate;
use crate::receive_imf::receive_imf; use crate::receive_imf::receive_imf;
use crate::stock_str::chat_protection_enabled; 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) let contact_bob = Contact::get_by_id(&alice.ctx, contact_bob_id)
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(contact_bob.is_verified(&alice.ctx).await.unwrap(), false);
contact_bob.is_verified(&alice.ctx).await.unwrap(),
VerifiedStatus::Unverified
);
// Step 5+6: Alice receives vc-request-with-auth, sends vc-contact-confirm // Step 5+6: Alice receives vc-request-with-auth, sends vc-contact-confirm
alice.recv_msg(&sent).await; alice.recv_msg(&sent).await;
assert_eq!( assert_eq!(contact_bob.is_verified(&alice.ctx).await.unwrap(), true);
contact_bob.is_verified(&alice.ctx).await.unwrap(),
VerifiedStatus::BidirectVerified
);
// exactly one one-to-one chat should be visible for both now // exactly one one-to-one chat should be visible for both now
// (check this before calling alice.create_chat() explicitly below) // (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) let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id)
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(contact_bob.is_verified(&bob.ctx).await.unwrap(), false);
contact_bob.is_verified(&bob.ctx).await.unwrap(),
VerifiedStatus::Unverified
);
// Step 7: Bob receives vc-contact-confirm, sends vc-contact-confirm-received // Step 7: Bob receives vc-contact-confirm, sends vc-contact-confirm-received
bob.recv_msg(&sent).await; bob.recv_msg(&sent).await;
assert_eq!( assert_eq!(contact_alice.is_verified(&bob.ctx).await.unwrap(), true);
contact_alice.is_verified(&bob.ctx).await.unwrap(),
VerifiedStatus::BidirectVerified
);
// Check Bob got the verified message in his 1:1 chat. // Check Bob got the verified message in his 1:1 chat.
{ {
@@ -1063,17 +1050,11 @@ mod tests {
) )
.await?; .await?;
let contact_bob = Contact::get_by_id(&alice.ctx, contact_bob_id).await?; let contact_bob = Contact::get_by_id(&alice.ctx, contact_bob_id).await?;
assert_eq!( assert_eq!(contact_bob.is_verified(&alice.ctx).await?, false);
contact_bob.is_verified(&alice.ctx).await?,
VerifiedStatus::Unverified
);
// Step 5+6: Alice receives vc-request-with-auth, sends vc-contact-confirm // Step 5+6: Alice receives vc-request-with-auth, sends vc-contact-confirm
alice.recv_msg(&sent).await; alice.recv_msg(&sent).await;
assert_eq!( assert_eq!(contact_bob.is_verified(&alice.ctx).await?, true);
contact_bob.is_verified(&alice.ctx).await?,
VerifiedStatus::BidirectVerified
);
let sent = alice.pop_sent_msg().await; let sent = alice.pop_sent_msg().await;
let msg = bob.parse_msg(&sent).await; let msg = bob.parse_msg(&sent).await;
@@ -1090,17 +1071,11 @@ mod tests {
.expect("Error looking up contact") .expect("Error looking up contact")
.expect("Contact not found"); .expect("Contact not found");
let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id).await?; let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id).await?;
assert_eq!( assert_eq!(contact_bob.is_verified(&bob.ctx).await?, false);
contact_bob.is_verified(&bob.ctx).await?,
VerifiedStatus::Unverified
);
// Step 7: Bob receives vc-contact-confirm, sends vc-contact-confirm-received // Step 7: Bob receives vc-contact-confirm, sends vc-contact-confirm-received
bob.recv_msg(&sent).await; bob.recv_msg(&sent).await;
assert_eq!( assert_eq!(contact_alice.is_verified(&bob.ctx).await?, true);
contact_alice.is_verified(&bob.ctx).await?,
VerifiedStatus::BidirectVerified
);
let sent = bob.pop_sent_msg().await; let sent = bob.pop_sent_msg().await;
let msg = alice.parse_msg(&sent).await; let msg = alice.parse_msg(&sent).await;
@@ -1231,17 +1206,11 @@ mod tests {
.await? .await?
.expect("Contact not found"); .expect("Contact not found");
let contact_bob = Contact::get_by_id(&alice.ctx, contact_bob_id).await?; let contact_bob = Contact::get_by_id(&alice.ctx, contact_bob_id).await?;
assert_eq!( assert_eq!(contact_bob.is_verified(&alice.ctx).await?, false);
contact_bob.is_verified(&alice.ctx).await?,
VerifiedStatus::Unverified
);
// Step 5+6: Alice receives vg-request-with-auth, sends vg-member-added // Step 5+6: Alice receives vg-request-with-auth, sends vg-member-added
alice.recv_msg(&sent).await; alice.recv_msg(&sent).await;
assert_eq!( assert_eq!(contact_bob.is_verified(&alice.ctx).await?, true);
contact_bob.is_verified(&alice.ctx).await?,
VerifiedStatus::BidirectVerified
);
let sent = alice.pop_sent_msg().await; let sent = alice.pop_sent_msg().await;
let msg = bob.parse_msg(&sent).await; let msg = bob.parse_msg(&sent).await;
@@ -1277,19 +1246,13 @@ mod tests {
.expect("Error looking up contact") .expect("Error looking up contact")
.expect("Contact not found"); .expect("Contact not found");
let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id).await?; let contact_alice = Contact::get_by_id(&bob.ctx, contact_alice_id).await?;
assert_eq!( assert_eq!(contact_bob.is_verified(&bob.ctx).await?, false);
contact_bob.is_verified(&bob.ctx).await?,
VerifiedStatus::Unverified
);
// Step 7: Bob receives vg-member-added, sends vg-member-added-received // Step 7: Bob receives vg-member-added, sends vg-member-added-received
bob.recv_msg(&sent).await; bob.recv_msg(&sent).await;
{ {
// Bob has Alice verified, message shows up in the group chat. // Bob has Alice verified, message shows up in the group chat.
assert_eq!( assert_eq!(contact_alice.is_verified(&bob.ctx).await?, true);
contact_alice.is_verified(&bob.ctx).await?,
VerifiedStatus::BidirectVerified
);
let chat = bob.get_chat(&alice).await; let chat = bob.get_chat(&alice).await;
assert_eq!( assert_eq!(
chat.blocked, chat.blocked,

View File

@@ -5,7 +5,6 @@ use crate::chat::ProtectionStatus;
use crate::chatlist::Chatlist; use crate::chatlist::Chatlist;
use crate::config::Config; use crate::config::Config;
use crate::constants::DC_GCL_FOR_FORWARDING; use crate::constants::DC_GCL_FOR_FORWARDING;
use crate::contact::VerifiedStatus;
use crate::contact::{Contact, Origin}; use crate::contact::{Contact, Origin};
use crate::message::{Message, Viewtype}; use crate::message::{Message, Viewtype};
use crate::mimefactory::MimeFactory; 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; tcm.send_recv(&bob, &alice, "Using DC again").await;
let contact = alice.add_or_lookup_contact(&bob).await; let contact = alice.add_or_lookup_contact(&bob).await;
assert_eq!( assert_eq!(contact.is_verified(&alice.ctx).await.unwrap(), true);
contact.is_verified(&alice.ctx).await.unwrap(),
VerifiedStatus::BidirectVerified
);
// Bob's chat is marked as verified again // Bob's chat is marked as verified again
assert_verified(&alice, &bob, ProtectionStatus::Protected).await; 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 // Alice and Fiona should now be verified because of gossip
let alice_fiona_contact = alice.add_or_lookup_contact(&fiona).await; let alice_fiona_contact = alice.add_or_lookup_contact(&fiona).await;
assert_eq!( assert!(alice_fiona_contact.is_verified(&alice).await.unwrap(),);
alice_fiona_contact.is_verified(&alice).await.unwrap(),
VerifiedStatus::BidirectVerified
);
// Alice should have a hidden protected chat with Fiona // 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 // 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 // the old key anymore. This means that Alice's device should show
// him as unverified: // him as unverified:
VerifiedStatus::Unverified false
); );
let chat = alice.get_chat(&bob_new).await; let chat = alice.get_chat(&bob_new).await;
assert_eq!(chat.is_protected(), false); 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) { async fn assert_verified(this: &TestContext, other: &TestContext, protected: ProtectionStatus) {
let contact = this.add_or_lookup_contact(other).await; let contact = this.add_or_lookup_contact(other).await;
assert_eq!( assert_eq!(contact.is_verified(this).await.unwrap(), true);
contact.is_verified(this).await.unwrap(),
VerifiedStatus::BidirectVerified
);
let chat = this.get_chat(other).await; let chat = this.get_chat(other).await;
let (expect_protected, expect_broken) = match protected { let (expect_protected, expect_broken) = match protected {