diff --git a/.deny.toml b/.deny.toml index 13e59807..d9daffe9 100644 --- a/.deny.toml +++ b/.deny.toml @@ -8,23 +8,21 @@ exclude = [ ] [advisories] -vulnerability = "deny" -unmaintained = "deny" +version = 2 [licenses] -default = "deny" -unlicensed = "deny" +version = 2 allow = [ "Apache-2.0", "BSD-3-Clause", "ISC", "MIT", + "MPL-2.0", "OpenSSL", + "Unicode-3.0", "Unicode-DFS-2016", "Zlib", ] -# MPL-2.0 is copyleft but not "infectuous" like GPL -copyleft = "allow" private = { ignore = true } [[licenses.clarify]] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cb96e48f..652199ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -132,6 +132,9 @@ jobs: - name: Run Tests cmd: test-all + - name: Run Compat Tests + cmd: test-compat + - name: Run Doc Tests cmd: test-doc diff --git a/crates/ruma-client-api/CHANGELOG.md b/crates/ruma-client-api/CHANGELOG.md index 1b6f5596..cbdee709 100644 --- a/crates/ruma-client-api/CHANGELOG.md +++ b/crates/ruma-client-api/CHANGELOG.md @@ -1,8 +1,27 @@ # [unreleased] +Breaking changes: + +- `RoomSummary::heroes` now properly contains only `UserId` instead of `String` + as before. +- Change type of `client_secret` field in `ThirdpartyIdCredentials` + from `Box` to `OwnedClientSecret` + Improvements: - Add support for MSC4108 OIDC sign in and E2EE set up via QR code +- Heroes in `sync::sync_events::v4`: `SyncRequestList` and `RoomSubscription` + both have a new `include_heroes` field. `SlidingSyncRoom` has a new `heroes` + field, with a new type `SlidingSyncRoomHero`. +- Add unstable support for authenticated media endpoints, according to MSC3916. + +Bug fixes: + +- Rename `avatar` to `avatar_url` when (De)serializing + +Bug fixes: + +- `user_id` of `SlidingSyncRoomHero` is now mandatory # 0.18.0 diff --git a/crates/ruma-client-api/Cargo.toml b/crates/ruma-client-api/Cargo.toml index b1c74215..c9167092 100644 --- a/crates/ruma-client-api/Cargo.toml +++ b/crates/ruma-client-api/Cargo.toml @@ -13,7 +13,6 @@ rust-version = { workspace = true } [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] [features] # OutgoingRequest and IncomingResponse implementations @@ -49,6 +48,7 @@ unstable-msc3488 = [] unstable-msc3575 = [] unstable-msc3814 = [] unstable-msc3843 = [] +unstable-msc3916 = [] unstable-msc3983 = [] unstable-msc4108 = [] unstable-msc4121 = [] diff --git a/crates/ruma-client-api/build.rs b/crates/ruma-client-api/build.rs deleted file mode 100644 index a83086e1..00000000 --- a/crates/ruma-client-api/build.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - // Prevent unnecessary rerunning of this build script - println!("cargo:rerun-if-changed=build.rs"); - // Prevent nightly CI from erroring on docsrs attributes - println!("cargo:rustc-check-cfg=cfg(docsrs)"); -} diff --git a/crates/ruma-client-api/src/authenticated_media.rs b/crates/ruma-client-api/src/authenticated_media.rs new file mode 100644 index 00000000..23e190fb --- /dev/null +++ b/crates/ruma-client-api/src/authenticated_media.rs @@ -0,0 +1,9 @@ +//! Authenticated endpoints for the media repository, according to [MSC3916]. +//! +//! [MSC3916]: https://github.com/matrix-org/matrix-spec-proposals/pull/3916 + +pub mod get_content; +pub mod get_content_as_filename; +pub mod get_content_thumbnail; +pub mod get_media_config; +pub mod get_media_preview; diff --git a/crates/ruma-client-api/src/authenticated_media/get_content.rs b/crates/ruma-client-api/src/authenticated_media/get_content.rs new file mode 100644 index 00000000..d1cfefb0 --- /dev/null +++ b/crates/ruma-client-api/src/authenticated_media/get_content.rs @@ -0,0 +1,92 @@ +//! `GET /_matrix/client/*/media/download/{serverName}/{mediaId}` +//! +//! Retrieve content from the media store. + +pub mod unstable { + //! `/unstable/org.matrix.msc3916/` ([MSC]) + //! + //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3916 + + use std::time::Duration; + + use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE}; + use ruma_common::{ + api::{request, response, Metadata}, + metadata, IdParseError, MxcUri, OwnedServerName, + }; + + const METADATA: Metadata = metadata! { + method: GET, + rate_limited: false, + authentication: AccessToken, + history: { + unstable => "/_matrix/client/unstable/org.matrix.msc3916/media/download/:server_name/:media_id", + } + }; + + /// Request type for the `get_media_content` endpoint. + #[request(error = crate::Error)] + pub struct Request { + /// The server name from the mxc:// URI (the authoritory component). + #[ruma_api(path)] + pub server_name: OwnedServerName, + + /// The media ID from the mxc:// URI (the path component). + #[ruma_api(path)] + pub media_id: String, + + /// The maximum duration that the client is willing to wait to start receiving data, in the + /// case that the content has not yet been uploaded. + /// + /// The default value is 20 seconds. + #[ruma_api(query)] + #[serde( + with = "ruma_common::serde::duration::ms", + default = "crate::media::default_download_timeout", + skip_serializing_if = "crate::media::is_default_download_timeout" + )] + pub timeout_ms: Duration, + } + + /// Response type for the `get_media_content` endpoint. + #[response(error = crate::Error)] + pub struct Response { + /// The content that was previously uploaded. + #[ruma_api(raw_body)] + pub file: Vec, + + /// The content type of the file that was previously uploaded. + #[ruma_api(header = CONTENT_TYPE)] + pub content_type: Option, + + /// The value of the `Content-Disposition` HTTP header, possibly containing the name of the + /// file that was previously uploaded. + /// + /// See [MDN] for the syntax. + /// + /// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax + #[ruma_api(header = CONTENT_DISPOSITION)] + pub content_disposition: Option, + } + + impl Request { + /// Creates a new `Request` with the given media ID and server name. + pub fn new(media_id: String, server_name: OwnedServerName) -> Self { + Self { media_id, server_name, timeout_ms: crate::media::default_download_timeout() } + } + + /// Creates a new `Request` with the given URI. + pub fn from_uri(uri: &MxcUri) -> Result { + let (server_name, media_id) = uri.parts()?; + + Ok(Self::new(media_id.to_owned(), server_name.to_owned())) + } + } + + impl Response { + /// Creates a new `Response` with the given file contents. + pub fn new(file: Vec) -> Self { + Self { file, content_type: None, content_disposition: None } + } + } +} diff --git a/crates/ruma-client-api/src/authenticated_media/get_content_as_filename.rs b/crates/ruma-client-api/src/authenticated_media/get_content_as_filename.rs new file mode 100644 index 00000000..d861678c --- /dev/null +++ b/crates/ruma-client-api/src/authenticated_media/get_content_as_filename.rs @@ -0,0 +1,101 @@ +//! `GET /_matrix/client/*/media/download/{serverName}/{mediaId}/{fileName}` +//! +//! Retrieve content from the media store, specifying a filename to return. + +pub mod unstable { + //! `/unstable/org.matrix.msc3916/` ([MSC]) + //! + //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3916 + + use std::time::Duration; + + use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE}; + use ruma_common::{ + api::{request, response, Metadata}, + metadata, IdParseError, MxcUri, OwnedServerName, + }; + + const METADATA: Metadata = metadata! { + method: GET, + rate_limited: false, + authentication: AccessToken, + history: { + unstable => "/_matrix/client/unstable/org.matrix.msc3916/media/download/:server_name/:media_id/:filename", + } + }; + + /// Request type for the `get_media_content_as_filename` endpoint. + #[request(error = crate::Error)] + pub struct Request { + /// The server name from the mxc:// URI (the authoritory component). + #[ruma_api(path)] + pub server_name: OwnedServerName, + + /// The media ID from the mxc:// URI (the path component). + #[ruma_api(path)] + pub media_id: String, + + /// The filename to return in the `Content-Disposition` header. + #[ruma_api(path)] + pub filename: String, + + /// The maximum duration that the client is willing to wait to start receiving data, in the + /// case that the content has not yet been uploaded. + /// + /// The default value is 20 seconds. + #[ruma_api(query)] + #[serde( + with = "ruma_common::serde::duration::ms", + default = "crate::media::default_download_timeout", + skip_serializing_if = "crate::media::is_default_download_timeout" + )] + pub timeout_ms: Duration, + } + + /// Response type for the `get_media_content_as_filename` endpoint. + #[response(error = crate::Error)] + pub struct Response { + /// The content that was previously uploaded. + #[ruma_api(raw_body)] + pub file: Vec, + + /// The content type of the file that was previously uploaded. + #[ruma_api(header = CONTENT_TYPE)] + pub content_type: Option, + + /// The value of the `Content-Disposition` HTTP header, possibly containing the name of the + /// file that was previously uploaded. + /// + /// See [MDN] for the syntax. + /// + /// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax + #[ruma_api(header = CONTENT_DISPOSITION)] + pub content_disposition: Option, + } + + impl Request { + /// Creates a new `Request` with the given media ID, server name and filename. + pub fn new(media_id: String, server_name: OwnedServerName, filename: String) -> Self { + Self { + media_id, + server_name, + filename, + timeout_ms: crate::media::default_download_timeout(), + } + } + + /// Creates a new `Request` with the given URI and filename. + pub fn from_uri(uri: &MxcUri, filename: String) -> Result { + let (server_name, media_id) = uri.parts()?; + + Ok(Self::new(media_id.to_owned(), server_name.to_owned(), filename)) + } + } + + impl Response { + /// Creates a new `Response` with the given file. + pub fn new(file: Vec) -> Self { + Self { file, content_type: None, content_disposition: None } + } + } +} diff --git a/crates/ruma-client-api/src/authenticated_media/get_content_thumbnail.rs b/crates/ruma-client-api/src/authenticated_media/get_content_thumbnail.rs new file mode 100644 index 00000000..3f08bc56 --- /dev/null +++ b/crates/ruma-client-api/src/authenticated_media/get_content_thumbnail.rs @@ -0,0 +1,134 @@ +//! `GET /_matrix/client/*/media/thumbnail/{serverName}/{mediaId}` +//! +//! Get a thumbnail of content from the media store. + +pub mod unstable { + //! `/unstable/org.matrix.msc3916/` ([MSC]) + //! + //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3916 + + use std::time::Duration; + + use http::header::CONTENT_TYPE; + use js_int::UInt; + use ruma_common::{ + api::{request, response, Metadata}, + metadata, IdParseError, MxcUri, OwnedServerName, + }; + + use crate::media::get_content_thumbnail::v3::Method; + + const METADATA: Metadata = metadata! { + method: GET, + rate_limited: true, + authentication: AccessToken, + history: { + unstable => "/_matrix/client/unstable/org.matrix.msc3916/media/thumbnail/:server_name/:media_id", + } + }; + + /// Request type for the `get_content_thumbnail` endpoint. + #[request(error = crate::Error)] + pub struct Request { + /// The server name from the mxc:// URI (the authoritory component). + #[ruma_api(path)] + pub server_name: OwnedServerName, + + /// The media ID from the mxc:// URI (the path component). + #[ruma_api(path)] + pub media_id: String, + + /// The desired resizing method. + #[ruma_api(query)] + #[serde(skip_serializing_if = "Option::is_none")] + pub method: Option, + + /// The *desired* width of the thumbnail. + /// + /// The actual thumbnail may not match the size specified. + #[ruma_api(query)] + pub width: UInt, + + /// The *desired* height of the thumbnail. + /// + /// The actual thumbnail may not match the size specified. + #[ruma_api(query)] + pub height: UInt, + + /// The maximum duration that the client is willing to wait to start receiving data, in the + /// case that the content has not yet been uploaded. + /// + /// The default value is 20 seconds. + #[ruma_api(query)] + #[serde( + with = "ruma_common::serde::duration::ms", + default = "crate::media::default_download_timeout", + skip_serializing_if = "crate::media::is_default_download_timeout" + )] + pub timeout_ms: Duration, + + /// Whether the server should return an animated thumbnail. + /// + /// When `true`, the server should return an animated thumbnail if possible and supported. + /// Otherwise it must not return an animated thumbnail. + /// + /// Defaults to `false`. + #[cfg(feature = "unstable-msc2705")] + #[ruma_api(query)] + #[serde( + rename = "org.matrix.msc2705.animated", + default, + skip_serializing_if = "ruma_common::serde::is_default" + )] + pub animated: bool, + } + + /// Response type for the `get_content_thumbnail` endpoint. + #[response(error = crate::Error)] + pub struct Response { + /// A thumbnail of the requested content. + #[ruma_api(raw_body)] + pub file: Vec, + + /// The content type of the thumbnail. + #[ruma_api(header = CONTENT_TYPE)] + pub content_type: Option, + } + + impl Request { + /// Creates a new `Request` with the given media ID, server name, desired thumbnail width + /// and desired thumbnail height. + pub fn new( + media_id: String, + server_name: OwnedServerName, + width: UInt, + height: UInt, + ) -> Self { + Self { + media_id, + server_name, + method: None, + width, + height, + timeout_ms: crate::media::default_download_timeout(), + #[cfg(feature = "unstable-msc2705")] + animated: false, + } + } + + /// Creates a new `Request` with the given URI, desired thumbnail width and + /// desired thumbnail height. + pub fn from_uri(uri: &MxcUri, width: UInt, height: UInt) -> Result { + let (server_name, media_id) = uri.parts()?; + + Ok(Self::new(media_id.to_owned(), server_name.to_owned(), width, height)) + } + } + + impl Response { + /// Creates a new `Response` with the given thumbnail. + pub fn new(file: Vec) -> Self { + Self { file, content_type: None } + } + } +} diff --git a/crates/ruma-client-api/src/authenticated_media/get_media_config.rs b/crates/ruma-client-api/src/authenticated_media/get_media_config.rs new file mode 100644 index 00000000..0c5639b2 --- /dev/null +++ b/crates/ruma-client-api/src/authenticated_media/get_media_config.rs @@ -0,0 +1,51 @@ +//! `GET /_matrix/client/*/media/config` +//! +//! Gets the config for the media repository. + +pub mod unstable { + //! `/unstable/org.matrix.msc3916/` ([MSC]) + //! + //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3916 + + use js_int::UInt; + use ruma_common::{ + api::{request, response, Metadata}, + metadata, + }; + + const METADATA: Metadata = metadata! { + method: GET, + rate_limited: true, + authentication: AccessToken, + history: { + unstable => "/_matrix/client/unstable/org.matrix.msc3916/media/config", + } + }; + + /// Request type for the `get_media_config` endpoint. + #[request(error = crate::Error)] + #[derive(Default)] + pub struct Request {} + + /// Response type for the `get_media_config` endpoint. + #[response(error = crate::Error)] + pub struct Response { + /// Maximum size of upload in bytes. + #[serde(rename = "m.upload.size")] + pub upload_size: UInt, + } + + impl Request { + /// Creates an empty `Request`. + pub fn new() -> Self { + Self {} + } + } + + impl Response { + /// Creates a new `Response` with the given maximum upload size. + pub fn new(upload_size: UInt) -> Self { + Self { upload_size } + } + } +} diff --git a/crates/ruma-client-api/src/authenticated_media/get_media_preview.rs b/crates/ruma-client-api/src/authenticated_media/get_media_preview.rs new file mode 100644 index 00000000..a9a6f435 --- /dev/null +++ b/crates/ruma-client-api/src/authenticated_media/get_media_preview.rs @@ -0,0 +1,105 @@ +//! `GET /_matrix/client/*/media/preview_url` +//! +//! Get a preview for a URL. + +pub mod unstable { + //! `/unstable/org.matrix.msc3916/` ([MSC]) + //! + //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3916 + + use ruma_common::{ + api::{request, response, Metadata}, + metadata, MilliSecondsSinceUnixEpoch, + }; + use serde::Serialize; + use serde_json::value::{to_raw_value as to_raw_json_value, RawValue as RawJsonValue}; + + const METADATA: Metadata = metadata! { + method: GET, + rate_limited: true, + authentication: AccessToken, + history: { + unstable => "/_matrix/client/unstable/org.matrix.msc3916/media/preview_url", + } + }; + + /// Request type for the `get_media_preview` endpoint. + #[request(error = crate::Error)] + pub struct Request { + /// URL to get a preview of. + #[ruma_api(query)] + pub url: String, + + /// Preferred point in time (in milliseconds) to return a preview for. + #[ruma_api(query)] + #[serde(skip_serializing_if = "Option::is_none")] + pub ts: Option, + } + + /// Response type for the `get_media_preview` endpoint. + #[response(error = crate::Error)] + #[derive(Default)] + pub struct Response { + /// OpenGraph-like data for the URL. + /// + /// Differences from OpenGraph: the image size in bytes is added to the `matrix:image:size` + /// field, and `og:image` returns the MXC URI to the image, if any. + #[ruma_api(body)] + pub data: Option>, + } + + impl Request { + /// Creates a new `Request` with the given URL. + pub fn new(url: String) -> Self { + Self { url, ts: None } + } + } + + impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self { data: None } + } + + /// Creates a new `Response` with the given OpenGraph data (in a + /// `serde_json::value::RawValue`). + pub fn from_raw_value(data: Box) -> Self { + Self { data: Some(data) } + } + + /// Creates a new `Response` with the given OpenGraph data (in any kind of serializable + /// object). + pub fn from_serialize(data: &T) -> serde_json::Result { + Ok(Self { data: Some(to_raw_json_value(data)?) }) + } + } + + #[cfg(test)] + mod tests { + use assert_matches2::assert_matches; + use serde_json::{ + from_value as from_json_value, json, + value::{to_raw_value as to_raw_json_value, RawValue as RawJsonValue}, + }; + + // Since BTreeMap> deserialization doesn't seem to + // work, test that Option works + #[test] + fn raw_json_deserialize() { + type OptRawJson = Option>; + + assert_matches!(from_json_value::(json!(null)).unwrap(), None); + from_json_value::(json!("test")).unwrap().unwrap(); + from_json_value::(json!({ "a": "b" })).unwrap().unwrap(); + } + + // For completeness sake, make sure serialization works too + #[test] + fn raw_json_serialize() { + to_raw_json_value(&json!(null)).unwrap(); + to_raw_json_value(&json!("string")).unwrap(); + to_raw_json_value(&json!({})).unwrap(); + to_raw_json_value(&json!({ "a": "b" })).unwrap(); + } + } +} diff --git a/crates/ruma-client-api/src/lib.rs b/crates/ruma-client-api/src/lib.rs index 60fb978d..f185fd23 100644 --- a/crates/ruma-client-api/src/lib.rs +++ b/crates/ruma-client-api/src/lib.rs @@ -12,6 +12,8 @@ pub mod account; pub mod alias; pub mod appservice; +#[cfg(feature = "unstable-msc3916")] +pub mod authenticated_media; pub mod backup; pub mod config; pub mod context; diff --git a/crates/ruma-client-api/src/media.rs b/crates/ruma-client-api/src/media.rs index 8b52a9bc..78729a7e 100644 --- a/crates/ruma-client-api/src/media.rs +++ b/crates/ruma-client-api/src/media.rs @@ -12,12 +12,12 @@ pub mod get_media_config; pub mod get_media_preview; /// The default duration that the client should be willing to wait to start receiving data. -fn default_download_timeout() -> Duration { +pub(crate) fn default_download_timeout() -> Duration { Duration::from_secs(20) } /// Whether the given duration is the default duration that the client should be willing to wait to /// start receiving data. -fn is_default_download_timeout(timeout: &Duration) -> bool { +pub(crate) fn is_default_download_timeout(timeout: &Duration) -> bool { timeout.as_secs() == 20 } diff --git a/crates/ruma-client-api/src/media/create_content_async.rs b/crates/ruma-client-api/src/media/create_content_async.rs index 4a2b7416..284c5df3 100644 --- a/crates/ruma-client-api/src/media/create_content_async.rs +++ b/crates/ruma-client-api/src/media/create_content_async.rs @@ -1,4 +1,4 @@ -//! `POST /_matrix/media/*/upload/{serverName}/{mediaId}` +//! `PUT /_matrix/media/*/upload/{serverName}/{mediaId}` //! //! Upload media to an MXC URI that was created with create_mxc_uri. diff --git a/crates/ruma-client-api/src/sync/sync_events/v3.rs b/crates/ruma-client-api/src/sync/sync_events/v3.rs index 1e6708bd..69b95674 100644 --- a/crates/ruma-client-api/src/sync/sync_events/v3.rs +++ b/crates/ruma-client-api/src/sync/sync_events/v3.rs @@ -10,7 +10,7 @@ use ruma_common::{ metadata, presence::PresenceState, serde::Raw, - DeviceKeyAlgorithm, OwnedEventId, OwnedRoomId, + DeviceKeyAlgorithm, OwnedEventId, OwnedRoomId, OwnedUserId, }; use ruma_events::{ presence::PresenceEvent, AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent, @@ -470,7 +470,7 @@ pub struct RoomSummary { /// /// Required if room name or canonical aliases are not set or empty. #[serde(rename = "m.heroes", default, skip_serializing_if = "Vec::is_empty")] - pub heroes: Vec, + pub heroes: Vec, /// Number of users whose membership status is `join`. /// Required if field has changed since last sync; otherwise, it may be diff --git a/crates/ruma-client-api/src/sync/sync_events/v4.rs b/crates/ruma-client-api/src/sync/sync_events/v4.rs index 3d3fc91e..2a6a56c6 100644 --- a/crates/ruma-client-api/src/sync/sync_events/v4.rs +++ b/crates/ruma-client-api/src/sync/sync_events/v4.rs @@ -12,7 +12,7 @@ use ruma_common::{ api::{request, response, Metadata}, metadata, serde::{deserialize_cow_str, duration::opt_ms, Raw}, - DeviceKeyAlgorithm, MilliSecondsSinceUnixEpoch, OwnedMxcUri, OwnedRoomId, RoomId, + DeviceKeyAlgorithm, MilliSecondsSinceUnixEpoch, OwnedMxcUri, OwnedRoomId, OwnedUserId, RoomId, }; use ruma_events::{ receipt::SyncReceiptEvent, typing::SyncTypingEvent, AnyGlobalAccountDataEvent, @@ -294,10 +294,17 @@ pub struct SyncRequestList { #[serde(flatten)] pub room_details: RoomDetailsConfig, - /// If tombstoned rooms should be returned and if so, with what information attached + /// If tombstoned rooms should be returned and if so, with what information attached. #[serde(skip_serializing_if = "Option::is_none")] pub include_old_rooms: Option, + /// Request a stripped variant of membership events for the users used to calculate the room + /// name. + /// + /// Sticky. + #[serde(skip_serializing_if = "Option::is_none")] + pub include_heroes: Option, + /// Filters to apply to the list before sorting. Sticky. #[serde(skip_serializing_if = "Option::is_none")] pub filters: Option, @@ -362,6 +369,10 @@ pub struct RoomSubscription { /// The maximum number of timeline events to return per room. Sticky. #[serde(skip_serializing_if = "Option::is_none")] pub timeline_limit: Option, + + /// Include the room heroes. Sticky. + #[serde(skip_serializing_if = "Option::is_none")] + pub include_heroes: Option, } /// Operation applied to the specific SlidingSyncList @@ -479,6 +490,10 @@ pub struct SlidingSyncRoom { /// relying on the latest event. #[serde(skip_serializing_if = "Option::is_none")] pub timestamp: Option, + + /// Heroes of the room, if requested by a room subscription. + #[serde(skip_serializing_if = "Option::is_none")] + pub heroes: Option>, } impl SlidingSyncRoom { @@ -488,6 +503,29 @@ impl SlidingSyncRoom { } } +/// A sliding sync room hero. +#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct SlidingSyncRoomHero { + /// The user ID of the hero. + pub user_id: OwnedUserId, + + /// The name of the hero. + #[serde(rename = "displayname", skip_serializing_if = "Option::is_none")] + pub name: Option, + + /// The avatar of the hero. + #[serde(rename = "avatar_url", skip_serializing_if = "Option::is_none")] + pub avatar: Option, +} + +impl SlidingSyncRoomHero { + /// Creates a new `SlidingSyncRoomHero` with the given user id. + pub fn new(user_id: OwnedUserId) -> Self { + Self { user_id, name: None, avatar: None } + } +} + /// Sliding-Sync extension configuration. #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] diff --git a/crates/ruma-client-api/src/uiaa.rs b/crates/ruma-client-api/src/uiaa.rs index 50814c12..060c90a7 100644 --- a/crates/ruma-client-api/src/uiaa.rs +++ b/crates/ruma-client-api/src/uiaa.rs @@ -9,7 +9,7 @@ use ruma_common::{ api::{error::IntoHttpError, EndpointError, OutgoingResponse}, serde::{from_raw_json_value, JsonObject, StringEnum}, thirdparty::Medium, - ClientSecret, OwnedSessionId, OwnedUserId, + OwnedClientSecret, OwnedSessionId, OwnedUserId, }; use serde::{ de::{self, DeserializeOwned}, @@ -555,7 +555,7 @@ pub struct ThirdpartyIdCredentials { pub sid: OwnedSessionId, /// Identity server client secret. - pub client_secret: Box, + pub client_secret: OwnedClientSecret, /// Identity server URL. pub id_server: String, @@ -569,7 +569,7 @@ impl ThirdpartyIdCredentials { /// server address and access token. pub fn new( sid: OwnedSessionId, - client_secret: Box, + client_secret: OwnedClientSecret, id_server: String, id_access_token: String, ) -> Self { diff --git a/crates/ruma-client/Cargo.toml b/crates/ruma-client/Cargo.toml index ecdb0243..74358c9f 100644 --- a/crates/ruma-client/Cargo.toml +++ b/crates/ruma-client/Cargo.toml @@ -13,7 +13,6 @@ rust-version = { workspace = true } [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] [features] client-api = ["dep:as_variant", "dep:ruma-client-api"] diff --git a/crates/ruma-client/build.rs b/crates/ruma-client/build.rs index 0e176c4c..ef92bd6a 100644 --- a/crates/ruma-client/build.rs +++ b/crates/ruma-client/build.rs @@ -3,8 +3,6 @@ use std::{env, process}; fn main() { // Prevent unnecessary rerunning of this build script println!("cargo:rerun-if-changed=build.rs"); - // Prevent nightly CI from erroring on docsrs attributes - println!("cargo:rustc-check-cfg=cfg(docsrs)"); let tls_features = [ ("tls-native", env::var_os("CARGO_FEATURE_TLS_NATIVE").is_some()), diff --git a/crates/ruma-common/Cargo.toml b/crates/ruma-common/Cargo.toml index 3ba173bf..5639a760 100644 --- a/crates/ruma-common/Cargo.toml +++ b/crates/ruma-common/Cargo.toml @@ -12,7 +12,6 @@ rust-version = { workspace = true } [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] [features] # These feature gates exist only for the tests. Disabling them results in a diff --git a/crates/ruma-common/build.rs b/crates/ruma-common/build.rs deleted file mode 100644 index a83086e1..00000000 --- a/crates/ruma-common/build.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - // Prevent unnecessary rerunning of this build script - println!("cargo:rerun-if-changed=build.rs"); - // Prevent nightly CI from erroring on docsrs attributes - println!("cargo:rustc-check-cfg=cfg(docsrs)"); -} diff --git a/crates/ruma-events/CHANGELOG.md b/crates/ruma-events/CHANGELOG.md index bd5762d8..79decec8 100644 --- a/crates/ruma-events/CHANGELOG.md +++ b/crates/ruma-events/CHANGELOG.md @@ -1,5 +1,13 @@ # [unreleased] +Improvements: + + - Add support for encrypted stickers as sent by several bridges under the flag `compat-encrypted-stickers` + +Breaking changes: + + - `StickerEventContent::url` was replaced by `StickerEventContent::source` which is a `StickerMediaSource` + # 0.28.1 Improvements: diff --git a/crates/ruma-events/Cargo.toml b/crates/ruma-events/Cargo.toml index b0b971e0..a8f13773 100644 --- a/crates/ruma-events/Cargo.toml +++ b/crates/ruma-events/Cargo.toml @@ -12,7 +12,6 @@ rust-version = { workspace = true } [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] [features] canonical-json = ["ruma-common/canonical-json"] @@ -56,6 +55,10 @@ compat-optional = [] # Allow TagInfo to contain a stringified floating-point value for the `order` field. compat-tag-info = [] +# Support encrypted stickers, as sent by several bridges. +# https://github.com/matrix-org/matrix-spec/issues/1667 +compat-encrypted-stickers = [] + [dependencies] as_variant = { workspace = true } indexmap = { version = "2.0.0", features = ["serde"] } diff --git a/crates/ruma-events/src/sticker.rs b/crates/ruma-events/src/sticker.rs index ed3484eb..f897b62c 100644 --- a/crates/ruma-events/src/sticker.rs +++ b/crates/ruma-events/src/sticker.rs @@ -4,9 +4,77 @@ use ruma_common::OwnedMxcUri; use ruma_macros::EventContent; -use serde::{Deserialize, Serialize}; +use serde::{de, Deserialize, Serialize}; -use crate::room::ImageInfo; +#[cfg(feature = "compat-encrypted-stickers")] +use crate::room::EncryptedFile; +use crate::room::{ImageInfo, MediaSource}; + +/// The source of a sticker media file. +#[derive(Clone, Debug, Serialize)] +#[non_exhaustive] +pub enum StickerMediaSource { + /// The MXC URI to the unencrypted media file. + #[serde(rename = "url")] + Plain(OwnedMxcUri), + + /// The encryption info of the encrypted media file. + #[cfg(feature = "compat-encrypted-stickers")] + #[serde(rename = "file")] + Encrypted(Box), +} + +// Custom implementation of `Deserialize`, because serde doesn't guarantee what variant will be +// deserialized for "externally tagged"¹ enums where multiple "tag" fields exist. +// +// ¹ https://serde.rs/enum-representations.html +impl<'de> Deserialize<'de> for StickerMediaSource { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + #[derive(Deserialize)] + struct StickerMediaSourceJsonRepr { + url: Option, + #[cfg(feature = "compat-encrypted-stickers")] + file: Option>, + } + + match StickerMediaSourceJsonRepr::deserialize(deserializer)? { + StickerMediaSourceJsonRepr { + url: None, + #[cfg(feature = "compat-encrypted-stickers")] + file: None, + } => Err(de::Error::missing_field("url")), + // Prefer file if it is set + #[cfg(feature = "compat-encrypted-stickers")] + StickerMediaSourceJsonRepr { file: Some(file), .. } => { + Ok(StickerMediaSource::Encrypted(file)) + } + StickerMediaSourceJsonRepr { url: Some(url), .. } => Ok(StickerMediaSource::Plain(url)), + } + } +} + +impl From for MediaSource { + fn from(value: StickerMediaSource) -> Self { + match value { + StickerMediaSource::Plain(url) => MediaSource::Plain(url), + #[cfg(feature = "compat-encrypted-stickers")] + StickerMediaSource::Encrypted(file) => MediaSource::Encrypted(file), + } + } +} + +#[cfg(feature = "compat-encrypted-stickers")] +impl From for StickerMediaSource { + fn from(value: MediaSource) -> Self { + match value { + MediaSource::Plain(url) => StickerMediaSource::Plain(url), + MediaSource::Encrypted(file) => StickerMediaSource::Encrypted(file), + } + } +} /// The content of an `m.sticker` event. /// @@ -24,13 +92,19 @@ pub struct StickerEventContent { /// Metadata about the image referred to in `url` including a thumbnail representation. pub info: ImageInfo, - /// The URL to the sticker image. - pub url: OwnedMxcUri, + /// The media source of the sticker image. + #[serde(flatten)] + pub source: StickerMediaSource, } impl StickerEventContent { /// Creates a new `StickerEventContent` with the given body, image info and URL. pub fn new(body: String, info: ImageInfo, url: OwnedMxcUri) -> Self { - Self { body, info, url } + Self { body, info, source: StickerMediaSource::Plain(url.clone()) } + } + + /// Creates a new `StickerEventContent` with the given body, image info, URL, and media source. + pub fn with_source(body: String, info: ImageInfo, source: StickerMediaSource) -> Self { + Self { body, info, source } } } diff --git a/crates/ruma-events/tests/it/sticker.rs b/crates/ruma-events/tests/it/sticker.rs index b38836a2..82ed7ad4 100644 --- a/crates/ruma-events/tests/it/sticker.rs +++ b/crates/ruma-events/tests/it/sticker.rs @@ -4,7 +4,7 @@ use js_int::{uint, UInt}; use ruma_common::{mxc_uri, serde::CanBeEmpty, MilliSecondsSinceUnixEpoch}; use ruma_events::{ room::{ImageInfo, MediaSource, ThumbnailInfo}, - sticker::StickerEventContent, + sticker::{StickerEventContent, StickerMediaSource}, AnyMessageLikeEvent, MessageLikeEvent, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -79,7 +79,44 @@ fn content_deserialization() { let content = from_json_value::(json_data).unwrap(); assert_eq!(content.body, "Upload: my_image.jpg"); - assert_eq!(content.url, "mxc://notareal.hs/file"); + assert_matches!(content.source, StickerMediaSource::Plain(sticker_url)); + assert_eq!(sticker_url, "mxc://notareal.hs/file"); + + let encrypted_json_data = json!({ + "body": "Upload: my_image.jpg", + "file": { + "url": "mxc://notareal.hs/file", + "key": { + "kty": "oct", + "key_ops": ["encrypt", "decrypt"], + "alg": "A256CTR", + "k": "TLlG_OpX807zzQuuwv4QZGJ21_u7weemFGYJFszMn9A", + "ext": true + }, + "iv": "S22dq3NAX8wAAAAAAAAAAA", + "hashes": { + "sha256": "aWOHudBnDkJ9IwaR1Nd8XKoI7DOrqDTwt6xDPfVGN6Q" + }, + "v": "v2", + }, + "info": {}, + }); + + #[cfg(not(feature = "compat-encrypted-stickers"))] + { + from_json_value::(encrypted_json_data).unwrap_err(); + } + #[cfg(feature = "compat-encrypted-stickers")] + { + let encrypted_content = + from_json_value::(encrypted_json_data).unwrap(); + assert_eq!(encrypted_content.body, "Upload: my_image.jpg"); + assert_matches!( + encrypted_content.source, + StickerMediaSource::Encrypted(encrypted_sticker_url) + ); + assert_eq!(encrypted_sticker_url.url, "mxc://notareal.hs/file"); + } } #[test] @@ -126,7 +163,8 @@ fn event_deserialization() { assert_eq!(content.info.width, Some(uint!(1011))); assert_eq!(content.info.mimetype.as_deref(), Some("image/png")); assert_eq!(content.info.size, Some(uint!(84242))); - assert_eq!(content.url, "mxc://matrix.org/jxPXTKpyydzdHJkdFNZjTZrD"); + assert_matches!(content.source, StickerMediaSource::Plain(sticker_url)); + assert_eq!(sticker_url, "mxc://matrix.org/jxPXTKpyydzdHJkdFNZjTZrD"); assert_matches!(content.info.thumbnail_source, Some(MediaSource::Plain(thumbnail_url))); assert_eq!(thumbnail_url, "mxc://matrix.org/irnsNRS2879"); diff --git a/crates/ruma-federation-api/Cargo.toml b/crates/ruma-federation-api/Cargo.toml index f0302736..7e225b92 100644 --- a/crates/ruma-federation-api/Cargo.toml +++ b/crates/ruma-federation-api/Cargo.toml @@ -13,7 +13,6 @@ rust-version = { workspace = true } [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] [features] # Allow some mandatory fields in requests / responses to be missing, defaulting diff --git a/crates/ruma-federation-api/build.rs b/crates/ruma-federation-api/build.rs deleted file mode 100644 index a83086e1..00000000 --- a/crates/ruma-federation-api/build.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - // Prevent unnecessary rerunning of this build script - println!("cargo:rerun-if-changed=build.rs"); - // Prevent nightly CI from erroring on docsrs attributes - println!("cargo:rustc-check-cfg=cfg(docsrs)"); -} diff --git a/crates/ruma-html/Cargo.toml b/crates/ruma-html/Cargo.toml index b651069b..1883a5f8 100644 --- a/crates/ruma-html/Cargo.toml +++ b/crates/ruma-html/Cargo.toml @@ -12,7 +12,6 @@ rust-version = { workspace = true } [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] [features] matrix = ["dep:ruma-common"] diff --git a/crates/ruma-html/build.rs b/crates/ruma-html/build.rs deleted file mode 100644 index a83086e1..00000000 --- a/crates/ruma-html/build.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - // Prevent unnecessary rerunning of this build script - println!("cargo:rerun-if-changed=build.rs"); - // Prevent nightly CI from erroring on docsrs attributes - println!("cargo:rustc-check-cfg=cfg(docsrs)"); -} diff --git a/crates/ruma-identity-service-api/CHANGELOG.md b/crates/ruma-identity-service-api/CHANGELOG.md index 6b7cae51..00a072b8 100644 --- a/crates/ruma-identity-service-api/CHANGELOG.md +++ b/crates/ruma-identity-service-api/CHANGELOG.md @@ -1,5 +1,10 @@ # [unreleased] +Breaking changes: + +- Change type of `client_secret` field in `ThreePidOwnershipProof` + from `Box` to `OwnedClientSecret` + # 0.9.0 Breaking changes: diff --git a/crates/ruma-identity-service-api/src/association/unbind_3pid.rs b/crates/ruma-identity-service-api/src/association/unbind_3pid.rs index 81c21205..5388e51b 100644 --- a/crates/ruma-identity-service-api/src/association/unbind_3pid.rs +++ b/crates/ruma-identity-service-api/src/association/unbind_3pid.rs @@ -11,7 +11,7 @@ pub mod v2 { api::{request, response, Metadata}, metadata, thirdparty::Medium, - ClientSecret, OwnedSessionId, OwnedUserId, + OwnedClientSecret, OwnedSessionId, OwnedUserId, }; use serde::{Deserialize, Serialize}; @@ -96,12 +96,12 @@ pub mod v2 { pub sid: OwnedSessionId, /// The client secret passed to the `requestToken` call. - pub client_secret: Box, + pub client_secret: OwnedClientSecret, } impl ThreePidOwnershipProof { /// Creates a new `ThreePidOwnershipProof` with the given session ID and client secret. - pub fn new(sid: OwnedSessionId, client_secret: Box) -> Self { + pub fn new(sid: OwnedSessionId, client_secret: OwnedClientSecret) -> Self { Self { sid, client_secret } } } diff --git a/crates/ruma-server-util/CHANGELOG.md b/crates/ruma-server-util/CHANGELOG.md index 540a40e0..a3037b9b 100644 --- a/crates/ruma-server-util/CHANGELOG.md +++ b/crates/ruma-server-util/CHANGELOG.md @@ -1,5 +1,10 @@ # [unreleased] +Breaking changes: + +- The `XMatrix::new` method now takes `OwnedServerName` instead of `Option` + for the destination, since servers must always set the destination. + # 0.3.0 Breaking changes: diff --git a/crates/ruma-server-util/Cargo.toml b/crates/ruma-server-util/Cargo.toml index 4bf8e98c..e435a7fc 100644 --- a/crates/ruma-server-util/Cargo.toml +++ b/crates/ruma-server-util/Cargo.toml @@ -13,7 +13,6 @@ rust-version = { workspace = true } [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] [dependencies] headers = "0.4.0" diff --git a/crates/ruma-server-util/src/authorization.rs b/crates/ruma-server-util/src/authorization.rs index 7270af63..09b79d52 100644 --- a/crates/ruma-server-util/src/authorization.rs +++ b/crates/ruma-server-util/src/authorization.rs @@ -31,11 +31,11 @@ impl XMatrix { /// Construct a new X-Matrix Authorization header. pub fn new( origin: OwnedServerName, - destination: Option, + destination: OwnedServerName, key: OwnedServerSigningKeyId, sig: String, ) -> Self { - Self { origin, destination, key, sig } + Self { origin, destination: Some(destination), key, sig } } } @@ -259,7 +259,7 @@ mod tests { assert_eq!(credentials.key, key); assert_eq!(credentials.sig, sig); - let credentials = XMatrix::new(origin, None, key, sig); + let credentials = XMatrix { origin, destination: None, key, sig }; assert_eq!(credentials.encode(), header); } @@ -277,7 +277,7 @@ mod tests { assert_eq!(credentials.key, key); assert_eq!(credentials.sig, sig); - let credentials = XMatrix::new(origin, Some(destination), key, sig); + let credentials = XMatrix::new(origin, destination, key, sig); assert_eq!(credentials.encode(), header); } diff --git a/crates/ruma/Cargo.toml b/crates/ruma/Cargo.toml index 098c2e31..34168bd2 100644 --- a/crates/ruma/Cargo.toml +++ b/crates/ruma/Cargo.toml @@ -13,7 +13,6 @@ rust-version = { workspace = true } [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs"] [features] api = ["ruma-common/api"] @@ -191,6 +190,10 @@ compat-signature-id = ["ruma-signatures?/compat-signature-id"] # Allow TagInfo to contain a stringified floating-point value for the `order` field. compat-tag-info = ["ruma-events?/compat-tag-info"] +# Support encrypted stickers, as sent by several bridges. +# https://github.com/matrix-org/matrix-spec/issues/1667 +compat-encrypted-stickers = ["ruma-events?/compat-encrypted-stickers"] + # Specific compatibility for past ring public/private key documents. ring-compat = ["dep:ruma-signatures", "ruma-signatures?/ring-compat"] @@ -255,6 +258,7 @@ unstable-msc3618 = ["ruma-federation-api?/unstable-msc3618"] unstable-msc3723 = ["ruma-federation-api?/unstable-msc3723"] unstable-msc3814 = ["ruma-client-api?/unstable-msc3814"] unstable-msc3843 = ["ruma-client-api?/unstable-msc3843", "ruma-federation-api?/unstable-msc3843"] +unstable-msc3916 = ["ruma-client-api?/unstable-msc3916"] unstable-msc3927 = ["ruma-events?/unstable-msc3927"] unstable-msc3930 = ["ruma-common/unstable-msc3930"] unstable-msc3931 = ["ruma-common/unstable-msc3931"] @@ -309,6 +313,7 @@ __ci = [ "unstable-msc3723", "unstable-msc3814", "unstable-msc3843", + "unstable-msc3916", "unstable-msc3927", "unstable-msc3930", "unstable-msc3931", diff --git a/crates/ruma/build.rs b/crates/ruma/build.rs deleted file mode 100644 index a83086e1..00000000 --- a/crates/ruma/build.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - // Prevent unnecessary rerunning of this build script - println!("cargo:rerun-if-changed=build.rs"); - // Prevent nightly CI from erroring on docsrs attributes - println!("cargo:rustc-check-cfg=cfg(docsrs)"); -} diff --git a/xtask/src/ci.rs b/xtask/src/ci.rs index 0f110314..0ff57663 100644 --- a/xtask/src/ci.rs +++ b/xtask/src/ci.rs @@ -46,6 +46,8 @@ pub enum CiCmd { StableCommon, /// Run all tests with almost all features (stable) TestAll, + /// Run all tests with almost all features, including the compat features (stable) + TestCompat, /// Run doc tests with almost all features (stable) TestDoc, /// Run all the tasks that use the nightly version @@ -108,6 +110,7 @@ impl CiTask { Some(CiCmd::StableClient) => self.stable_client()?, Some(CiCmd::StableCommon) => self.stable_common()?, Some(CiCmd::TestAll) => self.test_all()?, + Some(CiCmd::TestCompat) => self.test_compat()?, Some(CiCmd::TestDoc) => self.test_doc()?, Some(CiCmd::Nightly) => self.nightly()?, Some(CiCmd::Fmt) => self.fmt()?, @@ -207,6 +210,14 @@ impl CiTask { cmd!("rustup run stable cargo test --tests --features __ci").run().map_err(Into::into) } + /// Run tests on all crates with almost all features and the compat features with the stable + /// version. + fn test_compat(&self) -> Result<()> { + cmd!("rustup run stable cargo test --tests --features __ci,compat") + .run() + .map_err(Into::into) + } + /// Run doctests on all crates with almost all features with the stable version. fn test_doc(&self) -> Result<()> { cmd!("rustup run stable cargo test --doc --features __ci").run().map_err(Into::into)