Skip to content

Commit

Permalink
test: Add a method to build m.room.member events in the EventFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
poljar committed Nov 29, 2024
1 parent e1f0037 commit 62a93d3
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions testing/matrix-sdk-test/src/event_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use ruma::{
relation::{Annotation, InReplyTo, Replacement, Thread},
room::{
encrypted::{EncryptedEventScheme, RoomEncryptedEventContent},
member::{MembershipState, RoomMemberEventContent},
message::{
FormattedBody, ImageMessageEventContent, MessageType, Relation,
RoomMessageEventContent, RoomMessageEventContentWithoutRelation,
Expand Down Expand Up @@ -175,6 +176,15 @@ where
Raw::new(map).unwrap().cast()
}

/// Build an event from the [`EventBuilder`] and convert it into a
/// serialized and [`Raw`] event.
///
/// The generic argument `T` allows you to automatically cast the [`Raw`]
/// event into any desired type.
pub fn into_raw<T>(self) -> Raw<T> {
self.construct_json(true)
}

pub fn into_raw_timeline(self) -> Raw<AnyTimelineEvent> {
self.construct_json(true)
}
Expand Down Expand Up @@ -343,6 +353,49 @@ impl EventFactory {
self.event(RoomMessageEventContent::text_plain(content.into()))
}

/// Create a new `m.room.member` event for the given member.
///
/// The given member will be used as the `sender` as well as the `state_key`
/// of the `m.room.member` event, unless the `sender` was already using
/// [`EventFactory::sender()`], in that case only the state key will be
/// set to the given `member`.
///
/// The `membership` field of the content is set to
/// [`MembershipState::Join`].
///
/// ```
/// use matrix_sdk_test::event_factory::EventFactory;
/// use ruma::{
/// events::{
/// room::member::{MembershipState, RoomMemberEventContent},
/// SyncStateEvent,
/// },
/// room_id,
/// serde::Raw,
/// user_id,
/// };
///
/// let factory = EventFactory::new().room(room_id!("!test:localhost"));
///
/// let event: Raw<SyncStateEvent<RoomMemberEventContent>> = factory
/// .member(user_id!("@alice:localhost"))
/// .display_name("Alice")
/// .into_raw();
/// ```
pub fn member(&self, member: &UserId) -> EventBuilder<RoomMemberEventContent> {
let mut event = self.event(RoomMemberEventContent::new(MembershipState::Join));

if self.sender.is_some() {
event.sender = self.sender.clone();
} else {
event.sender = Some(member.to_owned());
}

event.state_key = Some(member.to_string());

event
}

/// Create a new plain/html `m.room.message`.
pub fn text_html(
&self,
Expand Down Expand Up @@ -463,3 +516,20 @@ impl EventFactory {
self.next_ts.store(value, SeqCst);
}
}

impl EventBuilder<RoomMemberEventContent> {
/// Set the `membership` of the `m.room.member` event to the given
/// [`MembershipState`].
///
/// The default is [`MembershipState::Join`].
pub fn membership(mut self, state: MembershipState) -> Self {
self.content.membership = state;
self
}

/// Set the display name of the `m.room.member` event.
pub fn display_name(mut self, display_name: impl Into<String>) -> Self {
self.content.displayname = Some(display_name.into());
self
}
}

0 comments on commit 62a93d3

Please sign in to comment.