appservice: Introduce appservice mode on Client

master
Johannes Becker 2021-06-10 11:36:20 +02:00
parent 97c7baab14
commit 1a5cd544e7
5 changed files with 27 additions and 25 deletions

View File

@ -52,14 +52,14 @@ jobs:
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
command: clippy command: clippy
args: --workspace --exclude matrix-sdk-appservice --all-targets -- -D warnings args: --all-targets -- -D warnings
- name: Clippy without default features - name: Clippy without default features
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
command: clippy command: clippy
# TODO: add `--all-targets` once all warnings in examples are resolved # 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: check-wasm:
name: linux / WASM name: linux / WASM
@ -251,10 +251,8 @@ jobs:
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
command: build command: build
args: --workspace --exclude matrix-sdk-appservice
- name: Test - name: Test
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
command: test command: test
args: --workspace --exclude matrix-sdk-appservice

View File

@ -172,6 +172,10 @@ pub struct Client {
/// Any implementor of EventHandler will act as the callbacks for various /// Any implementor of EventHandler will act as the callbacks for various
/// events. /// events.
event_handler: Arc<RwLock<Option<Handler>>>, event_handler: Arc<RwLock<Option<Handler>>>,
/// 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))] #[cfg(not(tarpaulin_include))]
@ -206,6 +210,7 @@ pub struct ClientConfig {
pub(crate) base_config: BaseClientConfig, pub(crate) base_config: BaseClientConfig,
pub(crate) request_config: RequestConfig, pub(crate) request_config: RequestConfig,
pub(crate) client: Option<Arc<dyn HttpSend>>, pub(crate) client: Option<Arc<dyn HttpSend>>,
pub(crate) appservice_mode: bool,
} }
#[cfg(not(tarpaulin_include))] #[cfg(not(tarpaulin_include))]
@ -320,6 +325,16 @@ impl ClientConfig {
self.client = Some(client); self.client = Some(client);
self 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)] #[derive(Debug, Clone)]
@ -417,7 +432,6 @@ pub struct RequestConfig {
pub(crate) retry_limit: Option<u64>, pub(crate) retry_limit: Option<u64>,
pub(crate) retry_timeout: Option<Duration>, pub(crate) retry_timeout: Option<Duration>,
pub(crate) force_auth: bool, pub(crate) force_auth: bool,
#[cfg(feature = "appservice")]
pub(crate) assert_identity: bool, pub(crate) assert_identity: bool,
} }
@ -440,7 +454,6 @@ impl Default for RequestConfig {
retry_limit: Default::default(), retry_limit: Default::default(),
retry_timeout: Default::default(), retry_timeout: Default::default(),
force_auth: false, force_auth: false,
#[cfg(feature = "appservice")]
assert_identity: false, assert_identity: false,
} }
} }
@ -479,9 +492,7 @@ impl RequestConfig {
} }
/// Force sending authorization even if the endpoint does not require it. /// Force sending authorization even if the endpoint does not require it.
/// Default is only sending authorization if it is required /// 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))))]
pub(crate) fn force_auth(mut self) -> Self { pub(crate) fn force_auth(mut self) -> Self {
self.force_auth = true; self.force_auth = true;
self self
@ -545,6 +556,7 @@ impl Client {
members_request_locks: Arc::new(DashMap::new()), members_request_locks: Arc::new(DashMap::new()),
typing_notice_times: Arc::new(DashMap::new()), typing_notice_times: Arc::new(DashMap::new()),
event_handler: Arc::new(RwLock::new(None)), event_handler: Arc::new(RwLock::new(None)),
appservice_mode: config.appservice_mode,
}) })
} }
@ -1363,11 +1375,11 @@ impl Client {
) -> Result<register::Response> { ) -> Result<register::Response> {
info!("Registering to {}", self.homeserver().await); info!("Registering to {}", self.homeserver().await);
#[cfg(not(feature = "appservice"))] let config = if self.appservice_mode {
let config = None; Some(self.http_client.request_config.force_auth())
} else {
#[cfg(feature = "appservice")] None
let config = Some(self.http_client.request_config.force_auth()); };
let request = registration.into(); let request = registration.into();
self.send(request, config).await self.send(request, config).await

View File

@ -91,7 +91,6 @@ pub enum HttpError {
/// Tried to send a request without `user_id` in the `Session` /// Tried to send a request without `user_id` in the `Session`
#[error("missing user_id in session")] #[error("missing user_id in session")]
#[cfg(feature = "appservice")]
UserIdRequired, UserIdRequired,
} }

View File

@ -25,7 +25,7 @@ use matrix_sdk_common::{async_trait, locks::RwLock, AsyncTraitDeps};
use reqwest::{Client, Response}; use reqwest::{Client, Response};
use ruma::api::{ use ruma::api::{
client::r0::media::create_content, error::FromHttpResponseError, AuthScheme, IncomingResponse, client::r0::media::create_content, error::FromHttpResponseError, AuthScheme, IncomingResponse,
OutgoingRequest, SendAccessToken, OutgoingRequest, OutgoingRequestAppserviceExt, SendAccessToken,
}; };
use tracing::trace; use tracing::trace;
use url::Url; use url::Url;
@ -101,9 +101,6 @@ pub(crate) struct HttpClient {
pub(crate) request_config: RequestConfig, pub(crate) request_config: RequestConfig,
} }
#[cfg(feature = "appservice")]
use ruma::api::OutgoingRequestAppserviceExt;
impl HttpClient { impl HttpClient {
pub(crate) fn new( pub(crate) fn new(
inner: Arc<dyn HttpSend>, inner: Arc<dyn HttpSend>,
@ -125,10 +122,6 @@ impl HttpClient {
None => self.request_config, 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 { let request = if !self.request_config.assert_identity {
self.try_into_http_request(request, session, config).await? self.try_into_http_request(request, session, config).await?
} else { } else {
@ -178,7 +171,6 @@ impl HttpClient {
Ok(http_request) Ok(http_request)
} }
#[cfg(feature = "appservice")]
async fn try_into_http_request_with_identity_assertion<Request: OutgoingRequest>( async fn try_into_http_request_with_identity_assertion<Request: OutgoingRequest>(
&self, &self,
request: Request, request: Request,

View File

@ -319,7 +319,8 @@ impl Appservice {
config 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 { let session = Session {
access_token: self.registration.as_token.clone(), access_token: self.registration.as_token.clone(),