Skip to content

Commit 3567c9d

Browse files
daniel-abramovjplatte
authored andcommitted
widget: move client into own module and split up
1 parent d8d136b commit 3567c9d

File tree

3 files changed

+157
-117
lines changed

3 files changed

+157
-117
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.
14+
15+
use std::{borrow::Cow, error::Error, ops::Deref};
16+
17+
use ruma::events::TimelineEventType;
18+
use serde::{Deserialize, Serialize};
19+
use serde_json::Value as JsonValue;
20+
21+
use crate::widget::Permissions;
22+
23+
/// Action (a command) that client (driver) must perform.
24+
#[allow(dead_code)] // TODO: Remove once all actions are implemented.
25+
pub enum Action {
26+
/// Send a raw message to the widget.
27+
SendToWidget(String),
28+
/// Acquire permissions from the user given the set of desired permissions.
29+
/// Must eventually be answered with `Event::PermissionsAcquired`.
30+
AcquirePermissions(Command<Permissions>),
31+
/// Get OpenId token for a given request ID.
32+
GetOpenId(Command<()>),
33+
/// Read matrix event(s) that corresponds to the given description.
34+
ReadMatrixEvent(Command<ReadEventCommand>),
35+
// Send matrix event that corresponds to the given description.
36+
SendMatrixEvent(Command<SendEventCommand>),
37+
/// Subscribe to the events in the *current* room, i.e. a room which this
38+
/// widget is instantiated with. The client is aware of the room.
39+
Subscribe,
40+
/// Unsuscribe from the events in the *current* room. Symmetrical to
41+
/// `Subscribe`.
42+
Unsubscribe,
43+
}
44+
45+
/// Command to read matrix event(s).
46+
pub struct ReadEventCommand {
47+
/// Read event(s) of a given type.
48+
pub event_type: TimelineEventType,
49+
/// Limits for the Matrix request.
50+
pub limit: u32,
51+
}
52+
53+
/// Command to send matrix event.
54+
#[derive(Clone, Debug, Deserialize, Serialize)]
55+
pub struct SendEventCommand {
56+
#[serde(rename = "type")]
57+
/// type of an event.
58+
pub event_type: TimelineEventType,
59+
/// State key of an event (if it's a state event).
60+
pub state_key: Option<String>,
61+
/// Raw content of an event.
62+
pub content: JsonValue,
63+
}
64+
65+
/// Command that is sent from the client widget API state machine to the
66+
/// client (driver) that must be performed. Once the command is executed,
67+
/// the client will typically generate an `Event` with the result of it.
68+
pub struct Command<T> {
69+
/// Certain commands are typically answered with certain event once the
70+
/// command is performed. The api state machine will "tag" each command
71+
/// with some "cookie" (in this case just an ID), so that once the
72+
/// result of the execution of this command is received, it could be
73+
/// matched.
74+
id: String,
75+
// Data associated with this command.
76+
data: T,
77+
}
78+
79+
impl<T> Command<T> {
80+
/// Consumes the command and produces a command result with given data.
81+
pub fn result<U, E: Error>(self, result: Result<U, E>) -> CommandResult<U> {
82+
CommandResult { id: self.id, result: result.map_err(|e| e.to_string().into()) }
83+
}
84+
85+
pub fn ok<U>(self, value: U) -> CommandResult<U> {
86+
CommandResult { id: self.id, result: Ok(value) }
87+
}
88+
}
89+
90+
impl<T> Deref for Command<T> {
91+
type Target = T;
92+
93+
fn deref(&self) -> &Self::Target {
94+
&self.data
95+
}
96+
}
97+
98+
/// The result of the execution of a command. Note that this type can only be
99+
/// constructed within this module, i.e. it can only be constructed as a result
100+
/// of a command that has been sent from this module, which means that the
101+
/// client (driver) won't be able to send "invalid" commands, because they could
102+
/// only be generated from a `Command` instance.
103+
#[allow(dead_code)] // TODO: Remove once results are used.
104+
pub struct CommandResult<T> {
105+
/// ID of the command that was executed. See `Command::id` for more details.
106+
id: String,
107+
/// Result of the execution of the command.
108+
result: Result<T, Cow<'static, str>>,
109+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.
14+
15+
use ruma::{
16+
api::client::account::request_openid_token::v3::Response as RumaOpenIdResponse,
17+
events::AnyTimelineEvent, serde::Raw, OwnedEventId,
18+
};
19+
20+
use super::actions::CommandResult;
21+
use crate::widget::Permissions;
22+
23+
/// Incoming event that the client API must process.
24+
pub enum Event {
25+
/// An incoming raw message from the widget.
26+
MessageFromWidget(String),
27+
/// Matrix event received. This one is delivered as a result of client
28+
/// subscribing to the events (`Action::Subscribe` command).
29+
MatrixEventReceived(Raw<AnyTimelineEvent>),
30+
/// Client acquired permissions from the user.
31+
/// A response to an `Action::AcquirePermissions` command.
32+
PermissionsAcquired(CommandResult<Permissions>),
33+
/// Client got OpenId token for a given request ID.
34+
/// A response to an `Action::GetOpenId` command.
35+
OpenIdReceived(CommandResult<RumaOpenIdResponse>),
36+
/// Client read some matrix event(s).
37+
/// A response to an `Action::ReadMatrixEvent` commands.
38+
MatrixEventRead(CommandResult<Vec<Raw<AnyTimelineEvent>>>),
39+
/// Client sent some matrix event. The response contains the event ID.
40+
/// A response to an `Action::SendMatrixEvent` command.
41+
MatrixEventSent(CommandResult<OwnedEventId>),
42+
}

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

Lines changed: 6 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414

1515
//! Internal client widget API implementation.
1616
17-
use std::{borrow::Cow, error::Error, ops::Deref};
17+
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
1818

19-
use ruma::{
20-
api::client::account::request_openid_token::v3::Response as OpenIdResponse,
21-
events::{AnyTimelineEvent, TimelineEventType},
22-
serde::Raw,
23-
OwnedEventId,
19+
pub use self::{
20+
actions::{Action, SendEventCommand},
21+
events::Event,
2422
};
25-
use serde_json::Value as JsonValue;
26-
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
2723

28-
use super::Permissions;
24+
mod actions;
25+
mod events;
2926

3027
/// State machine that handles the client widget API interractions.
3128
pub struct ClientApi;
@@ -46,111 +43,3 @@ impl ClientApi {
4643
// TODO: Process the event.
4744
}
4845
}
49-
50-
/// Incoming event that the client API must process.
51-
pub enum Event {
52-
/// An incoming raw message from the widget.
53-
MessageFromWidget(String),
54-
/// Matrix event received. This one is delivered as a result of client
55-
/// subscribing to the events (`Action::Subscribe` command).
56-
MatrixEventReceived(Raw<AnyTimelineEvent>),
57-
/// Client acquired permissions from the user.
58-
/// A response to an `Action::AcquirePermissions` command.
59-
PermissionsAcquired(CommandResult<Permissions>),
60-
/// Client got OpenId token for a given request ID.
61-
/// A response to an `Action::GetOpenId` command.
62-
OpenIdReceived(CommandResult<OpenIdResponse>),
63-
/// Client read some matrix event(s).
64-
/// A response to an `Action::ReadMatrixEvent` commands.
65-
MatrixEventRead(CommandResult<Vec<Raw<AnyTimelineEvent>>>),
66-
/// Client sent some matrix event. The response contains the event ID.
67-
/// A response to an `Action::SendMatrixEvent` command.
68-
MatrixEventSent(CommandResult<OwnedEventId>),
69-
}
70-
71-
/// Action (a command) that client (driver) must perform.
72-
#[allow(dead_code)] // TODO: Remove once all actions are implemented.
73-
pub enum Action {
74-
/// Send a raw message to the widget.
75-
SendToWidget(String),
76-
/// Acquire permissions from the user given the set of desired permissions.
77-
/// Must eventually be answered with `Event::PermissionsAcquired`.
78-
AcquirePermissions(Command<Permissions>),
79-
/// Get OpenId token for a given request ID.
80-
GetOpenId(Command<()>),
81-
/// Read matrix event(s) that corresponds to the given description.
82-
ReadMatrixEvent(Command<ReadEventCommand>),
83-
// Send matrix event that corresponds to the given description.
84-
SendMatrixEvent(Command<SendEventCommand>),
85-
/// Subscribe to the events in the *current* room, i.e. a room which this
86-
/// widget is instantiated with. The client is aware of the room.
87-
Subscribe,
88-
/// Unsuscribe from the events in the *current* room. Symmetrical to
89-
/// `Subscribe`.
90-
Unsubscribe,
91-
}
92-
93-
/// Command to read matrix event(s).
94-
pub struct ReadEventCommand {
95-
/// Read event(s) of a given type.
96-
pub event_type: TimelineEventType,
97-
/// Limits for the Matrix request.
98-
pub limit: u32,
99-
}
100-
101-
/// Command to send matrix event.
102-
#[derive(Clone)]
103-
pub struct SendEventCommand {
104-
/// type of an event.
105-
pub event_type: TimelineEventType,
106-
/// State key of an event (if it's a state event).
107-
pub state_key: Option<String>,
108-
/// Raw content of an event.
109-
pub content: JsonValue,
110-
}
111-
112-
/// Command that is sent from the client widget API state machine to the
113-
/// client (driver) that must be performed. Once the command is executed,
114-
/// the client will typically generate an `Event` with the result of it.
115-
pub struct Command<T> {
116-
/// Certain commands are typically answered with certain event once the
117-
/// command is performed. The api state machine will "tag" each command
118-
/// with some "cookie" (in this case just an ID), so that once the
119-
/// result of the execution of this command is received, it could be
120-
/// matched.
121-
id: String,
122-
// Data associated with this command.
123-
data: T,
124-
}
125-
126-
impl<T> Command<T> {
127-
/// Consumes the command and produces a command result with given data.
128-
pub fn result<U, E: Error>(self, result: Result<U, E>) -> CommandResult<U> {
129-
CommandResult { id: self.id, result: result.map_err(|e| e.to_string().into()) }
130-
}
131-
132-
pub fn ok<U>(self, value: U) -> CommandResult<U> {
133-
CommandResult { id: self.id, result: Ok(value) }
134-
}
135-
}
136-
137-
impl<T> Deref for Command<T> {
138-
type Target = T;
139-
140-
fn deref(&self) -> &Self::Target {
141-
&self.data
142-
}
143-
}
144-
145-
/// The result of the execution of a command. Note that this type can only be
146-
/// constructed within this module, i.e. it can only be constructed as a result
147-
/// of a command that has been sent from this module, which means that the
148-
/// client (driver) won't be able to send "invalid" commands, because they could
149-
/// only be generated from a `Command` instance.
150-
#[allow(dead_code)] // TODO: Remove once results are used.
151-
pub struct CommandResult<T> {
152-
/// ID of the command that was executed. See `Command::id` for more details.
153-
id: String,
154-
/// Result of the execution of the command.
155-
result: Result<T, Cow<'static, str>>,
156-
}

0 commit comments

Comments
 (0)