Skip to content

Commit 85a42c8

Browse files
committed
refactor(widget): Extract method for acquire capabilities response
1 parent a3b69c8 commit 85a42c8

File tree

1 file changed

+32
-26
lines changed
  • crates/matrix-sdk/src/widget/machine

1 file changed

+32
-26
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,37 @@ impl WidgetMachine {
655655
))
656656
}
657657

658+
/// Processes a response from the [`crate::widget::MatrixDriver`] saying
659+
/// that the widget is approved to acquire some capabilities. This will
660+
/// store those capabilities in the state machine and then notify the
661+
/// widget.
662+
fn process_acquired_capabilities(
663+
&mut self,
664+
approved: Result<Capabilities, Error>,
665+
requested: Capabilities,
666+
) -> Vec<Action> {
667+
let approved = approved.unwrap_or_else(|e| {
668+
error!("Acquiring capabilities failed: {e}");
669+
Capabilities::default()
670+
});
671+
672+
let mut actions = Vec::new();
673+
if !approved.read.is_empty() {
674+
actions.push(Action::Subscribe);
675+
}
676+
677+
self.capabilities = CapabilitiesState::Negotiated(approved.clone());
678+
679+
if let Some(action) = self
680+
.send_to_widget_request(NotifyCapabilitiesChanged { approved, requested })
681+
.map(|(_request, action)| action)
682+
{
683+
actions.push(action);
684+
}
685+
686+
actions
687+
}
688+
658689
fn negotiate_capabilities(&mut self) -> Vec<Action> {
659690
let mut actions = Vec::new();
660691

@@ -680,33 +711,8 @@ impl WidgetMachine {
680711
};
681712

682713
request.add_response_handler(|result, machine| {
683-
let approved_capabilities = result.unwrap_or_else(|e| {
684-
error!("Acquiring capabilities failed: {e}");
685-
Capabilities::default()
686-
});
687-
688-
let mut actions = Vec::new();
689-
if !approved_capabilities.read.is_empty() {
690-
actions.push(Action::Subscribe);
691-
}
692-
693-
machine.capabilities = CapabilitiesState::Negotiated(approved_capabilities.clone());
694-
695-
let notify_caps_changed = NotifyCapabilitiesChanged {
696-
approved: approved_capabilities,
697-
requested: requested_capabilities,
698-
};
699-
700-
if let Some(action) = machine
701-
.send_to_widget_request(notify_caps_changed)
702-
.map(|(_request, action)| action)
703-
{
704-
actions.push(action);
705-
}
706-
707-
actions
714+
machine.process_acquired_capabilities(result, requested_capabilities)
708715
});
709-
710716
vec![action]
711717
});
712718

0 commit comments

Comments
 (0)