Skip to content

Commit 5bc12ea

Browse files
Hocurilink2xt
authored andcommitted
feat: If it's unclear who verified a contact, mark it as verified by itself
1 parent 0d06131 commit 5bc12ea

File tree

5 files changed

+42
-28
lines changed

5 files changed

+42
-28
lines changed

deltachat-ffi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4361,6 +4361,7 @@ pub unsafe extern "C" fn dc_contact_get_verifier_id(contact: *mut dc_contact_t)
43614361
.context("failed to get verifier")
43624362
.log_err(ctx)
43634363
.unwrap_or_default()
4364+
.unwrap_or_default()
43644365
.unwrap_or_default();
43654366

43664367
verifier_contact_id.to_u32()

deltachat-jsonrpc/src/api/types/contact.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl ContactObject {
7777
let verifier_id = contact
7878
.get_verifier_id(context)
7979
.await?
80+
.flatten()
8081
.map(|contact_id| contact_id.to_u32());
8182

8283
Ok(ContactObject {

src/contact.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,11 +1547,14 @@ impl Contact {
15471547

15481548
/// Returns the `ContactId` that verified the contact.
15491549
///
1550-
/// If the function returns non-zero result,
1550+
/// If this returns Some(_),
15511551
/// display green checkmark in the profile and "Introduced by ..." line
15521552
/// with the name and address of the contact
15531553
/// formatted by [Self::get_name_n_addr].
1554-
pub async fn get_verifier_id(&self, context: &Context) -> Result<Option<ContactId>> {
1554+
///
1555+
/// If this returns `Some(None)`, then the contact is verified,
1556+
/// but it's unclear by whom.
1557+
pub async fn get_verifier_id(&self, context: &Context) -> Result<Option<Option<ContactId>>> {
15551558
let verifier_id: u32 = context
15561559
.sql
15571560
.query_get_value("SELECT verifier FROM contacts WHERE id=?", (self.id,))
@@ -1560,8 +1563,10 @@ impl Contact {
15601563

15611564
if verifier_id == 0 {
15621565
Ok(None)
1566+
} else if verifier_id == self.id.to_u32() {
1567+
Ok(Some(None))
15631568
} else {
1564-
Ok(Some(ContactId::new(verifier_id)))
1569+
Ok(Some(Some(ContactId::new(verifier_id))))
15651570
}
15661571
}
15671572

src/sql/migrations.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,19 +1466,16 @@ fn migrate_pgp_contacts(
14661466
);
14671467
Ok(id)
14681468
};
1469-
let mut original_contact_id_from_addr = |addr: &str| -> Result<u32> {
1470-
if addr_cmp(addr, &self_addr) || addr.is_empty() {
1471-
// TODO not sure what to do when addr is empty,
1472-
// i.e. the contact was verified before we recorded who introduced whom.
1473-
// Right now, we put ContactId::SELF, i.e. mark it as directly verified by us.
1474-
// An alternative would be putting `new_id` here,
1475-
// in order to record that it's unclear who verified.
1476-
1477-
return Ok(1); // ContactId::SELF
1469+
let mut original_contact_id_from_addr = |addr: &str, default: u32| -> Result<u32> {
1470+
if addr_cmp(addr, &self_addr) {
1471+
Ok(1) // ContactId::SELF
1472+
} else if addr.is_empty() {
1473+
Ok(default)
1474+
} else {
1475+
original_contact_id_from_addr_stmt
1476+
.query_row((addr,), |row| row.get(0))
1477+
.with_context(|| format!("Original contact '{addr}' not found"))
14781478
}
1479-
original_contact_id_from_addr_stmt
1480-
.query_row((addr,), |row| row.get(0))
1481-
.with_context(|| format!("Verifier '{addr}' not found in original contacts"))
14821479
};
14831480

14841481
let Some(autocrypt_key) = autocrypt_key else {
@@ -1500,15 +1497,19 @@ fn migrate_pgp_contacts(
15001497
};
15011498
let new_id = insert_contact(verified_key).context("Step 13")?;
15021499
verified_pgp_contacts.insert(original_id.try_into().context("Step 14")?, new_id);
1503-
let verifier_id = original_contact_id_from_addr(&verifier).context("Step 15")?;
1500+
// If the original verifier is unknown, we represent this in the database
1501+
// by putting `new_id` into the place of the verifier,
1502+
// i.e. we say that this contact verified itself.
1503+
let verifier_id =
1504+
original_contact_id_from_addr(&verifier, new_id).context("Step 15")?;
15041505
verifications.insert(new_id, verifier_id);
15051506

15061507
let Some(secondary_verified_key) = secondary_verified_key else {
15071508
continue;
15081509
};
15091510
let new_id = insert_contact(secondary_verified_key).context("Step 16")?;
15101511
let verifier_id: u32 =
1511-
original_contact_id_from_addr(&secondary_verifier).context("Step 17")?;
1512+
original_contact_id_from_addr(&secondary_verifier, new_id).context("Step 17")?;
15121513
// Only use secondary verification if there is no primary verification:
15131514
verifications.entry(new_id).or_insert(verifier_id);
15141515
}
@@ -1524,20 +1525,26 @@ fn migrate_pgp_contacts(
15241525

15251526
for (&new_contact, &verifier_original_contact) in &verifications {
15261527
let verifier = if verifier_original_contact == 1 {
1527-
Some(&1) // Verified by ContactId::SELF
1528+
1 // Verified by ContactId::SELF
1529+
} else if verifier_original_contact == new_contact {
1530+
new_contact // unkwnown verifier
15281531
} else {
15291532
// `verifications` contains the original contact id.
15301533
// We need to get the new, verified-pgp-identified contact id.
1531-
verified_pgp_contacts.get(&verifier_original_contact)
1534+
match verified_pgp_contacts.get(&verifier_original_contact) {
1535+
Some(v) => *v,
1536+
None => {
1537+
warn!(context, "Couldn't find PGP-contact for {verifier_original_contact} who verified {new_contact}");
1538+
continue;
1539+
}
1540+
}
15321541
};
1533-
if let Some(&verifier) = verifier {
1534-
transaction
1535-
.execute(
1536-
"UPDATE contacts SET verifier=? WHERE id=?",
1537-
(verifier, new_contact),
1538-
)
1539-
.context("Step 18")?;
1540-
}
1542+
transaction
1543+
.execute(
1544+
"UPDATE contacts SET verifier=? WHERE id=?",
1545+
(verifier, new_contact),
1546+
)
1547+
.context("Step 18")?;
15411548
}
15421549
info!(context, "Migrated verifications: {verifications:?}");
15431550
}

src/sql/migrations/migrations_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ async fn test_pgp_contacts_migration_verified() -> Result<()> {
171171
pgp_bob.fingerprint().unwrap(),
172172
pgp_bob.public_key(&t).await?.unwrap().dc_fingerprint()
173173
);
174-
assert_eq!(pgp_bob.get_verifier_id(&t).await?.unwrap(), ContactId::SELF);
174+
assert_eq!(pgp_bob.get_verifier_id(&t).await?, Some(None));
175175

176176
Ok(())
177177
}

0 commit comments

Comments
 (0)