From 84bc6be8221ebef2e104f18456910a5569c40dac Mon Sep 17 00:00:00 2001 From: Ulyssa Date: Wed, 28 Feb 2024 23:21:31 -0800 Subject: [PATCH] Support following the .well-known entries for a username's domain (#209) --- README.md | 15 +++++++++++++++ src/config.rs | 2 +- src/tests.rs | 2 +- src/worker.rs | 18 ++++++++++++------ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d5fe737..2c738ec 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,20 @@ nix profile install "github:ulyssa/iamb" You can create a basic configuration in `$CONFIG_DIR/iamb/config.json` that looks like: +```json +{ + "profiles": { + "example.com": { + "user_id": "@user:example.com" + } + } +} +``` + +If you homeserver is located on a different domain than the server part of the +`user_id` and you don't have a [`/.well-known`][well_known_entry] entry, then +you can explicitly specify the homeserver URL to use: + ```json { "profiles": { @@ -122,6 +136,7 @@ iamb is released under the [Apache License, Version 2.0]. [iamb.chat]: https://iamb.chat [gomuks]: https://github.com/tulir/gomuks [weechat-matrix]: https://github.com/poljar/weechat-matrix +[well_known_entry]: https://spec.matrix.org/latest/client-server-api/#getwell-knownmatrixclient [#8]: https://github.com/ulyssa/iamb/issues/8 [#14]: https://github.com/ulyssa/iamb/issues/14 [#16]: https://github.com/ulyssa/iamb/issues/16 diff --git a/src/config.rs b/src/config.rs index 1922817..5fb28b3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -505,7 +505,7 @@ pub enum Layout { #[derive(Clone, Deserialize)] pub struct ProfileConfig { pub user_id: OwnedUserId, - pub url: Url, + pub url: Option, pub settings: Option, pub dirs: Option, pub layout: Option, diff --git a/src/tests.rs b/src/tests.rs index 9a4bbeb..975d167 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -209,7 +209,7 @@ pub fn mock_settings() -> ApplicationSettings { profile_name: "test".into(), profile: ProfileConfig { user_id: user_id!("@user:example.com").to_owned(), - url: Url::parse("https://example.com").unwrap(), + url: None, settings: None, dirs: None, layout: None, diff --git a/src/worker.rs b/src/worker.rs index 23fb105..2d2b1dc 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -738,15 +738,21 @@ impl ClientWorker { let req_config = RequestConfig::new().timeout(req_timeout).retry_timeout(req_timeout); // Set up the Matrix client for the selected profile. - let client = Client::builder() + let builder = Client::builder() .http_client(Arc::new(http)) - .homeserver_url(account.url.clone()) .sled_store(settings.matrix_dir.as_path(), None) .expect("Failed to setup up sled store for Matrix SDK") - .request_config(req_config) - .build() - .await - .expect("Failed to instantiate Matrix client"); + .request_config(req_config); + + let builder = if let Some(url) = account.url.as_ref() { + // Use the explicitly specified homeserver. + builder.homeserver_url(url.as_str()) + } else { + // Try to discover the homeserver from the user ID. + builder.server_name(account.user_id.server_name()) + }; + + let client = builder.build().await.expect("Failed to instantiate Matrix client"); let mut worker = ClientWorker { initialized: false,