Skip to content

Commit

Permalink
appservice: Add new_with_client_config
Browse files Browse the repository at this point in the history
  • Loading branch information
johannescpk committed May 31, 2021
1 parent 2becb88 commit 3f2fecd
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions matrix_sdk_appservice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -210,22 +214,36 @@ impl Appservice {
server_name: impl TryInto<ServerNameBox, Error = identifiers::Error>,
registration: AppserviceRegistration,
) -> Result<Self> {
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,
&registration,
registration.sender_localpart.as_ref(),
&server_name,
let appservice = Self::new_with_client_config(
homeserver_url,
server_name,
registration,
ClientConfig::default(),
)
.await?;

Ok(appservice)
}

/// Same as [`Self::new()`] but lets you provide a [`ClientConfig`] for the
/// [`Client`]
pub async fn new_with_client_config(
homeserver_url: impl TryInto<Url, Error = url::ParseError>,
server_name: impl TryInto<ServerNameBox, Error = identifiers::Error>,
registration: AppserviceRegistration,
client_config: ClientConfig,
) -> Result<Self> {
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 { homeserver_url, server_name, registration, client_sender_localpart })
Ok(appservice)
}

/// Create a [`Client`]
Expand All @@ -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<Client> {
pub async fn client(&self, localpart: Option<&str>) -> Result<Client> {
let client = self.client_with_config(localpart, ClientConfig::default()).await?;

Ok(client)
Expand All @@ -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<Client> {
Expand Down

0 comments on commit 3f2fecd

Please sign in to comment.