@@ -27,6 +27,8 @@ use itertools::Itertools;
27
27
use matrix_sdk_common:: {
28
28
deserialized_responses:: WithheldCode , executor:: spawn, locks:: RwLock as StdRwLock ,
29
29
} ;
30
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
31
+ use ruma:: events:: AnyStateEventContent ;
30
32
use ruma:: {
31
33
events:: { AnyMessageLikeEventContent , AnyToDeviceEventContent , ToDeviceEventType } ,
32
34
serde:: Raw ,
@@ -224,6 +226,50 @@ impl GroupSessionManager {
224
226
Ok ( content)
225
227
}
226
228
229
+ /// Encrypts a state event for the given room using its outbound group
230
+ /// session.
231
+ ///
232
+ /// # Arguments
233
+ ///
234
+ /// * `room_id` - The ID of the room where the state event will be sent.
235
+ /// * `event_type` - The type of the state event to encrypt.
236
+ /// * `state_key` - The state key associated with the event.
237
+ /// * `content` - The raw content of the state event to encrypt.
238
+ ///
239
+ /// # Returns
240
+ ///
241
+ /// Returns the raw encrypted state event content.
242
+ ///
243
+ /// # Errors
244
+ ///
245
+ /// Returns an error if saving changes to the store fails.
246
+ ///
247
+ /// # Panics
248
+ ///
249
+ /// Panics if no session exists for the given room ID, or the session
250
+ /// has expired.
251
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
252
+ pub async fn encrypt_state (
253
+ & self ,
254
+ room_id : & RoomId ,
255
+ event_type : & str ,
256
+ state_key : & str ,
257
+ content : & Raw < AnyStateEventContent > ,
258
+ ) -> MegolmResult < Raw < RoomEncryptedEventContent > > {
259
+ let session =
260
+ self . sessions . get_or_load ( room_id) . await . expect ( "Session wasn't created nor shared" ) ;
261
+
262
+ assert ! ( !session. expired( ) , "Session expired" ) ;
263
+
264
+ let content = session. encrypt_state ( event_type, state_key, content) . await ;
265
+
266
+ let mut changes = Changes :: default ( ) ;
267
+ changes. outbound_group_sessions . push ( session) ;
268
+ self . store . save_changes ( changes) . await ?;
269
+
270
+ Ok ( content)
271
+ }
272
+
227
273
/// Create a new outbound group session.
228
274
///
229
275
/// This also creates a matching inbound group session.
0 commit comments