Skip to content

Commit 4e64f28

Browse files
toger5poljar
authored andcommitted
WidgetDriver: filter event_type change from dedicated ...EventType -> String
This is (sadly) required since we cannot do `as_str()` for `TimlineEventType` and other `ruma` event types. So we need to use `String`. We also never used them more specifically than strings.
1 parent 5e2f775 commit 4e64f28

File tree

5 files changed

+64
-75
lines changed

5 files changed

+64
-75
lines changed

crates/matrix-sdk/src/widget/filter.rs

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -197,34 +197,19 @@ mod tests {
197197
}
198198

199199
#[test]
200-
fn reaction_event_filter_matches_reaction() {
201-
assert!(reaction_event_filter().matches(&message_event(TimelineEventType::Reaction)));
200+
fn test_reaction_event_filter_matches_reaction() {
201+
assert!(reaction_event_filter()
202+
.matches(&message_event(&MessageLikeEventType::Reaction.to_string())));
202203
}
203204

204205
#[test]
205-
fn reaction_event_filter_does_not_match_room_message() {
206-
assert!(!reaction_event_filter().matches(&message_event_with_msgtype(
207-
TimelineEventType::RoomMessage,
208-
"m.text".to_owned()
209-
)));
210-
}
211-
212-
#[test]
213-
fn reaction_event_filter_does_not_match_state_event() {
214-
assert!(!reaction_event_filter().matches(&state_event(
215-
// Use the `m.reaction` event type to make sure the event would pass
216-
// the filter without state event checks, even though in practice
217-
// that event type won't be used for a state event.
218-
TimelineEventType::Reaction,
219-
"".to_owned()
220-
)));
206+
fn test_reaction_event_filter_does_not_match_room_message() {
207+
assert!(!reaction_event_filter().matches(&FilterInput::message_with_msgtype("m.text")));
221208
}
222209

223210
#[test]
224-
fn reaction_event_filter_does_not_match_state_event_any_key() {
225-
assert!(
226-
!reaction_event_filter().matches_state_event_with_any_state_key(&"m.reaction".into())
227-
);
211+
fn test_reaction_event_filter_does_not_match_state_event_any_key() {
212+
assert!(!reaction_event_filter().matches(&FilterInput::state("m.reaction", "")));
228213
}
229214

230215
// Tests against an `m.room.member` filter with `state_key = "@self:example.me"`
@@ -236,36 +221,37 @@ mod tests {
236221
}
237222

238223
#[test]
239-
fn self_member_event_filter_matches_self_member_event() {
240-
assert!(self_member_event_filter()
241-
.matches(&state_event(TimelineEventType::RoomMember, "@self:example.me".to_owned())));
224+
fn test_self_member_event_filter_matches_self_member_event() {
225+
assert!(self_member_event_filter().matches(&FilterInput::state(
226+
&TimelineEventType::RoomMember.to_string(),
227+
"@self:example.me"
228+
)));
242229
}
243230

244231
#[test]
245-
fn self_member_event_filter_does_not_match_somebody_elses_member_event() {
246-
assert!(!self_member_event_filter().matches(&state_event(
247-
TimelineEventType::RoomMember,
248-
"@somebody_else.example.me".to_owned()
232+
fn test_self_member_event_filter_does_not_match_somebody_elses_member_event() {
233+
assert!(!self_member_event_filter().matches(&FilterInput::state(
234+
&TimelineEventType::RoomMember.to_string(),
235+
"@somebody_else.example.me"
249236
)));
250237
}
251238

252239
#[test]
253240
fn self_member_event_filter_does_not_match_unrelated_state_event_with_same_state_key() {
254-
assert!(!self_member_event_filter().matches(&state_event(
255-
TimelineEventType::from("io.element.test_state_event"),
256-
"@self.example.me".to_owned()
257-
)));
241+
assert!(!self_member_event_filter()
242+
.matches(&FilterInput::state("io.element.test_state_event", "@self.example.me")));
258243
}
259244

260245
#[test]
261-
fn self_member_event_filter_does_not_match_reaction_event() {
262-
assert!(!self_member_event_filter().matches(&message_event(TimelineEventType::Reaction)));
246+
fn test_self_member_event_filter_does_not_match_reaction_event() {
247+
assert!(!self_member_event_filter()
248+
.matches(&message_event(&MessageLikeEventType::Reaction.to_string())));
263249
}
264250

265251
#[test]
266-
fn self_member_event_filter_only_matches_specific_state_key() {
252+
fn test_self_member_event_filter_only_matches_specific_state_key() {
267253
assert!(!self_member_event_filter()
268-
.matches_state_event_with_any_state_key(&StateEventType::RoomMember));
254+
.matches(&FilterInput::state(&StateEventType::RoomMember.to_string(), "")));
269255
}
270256

271257
// Tests against an `m.room.member` filter with any `state_key`.
@@ -274,26 +260,29 @@ mod tests {
274260
}
275261

276262
#[test]
277-
fn member_event_filter_matches_some_member_event() {
278-
assert!(member_event_filter()
279-
.matches(&state_event(TimelineEventType::RoomMember, "@foo.bar.baz".to_owned())));
263+
fn test_member_event_filter_matches_some_member_event() {
264+
assert!(member_event_filter().matches(&FilterInput::state(
265+
&TimelineEventType::RoomMember.to_string(),
266+
"@foo.bar.baz"
267+
)));
280268
}
281269

282270
#[test]
283-
fn member_event_filter_does_not_match_room_name_event() {
271+
fn test_member_event_filter_does_not_match_room_name_event() {
284272
assert!(!member_event_filter()
285-
.matches(&state_event(TimelineEventType::RoomName, "".to_owned())));
273+
.matches(&FilterInput::state(&TimelineEventType::RoomName.to_string(), "")));
286274
}
287275

288276
#[test]
289-
fn member_event_filter_does_not_match_reaction_event() {
290-
assert!(!member_event_filter().matches(&message_event(TimelineEventType::Reaction)));
277+
fn test_member_event_filter_does_not_match_reaction_event() {
278+
assert!(!member_event_filter()
279+
.matches(&message_event(&MessageLikeEventType::Reaction.to_string())));
291280
}
292281

293282
#[test]
294-
fn member_event_filter_matches_any_state_key() {
283+
fn test_member_event_filter_matches_any_state_key() {
295284
assert!(member_event_filter()
296-
.matches_state_event_with_any_state_key(&StateEventType::RoomMember));
285+
.matches(&FilterInput::state(&StateEventType::RoomMember.to_string(), "")));
297286
}
298287

299288
// Tests against an `m.room.topic` filter with `state_key = ""`
@@ -305,9 +294,9 @@ mod tests {
305294
}
306295

307296
#[test]
308-
fn topic_event_filter_does_not_match_any_state_key() {
309-
assert!(!topic_event_filter()
310-
.matches_state_event_with_any_state_key(&StateEventType::RoomTopic));
297+
fn test_topic_event_filter_does_match() {
298+
assert!(topic_event_filter()
299+
.matches(&FilterInput::state(&StateEventType::RoomTopic.to_string(), "")));
311300
}
312301

313302
// Tests against an `m.room.message` filter with `msgtype = m.custom`
@@ -321,33 +310,27 @@ mod tests {
321310
}
322311

323312
#[test]
324-
fn room_message_event_type_matches_room_message_text_event_filter() {
325-
assert!(room_message_text_event_filter()
326-
.matches_message_like_event_type(&MessageLikeEventType::RoomMessage));
327-
}
328-
329-
#[test]
330-
fn reaction_event_type_does_not_match_room_message_text_event_filter() {
313+
fn test_reaction_event_type_does_not_match_room_message_text_event_filter() {
331314
assert!(!room_message_text_event_filter()
332-
.matches_message_like_event_type(&MessageLikeEventType::Reaction));
315+
.matches(&FilterInput::message_like(&MessageLikeEventType::Reaction.to_string())));
333316
}
334317

335318
#[test]
336-
fn room_message_event_type_matches_room_message_custom_event_filter() {
337-
assert!(room_message_custom_event_filter()
338-
.matches_message_like_event_type(&MessageLikeEventType::RoomMessage));
319+
fn test_room_message_event_without_msgtype_does_not_match_custom_msgtype_filter() {
320+
assert!(!room_message_custom_event_filter()
321+
.matches(&FilterInput::message_like(&MessageLikeEventType::RoomMessage.to_string())));
339322
}
340323

341324
#[test]
342-
fn reaction_event_type_does_not_match_room_message_custom_event_filter() {
325+
fn test_reaction_event_type_does_not_match_room_message_custom_event_filter() {
343326
assert!(!room_message_custom_event_filter()
344-
.matches_message_like_event_type(&MessageLikeEventType::Reaction));
327+
.matches(&FilterInput::message_like(&MessageLikeEventType::Reaction.to_string())));
345328
}
346329

347330
#[test]
348-
fn room_message_event_type_matches_room_message_event_filter() {
331+
fn test_room_message_event_type_matches_room_message_event_filter() {
349332
assert!(room_message_filter()
350-
.matches_message_like_event_type(&MessageLikeEventType::RoomMessage));
333+
.matches(&FilterInput::message_like(&MessageLikeEventType::RoomMessage.to_string())));
351334
}
352335

353336
#[test]

crates/matrix-sdk/src/widget/machine/driver_req.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::marker::PhantomData;
1818

1919
use ruma::{
2020
api::client::{account::request_openid_token, delayed_events::update_delayed_event},
21-
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType, TimelineEventType},
21+
events::AnyTimelineEvent,
2222
serde::Raw,
2323
};
2424
use serde::Deserialize;
@@ -157,7 +157,9 @@ impl FromMatrixDriverResponse for request_openid_token::v3::Response {
157157
#[derive(Clone, Debug)]
158158
pub(crate) struct ReadMessageLikeEventRequest {
159159
/// The event type to read.
160-
pub(crate) event_type: MessageLikeEventType,
160+
// TODO: This wants to be `MessageLikeEventType`` but we need a type which supports `as_str()`
161+
// as soon as ruma supports `as_str()` on `MessageLikeEventType` we can use it here.
162+
pub(crate) event_type: String,
161163

162164
/// The maximum number of events to return.
163165
pub(crate) limit: u32,
@@ -190,7 +192,9 @@ impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
190192
#[derive(Clone, Debug)]
191193
pub(crate) struct ReadStateEventRequest {
192194
/// The event type to read.
193-
pub(crate) event_type: StateEventType,
195+
// TODO: This wants to be `TimelineEventType` but we need a type which supports `as_str()`
196+
// as soon as ruma supports `as_str()` on `TimelineEventType` we can use it here.
197+
pub(crate) event_type: String,
194198

195199
/// The `state_key` to read, or `Any` to receive any/all events of the given
196200
/// type, regardless of their `state_key`.
@@ -213,8 +217,10 @@ impl MatrixDriverRequest for ReadStateEventRequest {
213217
#[derive(Clone, Debug, Deserialize)]
214218
pub(crate) struct SendEventRequest {
215219
/// The type of the event.
220+
// TODO: This wants to be `TimelineEventType` but we need a type which supports `as_str()`
221+
// as soon as ruma supports `as_str()` on `TimelineEventType` we can use it here.
216222
#[serde(rename = "type")]
217-
pub(crate) event_type: TimelineEventType,
223+
pub(crate) event_type: String,
218224
/// State key of an event (if it's a state event).
219225
pub(crate) state_key: Option<String>,
220226
/// Raw content of an event.

crates/matrix-sdk/src/widget/machine/from_widget.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ruma::{
1818
delayed_events::{delayed_message_event, delayed_state_event, update_delayed_event},
1919
error::{ErrorBody, StandardErrorBody},
2020
},
21-
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType},
21+
events::AnyTimelineEvent,
2222
serde::Raw,
2323
OwnedEventId, OwnedRoomId,
2424
};
@@ -178,12 +178,12 @@ pub(super) enum ApiVersion {
178178
pub(super) enum ReadEventRequest {
179179
ReadStateEvent {
180180
#[serde(rename = "type")]
181-
event_type: StateEventType,
181+
event_type: String,
182182
state_key: StateKeySelector,
183183
},
184184
ReadMessageLikeEvent {
185185
#[serde(rename = "type")]
186-
event_type: MessageLikeEventType,
186+
event_type: String,
187187
limit: Option<u32>,
188188
},
189189
}

crates/matrix-sdk/src/widget/machine/tests/send_event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ fn parse_delayed_event_widget_action() {
3232
assert_let!(delay = send_event_request.delay.unwrap());
3333

3434
assert_eq!(delay, 10000);
35-
assert_eq!(send_event_request.event_type, TimelineEventType::CallMember);
35+
assert_eq!(send_event_request.event_type, TimelineEventType::CallMember.to_string());
3636
assert_eq!(send_event_request.state_key.unwrap(), "_@abc:example.org_VFKPEKYWMP".to_owned());
3737
}

crates/matrix-sdk/src/widget/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ impl WidgetDriver {
208208
}
209209

210210
MatrixDriverRequestData::ReadMessageLikeEvent(cmd) => matrix_driver
211-
.read_message_like_events(cmd.event_type.clone(), cmd.limit)
211+
.read_message_like_events(cmd.event_type.into(), cmd.limit)
212212
.await
213213
.map(MatrixDriverResponse::MatrixEventRead),
214214

215215
MatrixDriverRequestData::ReadStateEvent(cmd) => matrix_driver
216-
.read_state_events(cmd.event_type.clone(), &cmd.state_key)
216+
.read_state_events(cmd.event_type.into(), &cmd.state_key)
217217
.await
218218
.map(MatrixDriverResponse::MatrixEventRead),
219219

@@ -227,7 +227,7 @@ impl WidgetDriver {
227227
timeout: Duration::from_millis(d),
228228
});
229229
matrix_driver
230-
.send(event_type, state_key, content, delay_event_parameter)
230+
.send(event_type.into(), state_key, content, delay_event_parameter)
231231
.await
232232
.map(MatrixDriverResponse::MatrixEventSent)
233233
}

0 commit comments

Comments
 (0)