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 {
131 Unreact(Option<String>), 131 Unreact(Option<String>),
132} 132}
133 133
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
134bitflags::bitflags! { 152bitflags::bitflags! {
135 pub struct DownloadFlags: u32 { 153 pub struct DownloadFlags: u32 {
136 const NONE = 0b00000000; 154 const NONE = 0b00000000;
src/windows/room/mod.rs +8
@@ -185,8 +185,16 @@ impl RoomState {
185 match act { 185 match act {
186 RoomAction::InviteAccept => { 186 RoomAction::InviteAccept => {
187 if let Some(room) = store.application.worker.client.get_invited_room(self.id()) { 187 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
188 room.accept_invitation().await.map_err(IambError::from)?; 192 room.accept_invitation().await.map_err(IambError::from)?;
189 193
194 if is_direct {
195 room.set_is_direct(true).await.map_err(IambError::from)?;
196 }
197
190 Ok(vec![]) 198 Ok(vec![])
191 } else { 199 } else {
192 Err(IambError::NotInvited.into()) 200 Err(IambError::NotInvited.into())
src/worker.rs +84 −9
@@ -24,6 +24,7 @@ use matrix_sdk::{
24 room::Visibility, 24 room::Visibility,
25 space::get_hierarchy::v1::Request as SpaceHierarchyRequest, 25 space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
26 }, 26 },
27 assign,
27 events::{ 28 events::{
28 key::verification::{ 29 key::verification::{
29 done::{OriginalSyncKeyVerificationDoneEvent, ToDeviceKeyVerificationDoneEvent}, 30 done::{OriginalSyncKeyVerificationDoneEvent, ToDeviceKeyVerificationDoneEvent},
@@ -35,16 +36,22 @@ use matrix_sdk::{
35 presence::PresenceEvent, 36 presence::PresenceEvent,
36 reaction::ReactionEventContent, 37 reaction::ReactionEventContent,
37 room::{ 38 room::{
39 encryption::RoomEncryptionEventContent,
38 message::{MessageType, RoomMessageEventContent}, 40 message::{MessageType, RoomMessageEventContent},
39 name::RoomNameEventContent, 41 name::RoomNameEventContent,
40 redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent}, 42 redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
41 }, 43 },
42 tag::Tags, 44 tag::Tags,
43 typing::SyncTypingEvent, 45 typing::SyncTypingEvent,
46 AnyInitialStateEvent,
44 AnyTimelineEvent, 47 AnyTimelineEvent,
48 EmptyStateKey,
49 InitialStateEvent,
45 SyncMessageLikeEvent, 50 SyncMessageLikeEvent,
46 SyncStateEvent, 51 SyncStateEvent,
47 }, 52 },
53 serde::Raw,
54 EventEncryptionAlgorithm,
48 OwnedRoomId, 55 OwnedRoomId,
49 OwnedRoomOrAliasId, 56 OwnedRoomOrAliasId,
50 OwnedUserId, 57 OwnedUserId,
@@ -58,7 +65,16 @@ use matrix_sdk::{
58use modalkit::editing::action::{EditInfo, InfoMessage, UIError}; 65use modalkit::editing::action::{EditInfo, InfoMessage, UIError};
59 66
60use crate::{ 67use 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 },
62 message::MessageFetchResult, 78 message::MessageFetchResult,
63 ApplicationSettings, 79 ApplicationSettings,
64}; 80};
@@ -71,6 +87,69 @@ fn initial_devname() -> String {
71 format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy()) 87 format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy())
72} 88}
73 89
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
74#[derive(Debug)] 153#[derive(Debug)]
75pub enum LoginStyle { 154pub enum LoginStyle {
76 SessionRestore(Session), 155 SessionRestore(Session),
@@ -788,15 +867,11 @@ impl ClientWorker {
788 } 867 }
789 } 868 }
790 869
791 let mut request = CreateRoomRequest::new(); 870 let rt = CreateRoomType::Direct(user.clone());
792 let invite = [user.clone()]; 871 let flags = CreateRoomFlags::ENCRYPTED;
793 request.is_direct = true;
794 request.invite = &invite;
795 request.visibility = Visibility::Private;
796 request.preset = Some(RoomPreset::PrivateChat);
797 872
798 match self.client.create_room(request).await { 873 match create_room(&self.client, None, rt, flags).await {
799 Ok(resp) => self.get_room(resp.room_id).await, 874 Ok(room_id) => self.get_room(room_id).await,
800 Err(e) => { 875 Err(e) => {
801 error!( 876 error!(
802 user_id = user.as_str(), 877 user_id = user.as_str(),