cloud.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. use std::collections::HashMap;
  2. use std::fmt::{Display, Formatter};
  3. use std::str::FromStr;
  4. use anyhow::Error;
  5. use collab_define::CollabObject;
  6. use serde::{Deserialize, Serialize};
  7. use serde_json::Value;
  8. use uuid::Uuid;
  9. use flowy_error::{ErrorCode, FlowyError};
  10. use lib_infra::box_any::BoxAny;
  11. use lib_infra::future::FutureResult;
  12. use crate::entities::{
  13. SignInResponse, SignUpResponse, ThirdPartyParams, UpdateUserProfileParams, UserCredentials,
  14. UserProfile, UserWorkspace,
  15. };
  16. #[derive(Debug, Clone, Serialize, Deserialize)]
  17. pub struct UserCloudConfig {
  18. pub enable_sync: bool,
  19. enable_encrypt: bool,
  20. // The secret used to encrypt the user's data
  21. pub encrypt_secret: String,
  22. }
  23. impl UserCloudConfig {
  24. pub fn new(encrypt_secret: String) -> Self {
  25. Self {
  26. enable_sync: true,
  27. enable_encrypt: false,
  28. encrypt_secret,
  29. }
  30. }
  31. pub fn enable_encrypt(&self) -> bool {
  32. self.enable_encrypt
  33. }
  34. pub fn with_enable_encrypt(mut self, enable_encrypt: bool) -> Self {
  35. self.enable_encrypt = enable_encrypt;
  36. // When the enable_encrypt is true, the encrypt_secret should not be empty
  37. debug_assert!(!self.encrypt_secret.is_empty());
  38. self
  39. }
  40. }
  41. impl Display for UserCloudConfig {
  42. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  43. write!(
  44. f,
  45. "enable_sync: {}, enable_encrypt: {}",
  46. self.enable_sync, self.enable_encrypt
  47. )
  48. }
  49. }
  50. /// Provide the generic interface for the user cloud service
  51. /// The user cloud service is responsible for the user authentication and user profile management
  52. pub trait UserService: Send + Sync {
  53. /// Sign up a new account.
  54. /// The type of the params is defined the this trait's implementation.
  55. /// Use the `unbox_or_error` of the [BoxAny] to get the params.
  56. fn sign_up(&self, params: BoxAny) -> FutureResult<SignUpResponse, Error>;
  57. /// Sign in an account
  58. /// The type of the params is defined the this trait's implementation.
  59. fn sign_in(&self, params: BoxAny) -> FutureResult<SignInResponse, Error>;
  60. /// Sign out an account
  61. fn sign_out(&self, token: Option<String>) -> FutureResult<(), Error>;
  62. /// Using the user's token to update the user information
  63. fn update_user(
  64. &self,
  65. credential: UserCredentials,
  66. params: UpdateUserProfileParams,
  67. ) -> FutureResult<(), Error>;
  68. /// Get the user information using the user's token or uid
  69. /// return None if the user is not found
  70. fn get_user_profile(
  71. &self,
  72. credential: UserCredentials,
  73. ) -> FutureResult<Option<UserProfile>, Error>;
  74. /// Return the all the workspaces of the user
  75. fn get_user_workspaces(&self, uid: i64) -> FutureResult<Vec<UserWorkspace>, Error>;
  76. fn check_user(&self, credential: UserCredentials) -> FutureResult<(), Error>;
  77. fn add_workspace_member(
  78. &self,
  79. user_email: String,
  80. workspace_id: String,
  81. ) -> FutureResult<(), Error>;
  82. fn remove_workspace_member(
  83. &self,
  84. user_email: String,
  85. workspace_id: String,
  86. ) -> FutureResult<(), Error>;
  87. fn get_user_awareness_updates(&self, uid: i64) -> FutureResult<Vec<Vec<u8>>, Error>;
  88. fn receive_realtime_event(&self, _json: Value) {}
  89. fn subscribe_user_update(&self) -> Option<UserUpdateReceiver> {
  90. None
  91. }
  92. fn reset_workspace(&self, collab_object: CollabObject) -> FutureResult<(), Error>;
  93. fn create_collab_object(
  94. &self,
  95. collab_object: &CollabObject,
  96. data: Vec<u8>,
  97. ) -> FutureResult<(), Error>;
  98. }
  99. pub type UserUpdateReceiver = tokio::sync::broadcast::Receiver<UserUpdate>;
  100. pub type UserUpdateSender = tokio::sync::broadcast::Sender<UserUpdate>;
  101. #[derive(Debug, Clone)]
  102. pub struct UserUpdate {
  103. pub uid: i64,
  104. pub name: String,
  105. pub email: String,
  106. pub encryption_sign: String,
  107. }
  108. pub fn third_party_params_from_box_any(any: BoxAny) -> Result<ThirdPartyParams, Error> {
  109. let map: HashMap<String, String> = any.unbox_or_error()?;
  110. let uuid = uuid_from_map(&map)?;
  111. let email = map.get("email").cloned().unwrap_or_default();
  112. let device_id = map.get("device_id").cloned().unwrap_or_default();
  113. Ok(ThirdPartyParams {
  114. uuid,
  115. email,
  116. device_id,
  117. })
  118. }
  119. pub fn uuid_from_map(map: &HashMap<String, String>) -> Result<Uuid, Error> {
  120. let uuid = map
  121. .get("uuid")
  122. .ok_or_else(|| FlowyError::new(ErrorCode::MissingAuthField, "Missing uuid field"))?
  123. .as_str();
  124. let uuid = Uuid::from_str(uuid)?;
  125. Ok(uuid)
  126. }