From 1a5cd544e7e6e5cf3231e8ce44a34d2dab18e02d Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Thu, 10 Jun 2021 11:36:20 +0200 Subject: [PATCH] appservice: Introduce appservice mode on Client --- .github/workflows/ci.yml | 6 ++---- matrix_sdk/src/client.rs | 32 ++++++++++++++++++++++---------- matrix_sdk/src/error.rs | 1 - matrix_sdk/src/http_client.rs | 10 +--------- matrix_sdk_appservice/src/lib.rs | 3 ++- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 63ca9edd..059d5a26 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,14 +52,14 @@ jobs: uses: actions-rs/cargo@v1 with: command: clippy - args: --workspace --exclude matrix-sdk-appservice --all-targets -- -D warnings + args: --all-targets -- -D warnings - name: Clippy without default features uses: actions-rs/cargo@v1 with: command: clippy # TODO: add `--all-targets` once all warnings in examples are resolved - args: --workspace --exclude matrix-sdk-appservice --no-default-features --features native-tls -- -D warnings + args: --no-default-features --features native-tls,warp -- -D warnings check-wasm: name: linux / WASM @@ -251,10 +251,8 @@ jobs: uses: actions-rs/cargo@v1 with: command: build - args: --workspace --exclude matrix-sdk-appservice - name: Test uses: actions-rs/cargo@v1 with: command: test - args: --workspace --exclude matrix-sdk-appservice diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 47227b47..8068c7a7 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -172,6 +172,10 @@ pub struct Client { /// Any implementor of EventHandler will act as the callbacks for various /// events. event_handler: Arc>>, + /// Whether the client should operate in application service style mode. + /// This is low-level functionality. For an high-level API check the + /// `matrix_sdk_appservice` crate. + appservice_mode: bool, } #[cfg(not(tarpaulin_include))] @@ -206,6 +210,7 @@ pub struct ClientConfig { pub(crate) base_config: BaseClientConfig, pub(crate) request_config: RequestConfig, pub(crate) client: Option>, + pub(crate) appservice_mode: bool, } #[cfg(not(tarpaulin_include))] @@ -320,6 +325,16 @@ impl ClientConfig { self.client = Some(client); self } + + /// Puts the client into application service mode + /// + /// This is low-level functionality. For an high-level API check the + /// `matrix_sdk_appservice` crate. + #[cfg(feature = "appservice")] + pub fn appservice_mode(mut self) -> Self { + self.appservice_mode = true; + self + } } #[derive(Debug, Clone)] @@ -417,7 +432,6 @@ pub struct RequestConfig { pub(crate) retry_limit: Option, pub(crate) retry_timeout: Option, pub(crate) force_auth: bool, - #[cfg(feature = "appservice")] pub(crate) assert_identity: bool, } @@ -440,7 +454,6 @@ impl Default for RequestConfig { retry_limit: Default::default(), retry_timeout: Default::default(), force_auth: false, - #[cfg(feature = "appservice")] assert_identity: false, } } @@ -479,9 +492,7 @@ impl RequestConfig { } /// Force sending authorization even if the endpoint does not require it. - /// Default is only sending authorization if it is required - #[cfg(any(feature = "require_auth_for_profile_requests", feature = "appservice"))] - #[cfg_attr(feature = "docs", doc(cfg(any(require_auth_for_profile_requests, appservice))))] + /// Default is only sending authorization if it is required. pub(crate) fn force_auth(mut self) -> Self { self.force_auth = true; self @@ -545,6 +556,7 @@ impl Client { members_request_locks: Arc::new(DashMap::new()), typing_notice_times: Arc::new(DashMap::new()), event_handler: Arc::new(RwLock::new(None)), + appservice_mode: config.appservice_mode, }) } @@ -1363,11 +1375,11 @@ impl Client { ) -> Result { info!("Registering to {}", self.homeserver().await); - #[cfg(not(feature = "appservice"))] - let config = None; - - #[cfg(feature = "appservice")] - let config = Some(self.http_client.request_config.force_auth()); + let config = if self.appservice_mode { + Some(self.http_client.request_config.force_auth()) + } else { + None + }; let request = registration.into(); self.send(request, config).await diff --git a/matrix_sdk/src/error.rs b/matrix_sdk/src/error.rs index 2eb14ecf..ca9c77d8 100644 --- a/matrix_sdk/src/error.rs +++ b/matrix_sdk/src/error.rs @@ -91,7 +91,6 @@ pub enum HttpError { /// Tried to send a request without `user_id` in the `Session` #[error("missing user_id in session")] - #[cfg(feature = "appservice")] UserIdRequired, } diff --git a/matrix_sdk/src/http_client.rs b/matrix_sdk/src/http_client.rs index 6b12a9e1..1899b5af 100644 --- a/matrix_sdk/src/http_client.rs +++ b/matrix_sdk/src/http_client.rs @@ -25,7 +25,7 @@ use matrix_sdk_common::{async_trait, locks::RwLock, AsyncTraitDeps}; use reqwest::{Client, Response}; use ruma::api::{ client::r0::media::create_content, error::FromHttpResponseError, AuthScheme, IncomingResponse, - OutgoingRequest, SendAccessToken, + OutgoingRequest, OutgoingRequestAppserviceExt, SendAccessToken, }; use tracing::trace; use url::Url; @@ -101,9 +101,6 @@ pub(crate) struct HttpClient { pub(crate) request_config: RequestConfig, } -#[cfg(feature = "appservice")] -use ruma::api::OutgoingRequestAppserviceExt; - impl HttpClient { pub(crate) fn new( inner: Arc, @@ -125,10 +122,6 @@ impl HttpClient { None => self.request_config, }; - #[cfg(not(feature = "appservice"))] - let request = self.try_into_http_request(request, session, config).await?; - - #[cfg(feature = "appservice")] let request = if !self.request_config.assert_identity { self.try_into_http_request(request, session, config).await? } else { @@ -178,7 +171,6 @@ impl HttpClient { Ok(http_request) } - #[cfg(feature = "appservice")] async fn try_into_http_request_with_identity_assertion( &self, request: Request, diff --git a/matrix_sdk_appservice/src/lib.rs b/matrix_sdk_appservice/src/lib.rs index 3a445460..d94eff1e 100644 --- a/matrix_sdk_appservice/src/lib.rs +++ b/matrix_sdk_appservice/src/lib.rs @@ -319,7 +319,8 @@ impl Appservice { config }; - let client = Client::new_with_config(self.homeserver_url.clone(), config)?; + let client = + Client::new_with_config(self.homeserver_url.clone(), config.appservice_mode())?; let session = Session { access_token: self.registration.as_token.clone(),