Skip to content

Commit 65c569d

Browse files
committed
feat(ffi, jsonrpc): Ensure that own key exists before returning self-color (#7374)
Done in CFFI and JSON-RPC APIs to avoid breaking Core tests on key import.
1 parent 7f05914 commit 65c569d

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

deltachat-ffi/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use deltachat::contact::{Contact, ContactId, Origin};
2828
use deltachat::context::{Context, ContextBuilder};
2929
use deltachat::ephemeral::Timer as EphemeralTimer;
3030
use deltachat::imex::BackupProvider;
31-
use deltachat::key::preconfigure_keypair;
31+
use deltachat::key::{load_self_public_key, preconfigure_keypair};
3232
use deltachat::message::MsgId;
3333
use deltachat::qr_code_generator::{create_qr_svg, generate_backup_qr, get_securejoin_qr_svg};
3434
use deltachat::stock_str::StockMessage;
@@ -4240,6 +4240,19 @@ pub unsafe extern "C" fn dc_contact_get_color(contact: *mut dc_contact_t) -> u32
42404240
return 0;
42414241
}
42424242
let ffi_contact = &*contact;
4243+
let ctx = &*ffi_contact.context;
4244+
if ffi_contact.contact.id == ContactId::SELF
4245+
&& block_on(async move {
4246+
// We don't want any UI displaying gray self-color until own key is generated.
4247+
load_self_public_key(ctx)
4248+
.await
4249+
.context("load_self_public_key")
4250+
.log_err(ctx)
4251+
.is_ok() as libc::c_int
4252+
}) == 0
4253+
{
4254+
return 0;
4255+
}
42434256
ffi_contact.contact.get_color()
42444257
}
42454258

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::Result;
22
use deltachat::config::Config;
33
use deltachat::contact::{Contact, ContactId};
4+
use deltachat::key;
45
use serde::Serialize;
56
use typescript_type_def::TypeDef;
67

@@ -28,6 +29,8 @@ pub enum Account {
2829
impl Account {
2930
pub async fn from_context(ctx: &deltachat::context::Context, id: u32) -> Result<Self> {
3031
if ctx.is_configured().await? {
32+
// We don't want any UI displaying gray self-color until own key is generated.
33+
key::load_self_public_key(ctx).await?;
3134
let display_name = ctx.get_config(Config::Displayname).await?;
3235
let addr = ctx.get_config(Config::Addr).await?;
3336
let profile_image = ctx.get_config(Config::Selfavatar).await?;

deltachat-rpc-client/tests/test_vcard.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
def test_vcard(acfactory) -> None:
22
alice, bob, fiona = acfactory.get_online_accounts(3)
33

4+
fiona_color = fiona.color
5+
assert fiona_color != 0x808080
6+
47
bob.create_chat(alice)
58
alice_contact_bob = alice.create_contact(bob, "Bob")
69
alice_contact_charlie = alice.create_contact("[email protected]", "Charlie")
@@ -25,3 +28,4 @@ def test_vcard(acfactory) -> None:
2528
assert snapshot.vcard_contact
2629
assert snapshot.vcard_contact.key
2730
assert snapshot.vcard_contact.color == alice_contact_fiona_snapshot.color
31+
assert alice_contact_fiona_snapshot.color == fiona_color

src/key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub(crate) async fn load_self_public_key_opt(context: &Context) -> Result<Option
145145
/// Loads own public key.
146146
///
147147
/// If no key is generated yet, generates a new one.
148-
pub(crate) async fn load_self_public_key(context: &Context) -> Result<SignedPublicKey> {
148+
pub async fn load_self_public_key(context: &Context) -> Result<SignedPublicKey> {
149149
match load_self_public_key_opt(context).await? {
150150
Some(public_key) => Ok(public_key),
151151
None => {

0 commit comments

Comments
 (0)