Skip to content

Commit

Permalink
WidgetDriver: Add authenticated future update action
Browse files Browse the repository at this point in the history
  • Loading branch information
toger5 committed Jul 25, 2024
1 parent dfdea0c commit 55652a8
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 94 deletions.
20 changes: 11 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ once_cell = "1.16.0"
pin-project-lite = "0.2.9"
rand = "0.8.5"
reqwest = { version = "0.12.4", default-features = false }
ruma = { git = "https://github.com/matrix-org/ruma", rev = "f25b3220d0c3ece7720020ed180af4955a855402", features = [
ruma = { git = "https://github.com/toger5/ruma", rev = "195cc6f57464b541d6ec0e8368c0993b3f62a760", features = [
"client-api-c",
"compat-upload-signatures",
"compat-user-id",
Expand All @@ -59,7 +59,7 @@ ruma = { git = "https://github.com/matrix-org/ruma", rev = "f25b3220d0c3ece77200
"unstable-msc4075",
"unstable-msc4140",
] }
ruma-common = { git = "https://github.com/matrix-org/ruma", rev = "f25b3220d0c3ece7720020ed180af4955a855402" }
ruma-common = { git = "https://github.com/toger5/ruma", rev = "195cc6f57464b541d6ec0e8368c0993b3f62a760" }
serde = "1.0.151"
serde_html_form = "0.2.0"
serde_json = "1.0.91"
Expand Down
10 changes: 10 additions & 0 deletions bindings/matrix-sdk-ffi/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ pub fn get_element_call_required_permissions(
},
],
requires_client: true,
update_future_event: true,
send_future_event: true,
}
}

Expand Down Expand Up @@ -379,6 +381,10 @@ pub struct WidgetCapabilities {
/// This means clients should not offer to open the widget in a separate
/// browser/tab/webview that is not connected to the postmessage widget-api.
pub requires_client: bool,
/// This allows the widget to ask the client to update future events.
pub update_future_event: bool,
/// This allows the widget to send events with a delay (Futures).
pub send_future_event: bool,
}

impl From<WidgetCapabilities> for matrix_sdk::widget::Capabilities {
Expand All @@ -387,6 +393,8 @@ impl From<WidgetCapabilities> for matrix_sdk::widget::Capabilities {
read: value.read.into_iter().map(Into::into).collect(),
send: value.send.into_iter().map(Into::into).collect(),
requires_client: value.requires_client,
update_future_event: value.update_future_event,
send_future_event: value.send_future_event,
}
}
}
Expand All @@ -397,6 +405,8 @@ impl From<matrix_sdk::widget::Capabilities> for WidgetCapabilities {
read: value.read.into_iter().map(Into::into).collect(),
send: value.send.into_iter().map(Into::into).collect(),
requires_client: value.requires_client,
update_future_event: value.update_future_event,
send_future_event: value.send_future_event,
}
}
}
Expand Down
35 changes: 33 additions & 2 deletions crates/matrix-sdk/src/widget/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub struct Capabilities {
/// This means clients should not offer to open the widget in a separate
/// browser/tab/webview that is not connected to the postmessage widget-api.
pub requires_client: bool,
/// This allows the widget to ask the client to update future events.
pub update_future_event: bool,
/// This allows the widget to send events with a delay (Futures).
pub send_future_event: bool,
}

impl Capabilities {
Expand All @@ -73,6 +77,8 @@ const READ_EVENT: &str = "org.matrix.msc2762.receive.event";
const SEND_STATE: &str = "org.matrix.msc2762.send.state_event";
const READ_STATE: &str = "org.matrix.msc2762.receive.state_event";
const REQUIRES_CLIENT: &str = "io.element.requires_client";
pub const SEND_FUTURE_EVENT: &str = "org.matrix.msc4157.send.delayed_event";
pub const UPDATE_FUTURE_EVENT: &str = "org.matrix.msc4157.update.delayed_event";

impl Serialize for Capabilities {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -117,12 +123,22 @@ impl Serialize for Capabilities {
}
}

let seq_len = self.requires_client as usize + self.read.len() + self.send.len();
let seq_len = self.requires_client as usize
+ self.update_future_event as usize
+ self.send_future_event as usize
+ self.read.len()
+ self.send.len();
let mut seq = serializer.serialize_seq(Some(seq_len))?;

if self.requires_client {
seq.serialize_element(REQUIRES_CLIENT)?;
}
if self.update_future_event {
seq.serialize_element(UPDATE_FUTURE_EVENT)?;
}
if self.send_future_event {
seq.serialize_element(SEND_FUTURE_EVENT)?;
}
for filter in &self.read {
let name = match filter {
EventFilter::MessageLike(_) => READ_EVENT,
Expand All @@ -149,6 +165,8 @@ impl<'de> Deserialize<'de> for Capabilities {
{
enum Permission {
RequiresClient,
UpdateFutureEvent,
SendFutureEvent,
Read(EventFilter),
Send(EventFilter),
Unknown,
Expand All @@ -163,6 +181,12 @@ impl<'de> Deserialize<'de> for Capabilities {
if s == REQUIRES_CLIENT {
return Ok(Self::RequiresClient);
}
if s == UPDATE_FUTURE_EVENT {
return Ok(Self::UpdateFutureEvent);
}
if s == SEND_FUTURE_EVENT {
return Ok(Self::SendFutureEvent);
}

match s.split_once(':') {
Some((READ_EVENT, filter_s)) => Ok(Permission::Read(EventFilter::MessageLike(
Expand Down Expand Up @@ -211,6 +235,8 @@ impl<'de> Deserialize<'de> for Capabilities {
Permission::Send(filter) => capabilities.send.push(filter),
// ignore unknown capabilities
Permission::Unknown => {}
Permission::UpdateFutureEvent => capabilities.update_future_event = true,
Permission::SendFutureEvent => capabilities.send_future_event = true,
}
}

Expand All @@ -233,7 +259,8 @@ mod tests {
"org.matrix.msc2762.receive.state_event:m.room.member",
"org.matrix.msc2762.receive.state_event:org.matrix.msc3401.call.member",
"org.matrix.msc2762.send.event:org.matrix.rageshake_request",
"org.matrix.msc2762.send.state_event:org.matrix.msc3401.call.member#@user:matrix.server"
"org.matrix.msc2762.send.state_event:org.matrix.msc3401.call.member#@user:matrix.server",
"org.matrix.msc4140.send.delayed_event"
]"#;

let parsed = serde_json::from_str::<Capabilities>(capabilities_str).unwrap();
Expand All @@ -257,6 +284,8 @@ mod tests {
)),
],
requires_client: true,
update_future_event: true,
send_future_event: false,
};

assert_eq!(parsed, expected);
Expand Down Expand Up @@ -285,6 +314,8 @@ mod tests {
)),
],
requires_client: true,
update_future_event: false,
send_future_event: false,
};

let capabilities_str = serde_json::to_string(&capabilities).unwrap();
Expand Down
40 changes: 38 additions & 2 deletions crates/matrix-sdk/src/widget/machine/driver_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
use std::marker::PhantomData;

use ruma::{
api::client::{account::request_openid_token, future::FutureParameters},
api::client::{
account::request_openid_token,
future::{update_future, FutureParameters},
},
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType, TimelineEventType},
serde::Raw,
};
use serde::Deserialize;
use serde::{Deserialize, Serialize};

use serde_json::value::RawValue as RawJsonValue;
use tracing::error;

Expand Down Expand Up @@ -51,6 +55,9 @@ pub(crate) enum MatrixDriverRequestData {

/// Send matrix event that corresponds to the given description.
SendMatrixEvent(SendEventRequest),

/// Data for sending a UpdateFuture client server api request.
UpdateFuture(UpdateFutureRequest),
}

/// A handle to a pending `toWidget` request.
Expand Down Expand Up @@ -245,3 +252,32 @@ impl FromMatrixDriverResponse for SendEventResponse {
}
}
}

/// Ask the client to send a UpdateFuture request with the given `future_id` and `action`.
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct UpdateFutureRequest {
pub(crate) action: update_future::unstable::UpdateAction,
pub(crate) future_id: String,
}

impl From<UpdateFutureRequest> for MatrixDriverRequestData {
fn from(value: UpdateFutureRequest) -> Self {
MatrixDriverRequestData::UpdateFuture(value)
}
}

impl MatrixDriverRequest for UpdateFutureRequest {
type Response = update_future::unstable::Response;
}

impl FromMatrixDriverResponse for update_future::unstable::Response {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::MatrixFutureUpdate(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
}
}
}
}
Loading

0 comments on commit 55652a8

Please sign in to comment.