appservice: Add client_with_config singleton

master
Johannes Becker 2021-05-26 14:00:59 +02:00
parent 7609c7445c
commit 2becb88c35
7 changed files with 85 additions and 50 deletions

View File

@ -16,6 +16,7 @@ docs = []
[dependencies] [dependencies]
actix-rt = { version = "2", optional = true } actix-rt = { version = "2", optional = true }
actix-web = { version = "4.0.0-beta.6", optional = true } actix-web = { version = "4.0.0-beta.6", optional = true }
dashmap = "4"
futures = "0.3" futures = "0.3"
futures-util = "0.3" futures-util = "0.3"
http = "0.2" http = "0.2"

View File

@ -34,9 +34,10 @@ impl EventHandler for AppserviceEventHandler {
if let MembershipState::Invite = event.content.membership { if let MembershipState::Invite = event.content.membership {
let user_id = UserId::try_from(event.state_key.clone()).unwrap(); let user_id = UserId::try_from(event.state_key.clone()).unwrap();
self.appservice.register(user_id.localpart()).await.unwrap(); let mut appservice = self.appservice.clone();
appservice.register(user_id.localpart()).await.unwrap();
let client = self.appservice.client(Some(user_id.localpart())).await.unwrap(); let client = appservice.client(Some(user_id.localpart())).await.unwrap();
client.join_room_by_id(room.room_id()).await.unwrap(); client.join_room_by_id(room.room_id()).await.unwrap();
} }
@ -53,7 +54,7 @@ pub async fn main() -> std::io::Result<()> {
let registration = let registration =
AppserviceRegistration::try_from_yaml_file("./tests/registration.yaml").unwrap(); AppserviceRegistration::try_from_yaml_file("./tests/registration.yaml").unwrap();
let appservice = Appservice::new(homeserver_url, server_name, registration).await.unwrap(); let mut appservice = Appservice::new(homeserver_url, server_name, registration).await.unwrap();
let event_handler = AppserviceEventHandler::new(appservice.clone()); let event_handler = AppserviceEventHandler::new(appservice.clone());

View File

@ -65,7 +65,7 @@ async fn push_transactions(
return Ok(HttpResponse::Unauthorized().finish()); return Ok(HttpResponse::Unauthorized().finish());
} }
appservice.client(None).await?.receive_transaction(request.incoming).await?; appservice.get_cached_client(None)?.receive_transaction(request.incoming).await?;
Ok(HttpResponse::Ok().json("{}")) Ok(HttpResponse::Ok().json("{}"))
} }

View File

@ -16,9 +16,6 @@ use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {
#[error("tried to run without webserver configured")]
RunWithoutServer,
#[error("missing access token")] #[error("missing access token")]
MissingAccessToken, MissingAccessToken,
@ -31,6 +28,9 @@ pub enum Error {
#[error("no port found")] #[error("no port found")]
MissingRegistrationPort, MissingRegistrationPort,
#[error("no client for localpart found")]
NoClientForLocalpart,
#[error(transparent)] #[error(transparent)]
HttpRequest(#[from] matrix_sdk::FromHttpRequestError), HttpRequest(#[from] matrix_sdk::FromHttpRequestError),

View File

@ -58,7 +58,7 @@
//! regex: '@_appservice_.*' //! regex: '@_appservice_.*'
//! ")?; //! ")?;
//! //!
//! let appservice = Appservice::new(homeserver_url, server_name, registration).await?; //! let mut appservice = Appservice::new(homeserver_url, server_name, registration).await?;
//! appservice.set_event_handler(Box::new(AppserviceEventHandler)).await?; //! appservice.set_event_handler(Box::new(AppserviceEventHandler)).await?;
//! //!
//! let (host, port) = appservice.registration().get_host_and_port()?; //! let (host, port) = appservice.registration().get_host_and_port()?;
@ -85,6 +85,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use dashmap::DashMap;
use http::Uri; use http::Uri;
#[doc(inline)] #[doc(inline)]
pub use matrix_sdk::api_appservice as api; pub use matrix_sdk::api_appservice as api;
@ -100,8 +101,7 @@ use matrix_sdk::{
assign, assign,
identifiers::{self, DeviceId, ServerNameBox, UserId}, identifiers::{self, DeviceId, ServerNameBox, UserId},
reqwest::Url, reqwest::Url,
Client, ClientConfig, EventHandler, FromHttpResponseError, HttpError, RequestConfig, Client, ClientConfig, EventHandler, FromHttpResponseError, HttpError, ServerError, Session,
ServerError, Session,
}; };
use regex::Regex; use regex::Regex;
use tracing::warn; use tracing::warn;
@ -175,21 +175,14 @@ impl Deref for AppserviceRegistration {
} }
} }
async fn client_session_with_login_restore( type Localpart = String;
client: &Client,
registration: &AppserviceRegistration,
localpart: impl AsRef<str> + Into<Box<str>>,
server_name: &ServerNameBox,
) -> Result<()> {
let session = Session {
access_token: registration.as_token.clone(),
user_id: UserId::parse_with_server_name(localpart, server_name)?,
device_id: DeviceId::new(),
};
client.restore_login(session).await?;
Ok(()) /// The main appservice user is the `sender_localpart` from the given
} /// [`AppserviceRegistration`]
///
/// Dummy type for shared documentation
#[allow(dead_code)]
pub type MainAppserviceUser = ();
/// Appservice /// Appservice
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -197,7 +190,7 @@ pub struct Appservice {
homeserver_url: Url, homeserver_url: Url,
server_name: ServerNameBox, server_name: ServerNameBox,
registration: Arc<AppserviceRegistration>, registration: Arc<AppserviceRegistration>,
client_sender_localpart: Client, clients: Arc<DashMap<Localpart, Client>>,
} }
impl Appservice { impl Appservice {
@ -235,12 +228,14 @@ impl Appservice {
Ok(Appservice { homeserver_url, server_name, registration, client_sender_localpart }) Ok(Appservice { homeserver_url, server_name, registration, client_sender_localpart })
} }
/// Get a [`Client`] /// Create a [`Client`]
/// ///
/// Will return a `Client` that's configured to [assert the identity] on all /// Will create and return a [`Client`] that's configured to [assert the
/// outgoing homeserver requests if `localpart` is given. If not given /// identity] on all outgoing homeserver requests if `localpart` is
/// the `Client` will use the main user associated with this appservice, /// given. If not given the [`Client`] will use the [`MainAppserviceUser`].
/// that is the `sender_localpart` in the [`AppserviceRegistration`] ///
/// This method is a singleton that saves the client internally for re-use
/// based on the `localpart`.
/// ///
/// # Arguments /// # Arguments
/// ///
@ -248,26 +243,47 @@ impl Appservice {
/// ///
/// [registration]: https://matrix.org/docs/spec/application_service/r0.1.2#registration /// [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 /// [assert the identity]: https://matrix.org/docs/spec/application_service/r0.1.2#identity-assertion
pub async fn client(&self, localpart: Option<&str>) -> Result<Client> { pub async fn client(&mut self, localpart: Option<&str>) -> Result<Client> {
let client = self.client_with_config(localpart, ClientConfig::default()).await?;
Ok(client)
}
/// Same as [`Self::client`] but with the ability to pass in a
/// [`ClientConfig`]
///
/// Since this method is a singleton follow-up calls with different
/// [`ClientConfig`]s will be ignored.
pub async fn client_with_config(
&mut self,
localpart: Option<&str>,
config: ClientConfig,
) -> Result<Client> {
let localpart = localpart.unwrap_or_else(|| self.registration.sender_localpart.as_ref()); let localpart = localpart.unwrap_or_else(|| self.registration.sender_localpart.as_ref());
// The `as_token` in the `Session` maps to the main appservice user let client = if let Some(client) = self.clients.get(localpart) {
client.clone()
} else {
let user_id = UserId::parse_with_server_name(localpart, &self.server_name)?;
// The `as_token` in the `Session` maps to the [`MainAppserviceUser`]
// (`sender_localpart`) by default, so we don't need to assert identity // (`sender_localpart`) by default, so we don't need to assert identity
// in that case // in that case
let client = if localpart == self.registration.sender_localpart { if localpart != self.registration.sender_localpart {
self.client_sender_localpart.clone() config.get_request_config().assert_identity();
} else { }
let request_config = RequestConfig::default().assert_identity();
let config = ClientConfig::default().request_config(request_config);
let client = Client::new_with_config(self.homeserver_url.clone(), config)?; let client = Client::new_with_config(self.homeserver_url.clone(), config)?;
client_session_with_login_restore( let session = Session {
&client, access_token: self.registration.as_token.clone(),
&self.registration, user_id: user_id.clone(),
localpart, // TODO: expose & proper E2EE
&self.server_name, device_id: DeviceId::new(),
) };
.await?;
client.restore_login(session).await?;
self.clients.insert(localpart.to_owned(), client.clone());
client client
}; };
@ -275,9 +291,26 @@ impl Appservice {
Ok(client) Ok(client)
} }
/// Get cached [`Client`]
///
/// Will return the client for the given `localpart` if previously
/// constructed with [`Self::client()`] or [`Self::client_with_config()`].
/// If no client for the `localpart` is found it will return an Error.
pub fn get_cached_client(&self, localpart: Option<&str>) -> Result<Client> {
let localpart = localpart.unwrap_or_else(|| self.registration.sender_localpart.as_ref());
let entry = self.clients.get(localpart).ok_or(Error::NoClientForLocalpart)?;
Ok(entry.value().clone())
}
/// Convenience wrapper around [`Client::set_event_handler()`] /// Convenience wrapper around [`Client::set_event_handler()`]
pub async fn set_event_handler(&self, handler: Box<dyn EventHandler>) -> Result<()> { ///
/// Attaches the event handler to [`Self::client()`] with `None` as
/// `localpart`
pub async fn set_event_handler(&mut self, handler: Box<dyn EventHandler>) -> Result<()> {
let client = self.client(None).await?; let client = self.client(None).await?;
client.set_event_handler(handler).await; client.set_event_handler(handler).await;
Ok(()) Ok(())
@ -290,7 +323,7 @@ impl Appservice {
/// ///
/// * `localpart` - The localpart of the user to register. Must be covered /// * `localpart` - The localpart of the user to register. Must be covered
/// by the namespaces in the [`Registration`] in order to succeed. /// by the namespaces in the [`Registration`] in order to succeed.
pub async fn register(&self, localpart: impl AsRef<str>) -> Result<()> { pub async fn register(&mut self, localpart: impl AsRef<str>) -> Result<()> {
let request = assign!(RegistrationRequest::new(), { let request = assign!(RegistrationRequest::new(), {
username: Some(localpart.as_ref()), username: Some(localpart.as_ref()),
login_type: Some(&LoginType::ApplicationService), login_type: Some(&LoginType::ApplicationService),

View File

@ -12,7 +12,7 @@ mod actix {
Appservice::new( Appservice::new(
mockito::server_url().as_ref(), mockito::server_url().as_ref(),
"test.local", "test.local",
AppserviceRegistration::try_from_yaml_file("./tests/registration.yaml").unwrap(), AppserviceRegistration::try_from_yaml_str(include_str!("./registration.yaml")).unwrap(),
) )
.await .await
.unwrap() .unwrap()

View File

@ -59,7 +59,7 @@ fn member_json() -> serde_json::Value {
#[async_test] #[async_test]
async fn test_event_handler() -> Result<()> { async fn test_event_handler() -> Result<()> {
let appservice = appservice(None).await?; let mut appservice = appservice(None).await?;
struct Example {} struct Example {}
@ -94,7 +94,7 @@ async fn test_event_handler() -> Result<()> {
#[async_test] #[async_test]
async fn test_transaction() -> Result<()> { async fn test_transaction() -> Result<()> {
let appservice = appservice(None).await?; let mut appservice = appservice(None).await?;
let event = serde_json::from_value::<AnyStateEvent>(member_json()).unwrap(); let event = serde_json::from_value::<AnyStateEvent>(member_json()).unwrap();
let event: Raw<AnyRoomEvent> = AnyRoomEvent::State(event).into(); let event: Raw<AnyRoomEvent> = AnyRoomEvent::State(event).into();