2021-08-20 06:38:03 +00:00
|
|
|
use crate::{
|
2021-09-04 08:53:58 +00:00
|
|
|
entities::{SignInParams, SignInResponse, SignUpParams, SignUpResponse, UserProfile},
|
2021-09-01 08:37:46 +00:00
|
|
|
errors::UserError,
|
2021-08-20 06:38:03 +00:00
|
|
|
};
|
|
|
|
|
2021-09-01 14:50:22 +00:00
|
|
|
use crate::{entities::UpdateUserParams, services::server::UserServerAPI};
|
2021-08-31 15:01:46 +00:00
|
|
|
use flowy_infra::future::ResultFuture;
|
2021-09-08 05:50:20 +00:00
|
|
|
use flowy_net::{
|
|
|
|
config::*,
|
|
|
|
request::{HttpRequestBuilder, ResponseMiddleware},
|
|
|
|
};
|
2021-09-01 08:08:32 +00:00
|
|
|
|
2021-09-27 15:23:23 +00:00
|
|
|
pub struct UserServer {
|
|
|
|
config: ServerConfig,
|
|
|
|
}
|
2021-08-29 14:00:42 +00:00
|
|
|
impl UserServer {
|
2021-09-27 15:23:23 +00:00
|
|
|
pub fn new(config: ServerConfig) -> Self { Self { config } }
|
2021-08-23 14:10:36 +00:00
|
|
|
}
|
2021-08-20 06:38:03 +00:00
|
|
|
|
2021-08-29 14:00:42 +00:00
|
|
|
impl UserServerAPI for UserServer {
|
2021-08-21 04:11:33 +00:00
|
|
|
fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError> {
|
2021-09-27 15:23:23 +00:00
|
|
|
let url = self.config.sign_up_url();
|
|
|
|
ResultFuture::new(async move { user_sign_up_request(params, &url).await })
|
2021-08-20 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 15:02:42 +00:00
|
|
|
fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError> {
|
2021-09-27 15:23:23 +00:00
|
|
|
let url = self.config.sign_in_url();
|
|
|
|
ResultFuture::new(async move { user_sign_in_request(params, &url).await })
|
2021-08-20 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:01:46 +00:00
|
|
|
fn sign_out(&self, token: &str) -> ResultFuture<(), UserError> {
|
2021-09-01 08:37:46 +00:00
|
|
|
let token = token.to_owned();
|
2021-09-27 15:23:23 +00:00
|
|
|
let url = self.config.sign_out_url();
|
2021-08-31 15:01:46 +00:00
|
|
|
ResultFuture::new(async move {
|
2021-09-27 15:23:23 +00:00
|
|
|
let _ = user_sign_out_request(&token, &url).await;
|
2021-08-31 15:01:46 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
2021-08-20 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 08:37:46 +00:00
|
|
|
fn update_user(&self, token: &str, params: UpdateUserParams) -> ResultFuture<(), UserError> {
|
|
|
|
let token = token.to_owned();
|
2021-09-27 15:23:23 +00:00
|
|
|
let url = self.config.user_profile_url();
|
|
|
|
ResultFuture::new(async move { update_user_profile_request(&token, params, &url).await })
|
2021-09-01 08:08:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 08:53:58 +00:00
|
|
|
fn get_user(&self, token: &str) -> ResultFuture<UserProfile, UserError> {
|
2021-09-01 08:08:32 +00:00
|
|
|
let token = token.to_owned();
|
2021-09-27 15:23:23 +00:00
|
|
|
let url = self.config.user_profile_url();
|
|
|
|
ResultFuture::new(async move { get_user_profile_request(&token, &url).await })
|
2021-08-20 06:38:03 +00:00
|
|
|
}
|
2021-09-27 15:23:23 +00:00
|
|
|
|
|
|
|
fn ws_addr(&self) -> String { self.config.ws_addr() }
|
2021-08-20 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 03:46:56 +00:00
|
|
|
use crate::{errors::ErrorCode, notify::*};
|
2021-09-08 05:50:20 +00:00
|
|
|
use flowy_net::response::FlowyResponse;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use std::sync::Arc;
|
|
|
|
lazy_static! {
|
2021-09-08 10:25:32 +00:00
|
|
|
static ref MIDDLEWARE: Arc<Middleware> = Arc::new(Middleware {});
|
2021-09-08 05:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Middleware {}
|
|
|
|
impl ResponseMiddleware for Middleware {
|
2021-09-08 10:25:32 +00:00
|
|
|
fn receive_response(&self, token: &Option<String>, response: &FlowyResponse) {
|
2021-09-08 05:50:20 +00:00
|
|
|
if let Some(error) = &response.error {
|
|
|
|
if error.is_unauthorized() {
|
|
|
|
log::error!("user unauthorized");
|
2021-09-08 10:25:32 +00:00
|
|
|
match token {
|
|
|
|
None => {},
|
|
|
|
Some(token) => {
|
|
|
|
let error = UserError::new(ErrorCode::UserUnauthorized, "");
|
2021-10-05 03:46:56 +00:00
|
|
|
dart_notify(token, UserObservable::UserUnauthorized).error(error).send()
|
2021-09-08 10:25:32 +00:00
|
|
|
},
|
|
|
|
}
|
2021-09-08 05:50:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 10:25:32 +00:00
|
|
|
pub(crate) fn request_builder() -> HttpRequestBuilder { HttpRequestBuilder::new().middleware(MIDDLEWARE.clone()) }
|
2021-09-08 05:50:20 +00:00
|
|
|
|
2021-09-01 14:50:22 +00:00
|
|
|
pub async fn user_sign_up_request(params: SignUpParams, url: &str) -> Result<SignUpResponse, UserError> {
|
2021-09-27 15:23:23 +00:00
|
|
|
let response = request_builder()
|
|
|
|
.post(&url.to_owned())
|
|
|
|
.protobuf(params)?
|
|
|
|
.response()
|
|
|
|
.await?;
|
2021-08-23 14:10:36 +00:00
|
|
|
Ok(response)
|
|
|
|
}
|
|
|
|
|
2021-09-01 14:50:22 +00:00
|
|
|
pub async fn user_sign_in_request(params: SignInParams, url: &str) -> Result<SignInResponse, UserError> {
|
2021-09-27 15:23:23 +00:00
|
|
|
let response = request_builder()
|
|
|
|
.post(&url.to_owned())
|
|
|
|
.protobuf(params)?
|
|
|
|
.response()
|
|
|
|
.await?;
|
2021-08-23 14:10:36 +00:00
|
|
|
Ok(response)
|
|
|
|
}
|
2021-08-31 09:25:08 +00:00
|
|
|
|
2021-09-01 14:50:22 +00:00
|
|
|
pub async fn user_sign_out_request(token: &str, url: &str) -> Result<(), UserError> {
|
2021-09-27 15:23:23 +00:00
|
|
|
let _ = request_builder()
|
|
|
|
.delete(&url.to_owned())
|
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.send()
|
|
|
|
.await?;
|
2021-08-31 09:25:08 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-08-31 09:56:38 +00:00
|
|
|
|
2021-09-04 08:53:58 +00:00
|
|
|
pub async fn get_user_profile_request(token: &str, url: &str) -> Result<UserProfile, UserError> {
|
2021-09-08 05:50:20 +00:00
|
|
|
let user_profile = request_builder()
|
|
|
|
.get(&url.to_owned())
|
2021-09-01 08:08:32 +00:00
|
|
|
.header(HEADER_TOKEN, token)
|
2021-08-31 09:56:38 +00:00
|
|
|
.response()
|
|
|
|
.await?;
|
2021-09-04 08:53:58 +00:00
|
|
|
Ok(user_profile)
|
2021-08-31 09:56:38 +00:00
|
|
|
}
|
2021-09-01 08:08:32 +00:00
|
|
|
|
2021-09-04 08:53:58 +00:00
|
|
|
pub async fn update_user_profile_request(token: &str, params: UpdateUserParams, url: &str) -> Result<(), UserError> {
|
2021-09-08 05:50:20 +00:00
|
|
|
let _ = request_builder()
|
|
|
|
.patch(&url.to_owned())
|
2021-09-01 08:08:32 +00:00
|
|
|
.header(HEADER_TOKEN, token)
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|