[fractal] API: manage register module from app directly
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] API: manage register module from app directly
- Date: Tue, 30 Jun 2020 07:42:58 +0000 (UTC)
commit 66a67e7b37918ab97f6d0ae71b0bbdaa01a336bf
Author: Alejandro Domínguez <adomu net-c com>
Date: Sat Jun 13 08:43:56 2020 +0200
API: manage register module from app directly
fractal-gtk/src/appop/login.rs | 91 +++----------------
fractal-matrix-api/src/backend/mod.rs | 9 --
fractal-matrix-api/src/backend/register.rs | 135 +++--------------------------
fractal-matrix-api/src/backend/types.rs | 7 +-
4 files changed, 27 insertions(+), 215 deletions(-)
---
diff --git a/fractal-gtk/src/appop/login.rs b/fractal-gtk/src/appop/login.rs
index a9c058c6..4eabc0e1 100644
--- a/fractal-gtk/src/appop/login.rs
+++ b/fractal-gtk/src/appop/login.rs
@@ -82,82 +82,6 @@ impl AppOp {
backend_loop(rx);
}
- #[allow(dead_code)]
- // TODO
- /*
- pub fn register(&mut self) {
- let user_entry: gtk::Entry = self
- .ui
- .builder
- .get_object("register_username")
- .expect("Can't find register_username in ui file.");
- let pass_entry: gtk::Entry = self
- .ui
- .builder
- .get_object("register_password")
- .expect("Can't find register_password in ui file.");
- let pass_conf: gtk::Entry = self
- .ui
- .builder
- .get_object("register_password_confirm")
- .expect("Can't find register_password_confirm in ui file.");
- let server_entry: gtk::Entry = self
- .ui
- .builder
- .get_object("register_server")
- .expect("Can't find register_server in ui file.");
- let _idp_entry: gtk::Entry = self
- .ui
- .builder
- .get_object("login_idp")
- .expect("Can't find login_idp in ui file.");
-
- let username = match user_entry.get_text() {
- Some(s) => s.to_string(),
- None => String::new(),
- };
- let password = match pass_entry.get_text() {
- Some(s) => s.to_string(),
- None => String::new(),
- };
- let passconf = match pass_conf.get_text() {
- Some(s) => s.to_string(),
- None => String::new(),
- };
-
- if password != passconf {
- let msg = i18n("Passwords didn’t match, try again");
- ErrorDialog::new(false, &msg);
- return;
- }
-
- let server = match server_entry
- .get_text()
- .as_ref()
- .map(Url::parse)
- .unwrap_or(Ok(globals::DEFAULT_HOMESERVER))
- {
- Ok(u) => u,
- Err(_) => {
- let msg = i18n("Malformed server URL");
- ErrorDialog::new(false, &msg);
- return;
- }
- }
- // FIXME: ask also for the identity server
-
- //self.store_pass(username.clone(), password.clone(), server_url.clone())
- // .unwrap_or_else(|_| {
- // // TODO: show an error
- // error!("Can't store the password using libsecret");
- // });
-
- self.backend
- .send(BKCommand::Register(username, password, server, id_s))
- .unwrap();
- }
- */
-
pub fn connect(&mut self, username: String, password: String, server: Url, identity: Url) {
self.store_pass(
username.clone(),
@@ -170,9 +94,18 @@ impl AppOp {
error!("Can't store the password using libsecret");
});
- self.backend
- .send(BKCommand::Login(username, password, server, identity))
- .unwrap();
+ let tx = self.backend.clone();
+ thread::spawn(
+ move || match register::login(username, password, server.clone()) {
+ Ok((uid, tk, dev)) => {
+ APPOP!(bk_login, (uid, tk, dev, server, identity));
+ }
+ Err(err) => {
+ tx.send(BKCommand::SendBKResponse(BKResponse::LoginError(err)))
+ .expect_log("Connection closed");
+ }
+ },
+ );
}
pub fn disconnect(&self) {
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index dabc1f76..16079225 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -41,15 +41,6 @@ impl Backend {
let tx = self.tx.clone();
match cmd {
- // Register module
- Ok(BKCommand::Login(user, passwd, server, id_url)) => {
- register::login(self, user, passwd, server, id_url)
- }
- Ok(BKCommand::Register(user, passwd, server, id_url)) => {
- register::register(self, user, passwd, server, id_url)
- }
- Ok(BKCommand::Guest(server, id_url)) => register::guest(self, server, id_url),
-
// Sync module
Ok(BKCommand::Sync(server, access_token, uid, jtr, since, initial, number_tries)) => {
sync::sync(
diff --git a/fractal-matrix-api/src/backend/register.rs b/fractal-matrix-api/src/backend/register.rs
index 47fbcfdb..b2b719ef 100644
--- a/fractal-matrix-api/src/backend/register.rs
+++ b/fractal-matrix-api/src/backend/register.rs
@@ -1,4 +1,4 @@
-use std::thread;
+use ruma_identifiers::{DeviceId, UserId};
use url::Url;
use crate::error::Error;
@@ -10,68 +10,19 @@ use crate::r0::account::login::Body as LoginBody;
use crate::r0::account::login::Response as LoginResponse;
use crate::r0::account::logout::request as logout_req;
use crate::r0::account::logout::Parameters as LogoutParameters;
-use crate::r0::account::register::request as register_req;
-use crate::r0::account::register::Body as RegisterBody;
-use crate::r0::account::register::Parameters as RegisterParameters;
-use crate::r0::account::register::RegistrationKind;
-use crate::r0::account::register::Response as RegisterResponse;
use crate::r0::account::Identifier;
use crate::r0::account::UserIdentifier;
use crate::r0::server::domain_info::request as domain_info;
use crate::r0::server::domain_info::Response as DomainInfoResponse;
use crate::r0::AccessToken;
use crate::r0::Medium;
-use crate::util::ResultExpectLog;
use crate::util::HTTP_CLIENT;
-use crate::backend::types::BKResponse;
-use crate::backend::types::Backend;
-
-pub fn guest(bk: &Backend, server: Url, id_url: Url) {
- let tx = bk.tx.clone();
-
- let params = RegisterParameters {
- kind: RegistrationKind::Guest,
- };
- let body = Default::default();
-
- thread::spawn(move || {
- let query = register_req(server.clone(), ¶ms, &body)
- .map_err(Into::into)
- .and_then(|request| {
- HTTP_CLIENT
- .get_client()?
- .execute(request)?
- .json::<RegisterResponse>()
- .map_err(Into::into)
- });
-
- match query {
- Ok(response) => {
- let uid = response.user_id;
- let dev = response.device_id;
-
- if let Some(tk) = response.access_token {
- tx.send(BKResponse::Token(uid, tk, dev, server, id_url)) // TODO: Use DeviceId
- .expect_log("Connection closed");
- tx.send(BKResponse::Rooms(Ok((vec![], None))))
- .expect_log("Connection closed");
- } else {
- tx.send(BKResponse::GuestLoginError(Error::BackendError))
- .expect_log("Connection closed");
- }
- }
- Err(err) => {
- tx.send(BKResponse::GuestLoginError(err))
- .expect_log("Connection closed");
- }
- }
- });
-}
-
-pub fn login(bk: &Backend, user: String, password: String, server: Url, id_url: Url) {
- let tx = bk.tx.clone();
-
+pub fn login(
+ user: String,
+ password: String,
+ server: Url,
+) -> Result<(UserId, AccessToken, Option<DeviceId>), Error> {
let body = if globals::EMAIL_RE.is_match(&user) {
LoginBody {
auth: Auth::Password { password },
@@ -91,35 +42,14 @@ pub fn login(bk: &Backend, user: String, password: String, server: Url, id_url:
}
};
- thread::spawn(move || {
- let query = login_req(server.clone(), &body)
- .map_err(Into::into)
- .and_then(|request| {
- HTTP_CLIENT
- .get_client()?
- .execute(request)?
- .json::<LoginResponse>()
- .map_err(Into::into)
- });
-
- match query {
- Ok(response) => {
- let dev = response.device_id;
+ let request = login_req(server, &body)?;
+ let response: LoginResponse = HTTP_CLIENT.get_client()?.execute(request)?.json()?;
- if let (Some(tk), Some(uid)) = (response.access_token, response.user_id) {
- tx.send(BKResponse::Token(uid, tk, dev, server, id_url)) // TODO: Use DeviceId
- .expect_log("Connection closed");
- } else {
- tx.send(BKResponse::LoginError(Error::BackendError))
- .expect_log("Connection closed");
- }
- }
- Err(err) => {
- tx.send(BKResponse::LoginError(err))
- .expect_log("Connection closed");
- }
- }
- });
+ if let (Some(tk), Some(uid)) = (response.access_token, response.user_id) {
+ Ok((uid, tk, response.device_id))
+ } else {
+ Err(Error::BackendError)
+ }
}
pub fn logout(server: Url, access_token: AccessToken) -> Result<(), Error> {
@@ -131,45 +61,6 @@ pub fn logout(server: Url, access_token: AccessToken) -> Result<(), Error> {
Ok(())
}
-pub fn register(bk: &Backend, user: String, password: String, server: Url, id_url: Url) {
- let tx = bk.tx.clone();
-
- let params = Default::default();
- let body = RegisterBody {
- username: Some(user),
- password: Some(password),
- ..Default::default()
- };
-
- thread::spawn(move || {
- let query = register_req(server.clone(), ¶ms, &body)
- .map_err(Into::into)
- .and_then(|request| {
- HTTP_CLIENT
- .get_client()?
- .execute(request)?
- .json::<RegisterResponse>()
- .map_err(Into::into)
- });
-
- match query {
- Ok(response) => {
- let uid = response.user_id;
- let dev = response.device_id;
-
- if let Some(tk) = response.access_token {
- tx.send(BKResponse::Token(uid, tk, dev, server, id_url)) // TODO: Use DeviceId
- .expect_log("Connection closed");
- }
- }
- Err(err) => {
- tx.send(BKResponse::LoginError(err))
- .expect_log("Connection closed");
- }
- }
- });
-}
-
pub fn get_well_known(domain: Url) -> Result<DomainInfoResponse, Error> {
let request = domain_info(domain)?;
diff --git a/fractal-matrix-api/src/backend/types.rs b/fractal-matrix-api/src/backend/types.rs
index c018dceb..18462470 100644
--- a/fractal-matrix-api/src/backend/types.rs
+++ b/fractal-matrix-api/src/backend/types.rs
@@ -1,4 +1,4 @@
-use ruma_identifiers::{EventId, RoomId, UserId};
+use ruma_identifiers::{DeviceId, EventId, RoomId, UserId};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
@@ -14,9 +14,6 @@ use url::Url;
#[derive(Debug)]
pub enum BKCommand {
- Login(String, String, Url, Url),
- Register(String, String, Url, Url),
- Guest(Url, Url),
Sync(
Url,
AccessToken,
@@ -34,7 +31,7 @@ pub enum BKCommand {
#[derive(Debug)]
pub enum BKResponse {
ShutDown,
- Token(UserId, AccessToken, Option<String>, Url, Url),
+ Token(UserId, AccessToken, Option<DeviceId>, Url, Url),
Sync(Result<String, (Error, u64)>),
Rooms(Result<(Vec<Room>, Option<Room>), Error>),
UpdateRooms(Result<Vec<Room>, Error>),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]