1// Copyright 2023 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
1415use std::marker::PhantomData;
1617use ruma::{events::AnyTimelineEvent, serde::Raw};
18use serde::{de::DeserializeOwned, Deserialize, Serialize};
19use serde_json::value::RawValue as RawJsonValue;
20use tracing::error;
2122use super::{openid::OpenIdResponse, Action, ToWidgetRequestMeta, WidgetMachine};
23use crate::widget::Capabilities;
2425/// A handle to a pending `toWidget` request.
26pub(crate) struct ToWidgetRequestHandle<'m, T> {
27 request_meta: Option<&'m mut ToWidgetRequestMeta>,
28 _phantom: PhantomData<fn() -> T>,
29}
3031impl<'m, T> ToWidgetRequestHandle<'m, T>
32where
33T: DeserializeOwned,
34{
35pub(crate) fn new(request_meta: &'m mut ToWidgetRequestMeta) -> Self {
36Self { request_meta: Some(request_meta), _phantom: PhantomData }
37 }
3839pub(crate) fn null() -> Self {
40Self { request_meta: None, _phantom: PhantomData }
41 }
4243pub(crate) fn then(
44self,
45 response_handler: impl FnOnce(T, &mut WidgetMachine) -> Vec<Action> + Send + 'static,
46 ) {
47if let Some(request_meta) = self.request_meta {
48 request_meta.response_fn = Some(Box::new(move |raw_response_data, machine| {
49match serde_json::from_str(raw_response_data.get()) {
50Ok(response_data) => response_handler(response_data, machine),
51Err(e) => {
52error!("Failed to deserialize toWidget response: {e}");
53 Vec::new()
54 }
55 }
56 }));
57 }
58 }
59}
6061#[derive(Deserialize, Debug)]
62#[serde(rename_all = "camelCase")]
63pub(super) struct ToWidgetResponse {
64/// The action from the original request.
65pub(super) action: String,
6667/// The data from the original request.
68#[allow(dead_code)]
69 #[serde(rename = "data")]
70pub(super) request_data: Box<RawJsonValue>,
7172/// The response data.
73#[serde(rename = "response")]
74pub(super) response_data: Box<RawJsonValue>,
75}
7677/// A request that the driver can send to the widget.
78///
79/// In postmessage interface terms: an `"api": "toWidget"` message.
80pub(crate) trait ToWidgetRequest: Serialize {
81const ACTION: &'static str;
82type ResponseData: DeserializeOwned;
83}
8485/// Request the widget to send the list of capabilities that it wants to have.
86#[derive(Serialize)]
87pub(super) struct RequestCapabilities {}
8889impl ToWidgetRequest for RequestCapabilities {
90const ACTION: &'static str = "capabilities";
91type ResponseData = RequestCapabilitiesResponse;
92}
9394#[derive(Deserialize)]
95pub(super) struct RequestCapabilitiesResponse {
96pub(super) capabilities: Capabilities,
97}
9899/// Notify the widget that the list of the granted capabilities has changed.
100#[derive(Serialize)]
101pub(super) struct NotifyCapabilitiesChanged {
102pub(super) requested: Capabilities,
103pub(super) approved: Capabilities,
104}
105106impl ToWidgetRequest for NotifyCapabilitiesChanged {
107const ACTION: &'static str = "notify_capabilities";
108type ResponseData = Empty;
109}
110111/// Notify the widget that the OpenID credentials changed.
112#[derive(Serialize)]
113pub(crate) struct NotifyOpenIdChanged(pub(crate) OpenIdResponse);
114115impl ToWidgetRequest for NotifyOpenIdChanged {
116const ACTION: &'static str = "openid_credentials";
117type ResponseData = OpenIdResponse;
118}
119120/// Notify the widget that we received a new matrix event.
121/// This is a "response" to the widget subscribing to the events in the room.
122#[derive(Serialize)]
123#[serde(transparent)]
124pub(crate) struct NotifyNewMatrixEvent(pub(crate) Raw<AnyTimelineEvent>);
125126impl ToWidgetRequest for NotifyNewMatrixEvent {
127const ACTION: &'static str = "send_event";
128type ResponseData = Empty;
129}
130131#[derive(Deserialize)]
132pub(crate) struct Empty {}