cloud.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. use std::collections::HashMap;
  2. use std::str::FromStr;
  3. use anyhow::Error;
  4. use uuid::Uuid;
  5. use flowy_error::{ErrorCode, FlowyError};
  6. use lib_infra::box_any::BoxAny;
  7. use lib_infra::future::FutureResult;
  8. use crate::entities::{
  9. SignInResponse, SignUpResponse, ThirdPartyParams, UpdateUserProfileParams, UserCredentials,
  10. UserProfile, UserWorkspace,
  11. };
  12. /// Provide the generic interface for the user cloud service
  13. /// The user cloud service is responsible for the user authentication and user profile management
  14. pub trait UserService: Send + Sync {
  15. /// Sign up a new account.
  16. /// The type of the params is defined the this trait's implementation.
  17. /// Use the `unbox_or_error` of the [BoxAny] to get the params.
  18. fn sign_up(&self, params: BoxAny) -> FutureResult<SignUpResponse, Error>;
  19. /// Sign in an account
  20. /// The type of the params is defined the this trait's implementation.
  21. fn sign_in(&self, params: BoxAny) -> FutureResult<SignInResponse, Error>;
  22. /// Sign out an account
  23. fn sign_out(&self, token: Option<String>) -> FutureResult<(), Error>;
  24. /// Using the user's token to update the user information
  25. fn update_user(
  26. &self,
  27. credential: UserCredentials,
  28. params: UpdateUserProfileParams,
  29. ) -> FutureResult<(), Error>;
  30. /// Get the user information using the user's token or uid
  31. /// return None if the user is not found
  32. fn get_user_profile(
  33. &self,
  34. credential: UserCredentials,
  35. ) -> FutureResult<Option<UserProfile>, Error>;
  36. /// Return the all the workspaces of the user
  37. fn get_user_workspaces(&self, uid: i64) -> FutureResult<Vec<UserWorkspace>, Error>;
  38. fn check_user(&self, credential: UserCredentials) -> FutureResult<(), Error>;
  39. fn add_workspace_member(
  40. &self,
  41. user_email: String,
  42. workspace_id: String,
  43. ) -> FutureResult<(), Error>;
  44. fn remove_workspace_member(
  45. &self,
  46. user_email: String,
  47. workspace_id: String,
  48. ) -> FutureResult<(), Error>;
  49. }
  50. pub fn third_party_params_from_box_any(any: BoxAny) -> Result<ThirdPartyParams, Error> {
  51. let map: HashMap<String, String> = any.unbox_or_error()?;
  52. let uuid = uuid_from_map(&map)?;
  53. let email = map.get("email").cloned().unwrap_or_default();
  54. let device_id = map.get("device_id").cloned().unwrap_or_default();
  55. Ok(ThirdPartyParams {
  56. uuid,
  57. email,
  58. device_id,
  59. })
  60. }
  61. pub fn uuid_from_map(map: &HashMap<String, String>) -> Result<Uuid, Error> {
  62. let uuid = map
  63. .get("uuid")
  64. .ok_or_else(|| FlowyError::new(ErrorCode::MissingAuthField, "Missing uuid field"))?
  65. .as_str();
  66. let uuid = Uuid::from_str(uuid)?;
  67. Ok(uuid)
  68. }