Skip to content

Commit c38d027

Browse files
committed
fix: recognize all transport addresses as own addresses
Fix get_secondary_addrs() which was using `secondary_addrs` config that is not updated anymore. Instead of using `secondary_addrs` config, use the `transports` table which contains all the addresses.
1 parent dea1b41 commit c38d027

File tree

5 files changed

+41
-88
lines changed

5 files changed

+41
-88
lines changed

deltachat-rpc-client/tests/test_multitransport.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,19 @@ def test_transport_synchronization(acfactory, log) -> None:
201201

202202
assert ac1.wait_for_incoming_msg().get_snapshot().text == "Hello!"
203203
assert ac1_clone.wait_for_incoming_msg().get_snapshot().text == "Hello!"
204+
205+
206+
def test_recognize_self_address(acfactory) -> None:
207+
alice, bob = acfactory.get_online_accounts(2)
208+
209+
bob_chat = bob.create_chat(alice)
210+
211+
qr = acfactory.get_account_qr()
212+
alice.add_transport_from_qr(qr)
213+
214+
new_alice_addr = alice.list_transports()[1]["addr"]
215+
alice.set_config("configured_addr", new_alice_addr)
216+
217+
bob_chat.send_text("Hello!")
218+
msg = alice.wait_for_incoming_msg().get_snapshot()
219+
assert msg.chat == alice.create_chat(bob)

src/config.rs

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ use strum_macros::{AsRefStr, Display, EnumIter, EnumString};
1313
use tokio::fs;
1414

1515
use crate::blob::BlobObject;
16-
use crate::configure::EnteredLoginParam;
1716
use crate::context::Context;
1817
use crate::events::EventType;
1918
use crate::log::LogExt;
2019
use crate::mimefactory::RECOMMENDED_FILE_SIZE;
2120
use crate::provider::Provider;
2221
use crate::sync::{self, Sync::*, SyncData};
2322
use crate::tools::get_abs_path;
24-
use crate::transport::ConfiguredLoginParam;
23+
use crate::transport::{ConfiguredLoginParam, add_pseudo_transport};
2524
use crate::{constants, stats};
2625

2726
/// The available configuration keys.
@@ -204,7 +203,7 @@ pub enum Config {
204203
/// `ProviderOptions::delete_to_trash`.
205204
DeleteToTrash,
206205

207-
/// The primary email address. Also see `SecondaryAddrs`.
206+
/// The primary email address.
208207
ConfiguredAddr,
209208

210209
/// List of configured IMAP servers as a JSON array.
@@ -306,10 +305,6 @@ pub enum Config {
306305
/// Meant to help profile owner to differ between profiles with similar names.
307306
PrivateTag,
308307

309-
/// All secondary self addresses separated by spaces
310-
311-
SecondaryAddrs,
312-
313308
/// Read-only core version string.
314309
#[strum(serialize = "sys.version")]
315310
SysVersion,
@@ -819,16 +814,7 @@ impl Context {
819814
self,
820815
"Creating a pseudo configured account which will not be able to send or receive messages. Only meant for tests!"
821816
);
822-
self.sql
823-
.execute(
824-
"INSERT INTO transports (addr, entered_param, configured_param) VALUES (?, ?, ?)",
825-
(
826-
addr,
827-
serde_json::to_string(&EnteredLoginParam::default())?,
828-
format!(r#"{{"addr":"{addr}","imap":[],"imap_user":"","imap_password":"","smtp":[],"smtp_user":"","smtp_password":"","certificate_checks":"Automatic","oauth2":false}}"#)
829-
),
830-
)
831-
.await?;
817+
add_pseudo_transport(self, addr).await?;
832818
self.sql
833819
.set_raw_config(Config::ConfiguredAddr.as_ref(), Some(addr))
834820
.await?;
@@ -958,16 +944,6 @@ impl Context {
958944
pub(crate) async fn set_primary_self_addr(&self, primary_new: &str) -> Result<()> {
959945
self.quota.write().await.take();
960946

961-
// add old primary address (if exists) to secondary addresses
962-
let mut secondary_addrs = self.get_all_self_addrs().await?;
963-
// never store a primary address also as a secondary
964-
secondary_addrs.retain(|a| !addr_cmp(a, primary_new));
965-
self.set_config_internal(
966-
Config::SecondaryAddrs,
967-
Some(secondary_addrs.join(" ").as_str()),
968-
)
969-
.await?;
970-
971947
self.sql
972948
.set_raw_config(Config::ConfiguredAddr.as_ref(), Some(primary_new))
973949
.await?;
@@ -985,14 +961,10 @@ impl Context {
985961

986962
/// Returns all secondary self addresses.
987963
pub(crate) async fn get_secondary_self_addrs(&self) -> Result<Vec<String>> {
988-
let secondary_addrs = self
989-
.get_config(Config::SecondaryAddrs)
990-
.await?
991-
.unwrap_or_default();
992-
Ok(secondary_addrs
993-
.split_ascii_whitespace()
994-
.map(|s| s.to_string())
995-
.collect())
964+
self.sql.query_map_vec("SELECT addr FROM transports WHERE addr NOT IN (SELECT value FROM config WHERE keyname='configured_addr')", (), |row| {
965+
let addr: String = row.get(0)?;
966+
Ok(addr)
967+
}).await
996968
}
997969

998970
/// Returns the primary self address.

src/config/config_tests.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -94,59 +94,6 @@ async fn test_set_config_bool() -> Result<()> {
9494
Ok(())
9595
}
9696

97-
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
98-
async fn test_self_addrs() -> Result<()> {
99-
let alice = TestContext::new_alice().await;
100-
101-
assert!(alice.is_self_addr("[email protected]").await?);
102-
assert_eq!(alice.get_all_self_addrs().await?, vec!["[email protected]"]);
103-
assert!(!alice.is_self_addr("[email protected]").await?);
104-
105-
// Test adding the same primary address
106-
alice.set_primary_self_addr("[email protected]").await?;
107-
alice.set_primary_self_addr("[email protected]").await?;
108-
assert_eq!(alice.get_all_self_addrs().await?, vec!["[email protected]"]);
109-
110-
// Test adding a new (primary) self address
111-
// The address is trimmed during configure by `LoginParam::from_database()`,
112-
// so `set_primary_self_addr()` doesn't have to trim it.
113-
alice.set_primary_self_addr("[email protected]").await?;
114-
assert!(alice.is_self_addr("[email protected]").await?);
115-
assert!(alice.is_self_addr("[email protected]").await?);
116-
assert_eq!(
117-
alice.get_all_self_addrs().await?,
118-
119-
);
120-
121-
// Check that the entry is not duplicated
122-
alice.set_primary_self_addr("[email protected]").await?;
123-
alice.set_primary_self_addr("[email protected]").await?;
124-
assert_eq!(
125-
alice.get_all_self_addrs().await?,
126-
127-
);
128-
129-
// Test switching back
130-
alice.set_primary_self_addr("[email protected]").await?;
131-
assert_eq!(
132-
alice.get_all_self_addrs().await?,
133-
134-
);
135-
136-
// Test setting a new primary self address, the previous self address
137-
// should be kept as a secondary self address
138-
alice.set_primary_self_addr("[email protected]").await?;
139-
assert_eq!(
140-
alice.get_all_self_addrs().await?,
141-
142-
);
143-
assert!(alice.is_self_addr("[email protected]").await?);
144-
assert!(alice.is_self_addr("[email protected]").await?);
145-
assert!(alice.is_self_addr("[email protected]").await?);
146-
147-
Ok(())
148-
}
149-
15097
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
15198
async fn test_mdns_default_behaviour() -> Result<()> {
15299
let t = &TestContext::new_alice().await;

src/imap/imap_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use crate::test_utils::TestContext;
3+
use crate::transport::add_pseudo_transport;
34

45
#[test]
56
fn test_get_folder_meaning_by_name() {
@@ -271,12 +272,14 @@ async fn test_get_imap_search_command() -> Result<()> {
271272
r#"FROM "[email protected]""#
272273
);
273274

275+
add_pseudo_transport(&t, "[email protected]").await?;
274276
t.ctx.set_primary_self_addr("[email protected]").await?;
275277
assert_eq!(
276278
get_imap_self_sent_search_command(&t.ctx).await?,
277279
r#"OR (FROM "[email protected]") (FROM "[email protected]")"#
278280
);
279281

282+
add_pseudo_transport(&t, "[email protected]").await?;
280283
t.ctx.set_primary_self_addr("[email protected]").await?;
281284
assert_eq!(
282285
get_imap_self_sent_search_command(&t.ctx).await?,

src/transport.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,21 @@ pub(crate) async fn sync_transports(
756756
Ok(())
757757
}
758758

759+
/// Adds transport entry to the `transports` table with empty configuration.
760+
pub(crate) async fn add_pseudo_transport(context: &Context, addr: &str) -> Result<()> {
761+
context.sql
762+
.execute(
763+
"INSERT INTO transports (addr, entered_param, configured_param) VALUES (?, ?, ?)",
764+
(
765+
addr,
766+
serde_json::to_string(&EnteredLoginParam::default())?,
767+
format!(r#"{{"addr":"{addr}","imap":[],"imap_user":"","imap_password":"","smtp":[],"smtp_user":"","smtp_password":"","certificate_checks":"Automatic","oauth2":false}}"#)
768+
),
769+
)
770+
.await?;
771+
Ok(())
772+
}
773+
759774
#[cfg(test)]
760775
mod tests {
761776
use super::*;

0 commit comments

Comments
 (0)