Skip to content

Commit a804c3d

Browse files
committed
refactor(tests): Make EventBuilders cloneable
By creating an event ID immediately, so that a cloned EventBuilder will receive the same event ID.
1 parent abc4f35 commit a804c3d

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

testing/matrix-sdk-test/src/event_factory.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl TimestampArg for u64 {
7878
}
7979

8080
/// A thin copy of [`ruma::events::UnsignedRoomRedactionEvent`].
81-
#[derive(Debug, Serialize)]
81+
#[derive(Clone, Debug, Serialize)]
8282
struct RedactedBecause {
8383
/// Data specific to the event type.
8484
content: RoomRedactionEventContent,
@@ -94,7 +94,7 @@ struct RedactedBecause {
9494
origin_server_ts: MilliSecondsSinceUnixEpoch,
9595
}
9696

97-
#[derive(Debug, Serialize)]
97+
#[derive(Clone, Debug, Serialize)]
9898
struct Unsigned<C: EventContent> {
9999
#[serde(skip_serializing_if = "Option::is_none")]
100100
prev_content: Option<C>,
@@ -116,16 +116,14 @@ impl<C: EventContent> Default for Unsigned<C> {
116116
}
117117
}
118118

119-
#[derive(Debug)]
119+
#[derive(Clone, Debug)]
120120
pub struct EventBuilder<C: EventContent> {
121121
sender: Option<OwnedUserId>,
122122
/// Whether the event is an ephemeral one. As such, it doesn't require a
123123
/// room id or a sender.
124124
is_ephemeral: bool,
125125
room: Option<OwnedRoomId>,
126126
event_id: Option<OwnedEventId>,
127-
/// Whether the event should *not* have an event id. False by default.
128-
no_event_id: bool,
129127
redacts: Option<OwnedEventId>,
130128
content: C,
131129
server_ts: MilliSecondsSinceUnixEpoch,
@@ -149,13 +147,11 @@ where
149147

150148
pub fn event_id(mut self, event_id: &EventId) -> Self {
151149
self.event_id = Some(event_id.to_owned());
152-
self.no_event_id = false;
153150
self
154151
}
155152

156153
pub fn no_event_id(mut self) -> Self {
157154
self.event_id = None;
158-
self.no_event_id = true;
159155
self
160156
}
161157

@@ -226,13 +222,7 @@ where
226222
map.insert("sender".to_owned(), json!(sender));
227223
}
228224

229-
let event_id = self
230-
.event_id
231-
.or_else(|| {
232-
self.room.as_ref().map(|room_id| EventId::new(room_id.server_name().unwrap()))
233-
})
234-
.or_else(|| (!self.no_event_id).then(|| EventId::new(server_name!("dummy.org"))));
235-
if let Some(event_id) = event_id {
225+
if let Some(event_id) = self.event_id {
236226
map.insert("event_id".to_owned(), json!(event_id));
237227
}
238228

@@ -420,8 +410,10 @@ impl EventFactory {
420410
is_ephemeral: false,
421411
room: self.room.clone(),
422412
server_ts: self.next_server_ts(),
423-
event_id: None,
424-
no_event_id: false,
413+
event_id: Some(match &self.room {
414+
Some(room_id) => EventId::new(room_id.server_name().unwrap()),
415+
None => EventId::new(server_name!("dummy.org")),
416+
}),
425417
redacts: None,
426418
content,
427419
unsigned: None,

0 commit comments

Comments
 (0)