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 ruma::{
16 api::client::{account::request_openid_token, delayed_events},
17 events::AnyTimelineEvent,
18 serde::Raw,
19};
20use serde::{de, Deserialize, Deserializer};
21use serde_json::value::RawValue as RawJsonValue;
22use uuid::Uuid;
2324use super::{
25 from_widget::{FromWidgetRequest, SendEventResponse},
26 to_widget::ToWidgetResponse,
27};
28use crate::widget::Capabilities;
2930/// Incoming event that the client API must process.
31pub(crate) enum IncomingMessage {
32/// An incoming raw message from the widget.
33WidgetMessage(String),
3435/// A response to a request to the `MatrixDriver`.
36MatrixDriverResponse {
37/// The ID of the request that this response corresponds to.
38request_id: Uuid,
3940/// Result of the request: the response data, or a matrix sdk error.
41 ///
42 /// Http errors will be forwarded to the widget in a specified format so
43 /// the widget can parse the error.
44response: Result<MatrixDriverResponse, crate::Error>,
45 },
4647/// The `MatrixDriver` notified the `WidgetMachine` of a new matrix event.
48 ///
49 /// This means that the machine previously subscribed to some events
50 /// ([`crate::widget::Action::Subscribe`] request).
51MatrixEventReceived(Raw<AnyTimelineEvent>),
52}
5354pub(crate) enum MatrixDriverResponse {
55/// Client acquired capabilities from the user.
56 ///
57 /// A response to an `Action::AcquireCapabilities` command.
58CapabilitiesAcquired(Capabilities),
59/// Client got OpenId token for a given request ID.
60 /// A response to an `Action::GetOpenId` command.
61OpenIdReceived(request_openid_token::v3::Response),
62/// Client read some matrix event(s).
63 /// A response to an `Action::ReadMatrixEvent` commands.
64MatrixEventRead(Vec<Raw<AnyTimelineEvent>>),
65/// Client sent some matrix event. The response contains the event ID.
66 /// A response to an `Action::SendMatrixEvent` command.
67MatrixEventSent(SendEventResponse),
68 MatrixDelayedEventUpdate(delayed_events::update_delayed_event::unstable::Response),
69}
7071pub(super) struct IncomingWidgetMessage {
72pub(super) widget_id: String,
73pub(super) request_id: String,
74pub(super) kind: IncomingWidgetMessageKind,
75}
7677#[derive(Debug)]
78pub(super) enum IncomingWidgetMessageKind {
79 Request(Raw<FromWidgetRequest>),
80 Response(ToWidgetResponse),
81}
8283impl<'de> Deserialize<'de> for IncomingWidgetMessage {
84fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
85where
86D: Deserializer<'de>,
87 {
88let raw: Box<RawJsonValue> = Box::deserialize(deserializer)?;
8990#[derive(Deserialize)]
91 #[serde(rename_all = "camelCase")]
92enum ApiTag {
93 FromWidget,
94 ToWidget,
95 }
9697#[derive(Deserialize)]
98 #[serde(rename_all = "camelCase")]
99struct ExtractHeader {
100 api: ApiTag,
101 widget_id: String,
102 request_id: String,
103 }
104105let ExtractHeader { api, widget_id, request_id } =
106 serde_json::from_str(raw.get()).map_err(de::Error::custom)?;
107108let kind = match api {
109 ApiTag::FromWidget => IncomingWidgetMessageKind::Request(Raw::from_json(raw)),
110 ApiTag::ToWidget => serde_json::from_str(raw.get())
111 .map(IncomingWidgetMessageKind::Response)
112 .map_err(de::Error::custom)?,
113 };
114115Ok(Self { widget_id, request_id, kind })
116 }
117}