Skip to content

Commit 9ad7cf9

Browse files
zecakehpoljar
authored andcommitted
refactor(base): Use PossiblyRedactedStateEventContent bound in MinimalStateEvent
We usually don't care if the event was redacted or not, we usually want to no whether a field is set or not, so we don't need `Original` and `Redacted` variants. This simplifies several parts of the code since we don't have to handle the intermediate enum to access the content now. Due to new APIs in Ruma we can also just convert original and redacted event contents to possibly redacted event contents. Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
1 parent 8f156cb commit 9ad7cf9

File tree

13 files changed

+284
-421
lines changed

13 files changed

+284
-421
lines changed

crates/matrix-sdk-base/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ pub use store::{
6868
QueueWedgeError, StateChanges, StateStore, StateStoreDataKey, StateStoreDataValue, StoreError,
6969
ThreadSubscriptionCatchupToken,
7070
};
71-
pub use utils::{
72-
MinimalRoomMemberEvent, MinimalStateEvent, OriginalMinimalStateEvent,
73-
RawSyncStateEventWithKeys, RedactedMinimalStateEvent,
74-
};
71+
pub use utils::{MinimalRoomMemberEvent, MinimalStateEvent, RawSyncStateEventWithKeys};
7572

7673
#[cfg(test)]
7774
matrix_sdk_test_utils::init_tracing_for_tests!();

crates/matrix-sdk-base/src/response_processors/state_events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ pub fn is_tombstone_event_valid(
501501
.state_changes
502502
.room_infos
503503
.get(&successor_room_id)
504-
.and_then(|room_info| Some(room_info.tombstone()?.replacement_room.clone()))
504+
.and_then(|room_info| room_info.tombstone()?.replacement_room.clone())
505505
.or_else(|| {
506506
state_store
507507
.room(&successor_room_id)

crates/matrix-sdk-base/src/room/create.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use matrix_sdk_common::ROOM_VERSION_RULES_FALLBACK;
1616
use ruma::{
1717
OwnedUserId, RoomVersionId, assign,
1818
events::{
19-
EmptyStateKey, RedactContent, RedactedStateEventContent, StateEventType,
19+
EmptyStateKey, PossiblyRedactedStateEventContent, RedactContent, RedactedStateEventContent,
20+
StateEventContent, StateEventType, StaticEventContent,
2021
macros::EventContent,
2122
room::create::{PreviousRoom, RoomCreateEventContent},
2223
},
@@ -135,10 +136,10 @@ impl RoomCreateWithCreatorEventContent {
135136
pub type RedactedRoomCreateWithCreatorEventContent = RoomCreateWithCreatorEventContent;
136137

137138
impl RedactedStateEventContent for RedactedRoomCreateWithCreatorEventContent {
138-
type StateKey = EmptyStateKey;
139+
type StateKey = <RoomCreateWithCreatorEventContent as StateEventContent>::StateKey;
139140

140141
fn event_type(&self) -> StateEventType {
141-
StateEventType::RoomCreate
142+
RoomCreateWithCreatorEventContent::TYPE.into()
142143
}
143144
}
144145

@@ -156,3 +157,11 @@ impl RedactContent for RoomCreateWithCreatorEventContent {
156157
fn default_create_room_version_id() -> RoomVersionId {
157158
RoomVersionId::V1
158159
}
160+
161+
impl PossiblyRedactedStateEventContent for RoomCreateWithCreatorEventContent {
162+
type StateKey = <RoomCreateWithCreatorEventContent as StateEventContent>::StateKey;
163+
164+
fn event_type(&self) -> StateEventType {
165+
RoomCreateWithCreatorEventContent::TYPE.into()
166+
}
167+
}

crates/matrix-sdk-base/src/room/display_name.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,11 @@ mod tests {
544544
events::{
545545
StateEventType,
546546
room::{
547-
canonical_alias::RoomCanonicalAliasEventContent,
547+
canonical_alias::{
548+
PossiblyRedactedRoomCanonicalAliasEventContent, RoomCanonicalAliasEventContent,
549+
},
548550
member::{MembershipState, RoomMemberEventContent, StrippedRoomMemberEvent},
549-
name::RoomNameEventContent,
551+
name::{PossiblyRedactedRoomNameEventContent, RoomNameEventContent},
550552
},
551553
},
552554
room_alias_id, room_id,
@@ -557,8 +559,7 @@ mod tests {
557559

558560
use super::{Room, RoomDisplayName, compute_display_name_from_heroes};
559561
use crate::{
560-
MinimalStateEvent, OriginalMinimalStateEvent, RoomHero, RoomState, StateChanges,
561-
StateStore, store::MemoryStore,
562+
MinimalStateEvent, RoomHero, RoomState, StateChanges, StateStore, store::MemoryStore,
562563
};
563564

564565
fn make_room_test_helper(room_type: RoomState) -> (Arc<MemoryStore>, Room) {
@@ -584,22 +585,22 @@ mod tests {
584585
}
585586

586587
fn make_canonical_alias_event() -> MinimalStateEvent<RoomCanonicalAliasEventContent> {
587-
MinimalStateEvent::Original(OriginalMinimalStateEvent {
588-
content: assign!(RoomCanonicalAliasEventContent::new(), {
588+
MinimalStateEvent {
589+
content: assign!(PossiblyRedactedRoomCanonicalAliasEventContent::new(), {
589590
alias: Some(room_alias_id!("#test:example.com").to_owned()),
590591
}),
591592
event_id: None,
592-
})
593+
}
593594
}
594595

595-
fn make_name_event_with(name: &str) -> MinimalStateEvent<RoomNameEventContent> {
596-
MinimalStateEvent::Original(OriginalMinimalStateEvent {
597-
content: RoomNameEventContent::new(name.to_owned()),
596+
fn make_name_event_with(name: &str) -> MinimalStateEvent<PossiblyRedactedRoomNameEventContent> {
597+
MinimalStateEvent {
598+
content: RoomNameEventContent::new(name.to_owned()).into(),
598599
event_id: None,
599-
})
600+
}
600601
}
601602

602-
fn make_name_event() -> MinimalStateEvent<RoomNameEventContent> {
603+
fn make_name_event() -> MinimalStateEvent<PossiblyRedactedRoomNameEventContent> {
603604
make_name_event_with("Test Room")
604605
}
605606

@@ -697,10 +698,7 @@ mod tests {
697698
async fn test_display_name_for_invited_room_is_empty_if_room_name_empty() {
698699
let (_, room) = make_room_test_helper(RoomState::Invited);
699700

700-
let room_name = MinimalStateEvent::Original(OriginalMinimalStateEvent {
701-
content: RoomNameEventContent::new(String::new()),
702-
event_id: None,
703-
});
701+
let room_name = make_name_event_with("");
704702
room.info.update(|info| info.base_info.name = Some(room_name));
705703

706704
assert_eq!(room.compute_display_name().await.unwrap().into_inner(), RoomDisplayName::Empty);

crates/matrix-sdk-base/src/room/members.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl RoomMember {
259259
/// Get the display name of the member if there is one.
260260
pub fn display_name(&self) -> Option<&str> {
261261
if let Some(p) = self.profile.as_ref() {
262-
p.as_original().and_then(|e| e.content.displayname.as_deref())
262+
p.content.displayname.as_deref()
263263
} else {
264264
self.event.displayname_value()
265265
}
@@ -276,7 +276,7 @@ impl RoomMember {
276276
/// Get the avatar url of the member, if there is one.
277277
pub fn avatar_url(&self) -> Option<&MxcUri> {
278278
if let Some(p) = self.profile.as_ref() {
279-
p.as_original().and_then(|e| e.content.avatar_url.as_deref())
279+
p.content.avatar_url.as_deref()
280280
} else {
281281
self.event.avatar_url()
282282
}

crates/matrix-sdk-base/src/room/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub use tombstone::{PredecessorRoom, SuccessorRoom};
6767
use tracing::{info, instrument, warn};
6868

6969
use crate::{
70-
Error, MinimalStateEvent,
70+
Error,
7171
deserialized_responses::MemberEvent,
7272
notification_settings::RoomNotificationMode,
7373
read_receipts::RoomReadReceipts,
@@ -244,10 +244,7 @@ impl Room {
244244
/// redacted, all fields except `creator` will be set to their default
245245
/// value.
246246
pub fn create_content(&self) -> Option<RoomCreateWithCreatorEventContent> {
247-
match self.info.read().base_info.create.as_ref()? {
248-
MinimalStateEvent::Original(ev) => Some(ev.content.clone()),
249-
MinimalStateEvent::Redacted(ev) => Some(ev.content.clone()),
250-
}
247+
Some(self.info.read().base_info.create.as_ref()?.content.clone())
251248
}
252249

253250
/// Is this room considered a direct message.

0 commit comments

Comments
 (0)