user_server.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. use crate::{
  2. entities::{SignInParams, SignInResponse, SignUpParams, SignUpResponse, UserDetail},
  3. errors::{ErrorBuilder, UserErrCode, UserError},
  4. };
  5. use flowy_net::{
  6. config::*,
  7. future::ResultFuture,
  8. request::{http_post, HttpRequestBuilder},
  9. };
  10. use std::sync::Arc;
  11. pub trait UserServer {
  12. fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError>;
  13. fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError>;
  14. fn sign_out(&self, user_id: &str) -> ResultFuture<(), UserError>;
  15. fn get_user_info(&self, user_id: &str) -> ResultFuture<UserDetail, UserError>;
  16. }
  17. pub(crate) fn construct_server() -> Arc<dyn UserServer + Send + Sync> {
  18. if cfg!(feature = "http_server") {
  19. Arc::new(UserServerImpl {})
  20. } else {
  21. Arc::new(UserServerMock {})
  22. }
  23. }
  24. pub struct UserServerImpl {}
  25. impl UserServerImpl {
  26. pub fn new() -> Self { Self {} }
  27. }
  28. impl UserServer for UserServerImpl {
  29. fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError> {
  30. ResultFuture::new(async move { user_sign_up(params, SIGN_UP_URL.as_ref()).await })
  31. }
  32. fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError> {
  33. ResultFuture::new(async move { user_sign_in(params, SIGN_IN_URL.as_ref()).await })
  34. }
  35. fn sign_out(&self, _user_id: &str) -> ResultFuture<(), UserError> {
  36. ResultFuture::new(async { Err(ErrorBuilder::new(UserErrCode::Unknown).build()) })
  37. }
  38. fn get_user_info(&self, _user_id: &str) -> ResultFuture<UserDetail, UserError> {
  39. ResultFuture::new(async { Err(ErrorBuilder::new(UserErrCode::Unknown).build()) })
  40. }
  41. }
  42. pub async fn user_sign_up(params: SignUpParams, url: &str) -> Result<SignUpResponse, UserError> {
  43. let response = HttpRequestBuilder::post(&url.to_owned())
  44. .protobuf(params)?
  45. .send()
  46. .await?
  47. .response()
  48. .await?;
  49. Ok(response)
  50. }
  51. pub async fn user_sign_in(params: SignInParams, url: &str) -> Result<SignInResponse, UserError> {
  52. let response = HttpRequestBuilder::post(&url.to_owned())
  53. .protobuf(params)?
  54. .send()
  55. .await?
  56. .response()
  57. .await?;
  58. Ok(response)
  59. }
  60. pub struct UserServerMock {}
  61. impl UserServer for UserServerMock {
  62. fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError> {
  63. let uid = params.email.clone();
  64. ResultFuture::new(async {
  65. Ok(SignUpResponse {
  66. uid,
  67. name: params.name,
  68. email: params.email,
  69. })
  70. })
  71. }
  72. fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError> {
  73. let uid = params.email.clone();
  74. ResultFuture::new(async {
  75. Ok(SignInResponse {
  76. uid,
  77. name: params.email.clone(),
  78. email: params.email,
  79. token: "".to_string(),
  80. })
  81. })
  82. }
  83. fn sign_out(&self, _user_id: &str) -> ResultFuture<(), UserError> {
  84. ResultFuture::new(async { Ok(()) })
  85. }
  86. fn get_user_info(&self, _user_id: &str) -> ResultFuture<UserDetail, UserError> {
  87. ResultFuture::new(async { Err(ErrorBuilder::new(UserErrCode::Unknown).build()) })
  88. }
  89. }