Skip to content

Commit fba7324

Browse files
committed
feat(widget): Distinguish room state and timeline events
This is an implementation of an update to MSC2762 (matrix-org/matrix-spec-proposals#4237). It changes the widget driver to split state updates out into an `update_state` action and use the `send_event` action exclusively for forwarding timeline events. Accordingly, `read_events` now always reads from /messages, never the state store.
1 parent 7ecb0bd commit fba7324

File tree

11 files changed

+543
-177
lines changed

11 files changed

+543
-177
lines changed

crates/matrix-sdk/src/widget/machine/driver_req.rs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::marker::PhantomData;
1818

1919
use ruma::{
2020
api::client::{account::request_openid_token, delayed_events::update_delayed_event},
21-
events::AnyTimelineEvent,
21+
events::{AnyStateEvent, AnyTimelineEvent},
2222
serde::Raw,
2323
};
2424
use serde::Deserialize;
@@ -43,11 +43,11 @@ pub(crate) enum MatrixDriverRequestData {
4343
/// Get OpenId token for a given request ID.
4444
GetOpenId,
4545

46-
/// Read message event(s).
47-
ReadMessageLikeEvent(ReadMessageLikeEventRequest),
46+
/// Read events from the timeline.
47+
ReadEvents(ReadEventsRequest),
4848

49-
/// Read state event(s).
50-
ReadStateEvent(ReadStateEventRequest),
49+
/// Read room state entries.
50+
ReadState(ReadStateRequest),
5151

5252
/// Send matrix event that corresponds to the given description.
5353
SendEvent(SendEventRequest),
@@ -152,26 +152,32 @@ impl FromMatrixDriverResponse for request_openid_token::v3::Response {
152152
}
153153
}
154154

155-
/// Ask the client to read matrix event(s) that corresponds to the given
155+
/// Ask the client to read matrix events that correspond to the given
156156
/// description and return a list of events as a response.
157157
#[derive(Clone, Debug)]
158-
pub(crate) struct ReadMessageLikeEventRequest {
158+
pub(crate) struct ReadEventsRequest {
159159
/// The event type to read.
160160
// TODO: This wants to be `MessageLikeEventType`` but we need a type which supports `as_str()`
161161
// as soon as ruma supports `as_str()` on `MessageLikeEventType` we can use it here.
162162
pub(crate) event_type: String,
163163

164+
/// The `state_key` to read. If None, this will read events regardless of
165+
/// whether they are state events. If `Some(Any)`, this will only read state
166+
/// events of the given type. If set to a specific state key, this will only
167+
/// read state events of the given type matching that state key.
168+
pub(crate) state_key: Option<StateKeySelector>,
169+
164170
/// The maximum number of events to return.
165171
pub(crate) limit: u32,
166172
}
167173

168-
impl From<ReadMessageLikeEventRequest> for MatrixDriverRequestData {
169-
fn from(value: ReadMessageLikeEventRequest) -> Self {
170-
MatrixDriverRequestData::ReadMessageLikeEvent(value)
174+
impl From<ReadEventsRequest> for MatrixDriverRequestData {
175+
fn from(value: ReadEventsRequest) -> Self {
176+
MatrixDriverRequestData::ReadEvents(value)
171177
}
172178
}
173179

174-
impl MatrixDriverRequest for ReadMessageLikeEventRequest {
180+
impl MatrixDriverRequest for ReadEventsRequest {
175181
type Response = Vec<Raw<AnyTimelineEvent>>;
176182
}
177183

@@ -187,28 +193,40 @@ impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
187193
}
188194
}
189195

190-
/// Ask the client to read matrix event(s) that corresponds to the given
191-
/// description and return a list of events as a response.
196+
/// Ask the client to read matrix room state entries corresponding to the given
197+
/// description and return a list of state events as a response.
192198
#[derive(Clone, Debug)]
193-
pub(crate) struct ReadStateEventRequest {
199+
pub(crate) struct ReadStateRequest {
194200
/// The event type to read.
195201
// TODO: This wants to be `TimelineEventType` but we need a type which supports `as_str()`
196202
// as soon as ruma supports `as_str()` on `TimelineEventType` we can use it here.
197203
pub(crate) event_type: String,
198204

199-
/// The `state_key` to read, or `Any` to receive any/all events of the given
200-
/// type, regardless of their `state_key`.
205+
/// The `state_key` to read, or `Any` to receive any/all room state entries
206+
/// of the given type, regardless of their `state_key`.
201207
pub(crate) state_key: StateKeySelector,
202208
}
203209

204-
impl From<ReadStateEventRequest> for MatrixDriverRequestData {
205-
fn from(value: ReadStateEventRequest) -> Self {
206-
MatrixDriverRequestData::ReadStateEvent(value)
210+
impl From<ReadStateRequest> for MatrixDriverRequestData {
211+
fn from(value: ReadStateRequest) -> Self {
212+
MatrixDriverRequestData::ReadState(value)
207213
}
208214
}
209215

210-
impl MatrixDriverRequest for ReadStateEventRequest {
211-
type Response = Vec<Raw<AnyTimelineEvent>>;
216+
impl MatrixDriverRequest for ReadStateRequest {
217+
type Response = Vec<Raw<AnyStateEvent>>;
218+
}
219+
220+
impl FromMatrixDriverResponse for Vec<Raw<AnyStateEvent>> {
221+
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
222+
match ev {
223+
MatrixDriverResponse::StateRead(response) => Some(response),
224+
_ => {
225+
error!("bug in MatrixDriver, received wrong event response");
226+
None
227+
}
228+
}
229+
}
212230
}
213231

214232
/// Ask the client to send matrix event that corresponds to the given

crates/matrix-sdk/src/widget/machine/from_widget.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(super) enum FromWidgetRequest {
3535
#[serde(rename = "get_openid")]
3636
GetOpenId {},
3737
#[serde(rename = "org.matrix.msc2876.read_events")]
38-
ReadEvent(ReadEventRequest),
38+
ReadEvent(ReadEventsRequest),
3939
SendEvent(SendEventRequest),
4040
#[serde(rename = "org.matrix.msc4157.update_delayed_event")]
4141
DelayedEventUpdate(UpdateDelayedEventRequest),
@@ -126,6 +126,7 @@ impl SupportedApiVersionsResponse {
126126
ApiVersion::V0_0_1,
127127
ApiVersion::V0_0_2,
128128
ApiVersion::MSC2762,
129+
ApiVersion::MSC2762UpdateState,
129130
ApiVersion::MSC2871,
130131
ApiVersion::MSC3819,
131132
],
@@ -148,6 +149,10 @@ pub(super) enum ApiVersion {
148149
#[serde(rename = "org.matrix.msc2762")]
149150
MSC2762,
150151

152+
/// Supports receiving of room state with the `update_state` action.
153+
#[serde(rename = "org.matrix.msc2762_update_state")]
154+
MSC2762UpdateState,
155+
151156
/// Supports sending of approved capabilities back to the widget.
152157
#[serde(rename = "org.matrix.msc2871")]
153158
MSC2871,
@@ -174,22 +179,15 @@ pub(super) enum ApiVersion {
174179
}
175180

176181
#[derive(Deserialize, Debug)]
177-
#[serde(untagged)]
178-
pub(super) enum ReadEventsRequest {
179-
ReadStateEvent {
180-
#[serde(rename = "type")]
181-
event_type: String,
182-
state_key: StateKeySelector,
183-
},
184-
ReadMessageLikeEvent {
185-
#[serde(rename = "type")]
186-
event_type: String,
187-
limit: Option<u32>,
188-
},
182+
pub(super) struct ReadEventsRequest {
183+
#[serde(rename = "type")]
184+
pub(super) event_type: String,
185+
pub(super) state_key: Option<StateKeySelector>,
186+
pub(super) limit: Option<u32>,
189187
}
190188

191189
#[derive(Debug, Serialize)]
192-
pub(super) struct ReadEventResponse {
190+
pub(super) struct ReadEventsResponse {
193191
pub(super) events: Vec<Raw<AnyTimelineEvent>>,
194192
}
195193

crates/matrix-sdk/src/widget/machine/incoming.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
use ruma::{
1616
api::client::{account::request_openid_token, delayed_events},
17-
events::AnyTimelineEvent,
17+
events::{AnyStateEvent, AnyTimelineEvent},
1818
serde::Raw,
1919
};
2020
use serde::{de, Deserialize, Deserializer};
2121
use serde_json::value::RawValue as RawJsonValue;
2222
use uuid::Uuid;
2323

24+
#[cfg(doc)]
25+
use super::MatrixDriverRequestData;
2426
use super::{
2527
from_widget::{FromWidgetRequest, SendEventResponse},
2628
to_widget::ToWidgetResponse,
@@ -49,24 +51,34 @@ pub(crate) enum IncomingMessage {
4951
/// This means that the machine previously subscribed to some events
5052
/// ([`crate::widget::Action::Subscribe`] request).
5153
MatrixEventReceived(Raw<AnyTimelineEvent>),
54+
55+
/// The `MatrixDriver` notified the `WidgetMachine` of a change in room
56+
/// state.
57+
///
58+
/// This means that the machine previously subscribed to some events
59+
/// ([`crate::widget::Action::Subscribe`] request).
60+
StateUpdateReceived(Vec<Raw<AnyStateEvent>>),
5261
}
5362

5463
pub(crate) enum MatrixDriverResponse {
5564
/// Client acquired capabilities from the user.
56-
///
57-
/// A response to an `Action::AcquireCapabilities` command.
65+
/// A response to a [`MatrixDriverRequestData::AcquireCapabilities`]
66+
/// command.
5867
CapabilitiesAcquired(Capabilities),
5968
/// Client got OpenId token for a given request ID.
60-
/// A response to an `Action::GetOpenId` command.
69+
/// A response to a [`MatrixDriverRequestData::GetOpenId`] command.
6170
OpenIdReceived(request_openid_token::v3::Response),
6271
/// Client read some matrix event(s).
63-
/// A response to a `Action::ReadEvent` command.
72+
/// A response to a [`MatrixDriverRequestData::ReadEvents`] command.
6473
EventsRead(Vec<Raw<AnyTimelineEvent>>),
74+
/// Client read some matrix room state entries.
75+
/// A response to a [`MatrixDriverRequestData::ReadState`] command.
76+
StateRead(Vec<Raw<AnyStateEvent>>),
6577
/// Client sent some matrix event. The response contains the event ID.
66-
/// A response to a `Action::SendEvent` command.
78+
/// A response to a [`MatrixDriverRequestData::SendEvent`] command.
6779
EventSent(SendEventResponse),
6880
/// Client updated a delayed event.
69-
/// A response to a `Action::UpdateDelayedEvent` command.
81+
/// A response to a [`MatrixDriverRequestData::UpdateDelayedEvent`] command.
7082
DelayedEventUpdated(delayed_events::update_delayed_event::unstable::Response),
7183
}
7284

0 commit comments

Comments
 (0)