Skip to content

Commit 5f3a4c9

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 2a02b21 commit 5f3a4c9

File tree

11 files changed

+614
-278
lines changed

11 files changed

+614
-278
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),
@@ -158,31 +158,37 @@ impl FromMatrixDriverResponse for request_openid_token::v3::Response {
158158
}
159159
}
160160

161-
/// Ask the client to read matrix event(s) that corresponds to the given
161+
/// Ask the client to read matrix events that correspond to the given
162162
/// description and return a list of events as a response.
163163
#[derive(Clone, Debug)]
164-
pub(crate) struct ReadMessageLikeEventRequest {
164+
pub(crate) struct ReadEventsRequest {
165165
/// The event type to read.
166-
pub(crate) event_type: MessageLikeEventType,
166+
pub(crate) event_type: TimelineEventType,
167+
168+
/// The `state_key` to read. If None, this will read events regardless of
169+
/// whether they are state events. If `Some(Any)`, this will read any state
170+
/// events of the given type. If set to a specific state key, this will only
171+
/// read state events matching that state key.
172+
pub(crate) state_key: Option<StateKeySelector>,
167173

168174
/// The maximum number of events to return.
169175
pub(crate) limit: u32,
170176
}
171177

172-
impl From<ReadMessageLikeEventRequest> for MatrixDriverRequestData {
173-
fn from(value: ReadMessageLikeEventRequest) -> Self {
174-
MatrixDriverRequestData::ReadMessageLikeEvent(value)
178+
impl From<ReadEventsRequest> for MatrixDriverRequestData {
179+
fn from(value: ReadEventsRequest) -> Self {
180+
MatrixDriverRequestData::ReadEvents(value)
175181
}
176182
}
177183

178-
impl MatrixDriverRequest for ReadMessageLikeEventRequest {
184+
impl MatrixDriverRequest for ReadEventsRequest {
179185
type Response = Vec<Raw<AnyTimelineEvent>>;
180186
}
181187

182188
impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
183189
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
184190
match ev {
185-
MatrixDriverResponse::MatrixEventRead(response) => Some(response),
191+
MatrixDriverResponse::EventsRead(response) => Some(response),
186192
_ => {
187193
error!("bug in MatrixDriver, received wrong event response");
188194
None
@@ -191,26 +197,38 @@ impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
191197
}
192198
}
193199

194-
/// Ask the client to read matrix event(s) that corresponds to the given
195-
/// description and return a list of events as a response.
200+
/// Ask the client to read matrix room state entries corresponding to the given
201+
/// description and return a list of state events as a response.
196202
#[derive(Clone, Debug)]
197-
pub(crate) struct ReadStateEventRequest {
203+
pub(crate) struct ReadStateRequest {
198204
/// The event type to read.
199205
pub(crate) event_type: StateEventType,
200206

201-
/// The `state_key` to read, or `Any` to receive any/all events of the given
202-
/// type, regardless of their `state_key`.
207+
/// The `state_key` to read, or `Any` to receive any/all room state entries
208+
/// of the given type, regardless of their `state_key`.
203209
pub(crate) state_key: StateKeySelector,
204210
}
205211

206-
impl From<ReadStateEventRequest> for MatrixDriverRequestData {
207-
fn from(value: ReadStateEventRequest) -> Self {
208-
MatrixDriverRequestData::ReadStateEvent(value)
212+
impl From<ReadStateRequest> for MatrixDriverRequestData {
213+
fn from(value: ReadStateRequest) -> Self {
214+
MatrixDriverRequestData::ReadState(value)
209215
}
210216
}
211217

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

216234
/// Ask the client to send matrix event that corresponds to the given
@@ -233,7 +251,7 @@ pub(crate) struct SendEventRequest {
233251

234252
impl From<SendEventRequest> for MatrixDriverRequestData {
235253
fn from(value: SendEventRequest) -> Self {
236-
MatrixDriverRequestData::SendMatrixEvent(value)
254+
MatrixDriverRequestData::SendEvent(value)
237255
}
238256
}
239257

@@ -244,7 +262,7 @@ impl MatrixDriverRequest for SendEventRequest {
244262
impl FromMatrixDriverResponse for SendEventResponse {
245263
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
246264
match ev {
247-
MatrixDriverResponse::MatrixEventSent(response) => Some(response),
265+
MatrixDriverResponse::EventSent(response) => Some(response),
248266
_ => {
249267
error!("bug in MatrixDriver, received wrong event response");
250268
None
@@ -274,7 +292,7 @@ impl MatrixDriverRequest for UpdateDelayedEventRequest {
274292
impl FromMatrixDriverResponse for update_delayed_event::unstable::Response {
275293
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
276294
match ev {
277-
MatrixDriverResponse::MatrixDelayedEventUpdate(response) => Some(response),
295+
MatrixDriverResponse::DelayedEventUpdated(response) => Some(response),
278296
_ => {
279297
error!("bug in MatrixDriver, received wrong event response");
280298
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)