12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
16
+ use std:: borrow:: Borrow ;
15
17
use std:: {
16
18
collections:: { BTreeMap , HashMap , HashSet } ,
17
19
sync:: Arc ,
@@ -31,6 +33,8 @@ use matrix_sdk_common::{
31
33
locks:: RwLock as StdRwLock ,
32
34
BoxFuture ,
33
35
} ;
36
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
37
+ use ruma:: events:: { AnyStateEventContent , StateEventContent } ;
34
38
use ruma:: {
35
39
api:: client:: {
36
40
dehydrated_device:: DehydratedDeviceData ,
@@ -1102,6 +1106,64 @@ impl OlmMachine {
1102
1106
self . inner . group_session_manager . encrypt ( room_id, event_type, content) . await
1103
1107
}
1104
1108
1109
+ /// Encrypt a state event for the given room.
1110
+ ///
1111
+ /// # Arguments
1112
+ ///
1113
+ /// * `room_id` - The id of the room for which the event should be
1114
+ /// encrypted.
1115
+ ///
1116
+ /// * `content` - The plaintext content of the event that should be
1117
+ /// encrypted.
1118
+ ///
1119
+ /// * `state_key` - The associated state key of the event.
1120
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
1121
+ pub async fn encrypt_state_event < C , K > (
1122
+ & self ,
1123
+ room_id : & RoomId ,
1124
+ content : C ,
1125
+ state_key : K ,
1126
+ ) -> MegolmResult < Raw < RoomEncryptedEventContent > >
1127
+ where
1128
+ C : StateEventContent ,
1129
+ C :: StateKey : Borrow < K > ,
1130
+ K : AsRef < str > ,
1131
+ {
1132
+ let event_type = content. event_type ( ) . to_string ( ) ;
1133
+ let content = Raw :: new ( & content) ?. cast_unchecked ( ) ;
1134
+ self . encrypt_state_event_raw ( room_id, & event_type, state_key. as_ref ( ) , & content) . await
1135
+ }
1136
+
1137
+ /// Encrypt a state event for the given state event using its raw JSON
1138
+ /// content and state key.
1139
+ ///
1140
+ /// This method is equivalent to [`OlmMachine::encrypt_state_event`]
1141
+ /// method but operates on an arbitrary JSON value instead of strongly-typed
1142
+ /// event content struct.
1143
+ ///
1144
+ /// # Arguments
1145
+ ///
1146
+ /// * `room_id` - The id of the room for which the message should be
1147
+ /// encrypted.
1148
+ ///
1149
+ /// * `content` - The plaintext content of the event that should be
1150
+ /// encrypted as a raw JSON value.
1151
+ ///
1152
+ /// * `state_key` - The associated state key of the event.
1153
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
1154
+ pub async fn encrypt_state_event_raw (
1155
+ & self ,
1156
+ room_id : & RoomId ,
1157
+ event_type : & str ,
1158
+ state_key : & str ,
1159
+ content : & Raw < AnyStateEventContent > ,
1160
+ ) -> MegolmResult < Raw < RoomEncryptedEventContent > > {
1161
+ self . inner
1162
+ . group_session_manager
1163
+ . encrypt_state ( room_id, event_type, state_key, content)
1164
+ . await
1165
+ }
1166
+
1105
1167
/// Forces the currently active room key, which is used to encrypt messages,
1106
1168
/// to be rotated.
1107
1169
///
0 commit comments