matrix_sdk/widget/machine/
to_widget.rs1use std::marker::PhantomData;
16
17use ruma::{
18 events::{AnyStateEvent, AnyTimelineEvent, AnyToDeviceEvent},
19 serde::Raw,
20};
21use serde::{Deserialize, Serialize, de::DeserializeOwned};
22use serde_json::value::RawValue as RawJsonValue;
23use tracing::error;
24
25use super::{Action, ToWidgetRequestMeta, WidgetMachine, openid::OpenIdResponse};
26use crate::widget::Capabilities;
27
28pub(crate) struct ToWidgetRequestHandle<'m, T> {
30 request_meta: &'m mut ToWidgetRequestMeta,
31 _phantom: PhantomData<fn() -> T>,
32}
33
34impl<'m, T> ToWidgetRequestHandle<'m, T>
35where
36 T: DeserializeOwned,
37{
38 pub(crate) fn new(request_meta: &'m mut ToWidgetRequestMeta) -> Self {
39 Self { request_meta, _phantom: PhantomData }
40 }
41
42 pub(crate) fn add_response_handler(
43 self,
44 response_handler: impl FnOnce(T, &mut WidgetMachine) -> Vec<Action> + Send + 'static,
45 ) {
46 self.request_meta.response_fn = Some(Box::new(move |raw_response_data, machine| {
47 match serde_json::from_str(raw_response_data.get()) {
48 Ok(response_data) => response_handler(response_data, machine),
49 Err(e) => {
50 error!("Failed to deserialize toWidget response: {e}");
51 Vec::new()
52 }
53 }
54 }));
55 }
56}
57
58#[derive(Deserialize, Debug)]
59#[serde(rename_all = "camelCase")]
60pub(super) struct ToWidgetResponse {
61 pub(super) action: String,
63
64 #[allow(dead_code)]
66 #[serde(rename = "data")]
67 pub(super) request_data: Box<RawJsonValue>,
68
69 #[serde(rename = "response")]
71 pub(super) response_data: Box<RawJsonValue>,
72}
73
74pub(crate) trait ToWidgetRequest: Serialize {
78 const ACTION: &'static str;
79 type ResponseData: DeserializeOwned;
80}
81
82#[derive(Serialize)]
84pub(super) struct RequestCapabilities {}
85
86impl ToWidgetRequest for RequestCapabilities {
87 const ACTION: &'static str = "capabilities";
88 type ResponseData = RequestCapabilitiesResponse;
89}
90
91#[derive(Deserialize)]
92pub(super) struct RequestCapabilitiesResponse {
93 pub(super) capabilities: Capabilities,
94}
95
96#[derive(Serialize)]
98pub(super) struct NotifyCapabilitiesChanged {
99 pub(super) requested: Capabilities,
100 pub(super) approved: Capabilities,
101}
102
103impl ToWidgetRequest for NotifyCapabilitiesChanged {
104 const ACTION: &'static str = "notify_capabilities";
105 type ResponseData = Empty;
106}
107
108#[derive(Serialize)]
110pub(crate) struct NotifyOpenIdChanged(pub(crate) OpenIdResponse);
111
112impl ToWidgetRequest for NotifyOpenIdChanged {
113 const ACTION: &'static str = "openid_credentials";
114 type ResponseData = OpenIdResponse;
115}
116
117#[derive(Serialize)]
120#[serde(transparent)]
121pub(crate) struct NotifyNewMatrixEvent(pub(crate) Raw<AnyTimelineEvent>);
122
123impl ToWidgetRequest for NotifyNewMatrixEvent {
124 const ACTION: &'static str = "send_event";
125 type ResponseData = Empty;
126}
127
128#[derive(Serialize)]
131pub(crate) struct NotifyStateUpdate {
132 pub(super) state: Vec<Raw<AnyStateEvent>>,
133}
134
135impl ToWidgetRequest for NotifyStateUpdate {
136 const ACTION: &'static str = "update_state";
137 type ResponseData = Empty;
138}
139
140#[derive(Deserialize)]
141pub(crate) struct Empty {}
142
143#[derive(Serialize)]
146#[serde(transparent)]
147pub(crate) struct NotifyNewToDeviceMessage(pub(crate) Raw<AnyToDeviceEvent>);
148
149impl ToWidgetRequest for NotifyNewToDeviceMessage {
150 const ACTION: &'static str = "send_to_device";
151 type ResponseData = Empty;
152}