appservice: Introduce appservice mode on Client
parent
97c7baab14
commit
1a5cd544e7
|
@ -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
|
||||
|
|
|
@ -172,6 +172,10 @@ pub struct Client {
|
|||
/// Any implementor of EventHandler will act as the callbacks for various
|
||||
/// events.
|
||||
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))]
|
||||
|
@ -206,6 +210,7 @@ pub struct ClientConfig {
|
|||
pub(crate) base_config: BaseClientConfig,
|
||||
pub(crate) request_config: RequestConfig,
|
||||
pub(crate) client: Option<Arc<dyn HttpSend>>,
|
||||
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<u64>,
|
||||
pub(crate) retry_timeout: Option<Duration>,
|
||||
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<register::Response> {
|
||||
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
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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<dyn HttpSend>,
|
||||
|
@ -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<Request: OutgoingRequest>(
|
||||
&self,
|
||||
request: Request,
|
||||
|
|
|
@ -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(),
|
||||
|
|
Loading…
Reference in New Issue