14
14
15
15
//! Internal client widget API implementation.
16
16
17
- use std :: { borrow :: Cow , error :: Error , ops :: Deref } ;
17
+ use tokio :: sync :: mpsc :: { unbounded_channel , UnboundedReceiver } ;
18
18
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 ,
24
22
} ;
25
- use serde_json:: Value as JsonValue ;
26
- use tokio:: sync:: mpsc:: { unbounded_channel, UnboundedReceiver } ;
27
23
28
- use super :: Permissions ;
24
+ mod actions;
25
+ mod events;
29
26
30
27
/// State machine that handles the client widget API interractions.
31
28
pub struct ClientApi ;
@@ -46,111 +43,3 @@ impl ClientApi {
46
43
// TODO: Process the event.
47
44
}
48
45
}
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