Skip to content

Commit e6f2f48

Browse files
committed
Support for sending futures events through the widget api.
1 parent 10fd5d0 commit e6f2f48

File tree

8 files changed

+135
-42
lines changed

8 files changed

+135
-42
lines changed

Cargo.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ once_cell = "1.16.0"
4444
pin-project-lite = "0.2.9"
4545
rand = "0.8.5"
4646
reqwest = { version = "0.12.4", default-features = false }
47-
ruma = { git = "https://github.com/ruma/ruma", rev = "e5a370f7e5fcebb0da6e4945e51c5fafba9aa5f0", features = [
47+
ruma = { git = "https://github.com/ruma/ruma", rev = "c37843e9be619ffac8c4d33ad3a6a175cc32610c", features = [
4848
"client-api-c",
4949
"compat-upload-signatures",
5050
"compat-user-id",
@@ -53,9 +53,10 @@ ruma = { git = "https://github.com/ruma/ruma", rev = "e5a370f7e5fcebb0da6e4945e5
5353
"compat-encrypted-stickers",
5454
"unstable-msc3401",
5555
"unstable-msc3266",
56-
"unstable-msc4075"
56+
"unstable-msc4075",
57+
"unstable-msc4140",
5758
] }
58-
ruma-common = { git = "https://github.com/ruma/ruma", rev = "e5a370f7e5fcebb0da6e4945e51c5fafba9aa5f0" }
59+
ruma-common = { git = "https://github.com/ruma/ruma", rev = "c37843e9be619ffac8c4d33ad3a6a175cc32610c" }
5960
serde = "1.0.151"
6061
serde_html_form = "0.2.0"
6162
serde_json = "1.0.91"

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717
use std::marker::PhantomData;
1818

1919
use ruma::{
20-
api::client::account::request_openid_token,
20+
api::client::{account::request_openid_token, future::FutureParameters},
2121
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType, TimelineEventType},
2222
serde::Raw,
23-
OwnedEventId,
2423
};
2524
use serde::Deserialize;
2625
use serde_json::value::RawValue as RawJsonValue;
2726
use tracing::error;
2827

29-
use super::{incoming::MatrixDriverResponse, Action, MatrixDriverRequestMeta, WidgetMachine};
28+
use super::{
29+
from_widget::SendEventResponse, incoming::MatrixDriverResponse, Action,
30+
MatrixDriverRequestMeta, WidgetMachine,
31+
};
3032
use crate::widget::{Capabilities, StateKeySelector};
3133

3234
#[derive(Clone, Debug)]
@@ -217,6 +219,8 @@ pub(crate) struct SendEventRequest {
217219
pub(crate) state_key: Option<String>,
218220
/// Raw content of an event.
219221
pub(crate) content: Box<RawJsonValue>,
222+
/// Addition send event parameters to send a future
223+
pub(crate) future_parameters: Option<FutureParameters>,
220224
}
221225

222226
impl From<SendEventRequest> for MatrixDriverRequestData {
@@ -226,10 +230,10 @@ impl From<SendEventRequest> for MatrixDriverRequestData {
226230
}
227231

228232
impl MatrixDriverRequest for SendEventRequest {
229-
type Response = OwnedEventId;
233+
type Response = SendEventResponse;
230234
}
231235

232-
impl FromMatrixDriverResponse for OwnedEventId {
236+
impl FromMatrixDriverResponse for SendEventResponse {
233237
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
234238
match ev {
235239
MatrixDriverResponse::MatrixEventSent(response) => Some(response),

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

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
use std::fmt;
1616

1717
use ruma::{
18+
api::client::future,
1819
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType},
1920
serde::Raw,
20-
OwnedEventId, RoomId,
21+
OwnedEventId, OwnedRoomId,
2122
};
2223
use serde::{Deserialize, Serialize};
2324

@@ -132,8 +133,65 @@ pub(super) struct ReadEventResponse {
132133
pub(super) events: Vec<Raw<AnyTimelineEvent>>,
133134
}
134135

135-
#[derive(Serialize)]
136-
pub(super) struct SendEventResponse<'a> {
137-
pub(super) room_id: &'a RoomId,
138-
pub(super) event_id: OwnedEventId,
136+
#[derive(Serialize, Debug)]
137+
pub struct SendEventResponse {
138+
/// The room id for the send event.
139+
pub room_id: Option<OwnedRoomId>,
140+
/// The event id of the send event. Its optional because if its a future one does not get
141+
/// the event_id at this point.
142+
pub event_id: Option<OwnedEventId>,
143+
/// A token to send/insert the future into the DAG.
144+
pub send_token: Option<String>,
145+
/// A token to cancel this future. It will never be send if this is called.
146+
pub cancel_token: Option<String>,
147+
/// The `future_group_id` generated for this future. Used to connect multiple futures
148+
/// only one of the connected futures will be sent and inserted into the DAG.
149+
pub future_group_id: Option<String>,
150+
/// A token used to refresh the timer of the future. This allows
151+
/// to implement heartbeat like capabilities. An event is only sent once
152+
/// a refresh in the timeout interval is missed.
153+
///
154+
/// If the future does not have a timeout this will be `None`.
155+
pub refresh_token: Option<String>,
156+
}
157+
158+
impl SendEventResponse {
159+
pub fn from_event_id(event_id: OwnedEventId) -> Self {
160+
SendEventResponse {
161+
room_id: None,
162+
event_id: Some(event_id),
163+
send_token: None,
164+
cancel_token: None,
165+
future_group_id: None,
166+
refresh_token: None,
167+
}
168+
}
169+
pub fn set_room_id(&mut self, room_id: OwnedRoomId) {
170+
self.room_id = Some(room_id);
171+
}
172+
}
173+
impl Into<SendEventResponse> for future::send_future_message_event::unstable::Response {
174+
fn into(self) -> SendEventResponse {
175+
SendEventResponse {
176+
room_id: None,
177+
event_id: None,
178+
send_token: Some(self.send_token),
179+
cancel_token: Some(self.cancel_token),
180+
future_group_id: Some(self.future_group_id),
181+
refresh_token: self.refresh_token,
182+
}
183+
}
184+
}
185+
186+
impl Into<SendEventResponse> for future::send_future_state_event::unstable::Response {
187+
fn into(self) -> SendEventResponse {
188+
SendEventResponse {
189+
room_id: None,
190+
event_id: None,
191+
send_token: Some(self.send_token),
192+
cancel_token: Some(self.cancel_token),
193+
future_group_id: Some(self.future_group_id),
194+
refresh_token: self.refresh_token,
195+
}
196+
}
139197
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use ruma::{
16-
api::client::account::request_openid_token, events::AnyTimelineEvent, serde::Raw, OwnedEventId,
17-
};
15+
use ruma::{api::client::account::request_openid_token, events::AnyTimelineEvent, serde::Raw};
1816
use serde::{de, Deserialize, Deserializer};
1917
use serde_json::value::RawValue as RawJsonValue;
2018
use uuid::Uuid;
2119

22-
use super::{from_widget::FromWidgetRequest, to_widget::ToWidgetResponse};
20+
use super::{
21+
from_widget::{FromWidgetRequest, SendEventResponse},
22+
to_widget::ToWidgetResponse,
23+
};
2324
use crate::widget::Capabilities;
2425

2526
/// Incoming event that the client API must process.
@@ -56,7 +57,7 @@ pub(crate) enum MatrixDriverResponse {
5657
MatrixEventRead(Vec<Raw<AnyTimelineEvent>>),
5758
/// Client sent some matrix event. The response contains the event ID.
5859
/// A response to an `Action::SendMatrixEvent` command.
59-
MatrixEventSent(OwnedEventId),
60+
MatrixEventSent(SendEventResponse),
6061
}
6162

6263
pub(super) struct IncomingWidgetMessage {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use self::{
3535
},
3636
from_widget::{
3737
FromWidgetErrorResponse, FromWidgetRequest, ReadEventRequest, ReadEventResponse,
38-
SendEventResponse, SupportedApiVersionsResponse,
38+
SupportedApiVersionsResponse,
3939
},
4040
incoming::{IncomingWidgetMessage, IncomingWidgetMessageKind},
4141
openid::{OpenIdResponse, OpenIdState},
@@ -64,6 +64,7 @@ mod to_widget;
6464

6565
pub(crate) use self::{
6666
driver_req::{MatrixDriverRequestData, ReadStateEventRequest, SendEventRequest},
67+
from_widget::SendEventResponse,
6768
incoming::{IncomingMessage, MatrixDriverResponse},
6869
};
6970

@@ -343,10 +344,11 @@ impl WidgetMachine {
343344
}
344345

345346
let (request, action) = self.send_matrix_driver_request(request);
346-
request.then(|result, machine| {
347-
let room_id = &machine.room_id;
348-
let response = result.map(|event_id| SendEventResponse { event_id, room_id });
349-
vec![machine.send_from_widget_result_response(raw_request, response)]
347+
request.then(|mut result, machine| {
348+
if let Ok(r) = result.as_mut() {
349+
r.set_room_id(machine.room_id.clone().to_owned());
350+
}
351+
vec![machine.send_from_widget_result_response(raw_request, result)]
350352
});
351353
action
352354
}

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ use ruma::{
2222
api::client::{
2323
account::request_openid_token::v3::{Request as OpenIdRequest, Response as OpenIdResponse},
2424
filter::RoomEventFilter,
25+
future,
2526
},
2627
assign,
2728
events::{
28-
AnySyncTimelineEvent, AnyTimelineEvent, MessageLikeEventType, StateEventType,
29-
TimelineEventType,
29+
AnyMessageLikeEventContent, AnyStateEventContent, AnySyncTimelineEvent, AnyTimelineEvent,
30+
MessageLikeEventType, StateEventType, TimelineEventType,
3031
},
3132
serde::Raw,
32-
OwnedEventId, RoomId,
33+
RoomId, TransactionId,
3334
};
3435
use serde_json::value::RawValue as RawJsonValue;
3536
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
3637
use tracing::error;
3738

38-
use super::StateKeySelector;
39+
use super::{machine::SendEventResponse, StateKeySelector};
3940
use crate::{
4041
event_handler::EventHandlerDropGuard, room::MessagesOptions, HttpResult, Result, Room,
4142
};
@@ -112,11 +113,36 @@ impl MatrixDriver {
112113
event_type: TimelineEventType,
113114
state_key: Option<String>,
114115
content: Box<RawJsonValue>,
115-
) -> Result<OwnedEventId> {
116+
future: Option<future::FutureParameters>,
117+
) -> Result<SendEventResponse> {
116118
let type_str = event_type.to_string();
117-
Ok(match state_key {
118-
Some(key) => self.room.send_state_event_raw(&type_str, &key, content).await?.event_id,
119-
None => self.room.send_raw(&type_str, content).await?.event_id,
119+
Ok(match (state_key, future) {
120+
(None, None) => SendEventResponse::from_event_id(
121+
self.room.send_raw(&type_str, content).await?.event_id,
122+
),
123+
(Some(key), None) => SendEventResponse::from_event_id(
124+
self.room.send_state_event_raw(&type_str, &key, content).await?.event_id,
125+
),
126+
(None, Some(future)) => {
127+
let r = future::send_future_message_event::unstable::Request::new_raw(
128+
self.room.room_id().to_owned(),
129+
TransactionId::new().to_owned(),
130+
MessageLikeEventType::from(type_str),
131+
future,
132+
Raw::<AnyMessageLikeEventContent>::from_json(content),
133+
);
134+
self.room.client.send(r, None).await.map(|r| r.into())?
135+
}
136+
(Some(key), Some(future)) => {
137+
let r = future::send_future_state_event::unstable::Request::new_raw(
138+
self.room.room_id().to_owned(),
139+
key,
140+
StateEventType::from(type_str),
141+
future,
142+
Raw::<AnyStateEventContent>::from_json(content),
143+
);
144+
self.room.client.send(r, None).await.map(|r| r.into())?
145+
}
120146
})
121147
}
122148

crates/matrix-sdk/src/widget/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,10 @@ impl<T: CapabilitiesProvider> ProcessingContext<T> {
225225
.map_err(|e| e.to_string()),
226226

227227
MatrixDriverRequestData::SendMatrixEvent(req) => {
228-
let SendEventRequest { event_type, state_key, content } = req;
228+
let SendEventRequest { event_type, state_key, content, future_parameters } =
229+
req;
229230
self.matrix_driver
230-
.send(event_type, state_key, content)
231+
.send(event_type, state_key, content, future_parameters)
231232
.await
232233
.map(MatrixDriverResponse::MatrixEventSent)
233234
.map_err(|e| e.to_string())

0 commit comments

Comments
 (0)