Skip to content

Commit

Permalink
refactor(widget): get rid of unused limits parameter when construct…
Browse files Browse the repository at this point in the history
…ing a `WidgetMachine`
  • Loading branch information
bnjbvr committed Nov 14, 2024
1 parent 21b4c94 commit 4130157
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 52 deletions.
7 changes: 2 additions & 5 deletions crates/matrix-sdk/src/widget/machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,9 @@ impl WidgetMachine {
widget_id: String,
room_id: OwnedRoomId,
init_on_content_load: bool,
limits: Option<RequestLimits>,
) -> (Self, Vec<Action>) {
let limits = limits.unwrap_or_else(|| RequestLimits {
max_pending_requests: 15,
response_timeout: Duration::from_secs(10),
});
let limits =
RequestLimits { max_pending_requests: 15, response_timeout: Duration::from_secs(10) };

let mut machine = Self {
widget_id,
Expand Down
10 changes: 3 additions & 7 deletions crates/matrix-sdk/src/widget/machine/tests/api_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ use super::WIDGET_ID;
use crate::widget::machine::{Action, IncomingMessage, WidgetMachine};

#[test]
fn get_supported_api_versions() {
let (mut machine, _) = WidgetMachine::new(
WIDGET_ID.to_owned(),
owned_room_id!("!a98sd12bjh:example.org"),
true,
None,
);
fn test_get_supported_api_versions() {
let (mut machine, _) =
WidgetMachine::new(WIDGET_ID.to_owned(), owned_room_id!("!a98sd12bjh:example.org"), true);

let actions = machine.process(IncomingMessage::WidgetMessage(json_string!({
"api": "fromWidget",
Expand Down
12 changes: 6 additions & 6 deletions crates/matrix-sdk/src/widget/machine/tests/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ use crate::widget::machine::{
};

#[test]
fn machine_can_negotiate_capabilities_immediately() {
fn test_machine_can_negotiate_capabilities_immediately() {
let room_id = owned_room_id!("!a98sd12bjh:example.org");
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false, None);
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false);
assert_capabilities_dance(&mut machine, actions, None);
}

#[test]
fn machine_can_request_capabilities_on_content_load() {
fn test_machine_can_request_capabilities_on_content_load() {
let room_id = owned_room_id!("!a98sd12bjh:example.org");
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, true, None);
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, true);
assert!(actions.is_empty());

// Content loaded event processed.
Expand Down Expand Up @@ -67,9 +67,9 @@ fn machine_can_request_capabilities_on_content_load() {
}

#[test]
fn capabilities_failure_results_into_empty_capabilities() {
fn test_capabilities_failure_results_into_empty_capabilities() {
let room_id = owned_room_id!("!a98sd12bjh:example.org");
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false, None);
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false);

// Ask widget to provide desired capabilities.
let actions = {
Expand Down
34 changes: 15 additions & 19 deletions crates/matrix-sdk/src/widget/machine/tests/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use super::{capabilities::assert_capabilities_dance, parse_msg, WIDGET_ID};
use crate::widget::machine::{Action, IncomingMessage, WidgetMachine};

#[test]
fn machine_sends_error_for_unknown_request() {
fn test_machine_sends_error_for_unknown_request() {
let room_id = owned_room_id!("!a98sd12bjh:example.org");
let (mut machine, _) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, true, None);
let (mut machine, _) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, true);

let actions = machine.process(IncomingMessage::WidgetMessage(json_string!({
"api": "fromWidget",
Expand All @@ -46,13 +46,9 @@ fn machine_sends_error_for_unknown_request() {
}

#[test]
fn read_messages_without_capabilities() {
let (mut machine, _) = WidgetMachine::new(
WIDGET_ID.to_owned(),
owned_room_id!("!a98sd12bjh:example.org"),
true,
None,
);
fn test_read_messages_without_capabilities() {
let (mut machine, _) =
WidgetMachine::new(WIDGET_ID.to_owned(), owned_room_id!("!a98sd12bjh:example.org"), true);

let actions = machine.process(IncomingMessage::WidgetMessage(json_string!({
"api": "fromWidget",
Expand All @@ -77,9 +73,9 @@ fn read_messages_without_capabilities() {
}

#[test]
fn read_request_for_non_allowed_message_like_events() {
fn test_read_request_for_non_allowed_message_like_events() {
let room_id = owned_room_id!("!a98sd12bjh:example.org");
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false, None);
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false);
assert_capabilities_dance(&mut machine, actions, None);

let actions = machine.process(IncomingMessage::WidgetMessage(json_string!({
Expand All @@ -105,9 +101,9 @@ fn read_request_for_non_allowed_message_like_events() {
}

#[test]
fn read_request_for_non_allowed_state_events() {
fn test_read_request_for_non_allowed_state_events() {
let room_id = owned_room_id!("!a98sd12bjh:example.org");
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false, None);
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false);
assert_capabilities_dance(&mut machine, actions, None);

let actions = machine.process(IncomingMessage::WidgetMessage(json_string!({
Expand All @@ -134,9 +130,9 @@ fn read_request_for_non_allowed_state_events() {
}

#[test]
fn send_request_for_non_allowed_state_events() {
fn test_send_request_for_non_allowed_state_events() {
let room_id = owned_room_id!("!a98sd12bjh:example.org");
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false, None);
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false);
assert_capabilities_dance(
&mut machine,
actions,
Expand Down Expand Up @@ -166,9 +162,9 @@ fn send_request_for_non_allowed_state_events() {
}

#[test]
fn send_request_for_non_allowed_message_like_events() {
fn test_send_request_for_non_allowed_message_like_events() {
let room_id = owned_room_id!("!a98sd12bjh:example.org");
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false, None);
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false);
assert_capabilities_dance(
&mut machine,
actions,
Expand Down Expand Up @@ -198,9 +194,9 @@ fn send_request_for_non_allowed_message_like_events() {
}

#[test]
fn read_request_for_message_like_with_disallowed_msg_type_fails() {
fn test_read_request_for_message_like_with_disallowed_msg_type_fails() {
let room_id = owned_room_id!("!a98sd12bjh:example.org");
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false, None);
let (mut machine, actions) = WidgetMachine::new(WIDGET_ID.to_owned(), room_id, false);
assert_capabilities_dance(
&mut machine,
actions,
Expand Down
20 changes: 6 additions & 14 deletions crates/matrix-sdk/src/widget/machine/tests/openid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ use crate::widget::machine::{
};

#[test]
fn openid_request_handling_works() {
let (mut machine, _) = WidgetMachine::new(
WIDGET_ID.to_owned(),
owned_room_id!("!a98sd12bjh:example.org"),
true,
None,
);
fn test_openid_request_handling_works() {
let (mut machine, _) =
WidgetMachine::new(WIDGET_ID.to_owned(), owned_room_id!("!a98sd12bjh:example.org"), true);

// Widget requests an open ID token, since we don't have any caching yet,
// we reply with a pending response right away.
Expand Down Expand Up @@ -112,13 +108,9 @@ fn openid_request_handling_works() {
}

#[test]
fn openid_fail_results_in_response_blocked() {
let (mut machine, _) = WidgetMachine::new(
WIDGET_ID.to_owned(),
owned_room_id!("!a98sd12bjh:example.org"),
true,
None,
);
fn test_openid_fail_results_in_response_blocked() {
let (mut machine, _) =
WidgetMachine::new(WIDGET_ID.to_owned(), owned_room_id!("!a98sd12bjh:example.org"), true);

// Widget requests an open ID token, since we don't have any caching yet,
// we reply with a pending response right away.
Expand Down
4 changes: 4 additions & 0 deletions crates/matrix-sdk/src/widget/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,16 @@ impl MatrixDriver {
self.room.redact(&redacts, None, None).await?.event_id,
));
}

Ok(match (state_key, delayed_event_parameters) {
(None, None) => SendEventResponse::from_event_id(
self.room.send_raw(&type_str, content).await?.event_id,
),

(Some(key), None) => SendEventResponse::from_event_id(
self.room.send_state_event_raw(&type_str, &key, content).await?.event_id,
),

(None, Some(delayed_event_parameters)) => {
let r = delayed_events::delayed_message_event::unstable::Request::new_raw(
self.room.room_id().to_owned(),
Expand All @@ -146,6 +149,7 @@ impl MatrixDriver {
);
self.room.client.send(r, None).await.map(|r| r.into())?
}

(Some(key), Some(delayed_event_parameters)) => {
let r = delayed_events::delayed_state_event::unstable::Request::new_raw(
self.room.room_id().to_owned(),
Expand Down
1 change: 0 additions & 1 deletion crates/matrix-sdk/src/widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ impl WidgetDriver {
self.settings.widget_id().to_owned(),
room.room_id().to_owned(),
self.settings.init_on_content_load(),
None,
);

let matrix_driver = MatrixDriver::new(room.clone());
Expand Down

0 comments on commit 4130157

Please sign in to comment.