Skip to content

Commit 148d6c7

Browse files
committed
fix: make test_broadcast work, return an error when trying to add manually add a contact to a broadcast list, don't have unpromoted broadcast lists, make basic multi-device, inviter side, work
1 parent a3a78e8 commit 148d6c7

File tree

3 files changed

+72
-41
lines changed

3 files changed

+72
-41
lines changed

src/chat.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,8 +3731,8 @@ pub(crate) async fn create_broadcast_ex(
37313731
}
37323732
t.execute(
37333733
"INSERT INTO chats \
3734-
(type, name, grpid, param, created_timestamp) \
3735-
VALUES(?, ?, ?, \'U=1\', ?);",
3734+
(type, name, grpid, created_timestamp) \
3735+
VALUES(?, ?, ?, ?);",
37363736
(
37373737
Chattype::OutBroadcast,
37383738
&chat_name,
@@ -3908,8 +3908,8 @@ pub(crate) async fn add_contact_to_chat_ex(
39083908
// this also makes sure, no contacts are added to special or normal chats
39093909
let mut chat = Chat::load_from_db(context, chat_id).await?;
39103910
ensure!(
3911-
chat.typ == Chattype::Group || chat.typ == Chattype::OutBroadcast,
3912-
"{} is not a group/broadcast where one can add members",
3911+
chat.typ == Chattype::Group,
3912+
"{} is not a group where one can add members",
39133913
chat_id
39143914
);
39153915
ensure!(
@@ -3918,10 +3918,6 @@ pub(crate) async fn add_contact_to_chat_ex(
39183918
contact_id
39193919
);
39203920
ensure!(!chat.is_mailing_list(), "Mailing lists can't be changed");
3921-
ensure!(
3922-
chat.typ != Chattype::OutBroadcast || contact_id != ContactId::SELF,
3923-
"Cannot add SELF to broadcast channel."
3924-
);
39253921
ensure!(
39263922
chat.is_encrypted(context).await? == contact.is_key_contact(),
39273923
"Only key-contacts can be added to encrypted chats"
@@ -3973,7 +3969,7 @@ pub(crate) async fn add_contact_to_chat_ex(
39733969
}
39743970
add_to_chat_contacts_table(context, time(), chat_id, &[contact_id]).await?;
39753971
}
3976-
if chat.typ == Chattype::Group && chat.is_promoted() {
3972+
if chat.is_promoted() {
39773973
msg.viewtype = Viewtype::Text;
39783974

39793975
let contact_addr = contact.get_addr().to_lowercase();

src/chat/chat_tests.rs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,44 +2607,67 @@ async fn test_can_send_group() -> Result<()> {
26072607
}
26082608

26092609
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2610-
async fn test_broadcast() -> Result<()> {
2610+
async fn test_broadcast_change_name() -> Result<()> {
26112611
// create two context, send two messages so both know the other
2612-
let alice = TestContext::new_alice().await;
2613-
let bob = TestContext::new_bob().await;
2614-
let fiona = TestContext::new_fiona().await;
2612+
let mut tcm = TestContextManager::new();
2613+
let alice = &tcm.alice().await;
2614+
let bob = &tcm.bob().await;
2615+
let fiona = &tcm.fiona().await;
26152616

2617+
tcm.section("Alice sends a message to Bob");
26162618
let chat_alice = alice.create_chat(&bob).await;
26172619
send_text_msg(&alice, chat_alice.id, "hi!".to_string()).await?;
26182620
bob.recv_msg(&alice.pop_sent_msg().await).await;
26192621

2622+
tcm.section("Bob sends a message to Alice");
26202623
let chat_bob = bob.create_chat(&alice).await;
26212624
send_text_msg(&bob, chat_bob.id, "ho!".to_string()).await?;
26222625
let msg = alice.recv_msg(&bob.pop_sent_msg().await).await;
26232626
assert!(msg.get_showpadlock());
26242627

2625-
// test broadcast channel
26262628
let broadcast_id = create_broadcast(&alice, "Channel".to_string()).await?;
2627-
add_contact_to_chat(
2628-
&alice,
2629-
broadcast_id,
2630-
get_chat_contacts(&alice, chat_bob.id).await?.pop().unwrap(),
2631-
)
2632-
.await?;
2633-
let fiona_contact_id = alice.add_or_lookup_contact_id(&fiona).await;
2634-
add_contact_to_chat(&alice, broadcast_id, fiona_contact_id).await?;
2635-
set_chat_name(&alice, broadcast_id, "Broadcast channel").await?;
2629+
let qr = get_securejoin_qr(alice, Some(broadcast_id)).await.unwrap();
2630+
2631+
tcm.section("Alice invites Bob to her channel");
2632+
tcm.exec_securejoin_qr(bob, alice, &qr).await;
2633+
tcm.section("Alice invites Fiona to her channel");
2634+
tcm.exec_securejoin_qr(fiona, alice, &qr).await;
2635+
26362636
{
2637+
tcm.section("Alice changes the chat name");
2638+
set_chat_name(&alice, broadcast_id, "My great broadcast").await?;
2639+
let sent = alice.pop_sent_msg().await;
2640+
2641+
tcm.section("Bob receives the name-change system message");
2642+
let msg = bob.recv_msg(&sent).await;
2643+
assert_eq!(msg.subject, "Re: My great broadcast");
2644+
let bob_chat = Chat::load_from_db(bob, msg.chat_id).await?;
2645+
assert_eq!(bob_chat.name, "My great broadcast");
2646+
2647+
tcm.section("Fiona receives the name-change system message");
2648+
let msg = fiona.recv_msg(&sent).await;
2649+
assert_eq!(msg.subject, "Re: My great broadcast");
2650+
let fiona_chat = Chat::load_from_db(fiona, msg.chat_id).await?;
2651+
assert_eq!(fiona_chat.name, "My great broadcast");
2652+
}
2653+
2654+
{
2655+
tcm.section("Alice changes the chat name again, but the system message is lost somehow");
2656+
set_chat_name(&alice, broadcast_id, "Broadcast channel").await?;
2657+
26372658
let chat = Chat::load_from_db(&alice, broadcast_id).await?;
26382659
assert_eq!(chat.typ, Chattype::OutBroadcast);
26392660
assert_eq!(chat.name, "Broadcast channel");
26402661
assert!(!chat.is_self_talk());
26412662

2663+
tcm.section("Alice sends a text message 'ola!'");
26422664
send_text_msg(&alice, broadcast_id, "ola!".to_string()).await?;
26432665
let msg = alice.get_last_msg().await;
26442666
assert_eq!(msg.chat_id, chat.id);
26452667
}
26462668

26472669
{
2670+
tcm.section("Bob receives the 'ola!' message");
26482671
let sent_msg = alice.pop_sent_msg().await;
26492672
let msg = bob.parse_msg(&sent_msg).await;
26502673
assert!(msg.was_encrypted());
@@ -2657,25 +2680,23 @@ async fn test_broadcast() -> Result<()> {
26572680

26582681
let msg = bob.recv_msg(&sent_msg).await;
26592682
assert_eq!(msg.get_text(), "ola!");
2660-
assert_eq!(msg.subject, "Broadcast channel");
2683+
assert_eq!(msg.subject, "Re: Broadcast channel");
26612684
assert!(msg.get_showpadlock());
26622685
assert!(msg.get_override_sender_name().is_none());
26632686
let chat = Chat::load_from_db(&bob, msg.chat_id).await?;
26642687
assert_eq!(chat.typ, Chattype::InBroadcast);
26652688
assert_ne!(chat.id, chat_bob.id);
26662689
assert_eq!(chat.name, "Broadcast channel");
26672690
assert!(!chat.is_self_talk());
2668-
}
2669-
2670-
{
2671-
// Alice changes the name:
2672-
set_chat_name(&alice, broadcast_id, "My great broadcast").await?;
2673-
let sent = alice.send_text(broadcast_id, "I changed the title!").await;
26742691

2675-
let msg = bob.recv_msg(&sent).await;
2676-
assert_eq!(msg.subject, "Re: My great broadcast");
2677-
let bob_chat = Chat::load_from_db(&bob, msg.chat_id).await?;
2678-
assert_eq!(bob_chat.name, "My great broadcast");
2692+
tcm.section("Fiona receives the 'ola!' message");
2693+
let msg = fiona.recv_msg(&sent_msg).await;
2694+
assert_eq!(msg.get_text(), "ola!");
2695+
assert!(msg.get_showpadlock());
2696+
assert!(msg.get_override_sender_name().is_none());
2697+
let chat = Chat::load_from_db(fiona, msg.chat_id).await?;
2698+
assert_eq!(chat.typ, Chattype::InBroadcast);
2699+
assert_eq!(chat.name, "Broadcast channel");
26792700
}
26802701

26812702
Ok(())

src/securejoin.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use deltachat_contact_tools::ContactAddress;
55
use percent_encoding::{NON_ALPHANUMERIC, utf8_percent_encode};
66

77
use crate::chat::{
8-
self, Chat, ChatId, ChatIdBlocked, ProtectionStatus, get_chat_id_by_grpid,
9-
load_broadcast_shared_secret,
8+
self, Chat, ChatId, ChatIdBlocked, ProtectionStatus, add_to_chat_contacts_table,
9+
get_chat_id_by_grpid, load_broadcast_shared_secret,
1010
};
1111
use crate::chatlist_events;
1212
use crate::config::Config;
@@ -26,6 +26,7 @@ use crate::qr::check_qr;
2626
use crate::securejoin::bob::JoinerProgress;
2727
use crate::sync::Sync::*;
2828
use crate::token;
29+
use crate::tools::time;
2930

3031
mod bob;
3132
mod qrinvite;
@@ -434,13 +435,26 @@ pub(crate) async fn handle_securejoin_handshake(
434435
mime_message.timestamp_sent,
435436
)
436437
.await?;
437-
chat::add_contact_to_chat_ex(context, Nosync, group_chat_id, contact_id, true)
438-
.await?;
438+
if step.starts_with("vb-") {
439+
// TODO extract into variable
440+
add_to_chat_contacts_table(context, time(), group_chat_id, &[contact_id])
441+
.await?;
442+
} else {
443+
chat::add_contact_to_chat_ex(context, Nosync, group_chat_id, contact_id, true)
444+
.await?;
445+
}
439446
inviter_progress(context, contact_id, 800);
440447
inviter_progress(context, contact_id, 1000);
441-
// IMAP-delete the message to avoid handling it by another device and adding the
442-
// member twice. Another device will know the member's key from Autocrypt-Gossip.
443-
Ok(HandshakeMessage::Done)
448+
if step.starts_with("vb-") {
449+
// For broadcasts, we don't want to delete the message,
450+
// because the other device should also internally add the member
451+
// and see the key (because it won't see the member via autocrypt-gossip).
452+
Ok(HandshakeMessage::Propagate)
453+
} else {
454+
// IMAP-delete the message to avoid handling it by another device and adding the
455+
// member twice. Another device will know the member's key from Autocrypt-Gossip.
456+
Ok(HandshakeMessage::Done)
457+
}
444458
} else {
445459
// Setup verified contact.
446460
secure_connection_established(

0 commit comments

Comments
 (0)