Commit 11625262f1

11625262f1393666afd35d2f78e083779827662f

parent: 0ed1d53946

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-03-04 00:37 UTC

Direct message rooms should be encrypted from creation (#29)

Layout: unified · split

src/base.rs +18
@@ -131,6 +131,24 @@ pub enum MessageAction {
131131 Unreact(Option<String>),
132132}
133133
134#[derive(Clone, Debug, Eq, PartialEq)]
135pub enum CreateRoomType {
136 /// A direct message room.
137 Direct(OwnedUserId),
138}
139
140bitflags::bitflags! {
141 pub struct CreateRoomFlags: u32 {
142 const NONE = 0b00000000;
143
144 /// Make the room public.
145 const PUBLIC = 0b00000001;
146
147 /// Encrypt this room.
148 const ENCRYPTED = 0b00000010;
149 }
150}
151
134152bitflags::bitflags! {
135153 pub struct DownloadFlags: u32 {
136154 const NONE = 0b00000000;
src/windows/room/mod.rs +8
@@ -185,8 +185,16 @@ impl RoomState {
185185 match act {
186186 RoomAction::InviteAccept => {
187187 if let Some(room) = store.application.worker.client.get_invited_room(self.id()) {
188 let details = room.invite_details().await.map_err(IambError::from)?;
189 let details = details.invitee.event().original_content();
190 let is_direct = details.and_then(|ev| ev.is_direct).unwrap_or_default();
191
188192 room.accept_invitation().await.map_err(IambError::from)?;
189193
194 if is_direct {
195 room.set_is_direct(true).await.map_err(IambError::from)?;
196 }
197
190198 Ok(vec![])
191199 } else {
192200 Err(IambError::NotInvited.into())
src/worker.rs +84 −9
@@ -24,6 +24,7 @@ use matrix_sdk::{
2424 room::Visibility,
2525 space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
2626 },
27 assign,
2728 events::{
2829 key::verification::{
2930 done::{OriginalSyncKeyVerificationDoneEvent, ToDeviceKeyVerificationDoneEvent},
@@ -35,16 +36,22 @@ use matrix_sdk::{
3536 presence::PresenceEvent,
3637 reaction::ReactionEventContent,
3738 room::{
39 encryption::RoomEncryptionEventContent,
3840 message::{MessageType, RoomMessageEventContent},
3941 name::RoomNameEventContent,
4042 redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
4143 },
4244 tag::Tags,
4345 typing::SyncTypingEvent,
46 AnyInitialStateEvent,
4447 AnyTimelineEvent,
48 EmptyStateKey,
49 InitialStateEvent,
4550 SyncMessageLikeEvent,
4651 SyncStateEvent,
4752 },
53 serde::Raw,
54 EventEncryptionAlgorithm,
4855 OwnedRoomId,
4956 OwnedRoomOrAliasId,
5057 OwnedUserId,
@@ -58,7 +65,16 @@ use matrix_sdk::{
5865use modalkit::editing::action::{EditInfo, InfoMessage, UIError};
5966
6067use crate::{
61 base::{AsyncProgramStore, EventLocation, IambError, IambResult, Receipts, VerifyAction},
68 base::{
69 AsyncProgramStore,
70 CreateRoomFlags,
71 CreateRoomType,
72 EventLocation,
73 IambError,
74 IambResult,
75 Receipts,
76 VerifyAction,
77 },
6278 message::MessageFetchResult,
6379 ApplicationSettings,
6480};
@@ -71,6 +87,69 @@ fn initial_devname() -> String {
7187 format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy())
7288}
7389
90pub async fn create_room(
91 client: &Client,
92 room_alias_name: Option<&str>,
93 rt: CreateRoomType,
94 flags: CreateRoomFlags,
95) -> IambResult<OwnedRoomId> {
96 let creation_content = None;
97 let mut initial_state = vec![];
98 let is_direct;
99 let preset;
100 let mut invite = vec![];
101
102 let visibility = if flags.contains(CreateRoomFlags::PUBLIC) {
103 Visibility::Public
104 } else {
105 Visibility::Private
106 };
107
108 match rt {
109 CreateRoomType::Direct(user) => {
110 invite.push(user);
111 is_direct = true;
112 preset = Some(RoomPreset::TrustedPrivateChat);
113 },
114 }
115
116 // Set up encryption.
117 if flags.contains(CreateRoomFlags::ENCRYPTED) {
118 // XXX: Once matrix-sdk uses ruma 0.8, then this can skip the cast.
119 let algo = EventEncryptionAlgorithm::MegolmV1AesSha2;
120 let content = RoomEncryptionEventContent::new(algo);
121 let encr = InitialStateEvent { content, state_key: EmptyStateKey };
122 let encr_raw = Raw::new(&encr).map_err(IambError::from)?;
123 let encr_raw = encr_raw.cast::<AnyInitialStateEvent>();
124 initial_state.push(encr_raw);
125 }
126
127 let request = assign!(CreateRoomRequest::new(), {
128 room_alias_name,
129 creation_content,
130 initial_state: initial_state.as_slice(),
131 invite: invite.as_slice(),
132 is_direct,
133 visibility,
134 preset,
135 });
136
137 let resp = client.create_room(request).await.map_err(IambError::from)?;
138
139 if is_direct {
140 if let Some(room) = client.get_room(&resp.room_id) {
141 room.set_is_direct(true).await.map_err(IambError::from)?;
142 } else {
143 error!(
144 room_id = resp.room_id.as_str(),
145 "Couldn't set is_direct for new direct message room"
146 );
147 }
148 }
149
150 return Ok(resp.room_id);
151}
152
74153#[derive(Debug)]
75154pub enum LoginStyle {
76155 SessionRestore(Session),
@@ -788,15 +867,11 @@ impl ClientWorker {
788867 }
789868 }
790869
791 let mut request = CreateRoomRequest::new();
792 let invite = [user.clone()];
793 request.is_direct = true;
794 request.invite = &invite;
795 request.visibility = Visibility::Private;
796 request.preset = Some(RoomPreset::PrivateChat);
870 let rt = CreateRoomType::Direct(user.clone());
871 let flags = CreateRoomFlags::ENCRYPTED;
797872
798 match self.client.create_room(request).await {
799 Ok(resp) => self.get_room(resp.room_id).await,
873 match create_room(&self.client, None, rt, flags).await {
874 Ok(room_id) => self.get_room(room_id).await,
800875 Err(e) => {
801876 error!(
802877 user_id = user.as_str(),