diff --git a/testing/matrix-sdk-test/src/event_factory.rs b/testing/matrix-sdk-test/src/event_factory.rs index 4edba9b8a..ec5fc2fae 100644 --- a/testing/matrix-sdk-test/src/event_factory.rs +++ b/testing/matrix-sdk-test/src/event_factory.rs @@ -29,19 +29,21 @@ use ruma::{ OwnedRoomAliasId, OwnedRoomId, OwnedTransactionId, OwnedUserId, OwnedVoipId, RoomId, RoomVersionId, TransactionId, UInt, UserId, VoipVersionId, events::{ - AnyGlobalAccountDataEvent, AnyMessageLikeEvent, AnyStateEvent, AnyStrippedStateEvent, - AnySyncEphemeralRoomEvent, AnySyncMessageLikeEvent, AnySyncStateEvent, - AnySyncTimelineEvent, AnyTimelineEvent, BundledMessageLikeRelations, + AnyGlobalAccountDataEvent, AnyMessageLikeEvent, AnyRoomAccountDataEvent, AnyStateEvent, + AnyStrippedStateEvent, AnySyncEphemeralRoomEvent, AnySyncMessageLikeEvent, + AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent, BundledMessageLikeRelations, EphemeralRoomEventContent, EventContentFromType, False, GlobalAccountDataEventContent, Mentions, MessageLikeEvent, MessageLikeEventContent, PossiblyRedactedStateEventContent, - RedactContent, RedactedMessageLikeEventContent, RedactedStateEventContent, StateEvent, - StateEventContent, StaticEventContent, StaticStateEventContent, StrippedStateEvent, - SyncMessageLikeEvent, SyncStateEvent, + RedactContent, RedactedMessageLikeEventContent, RedactedStateEventContent, + RoomAccountDataEventContent, StateEvent, StateEventContent, StaticEventContent, + StaticStateEventContent, StrippedStateEvent, SyncMessageLikeEvent, SyncStateEvent, beacon::BeaconEventContent, call::{SessionDescription, invite::CallInviteEventContent}, direct::{DirectEventContent, OwnedDirectUserIdentifier}, + fully_read::FullyReadEventContent, ignored_user_list::IgnoredUserListEventContent, macros::EventContent, + marked_unread::MarkedUnreadEventContent, member_hints::MemberHintsEventContent, poll::{ unstable_end::UnstablePollEndEventContent, @@ -180,6 +182,8 @@ enum EventFormat { Ephemeral, /// A global account data. GlobalAccountData, + /// A room account data. + RoomAccountData, } impl EventFormat { @@ -615,6 +619,24 @@ where } } +impl> From> for Raw +where + E: Serialize, +{ + fn from(val: EventBuilder) -> Self { + val.format(EventFormat::RoomAccountData).into_raw() + } +} + +impl> From> for AnyRoomAccountDataEvent +where + E: Serialize, +{ + fn from(val: EventBuilder) -> Self { + Raw::::from(val).deserialize().expect("expected room account data") + } +} + impl> From> for TimelineEvent where E: Serialize, @@ -1466,6 +1488,24 @@ impl EventFactory { { self.event(content).format(EventFormat::GlobalAccountData) } + + /// Create a new room account data event of the given `C` content type. + pub fn room_account_data(&self, content: C) -> EventBuilder + where + C: RoomAccountDataEventContent + StaticEventContent, + { + self.event(content).format(EventFormat::RoomAccountData) + } + + /// Create a new `m.fully_read` room account data event. + pub fn fully_read(&self, event_id: &EventId) -> EventBuilder { + self.room_account_data(FullyReadEventContent::new(event_id.to_owned())) + } + + /// Create a new `m.marked_unread` room account data event. + pub fn marked_unread(&self, unread: bool) -> EventBuilder { + self.room_account_data(MarkedUnreadEventContent::new(unread)) + } } impl EventBuilder { diff --git a/testing/matrix-sdk-test/src/sync_builder/test_event.rs b/testing/matrix-sdk-test/src/sync_builder/test_event.rs index 8ea941609..4f4a3113f 100644 --- a/testing/matrix-sdk-test/src/sync_builder/test_event.rs +++ b/testing/matrix-sdk-test/src/sync_builder/test_event.rs @@ -1,7 +1,7 @@ -use ruma::{events::AnyRoomAccountDataEvent, serde::Raw}; -use serde_json::{Value as JsonValue, from_value as from_json_value}; +use ruma::{event_id, events::AnyRoomAccountDataEvent, serde::Raw}; +use serde_json::Value as JsonValue; -use crate::test_json; +use crate::event_factory::EventFactory; /// Test events that can be added to the room account data. pub enum RoomAccountDataTestEvent { @@ -10,20 +10,17 @@ pub enum RoomAccountDataTestEvent { Custom(JsonValue), } -impl From for JsonValue { +impl From for Raw { fn from(val: RoomAccountDataTestEvent) -> Self { + let f = EventFactory::new(); match val { - RoomAccountDataTestEvent::FullyRead => test_json::sync_events::FULLY_READ.to_owned(), - RoomAccountDataTestEvent::MarkedUnread => { - test_json::sync_events::MARKED_UNREAD.to_owned() + RoomAccountDataTestEvent::FullyRead => { + f.fully_read(event_id!("$someplace:example.org")).into() + } + RoomAccountDataTestEvent::MarkedUnread => f.marked_unread(true).into(), + RoomAccountDataTestEvent::Custom(json) => { + serde_json::from_value(json).expect("Custom JSON should be valid") } - RoomAccountDataTestEvent::Custom(json) => json, } } } - -impl From for Raw { - fn from(val: RoomAccountDataTestEvent) -> Self { - from_json_value(val.into()).unwrap() - } -} diff --git a/testing/matrix-sdk-test/src/test_json/sync_events.rs b/testing/matrix-sdk-test/src/test_json/sync_events.rs index 46b80d717..9097d81ab 100644 --- a/testing/matrix-sdk-test/src/test_json/sync_events.rs +++ b/testing/matrix-sdk-test/src/test_json/sync_events.rs @@ -3,16 +3,6 @@ use once_cell::sync::Lazy; use serde_json::{Value as JsonValue, json}; -pub static FULLY_READ: Lazy = Lazy::new(|| { - json!({ - "content": { - "event_id": "$someplace:example.org" - }, - "room_id": "!somewhere:example.org", - "type": "m.fully_read" - }) -}); - // TODO: Move `prev_content` into `unsigned` once ruma supports it pub static MEMBER: Lazy = Lazy::new(|| { json!({ @@ -198,12 +188,3 @@ pub static TAG: Lazy = Lazy::new(|| { "type": "m.tag" }) }); - -pub static MARKED_UNREAD: Lazy = Lazy::new(|| { - json!({ - "content": { - "unread": true, - }, - "type": "m.marked_unread", - }) -});