chore(base): Move code inside the same module.

This patch puts tests at the end of the file.
This commit is contained in:
Ivan Enderlin
2025-05-21 10:54:46 +02:00
parent bcd75362f7
commit 48d2a1543c

View File

@@ -534,6 +534,110 @@ fn test_send_sync_for_room() {
assert_send_sync::<Room>();
}
/// The content of an `m.room.create` event, with a required `creator` field.
///
/// Starting with room version 11, the `creator` field should be removed and the
/// `sender` field of the event should be used instead. This is reflected on
/// [`RoomCreateEventContent`].
///
/// This type was created as an alternative for ease of use. When it is used in
/// the SDK, it is constructed by copying the `sender` of the original event as
/// the `creator`.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[ruma_event(type = "m.room.create", kind = State, state_key_type = EmptyStateKey, custom_redacted)]
pub struct RoomCreateWithCreatorEventContent {
/// The `user_id` of the room creator.
///
/// This is set by the homeserver.
///
/// While this should be optional since room version 11, we copy the sender
/// of the event so we can still access it.
pub creator: OwnedUserId,
/// Whether or not this room's data should be transferred to other
/// homeservers.
#[serde(
rename = "m.federate",
default = "ruma::serde::default_true",
skip_serializing_if = "ruma::serde::is_true"
)]
pub federate: bool,
/// The version of the room.
///
/// Defaults to `RoomVersionId::V1`.
#[serde(default = "default_create_room_version_id")]
pub room_version: RoomVersionId,
/// A reference to the room this room replaces, if the previous room was
/// upgraded.
#[serde(skip_serializing_if = "Option::is_none")]
pub predecessor: Option<PreviousRoom>,
/// The room type.
///
/// This is currently only used for spaces.
#[serde(skip_serializing_if = "Option::is_none", rename = "type")]
pub room_type: Option<RoomType>,
}
impl RoomCreateWithCreatorEventContent {
/// Constructs a `RoomCreateWithCreatorEventContent` with the given original
/// content and sender.
pub fn from_event_content(content: RoomCreateEventContent, sender: OwnedUserId) -> Self {
let RoomCreateEventContent { federate, room_version, predecessor, room_type, .. } = content;
Self { creator: sender, federate, room_version, predecessor, room_type }
}
fn into_event_content(self) -> (RoomCreateEventContent, OwnedUserId) {
let Self { creator, federate, room_version, predecessor, room_type } = self;
#[allow(deprecated)]
let content = assign!(RoomCreateEventContent::new_v11(), {
creator: Some(creator.clone()),
federate,
room_version,
predecessor,
room_type,
});
(content, creator)
}
}
/// Redacted form of [`RoomCreateWithCreatorEventContent`].
pub type RedactedRoomCreateWithCreatorEventContent = RoomCreateWithCreatorEventContent;
impl RedactedStateEventContent for RedactedRoomCreateWithCreatorEventContent {
type StateKey = EmptyStateKey;
}
impl RedactContent for RoomCreateWithCreatorEventContent {
type Redacted = RedactedRoomCreateWithCreatorEventContent;
fn redact(self, version: &RoomVersionId) -> Self::Redacted {
let (content, sender) = self.into_event_content();
// Use Ruma's redaction algorithm.
let content = content.redact(version);
Self::from_event_content(content, sender)
}
}
fn default_create_room_version_id() -> RoomVersionId {
RoomVersionId::V1
}
/// The possible sources of an account data type.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum AccountDataSource {
/// The source is account data with the stable prefix.
Stable,
/// The source is account data with the unstable prefix.
#[default]
Unstable,
}
#[cfg(test)]
mod tests {
use std::{
@@ -777,107 +881,3 @@ mod tests {
assert!(!room.has_active_room_call());
}
}
/// The content of an `m.room.create` event, with a required `creator` field.
///
/// Starting with room version 11, the `creator` field should be removed and the
/// `sender` field of the event should be used instead. This is reflected on
/// [`RoomCreateEventContent`].
///
/// This type was created as an alternative for ease of use. When it is used in
/// the SDK, it is constructed by copying the `sender` of the original event as
/// the `creator`.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[ruma_event(type = "m.room.create", kind = State, state_key_type = EmptyStateKey, custom_redacted)]
pub struct RoomCreateWithCreatorEventContent {
/// The `user_id` of the room creator.
///
/// This is set by the homeserver.
///
/// While this should be optional since room version 11, we copy the sender
/// of the event so we can still access it.
pub creator: OwnedUserId,
/// Whether or not this room's data should be transferred to other
/// homeservers.
#[serde(
rename = "m.federate",
default = "ruma::serde::default_true",
skip_serializing_if = "ruma::serde::is_true"
)]
pub federate: bool,
/// The version of the room.
///
/// Defaults to `RoomVersionId::V1`.
#[serde(default = "default_create_room_version_id")]
pub room_version: RoomVersionId,
/// A reference to the room this room replaces, if the previous room was
/// upgraded.
#[serde(skip_serializing_if = "Option::is_none")]
pub predecessor: Option<PreviousRoom>,
/// The room type.
///
/// This is currently only used for spaces.
#[serde(skip_serializing_if = "Option::is_none", rename = "type")]
pub room_type: Option<RoomType>,
}
impl RoomCreateWithCreatorEventContent {
/// Constructs a `RoomCreateWithCreatorEventContent` with the given original
/// content and sender.
pub fn from_event_content(content: RoomCreateEventContent, sender: OwnedUserId) -> Self {
let RoomCreateEventContent { federate, room_version, predecessor, room_type, .. } = content;
Self { creator: sender, federate, room_version, predecessor, room_type }
}
fn into_event_content(self) -> (RoomCreateEventContent, OwnedUserId) {
let Self { creator, federate, room_version, predecessor, room_type } = self;
#[allow(deprecated)]
let content = assign!(RoomCreateEventContent::new_v11(), {
creator: Some(creator.clone()),
federate,
room_version,
predecessor,
room_type,
});
(content, creator)
}
}
/// Redacted form of [`RoomCreateWithCreatorEventContent`].
pub type RedactedRoomCreateWithCreatorEventContent = RoomCreateWithCreatorEventContent;
impl RedactedStateEventContent for RedactedRoomCreateWithCreatorEventContent {
type StateKey = EmptyStateKey;
}
impl RedactContent for RoomCreateWithCreatorEventContent {
type Redacted = RedactedRoomCreateWithCreatorEventContent;
fn redact(self, version: &RoomVersionId) -> Self::Redacted {
let (content, sender) = self.into_event_content();
// Use Ruma's redaction algorithm.
let content = content.redact(version);
Self::from_event_content(content, sender)
}
}
fn default_create_room_version_id() -> RoomVersionId {
RoomVersionId::V1
}
/// The possible sources of an account data type.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum AccountDataSource {
/// The source is account data with the stable prefix.
Stable,
/// The source is account data with the unstable prefix.
#[default]
Unstable,
}