Skip to content

Commit 38bc403

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 476a420 commit 38bc403

File tree

11 files changed

+572
-172
lines changed

11 files changed

+572
-172
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
@@ -21,7 +21,7 @@ use ruma::{
2121
account::request_openid_token, delayed_events::update_delayed_event,
2222
to_device::send_event_to_device,
2323
},
24-
events::{AnyTimelineEvent, AnyToDeviceEventContent},
24+
events::{AnyStateEvent, AnyTimelineEvent, AnyToDeviceEventContent},
2525
serde::Raw,
2626
to_device::DeviceIdOrAllDevices,
2727
OwnedUserId,
@@ -48,11 +48,11 @@ pub(crate) enum MatrixDriverRequestData {
4848
/// Get OpenId token for a given request ID.
4949
GetOpenId,
5050

51-
/// Read message event(s).
52-
ReadMessageLikeEvent(ReadMessageLikeEventRequest),
51+
/// Read events from the timeline.
52+
ReadEvents(ReadEventsRequest),
5353

54-
/// Read state event(s).
55-
ReadStateEvent(ReadStateEventRequest),
54+
/// Read room state entries.
55+
ReadState(ReadStateRequest),
5656

5757
/// Send Matrix event that corresponds to the given description.
5858
SendEvent(SendEventRequest),
@@ -170,26 +170,32 @@ impl FromMatrixDriverResponse for request_openid_token::v3::Response {
170170
}
171171
}
172172

173-
/// Ask the client to read Matrix event(s) that corresponds to the given
173+
/// Ask the client to read Matrix events that correspond to the given
174174
/// description and return a list of events as a response.
175175
#[derive(Clone, Debug)]
176-
pub(crate) struct ReadMessageLikeEventRequest {
176+
pub(crate) struct ReadEventsRequest {
177177
/// The event type to read.
178178
// TODO: This wants to be `MessageLikeEventType`` but we need a type which supports `as_str()`
179179
// as soon as ruma supports `as_str()` on `MessageLikeEventType` we can use it here.
180180
pub(crate) event_type: String,
181181

182+
/// The `state_key` to read. If None, this will read events regardless of
183+
/// whether they are state events. If `Some(Any)`, this will only read state
184+
/// events of the given type. If set to a specific state key, this will only
185+
/// read state events of the given type matching that state key.
186+
pub(crate) state_key: Option<StateKeySelector>,
187+
182188
/// The maximum number of events to return.
183189
pub(crate) limit: u32,
184190
}
185191

186-
impl From<ReadMessageLikeEventRequest> for MatrixDriverRequestData {
187-
fn from(value: ReadMessageLikeEventRequest) -> Self {
188-
MatrixDriverRequestData::ReadMessageLikeEvent(value)
192+
impl From<ReadEventsRequest> for MatrixDriverRequestData {
193+
fn from(value: ReadEventsRequest) -> Self {
194+
MatrixDriverRequestData::ReadEvents(value)
189195
}
190196
}
191197

192-
impl MatrixDriverRequest for ReadMessageLikeEventRequest {
198+
impl MatrixDriverRequest for ReadEventsRequest {
193199
type Response = Vec<Raw<AnyTimelineEvent>>;
194200
}
195201

@@ -205,28 +211,40 @@ impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
205211
}
206212
}
207213

208-
/// Ask the client to read Matrix event(s) that corresponds to the given
209-
/// description and return a list of events as a response.
214+
/// Ask the client to read Matrix room state entries corresponding to the given
215+
/// description and return a list of state events as a response.
210216
#[derive(Clone, Debug)]
211-
pub(crate) struct ReadStateEventRequest {
217+
pub(crate) struct ReadStateRequest {
212218
/// The event type to read.
213219
// TODO: This wants to be `TimelineEventType` but we need a type which supports `as_str()`
214220
// as soon as ruma supports `as_str()` on `TimelineEventType` we can use it here.
215221
pub(crate) event_type: String,
216222

217-
/// The `state_key` to read, or `Any` to receive any/all events of the given
218-
/// type, regardless of their `state_key`.
223+
/// The `state_key` to read, or `Any` to receive any/all room state entries
224+
/// of the given type, regardless of their `state_key`.
219225
pub(crate) state_key: StateKeySelector,
220226
}
221227

222-
impl From<ReadStateEventRequest> for MatrixDriverRequestData {
223-
fn from(value: ReadStateEventRequest) -> Self {
224-
MatrixDriverRequestData::ReadStateEvent(value)
228+
impl From<ReadStateRequest> for MatrixDriverRequestData {
229+
fn from(value: ReadStateRequest) -> Self {
230+
MatrixDriverRequestData::ReadState(value)
225231
}
226232
}
227233

228-
impl MatrixDriverRequest for ReadStateEventRequest {
229-
type Response = Vec<Raw<AnyTimelineEvent>>;
234+
impl MatrixDriverRequest for ReadStateRequest {
235+
type Response = Vec<Raw<AnyStateEvent>>;
236+
}
237+
238+
impl FromMatrixDriverResponse for Vec<Raw<AnyStateEvent>> {
239+
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
240+
match ev {
241+
MatrixDriverResponse::StateRead(response) => Some(response),
242+
_ => {
243+
error!("bug in MatrixDriver, received wrong event response");
244+
None
245+
}
246+
}
247+
}
230248
}
231249

232250
/// 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
SendToDevice(SendToDeviceRequest),
4141
#[serde(rename = "org.matrix.msc4157.update_delayed_event")]
@@ -133,6 +133,7 @@ impl SupportedApiVersionsResponse {
133133
ApiVersion::V0_0_1,
134134
ApiVersion::V0_0_2,
135135
ApiVersion::MSC2762,
136+
ApiVersion::MSC2762UpdateState,
136137
ApiVersion::MSC2871,
137138
ApiVersion::MSC3819,
138139
],
@@ -155,6 +156,10 @@ pub(super) enum ApiVersion {
155156
#[serde(rename = "org.matrix.msc2762")]
156157
MSC2762,
157158

159+
/// Supports receiving of room state with the `update_state` action.
160+
#[serde(rename = "org.matrix.msc2762_update_state")]
161+
MSC2762UpdateState,
162+
158163
/// Supports sending of approved capabilities back to the widget.
159164
#[serde(rename = "org.matrix.msc2871")]
160165
MSC2871,
@@ -181,22 +186,15 @@ pub(super) enum ApiVersion {
181186
}
182187

183188
#[derive(Deserialize, Debug)]
184-
#[serde(untagged)]
185-
pub(super) enum ReadEventRequest {
186-
ReadStateEvent {
187-
#[serde(rename = "type")]
188-
event_type: String,
189-
state_key: StateKeySelector,
190-
},
191-
ReadMessageLikeEvent {
192-
#[serde(rename = "type")]
193-
event_type: String,
194-
limit: Option<u32>,
195-
},
189+
pub(super) struct ReadEventsRequest {
190+
#[serde(rename = "type")]
191+
pub(super) event_type: String,
192+
pub(super) state_key: Option<StateKeySelector>,
193+
pub(super) limit: Option<u32>,
196194
}
197195

198196
#[derive(Debug, Serialize)]
199-
pub(super) struct ReadEventResponse {
197+
pub(super) struct ReadEventsResponse {
200198
pub(super) events: Vec<Raw<AnyTimelineEvent>>,
201199
}
202200

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, to_device::send_event_to_device},
17-
events::{AnyTimelineEvent, AnyToDeviceEvent},
17+
events::{AnyStateEvent, AnyTimelineEvent, AnyToDeviceEvent},
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,
@@ -50,29 +52,39 @@ pub(crate) enum IncomingMessage {
5052
/// ([`crate::widget::Action::SubscribeTimeline`] request).
5153
MatrixEventReceived(Raw<AnyTimelineEvent>),
5254

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>>),
61+
5362
/// The `MatrixDriver` notified the `WidgetMachine` of a new to-device
5463
/// event.
5564
ToDeviceReceived(Raw<AnyToDeviceEvent>),
5665
}
5766

5867
pub(crate) enum MatrixDriverResponse {
5968
/// Client acquired capabilities from the user.
60-
///
61-
/// A response to an `Action::AcquireCapabilities` command.
69+
/// A response to a [`MatrixDriverRequestData::AcquireCapabilities`]
70+
/// command.
6271
CapabilitiesAcquired(Capabilities),
6372
/// Client got OpenId token for a given request ID.
64-
/// A response to an `Action::GetOpenId` command.
73+
/// A response to a [`MatrixDriverRequestData::GetOpenId`] command.
6574
OpenIdReceived(request_openid_token::v3::Response),
6675
/// Client read some Matrix event(s).
67-
/// A response to a `Action::ReadEvent` command.
76+
/// A response to a [`MatrixDriverRequestData::ReadEvents`] command.
6877
EventsRead(Vec<Raw<AnyTimelineEvent>>),
78+
/// Client read some Matrix room state entries.
79+
/// A response to a [`MatrixDriverRequestData::ReadState`] command.
80+
StateRead(Vec<Raw<AnyStateEvent>>),
6981
/// Client sent some Matrix event. The response contains the event ID.
70-
/// A response to a `Action::SendEvent` command.
82+
/// A response to a [`MatrixDriverRequestData::SendEvent`] command.
7183
EventSent(SendEventResponse),
7284
/// A response to a `Action::SendToDevice` command.
7385
ToDeviceSent(send_event_to_device::v3::Response),
7486
/// Client updated a delayed event.
75-
/// A response to a `Action::UpdateDelayedEvent` command.
87+
/// A response to a [`MatrixDriverRequestData::UpdateDelayedEvent`] command.
7688
DelayedEventUpdated(delayed_events::update_delayed_event::unstable::Response),
7789
}
7890

0 commit comments

Comments
 (0)