mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-02-15 18:12:57 -05:00
test: Add a method to build m.room.member events in the EventFactory
This commit is contained in:
@@ -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,
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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,
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user