From 5a4aa71f6aa041c700bb776ef46ce1fe7b43d9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 19 Nov 2021 09:41:48 +0100 Subject: [PATCH] feat(base): Add a From login response implementation for the Session --- crates/matrix-sdk-base/src/session.rs | 10 ++++++++++ crates/matrix-sdk/src/client.rs | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/crates/matrix-sdk-base/src/session.rs b/crates/matrix-sdk-base/src/session.rs index 6fd763f1f..a5f0620f4 100644 --- a/crates/matrix-sdk-base/src/session.rs +++ b/crates/matrix-sdk-base/src/session.rs @@ -44,3 +44,13 @@ pub struct Session { /// The ID of the client device pub device_id: DeviceIdBox, } + +impl From for Session { + fn from(response: ruma::api::client::r0::session::login::Response) -> Self { + Self { + access_token: response.access_token, + user_id: response.user_id, + device_id: response.device_id, + } + } +} diff --git a/crates/matrix-sdk/src/client.rs b/crates/matrix-sdk/src/client.rs index 54805f9a3..3b4605092 100644 --- a/crates/matrix-sdk/src/client.rs +++ b/crates/matrix-sdk/src/client.rs @@ -1213,6 +1213,28 @@ impl Client { /// # anyhow::Result::<()>::Ok(()) }); /// ``` /// + /// The `Session` object can also be created from the response the + /// [`Client::login()`] method returns: + /// + /// ```no_run + /// use matrix_sdk::{Client, Session, ruma::{DeviceIdBox, user_id}}; + /// # use url::Url; + /// # use futures::executor::block_on; + /// # block_on(async { + /// + /// let homeserver = Url::parse("http://example.com")?; + /// let client = Client::new(homeserver)?; + /// + /// let session: Session = client + /// .login("example", "my-password", None, None) + /// .await? + /// .into(); + /// + /// // Persist the `Session` so it can later be used to restore the login. + /// // client.restore_session(session).await?; + /// # anyhow::Result::<()>::Ok(()) }); + /// ``` + /// /// [`login`]: #method.login pub async fn restore_login(&self, session: Session) -> Result<()> { Ok(self.base_client.restore_login(session).await?)