Skip to content

Commit eadbf41

Browse files
committed
Fix multidevice
1 parent 70ab41d commit eadbf41

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed

src/chat.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3685,14 +3685,16 @@ pub async fn create_group_chat(
36853685
/// Returns the created chat's id.
36863686
pub async fn create_broadcast(context: &Context, chat_name: String) -> Result<ChatId> {
36873687
let grpid = create_id();
3688-
create_broadcast_ex(context, Sync, grpid, chat_name).await
3688+
let secret = create_broadcast_shared_secret();
3689+
create_broadcast_ex(context, Sync, grpid, chat_name, secret).await
36893690
}
36903691

36913692
pub(crate) async fn create_broadcast_ex(
36923693
context: &Context,
36933694
sync: sync::Sync,
36943695
grpid: String,
36953696
chat_name: String,
3697+
secret: String,
36963698
) -> Result<ChatId> {
36973699
let row_id = {
36983700
let chat_name = &chat_name;
@@ -3712,7 +3714,7 @@ pub(crate) async fn create_broadcast_ex(
37123714
}
37133715
let mut param = Params::new();
37143716
// param.set(Param::Unpromoted, 1); // TODO broadcasts will just never be unpromoted for now
3715-
param.set(Param::SymmetricKey, create_broadcast_shared_secret());
3717+
param.set(Param::SymmetricKey, &secret);
37163718
t.execute(
37173719
"INSERT INTO chats \
37183720
(type, name, grpid, param, created_timestamp) \
@@ -3725,6 +3727,13 @@ pub(crate) async fn create_broadcast_ex(
37253727
create_smeared_timestamp(context),
37263728
),
37273729
)?;
3730+
let chat_id = t.last_insert_rowid();
3731+
// TODO code duplication of `INSERT INTO broadcasts_shared_secrets`
3732+
t.execute(
3733+
"INSERT INTO broadcasts_shared_secrets (chat_id, secret) VALUES (?, ?)
3734+
ON CONFLICT(chat_id) DO UPDATE SET secret=excluded.chat_id",
3735+
(chat_id, &secret),
3736+
)?;
37283737
Ok(t.last_insert_rowid().try_into()?)
37293738
};
37303739
context.sql.transaction(trans_fn).await?
@@ -3736,7 +3745,7 @@ pub(crate) async fn create_broadcast_ex(
37363745

37373746
if sync.into() {
37383747
let id = SyncId::Grpid(grpid);
3739-
let action = SyncAction::CreateBroadcast(chat_name);
3748+
let action = SyncAction::CreateBroadcast { chat_name, secret };
37403749
self::sync(context, id, action).await.log_err(context).ok();
37413750
}
37423751

@@ -4952,7 +4961,10 @@ pub(crate) enum SyncAction {
49524961
SetVisibility(ChatVisibility),
49534962
SetMuted(MuteDuration),
49544963
/// Create broadcast channel with the given name.
4955-
CreateBroadcast(String),
4964+
CreateBroadcast {
4965+
chat_name: String,
4966+
secret: String,
4967+
},
49564968
Rename(String),
49574969
/// Set chat contacts by their addresses.
49584970
SetContacts(Vec<String>),
@@ -5015,8 +5027,15 @@ impl Context {
50155027
.id
50165028
}
50175029
SyncId::Grpid(grpid) => {
5018-
if let SyncAction::CreateBroadcast(name) = action {
5019-
create_broadcast_ex(self, Nosync, grpid.clone(), name.clone()).await?;
5030+
if let SyncAction::CreateBroadcast { chat_name, secret } = action {
5031+
create_broadcast_ex(
5032+
self,
5033+
Nosync,
5034+
grpid.clone(),
5035+
chat_name.clone(),
5036+
secret.to_string(),
5037+
)
5038+
.await?;
50205039
return Ok(());
50215040
}
50225041
get_chat_id_by_grpid(self, grpid)
@@ -5039,7 +5058,7 @@ impl Context {
50395058
SyncAction::Accept => chat_id.accept_ex(self, Nosync).await,
50405059
SyncAction::SetVisibility(v) => chat_id.set_visibility_ex(self, Nosync, *v).await,
50415060
SyncAction::SetMuted(duration) => set_muted_ex(self, Nosync, chat_id, *duration).await,
5042-
SyncAction::CreateBroadcast(_) => {
5061+
SyncAction::CreateBroadcast { .. } => {
50435062
Err(anyhow!("sync_alter_chat({id:?}, {action:?}): Bad request."))
50445063
}
50455064
SyncAction::Rename(to) => rename_ex(self, Nosync, chat_id, to).await,

src/chat/chat_tests.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3827,12 +3827,25 @@ async fn test_sync_name() -> Result<()> {
38273827
let a0_broadcast_id = create_broadcast(alice0, "Channel".to_string()).await?;
38283828
sync(alice0, alice1).await;
38293829
let a0_broadcast_chat = Chat::load_from_db(alice0, a0_broadcast_id).await?;
3830+
38303831
set_chat_name(alice0, a0_broadcast_id, "Broadcast channel 42").await?;
3831-
sync(alice0, alice1).await;
3832+
//sync(alice0, alice1).await; // crash
3833+
3834+
let sent = alice0.pop_sent_msg().await;
3835+
let rcvd = alice1.recv_msg(&sent).await;
3836+
assert_eq!(rcvd.from_id, ContactId::SELF);
3837+
assert_eq!(rcvd.to_id, ContactId::SELF);
3838+
assert_eq!(
3839+
rcvd.text,
3840+
"You changed group name from \"Channel\" to \"Broadcast channel 42\"."
3841+
);
3842+
assert_eq!(rcvd.param.get_cmd(), SystemMessage::GroupNameChanged);
38323843
let a1_broadcast_id = get_chat_id_by_grpid(alice1, &a0_broadcast_chat.grpid)
38333844
.await?
38343845
.unwrap()
38353846
.0;
3847+
assert_eq!(rcvd.chat_id, a1_broadcast_id);
3848+
38363849
let a1_broadcast_chat = Chat::load_from_db(alice1, a1_broadcast_id).await?;
38373850
assert_eq!(a1_broadcast_chat.get_type(), Chattype::OutBroadcast);
38383851
assert_eq!(a1_broadcast_chat.get_name(), "Broadcast channel 42");

src/param.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub enum Param {
174174
/// and for messages adding members to such a chat.
175175
/// The symmetric key shared among all chat participants,
176176
/// used to encrypt and decrypt messages.
177-
SymmetricKey = b'z',
177+
SymmetricKey = b'z', // TODO remove this again
178178

179179
/// For Contacts: If this is the List-Post address of a mailing list, contains
180180
/// the List-Id of the mailing list (which is also used as the group id of the chat).

src/receive_imf.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,10 +1540,25 @@ async fn do_chat_assignment(
15401540
if let Some((id, ..)) = chat::get_chat_id_by_grpid(context, &listid).await?
15411541
{
15421542
id
1543-
} else {
1543+
} else if let Some(secret) =
1544+
mime_parser.get_header(HeaderDef::ChatBroadcastSecret)
1545+
{
15441546
let name =
15451547
compute_mailinglist_name(mailinglist_header, &listid, mime_parser);
1546-
chat::create_broadcast_ex(context, Nosync, listid, name).await?
1548+
chat::create_broadcast_ex(
1549+
context,
1550+
Nosync,
1551+
listid,
1552+
name,
1553+
secret.to_string(),
1554+
)
1555+
.await?
1556+
} else {
1557+
warn!(
1558+
context,
1559+
"Unknown shared secret for outgoing broadcast (TRASH)"
1560+
);
1561+
DC_CHAT_ID_TRASH
15471562
},
15481563
);
15491564
}
@@ -3450,8 +3465,22 @@ async fn apply_out_broadcast_changes(
34503465
chat: &mut Chat,
34513466
from_id: ContactId,
34523467
) -> Result<GroupChangesInfo> {
3468+
// TODO code duplication with apply_in_broadcast_changes()
34533469
ensure!(chat.typ == Chattype::OutBroadcast);
34543470

3471+
let mut send_event_chat_modified = false;
3472+
let mut better_msg = None;
3473+
3474+
apply_chat_name_and_avatar_changes(
3475+
context,
3476+
mime_parser,
3477+
from_id,
3478+
chat,
3479+
&mut send_event_chat_modified,
3480+
&mut better_msg,
3481+
)
3482+
.await?;
3483+
34553484
if let Some(_removed_addr) = mime_parser.get_header(HeaderDef::ChatGroupMemberRemoved) {
34563485
// The sender of the message left the broadcast channel
34573486
remove_from_chat_contacts_table(context, chat.id, from_id).await?;
@@ -3464,7 +3493,16 @@ async fn apply_out_broadcast_changes(
34643493
});
34653494
}
34663495

3467-
Ok(GroupChangesInfo::default())
3496+
if send_event_chat_modified {
3497+
context.emit_event(EventType::ChatModified(chat.id));
3498+
chatlist_events::emit_chatlist_item_changed(context, chat.id);
3499+
}
3500+
Ok(GroupChangesInfo {
3501+
better_msg,
3502+
added_removed_id: None,
3503+
silent: false,
3504+
extra_msgs: vec![],
3505+
})
34683506
}
34693507

34703508
async fn apply_in_broadcast_changes(

0 commit comments

Comments
 (0)