Skip to content

Commit c5fc7b1

Browse files
committed
feat(crypto): Add GroupSessionManager::encrypt_state
Signed-off-by: Skye Elliot <[email protected]>
1 parent 5463a8a commit c5fc7b1

File tree

1 file changed

+46
-0
lines changed
  • crates/matrix-sdk-crypto/src/session_manager/group_sessions

1 file changed

+46
-0
lines changed

crates/matrix-sdk-crypto/src/session_manager/group_sessions/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use itertools::Itertools;
2727
use matrix_sdk_common::{
2828
deserialized_responses::WithheldCode, executor::spawn, locks::RwLock as StdRwLock,
2929
};
30+
#[cfg(feature = "experimental-encrypted-state-events")]
31+
use ruma::events::AnyStateEventContent;
3032
use ruma::{
3133
events::{AnyMessageLikeEventContent, AnyToDeviceEventContent, ToDeviceEventType},
3234
serde::Raw,
@@ -224,6 +226,50 @@ impl GroupSessionManager {
224226
Ok(content)
225227
}
226228

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+
227273
/// Create a new outbound group session.
228274
///
229275
/// This also creates a matching inbound group session.

0 commit comments

Comments
 (0)