Skip to content

Commit 161b165

Browse files
committed
feat(crypto): Add state event encryption methods to OlmMachine
Signed-off-by: Skye Elliot <[email protected]>
1 parent 512e5ec commit 161b165

File tree

1 file changed

+62
-0
lines changed
  • crates/matrix-sdk-crypto/src/machine

1 file changed

+62
-0
lines changed

crates/matrix-sdk-crypto/src/machine/mod.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#[cfg(feature = "experimental-encrypted-state-events")]
16+
use std::borrow::Borrow;
1517
use std::{
1618
collections::{BTreeMap, HashMap, HashSet},
1719
sync::Arc,
@@ -31,6 +33,8 @@ use matrix_sdk_common::{
3133
locks::RwLock as StdRwLock,
3234
BoxFuture,
3335
};
36+
#[cfg(feature = "experimental-encrypted-state-events")]
37+
use ruma::events::{AnyStateEventContent, StateEventContent};
3438
use ruma::{
3539
api::client::{
3640
dehydrated_device::DehydratedDeviceData,
@@ -1102,6 +1106,64 @@ impl OlmMachine {
11021106
self.inner.group_session_manager.encrypt(room_id, event_type, content).await
11031107
}
11041108

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+
11051167
/// Forces the currently active room key, which is used to encrypt messages,
11061168
/// to be rotated.
11071169
///

0 commit comments

Comments
 (0)