Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(appservice): Add default request config #998

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions crates/matrix-sdk-appservice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ use event_handler::AppserviceFn;
pub use matrix_sdk;
#[doc(no_inline)]
pub use matrix_sdk::ruma;
use matrix_sdk::{reqwest::Url, Client, ClientBuilder};
use matrix_sdk::{config::RequestConfig, reqwest::Url, Client, ClientBuilder};
use ruma::{
api::{
appservice::{
Expand Down Expand Up @@ -141,6 +141,7 @@ pub struct AppService {
namespaces: Arc<NamespaceCache>,
clients: Arc<DashMap<Localpart, Client>>,
event_handler: event_handler::EventHandler,
default_request_config: Option<RequestConfig>,
}

/// Builder for an AppService
Expand All @@ -150,6 +151,7 @@ pub struct AppServiceBuilder {
server_name: OwnedServerName,
registration: AppServiceRegistration,
client_builder: Option<ClientBuilder>,
default_request_config: Option<RequestConfig>,
}

impl AppServiceBuilder {
Expand All @@ -169,7 +171,13 @@ impl AppServiceBuilder {
server_name: OwnedServerName,
registration: AppServiceRegistration,
) -> Self {
AppServiceBuilder { homeserver_url, server_name, registration, client_builder: None }
AppServiceBuilder {
homeserver_url,
server_name,
registration,
client_builder: None,
default_request_config: None,
}
}

/// Set the client builder to use for the virtual user.
Expand All @@ -178,6 +186,12 @@ impl AppServiceBuilder {
self
}

/// Set the default `[RequestConfig]` to use for virtual users.
pub fn default_request_config(mut self, default_request_config: RequestConfig) -> Self {
self.default_request_config = Some(default_request_config);
self
}

/// Build the AppService.
///
/// This will also construct a [`virtual_user()`][AppService::virtual_user]
Expand All @@ -193,6 +207,7 @@ impl AppServiceBuilder {
let clients = Arc::new(DashMap::new());
let sender_localpart = registration.sender_localpart.clone();
let event_handler = event_handler::EventHandler::default();
let default_request_config = self.default_request_config;

let appservice = AppService {
homeserver_url,
Expand All @@ -201,6 +216,7 @@ impl AppServiceBuilder {
namespaces,
clients,
event_handler,
default_request_config,
};
if let Some(client_builder) = self.client_builder {
appservice
Expand Down Expand Up @@ -248,7 +264,13 @@ impl AppService {
/// [assert the identity]: https://matrix.org/docs/spec/application_service/r0.1.2#identity-assertion
pub async fn virtual_user(&self, localpart: Option<&str>) -> Result<Client> {
let localpart = localpart.unwrap_or_else(|| self.registration.sender_localpart.as_ref());
self.virtual_user_builder(localpart).build().await
let builder = match self.default_request_config {
Some(config) => self
.virtual_user_builder(localpart)
.client_builder(Client::builder().request_config(config)),
None => self.virtual_user_builder(localpart),
};
builder.build().await
}

/// Same as [`virtual_user()`][Self::virtual_user] but with
Expand Down Expand Up @@ -542,6 +564,15 @@ impl AppService {
webserver::run_server(self.clone(), host, port).await?;
Ok(())
}

/// Set the default RequestConfig
pub fn set_default_request_config(
&mut self,
request_config: Option<RequestConfig>,
) -> Result<()> {
self.default_request_config = request_config;
Ok(())
}
}

#[cfg(test)]
Expand Down