Skip to content

Commit 089c552

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 f121dd2 commit 089c552

File tree

11 files changed

+617
-283
lines changed

11 files changed

+617
-283
lines changed

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

Lines changed: 45 additions & 27 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, MessageLikeEventType, StateEventType, TimelineEventType},
21+
events::{AnyStateEvent, AnyTimelineEvent, StateEventType, TimelineEventType},
2222
serde::Raw,
2323
};
2424
use serde::Deserialize;
@@ -43,14 +43,14 @@ 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.
53-
SendMatrixEvent(SendEventRequest),
53+
SendEvent(SendEventRequest),
5454

5555
/// Data for sending a UpdateDelayedEvent client server api request.
5656
UpdateDelayedEvent(UpdateDelayedEventRequest),
@@ -152,31 +152,37 @@ 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.
160-
pub(crate) event_type: MessageLikeEventType,
160+
pub(crate) event_type: TimelineEventType,
161+
162+
/// The `state_key` to read. If None, this will read events regardless of
163+
/// whether they are state events. If `Some(Any)`, this will read any state
164+
/// events of the given type. If set to a specific state key, this will only
165+
/// read state events matching that state key.
166+
pub(crate) state_key: Option<StateKeySelector>,
161167

162168
/// The maximum number of events to return.
163169
pub(crate) limit: u32,
164170
}
165171

166-
impl From<ReadMessageLikeEventRequest> for MatrixDriverRequestData {
167-
fn from(value: ReadMessageLikeEventRequest) -> Self {
168-
MatrixDriverRequestData::ReadMessageLikeEvent(value)
172+
impl From<ReadEventsRequest> for MatrixDriverRequestData {
173+
fn from(value: ReadEventsRequest) -> Self {
174+
MatrixDriverRequestData::ReadEvents(value)
169175
}
170176
}
171177

172-
impl MatrixDriverRequest for ReadMessageLikeEventRequest {
178+
impl MatrixDriverRequest for ReadEventsRequest {
173179
type Response = Vec<Raw<AnyTimelineEvent>>;
174180
}
175181

176182
impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
177183
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
178184
match ev {
179-
MatrixDriverResponse::MatrixEventRead(response) => Some(response),
185+
MatrixDriverResponse::EventsRead(response) => Some(response),
180186
_ => {
181187
error!("bug in MatrixDriver, received wrong event response");
182188
None
@@ -185,26 +191,38 @@ impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
185191
}
186192
}
187193

188-
/// Ask the client to read matrix event(s) that corresponds to the given
189-
/// description and return a list of events as a response.
194+
/// Ask the client to read matrix room state entries corresponding to the given
195+
/// description and return a list of state events as a response.
190196
#[derive(Clone, Debug)]
191-
pub(crate) struct ReadStateEventRequest {
197+
pub(crate) struct ReadStateRequest {
192198
/// The event type to read.
193199
pub(crate) event_type: StateEventType,
194200

195-
/// The `state_key` to read, or `Any` to receive any/all events of the given
196-
/// type, regardless of their `state_key`.
201+
/// The `state_key` to read, or `Any` to receive any/all room state entries
202+
/// of the given type, regardless of their `state_key`.
197203
pub(crate) state_key: StateKeySelector,
198204
}
199205

200-
impl From<ReadStateEventRequest> for MatrixDriverRequestData {
201-
fn from(value: ReadStateEventRequest) -> Self {
202-
MatrixDriverRequestData::ReadStateEvent(value)
206+
impl From<ReadStateRequest> for MatrixDriverRequestData {
207+
fn from(value: ReadStateRequest) -> Self {
208+
MatrixDriverRequestData::ReadState(value)
203209
}
204210
}
205211

206-
impl MatrixDriverRequest for ReadStateEventRequest {
207-
type Response = Vec<Raw<AnyTimelineEvent>>;
212+
impl MatrixDriverRequest for ReadStateRequest {
213+
type Response = Vec<Raw<AnyStateEvent>>;
214+
}
215+
216+
impl FromMatrixDriverResponse for Vec<Raw<AnyStateEvent>> {
217+
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
218+
match ev {
219+
MatrixDriverResponse::StateRead(response) => Some(response),
220+
_ => {
221+
error!("bug in MatrixDriver, received wrong event response");
222+
None
223+
}
224+
}
225+
}
208226
}
209227

210228
/// Ask the client to send matrix event that corresponds to the given
@@ -227,7 +245,7 @@ pub(crate) struct SendEventRequest {
227245

228246
impl From<SendEventRequest> for MatrixDriverRequestData {
229247
fn from(value: SendEventRequest) -> Self {
230-
MatrixDriverRequestData::SendMatrixEvent(value)
248+
MatrixDriverRequestData::SendEvent(value)
231249
}
232250
}
233251

@@ -238,7 +256,7 @@ impl MatrixDriverRequest for SendEventRequest {
238256
impl FromMatrixDriverResponse for SendEventResponse {
239257
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
240258
match ev {
241-
MatrixDriverResponse::MatrixEventSent(response) => Some(response),
259+
MatrixDriverResponse::EventSent(response) => Some(response),
242260
_ => {
243261
error!("bug in MatrixDriver, received wrong event response");
244262
None
@@ -268,7 +286,7 @@ impl MatrixDriverRequest for UpdateDelayedEventRequest {
268286
impl FromMatrixDriverResponse for update_delayed_event::unstable::Response {
269287
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
270288
match ev {
271-
MatrixDriverResponse::MatrixDelayedEventUpdate(response) => Some(response),
289+
MatrixDriverResponse::DelayedEventUpdated(response) => Some(response),
272290
_ => {
273291
error!("bug in MatrixDriver, received wrong event response");
274292
None

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ruma::{
1818
delayed_events::{delayed_message_event, delayed_state_event, update_delayed_event},
1919
error::{ErrorBody, StandardErrorBody},
2020
},
21-
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType},
21+
events::{AnyTimelineEvent, TimelineEventType},
2222
serde::Raw,
2323
OwnedEventId, OwnedRoomId,
2424
};
@@ -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 ReadEventRequest {
179-
ReadStateEvent {
180-
#[serde(rename = "type")]
181-
event_type: StateEventType,
182-
state_key: StateKeySelector,
183-
},
184-
ReadMessageLikeEvent {
185-
#[serde(rename = "type")]
186-
event_type: MessageLikeEventType,
187-
limit: Option<u32>,
188-
},
182+
pub(super) struct ReadEventsRequest {
183+
#[serde(rename = "type")]
184+
pub(super) event_type: TimelineEventType,
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: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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};
@@ -49,23 +49,34 @@ pub(crate) enum IncomingMessage {
4949
/// This means that the machine previously subscribed to some events
5050
/// ([`crate::widget::Action::Subscribe`] request).
5151
MatrixEventReceived(Raw<AnyTimelineEvent>),
52+
53+
/// The `MatrixDriver` notified the `WidgetMachine` of a change in room
54+
/// state.
55+
///
56+
/// This means that the machine previously subscribed to some events
57+
/// ([`crate::widget::Action::Subscribe`] request).
58+
StateUpdateReceived(Vec<Raw<AnyStateEvent>>),
5259
}
5360

5461
pub(crate) enum MatrixDriverResponse {
5562
/// Client acquired capabilities from the user.
56-
///
57-
/// A response to an `Action::AcquireCapabilities` command.
63+
/// A response to a `MatrixDriverRequestData::AcquireCapabilities` command.
5864
CapabilitiesAcquired(Capabilities),
5965
/// Client got OpenId token for a given request ID.
60-
/// A response to an `Action::GetOpenId` command.
66+
/// A response to a `MatrixDriverRequestData::GetOpenId` command.
6167
OpenIdReceived(request_openid_token::v3::Response),
6268
/// Client read some matrix event(s).
63-
/// A response to an `Action::ReadMatrixEvent` commands.
64-
MatrixEventRead(Vec<Raw<AnyTimelineEvent>>),
69+
/// A response to a `MatrixDriverRequestData::ReadEvents` command.
70+
EventsRead(Vec<Raw<AnyTimelineEvent>>),
71+
/// Client read some matrix room state entries.
72+
/// A response to a `MatrixDriverRequestData::ReadState` command.
73+
StateRead(Vec<Raw<AnyStateEvent>>),
6574
/// Client sent some matrix event. The response contains the event ID.
66-
/// A response to an `Action::SendMatrixEvent` command.
67-
MatrixEventSent(SendEventResponse),
68-
MatrixDelayedEventUpdate(delayed_events::update_delayed_event::unstable::Response),
75+
/// A response to a `MatrixDriverRequestData::SendEvent` command.
76+
EventSent(SendEventResponse),
77+
/// Client updated a delayed event.
78+
/// A response to a `MatrixDriverRequestData::UpdateDelayedEvent` command.
79+
DelayedEventUpdated(delayed_events::update_delayed_event::unstable::Response),
6980
}
7081

7182
pub(super) struct IncomingWidgetMessage {

0 commit comments

Comments
 (0)