From 3f2fecd309b071c791a7183843e8aae341f9aa0d Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Mon, 31 May 2021 12:50:53 +0200 Subject: [PATCH] appservice: Add new_with_client_config --- matrix_sdk_appservice/src/lib.rs | 46 ++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/matrix_sdk_appservice/src/lib.rs b/matrix_sdk_appservice/src/lib.rs index c597fddc..82c5331f 100644 --- a/matrix_sdk_appservice/src/lib.rs +++ b/matrix_sdk_appservice/src/lib.rs @@ -196,6 +196,10 @@ pub struct Appservice { impl Appservice { /// Create new Appservice /// + /// Also creates and caches a [`Client`] with the [`MainAppserviceUser`]. + /// The default [`ClientConfig`] is used, if you want to customize it + /// use [`Self::new_with_client_config()`] instead. + /// /// # Arguments /// /// * `homeserver_url` - The homeserver that the client should connect to. @@ -210,22 +214,36 @@ impl Appservice { server_name: impl TryInto, registration: AppserviceRegistration, ) -> Result { - let homeserver_url = homeserver_url.try_into()?; - let server_name = server_name.try_into()?; - - let client_sender_localpart = Client::new(homeserver_url.clone())?; - - client_session_with_login_restore( - &client_sender_localpart, - ®istration, - registration.sender_localpart.as_ref(), - &server_name, + let appservice = Self::new_with_client_config( + homeserver_url, + server_name, + registration, + ClientConfig::default(), ) .await?; - let registration = Arc::new(registration); + Ok(appservice) + } - Ok(Appservice { homeserver_url, server_name, registration, client_sender_localpart }) + /// Same as [`Self::new()`] but lets you provide a [`ClientConfig`] for the + /// [`Client`] + pub async fn new_with_client_config( + homeserver_url: impl TryInto, + server_name: impl TryInto, + registration: AppserviceRegistration, + client_config: ClientConfig, + ) -> Result { + let homeserver_url = homeserver_url.try_into()?; + let server_name = server_name.try_into()?; + let registration = Arc::new(registration); + let clients = Arc::new(DashMap::new()); + + let appservice = Appservice { homeserver_url, server_name, registration, clients }; + + // we cache the [`MainAppserviceUser`] by default + appservice.client_with_config(None, client_config).await?; + + Ok(appservice) } /// Create a [`Client`] @@ -243,7 +261,7 @@ impl Appservice { /// /// [registration]: https://matrix.org/docs/spec/application_service/r0.1.2#registration /// [assert the identity]: https://matrix.org/docs/spec/application_service/r0.1.2#identity-assertion - pub async fn client(&mut self, localpart: Option<&str>) -> Result { + pub async fn client(&self, localpart: Option<&str>) -> Result { let client = self.client_with_config(localpart, ClientConfig::default()).await?; Ok(client) @@ -255,7 +273,7 @@ impl Appservice { /// Since this method is a singleton follow-up calls with different /// [`ClientConfig`]s will be ignored. pub async fn client_with_config( - &mut self, + &self, localpart: Option<&str>, config: ClientConfig, ) -> Result {