Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Default homeserver (#52)
Browse files Browse the repository at this point in the history
* refactor: set a default homeserver

* fix: login with different homeserver

* chore: use unwrap_or_else
  • Loading branch information
b-avb authored Feb 28, 2024
1 parent 04d9bac commit 48d1a6f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
23 changes: 12 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = "2021"
[dependencies]
dioxus = "0.4"
dioxus-web = "0.4"
dioxus-logger = "0.4.0"
dioxus-router = "0.4.0"
futures-util = "0.3.27"
futures = "0.3"
Expand All @@ -33,3 +32,4 @@ uuid = "0.8"
unic-langid = "0.9.1"
reqwest = "0.11"
http = "0.2"
wasm-logger = "0.2.0"
20 changes: 13 additions & 7 deletions src/hooks/use_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub struct UseAuthState {
login_cache: UseSharedState<Option<CacheLogin>>,
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct UseAuth {
pub data: LoginInfoBuilder,
pub error: Option<AuthError>,
Expand Down Expand Up @@ -172,12 +172,18 @@ impl UseAuthState {
username_parse = format!("@{}", username_parse);
}

if let Some(server) = &self.data.read().server {
if let Some(domain) = server.domain() {
let domain_name = extract_domain_name(domain);
if !username_parse.ends_with(domain_name.as_str()) {
username_parse = format!("{}:{}", username_parse, domain_name);
}
if !username_parse.contains(':') {
let Some(server) = &self.data.read().server else {
return;
};

let Some(domain) = server.domain() else {
return;
};

let domain_name = extract_domain_name(domain);
if !username_parse.ends_with(domain_name.as_str()) {
username_parse = format!("{}:{}", username_parse, domain_name);
}
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use unic_langid::LanguageIdentifier;
use web_sys::window;

fn main() {
dioxus_logger::init(LevelFilter::Info).expect("failed to init logger");
wasm_logger::init(wasm_logger::Config::default());
dioxus_web::launch(App);
}

Expand Down Expand Up @@ -98,19 +98,20 @@ fn App(cx: Scope) -> Element {
HomeserverError::InvalidUrl => key_main_error_homeserver_invalid_url,
})?;

let c = create_client(&homeserver.get_base_url())
.await
.map_err(|_| {
format!(
"{} {}",
key_chat_common_error_default_server,
homeserver.get_base_url()
)
})?;
let c = match create_client(&homeserver.get_base_url()).await {
Ok(c) => c,
Err(_) => create_client(&Homeserver::default().get_base_url())
.await
.map_err(|_| {
format!(
"{} {}",
key_chat_common_error_default_server,
homeserver.get_base_url()
)
})?,
};

client.set(MatrixClientState {
client: Some(c.clone()),
});
client.set(MatrixClientState { client: Some(c) });

let serialized_session: Result<String, StorageError> =
<LocalStorage as gloo::storage::Storage>::get("session_file");
Expand Down
3 changes: 2 additions & 1 deletion src/pages/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ pub fn Login(cx: Scope) -> Element {
is_loading_loggedin.set(LoggedInStatus::Start);
return;
};
}else {
auth.set_server(homeserver.get()).await;
}

auth.set_server(homeserver.get()).await;
auth.set_username(username.get(), true);
auth.set_password(password.get());

Expand Down
8 changes: 8 additions & 0 deletions src/utils/get_homeserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ pub struct Homeserver {
base_url: String,
}

impl Default for Homeserver {
fn default() -> Self {
Self {
base_url: "https://matrix.org".to_string(),
}
}
}

pub enum HomeserverError {
InvalidUrl,
}
Expand Down

0 comments on commit 48d1a6f

Please sign in to comment.