Skip to content

Commit abc4e12

Browse files
committed
Make basic multi-device work on joiner side, fix test_only_minimal_data_are_forwarded
1 parent 148d6c7 commit abc4e12

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

src/chat.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3758,7 +3758,10 @@ pub(crate) async fn create_broadcast_ex(
37583758

37593759
if sync.into() {
37603760
let id = SyncId::Grpid(grpid);
3761-
let action = SyncAction::CreateBroadcast { chat_name, secret };
3761+
let action = SyncAction::CreateOutBroadcast {
3762+
chat_name,
3763+
shared_secret: secret,
3764+
};
37623765
self::sync(context, id, action).await.log_err(context).ok();
37633766
}
37643767

@@ -3781,16 +3784,17 @@ pub(crate) async fn load_broadcast_shared_secret(
37813784
pub(crate) async fn save_broadcast_shared_secret(
37823785
context: &Context,
37833786
chat_id: ChatId,
3784-
shared_secret: &str,
3787+
secret: &str,
37853788
) -> Result<()> {
37863789
context
37873790
.sql
37883791
.execute(
37893792
"INSERT INTO broadcasts_shared_secrets (chat_id, secret) VALUES (?, ?)
37903793
ON CONFLICT(chat_id) DO UPDATE SET secret=excluded.chat_id",
3791-
(chat_id, shared_secret),
3794+
(chat_id, secret),
37923795
)
37933796
.await?;
3797+
37943798
Ok(())
37953799
}
37963800

@@ -4999,9 +5003,13 @@ pub(crate) enum SyncAction {
49995003
SetVisibility(ChatVisibility),
50005004
SetMuted(MuteDuration),
50015005
/// Create broadcast channel with the given name.
5002-
CreateBroadcast {
5006+
CreateOutBroadcast {
50035007
chat_name: String,
5004-
secret: String,
5008+
shared_secret: String,
5009+
},
5010+
CreateInBroadcast {
5011+
chat_name: String,
5012+
shared_secret: String,
50055013
},
50065014
Rename(String),
50075015
/// Set chat contacts by their addresses.
@@ -5065,7 +5073,11 @@ impl Context {
50655073
.id
50665074
}
50675075
SyncId::Grpid(grpid) => {
5068-
if let SyncAction::CreateBroadcast { chat_name, secret } = action {
5076+
if let SyncAction::CreateOutBroadcast {
5077+
chat_name,
5078+
shared_secret: secret,
5079+
} = action
5080+
{
50695081
create_broadcast_ex(
50705082
self,
50715083
Nosync,
@@ -5096,7 +5108,7 @@ impl Context {
50965108
SyncAction::Accept => chat_id.accept_ex(self, Nosync).await,
50975109
SyncAction::SetVisibility(v) => chat_id.set_visibility_ex(self, Nosync, *v).await,
50985110
SyncAction::SetMuted(duration) => set_muted_ex(self, Nosync, chat_id, *duration).await,
5099-
SyncAction::CreateBroadcast { .. } => {
5111+
SyncAction::CreateOutBroadcast { .. } | SyncAction::CreateInBroadcast { .. } => {
51005112
Err(anyhow!("sync_alter_chat({id:?}, {action:?}): Bad request."))
51015113
}
51025114
SyncAction::Rename(to) => rename_ex(self, Nosync, chat_id, to).await,

src/chat/chat_tests.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,8 @@ async fn test_only_minimal_data_are_forwarded() -> Result<()> {
22632263
let group_id = create_group_chat(&bob, ProtectionStatus::Unprotected, "group2").await?;
22642264
add_contact_to_chat(&bob, group_id, charlie_id).await?;
22652265
let broadcast_id = create_broadcast(&bob, "Channel".to_string()).await?;
2266-
add_contact_to_chat(&bob, broadcast_id, charlie_id).await?;
2266+
let qr = get_securejoin_qr(&bob, Some(broadcast_id)).await?;
2267+
tcm.exec_securejoin_qr(&charlie, &bob, &qr).await;
22672268
for chat_id in &[single_id, group_id, broadcast_id] {
22682269
forward_msgs(&bob, &[orig_msg.id], *chat_id).await?;
22692270
let sent_msg = bob.pop_sent_msg().await;
@@ -3027,8 +3028,9 @@ async fn test_leave_broadcast_multidevice() -> Result<()> {
30273028

30283029
tcm.section("Alice creates broadcast channel with Bob.");
30293030
let alice_chat_id = create_broadcast(alice, "foo".to_string()).await?;
3030-
let bob_contact = alice.add_or_lookup_contact(bob0).await.id;
3031-
add_contact_to_chat(alice, alice_chat_id, bob_contact).await?;
3031+
let qr = get_securejoin_qr(alice, Some(alice_chat_id)).await.unwrap();
3032+
tcm.exec_securejoin_qr(bob0, alice, &qr).await;
3033+
sync(bob0, bob1).await;
30323034

30333035
tcm.section("Alice sends first message to broadcast.");
30343036
let sent_msg = alice.send_text(alice_chat_id, "Hello!").await;

src/securejoin/bob.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::contact::Origin;
1212
use crate::context::Context;
1313
use crate::events::EventType;
1414
use crate::key::self_fingerprint;
15-
use crate::log::info;
15+
use crate::log::{LogExt as _, info};
1616
use crate::message::{Message, Viewtype};
1717
use crate::mimeparser::{MimeMessage, SystemMessage};
1818
use crate::param::Param;
@@ -58,13 +58,26 @@ pub(super) async fn start_protocol(context: &Context, invite: QrInvite) -> Resul
5858
ContactId::scaleup_origin(context, &[invite.contact_id()], Origin::SecurejoinJoined).await?;
5959
context.emit_event(EventType::ContactsChanged(None));
6060

61-
if let QrInvite::Broadcast { shared_secret, .. } = &invite {
61+
if let QrInvite::Broadcast {
62+
shared_secret,
63+
grpid,
64+
broadcast_name,
65+
..
66+
} = &invite
67+
{
6268
// TODO this causes some performance penalty because joining_chat_id is used again below,
6369
// but maybe it's fine
6470
let broadcast_chat_id = joining_chat_id(context, &invite, chat_id).await?;
65-
// TODO save the secret to the second device
71+
6672
save_broadcast_shared_secret(context, broadcast_chat_id, shared_secret).await?;
6773

74+
let id = chat::SyncId::Grpid(grpid.to_string());
75+
let action = chat::SyncAction::CreateInBroadcast {
76+
chat_name: broadcast_name.to_string(),
77+
shared_secret: shared_secret.to_string(),
78+
};
79+
chat::sync(context, id, action).await.log_err(context).ok();
80+
6881
if verify_sender_by_fingerprint(context, invite.fingerprint(), invite.contact_id()).await? {
6982
info!(context, "Using fast securejoin with symmetric encryption");
7083

0 commit comments

Comments
 (0)