-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use ClientBuilder pattern in SDK FFI #739
Changes from 1 commit
a9221c8
943ba8d
3cfaab8
184328a
f72a93d
6c23a69
f239c9d
1f6cd44
1d39e15
c155c61
79be457
ed8a69c
55b3710
0aa33df
a7c9dda
3f970ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,9 +45,10 @@ pub fn guest_client(base_path: String, homeurl: String) -> anyhow::Result<Arc<Cl | |
|
||
pub fn login_with_token(base_path: String, restore_token: String) -> anyhow::Result<Arc<Client>> { | ||
let RestoreToken { session, homeurl, is_guest } = serde_json::from_str(&restore_token)?; | ||
|
||
let builder = new_client_builder(base_path, session.user_id.to_string())? | ||
.homeserver_url(&homeurl) | ||
.user_id(&session.user_id); | ||
.homeserver_url(&homeurl); | ||
|
||
// First we need to log in. | ||
RUNTIME.block_on(async move { | ||
let client = builder.build().await?; | ||
|
@@ -61,12 +62,15 @@ pub fn login_new_client( | |
base_path: String, | ||
username: String, | ||
password: String, | ||
config: ClientConfig | ||
) -> anyhow::Result<Arc<Client>> { | ||
let builder = new_client_builder(base_path, username.clone())?; | ||
let user = Box::<UserId>::try_from(username)?; | ||
let user = Box::<UserId>::try_from(username.clone())?; | ||
let builder = new_client_builder_with_config(base_path, username, config, &user)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this ... isn't really making sense to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea I can see this is not ideal. Preferring username if both are available would not work for localhost (without the support of https), but an alternative could be to always set the server from |
||
|
||
// First we need to log in. | ||
RUNTIME.block_on(async move { | ||
let client = builder.user_id(&user).build().await?; | ||
let client = builder.build().await?; | ||
|
||
client.login(user, &password, None, None).await?; | ||
let c = Client::new(client, ClientState { is_guest: false, ..ClientState::default() }); | ||
Ok(Arc::new(c)) | ||
|
@@ -82,6 +86,28 @@ fn new_client_builder(base_path: String, home: String) -> anyhow::Result<ClientB | |
Ok(MatrixClient::builder().user_agent("rust-sdk-ios").store_config(store_config)) | ||
} | ||
|
||
fn new_client_builder_with_config(base_path: String, home: String, config: ClientConfig, user: &UserId) -> anyhow::Result<ClientBuilder> { | ||
let builder = new_client_builder(base_path, home)?; | ||
|
||
let builder = match config.homeserver { | ||
Anderas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Some(homeserver) => builder.homeserver_url(&homeserver), | ||
None => builder.user_id_server(&user) | ||
}; | ||
Anderas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let builder = match config.http_proxy { | ||
Some(proxy) => builder.proxy(proxy), | ||
None => builder | ||
}; | ||
|
||
Ok(builder) | ||
} | ||
|
||
#[derive(Default, Debug)] | ||
pub struct ClientConfig { | ||
homeserver: Option<String>, | ||
http_proxy: Option<String>, | ||
} | ||
|
||
#[derive(Default, Debug)] | ||
pub struct ClientState { | ||
is_guest: bool, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,7 @@ impl ClientBuilder { | |
/// This method is mutually exclusive with | ||
/// [`homeserver_url()`][Self::homeserver_url], if you set both whatever was | ||
/// set last will be used. | ||
pub fn user_id(self, user_id: &UserId) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we please not do this in this PR? I'd rather deprecate it for now, not remove it entirely. |
||
pub fn user_id_server(self, user_id: &UserId) -> Self { | ||
gnunicorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.server_name(user_id.server_name()) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in another comment
user_id
was actually setting server name, and that method is mutually exclusive withhomeserver_url
(as the docs state, only the last will be used). In effect thehomeurl
is always overriden byuser_id_server
, and for examplelocalhost
cannot be used properly.