event_map.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. use std::sync::{Arc, Weak};
  2. use collab_folder::core::FolderData;
  3. use strum_macros::Display;
  4. use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
  5. use flowy_error::FlowyResult;
  6. use flowy_server_config::supabase_config::SupabaseConfiguration;
  7. use flowy_user_deps::cloud::UserService;
  8. use flowy_user_deps::entities::*;
  9. use lib_dispatch::prelude::*;
  10. use lib_infra::future::{to_fut, Fut};
  11. use crate::event_handler::*;
  12. use crate::{errors::FlowyError, services::UserSession};
  13. pub fn init(user_session: Weak<UserSession>) -> AFPlugin {
  14. AFPlugin::new()
  15. .name("Flowy-User")
  16. .state(user_session)
  17. .event(UserEvent::SignIn, sign_in)
  18. .event(UserEvent::SignUp, sign_up)
  19. .event(UserEvent::InitUser, init_user_handler)
  20. .event(UserEvent::GetUserProfile, get_user_profile_handler)
  21. .event(UserEvent::SignOut, sign_out)
  22. .event(UserEvent::UpdateUserProfile, update_user_profile_handler)
  23. .event(UserEvent::CheckUser, check_user_handler)
  24. .event(UserEvent::SetAppearanceSetting, set_appearance_setting)
  25. .event(UserEvent::GetAppearanceSetting, get_appearance_setting)
  26. .event(UserEvent::GetUserSetting, get_user_setting)
  27. .event(UserEvent::SetSupabaseConfig, set_supabase_config_handler)
  28. .event(UserEvent::GetSupabaseConfig, get_supabase_config_handler)
  29. .event(UserEvent::ThirdPartyAuth, third_party_auth_handler)
  30. .event(
  31. UserEvent::GetAllUserWorkspaces,
  32. get_all_user_workspace_handler,
  33. )
  34. .event(UserEvent::OpenWorkspace, open_workspace_handler)
  35. .event(UserEvent::AddUserToWorkspace, add_user_to_workspace_handler)
  36. .event(
  37. UserEvent::RemoveUserToWorkspace,
  38. remove_user_from_workspace_handler,
  39. )
  40. .event(UserEvent::UpdateNetworkState, update_network_state_handler)
  41. }
  42. pub struct SignUpContext {
  43. /// Indicate whether the user is new or not.
  44. pub is_new: bool,
  45. /// If the user is sign in as guest, and the is_new is true, then the folder data will be not
  46. /// None.
  47. pub local_folder: Option<FolderData>,
  48. }
  49. pub trait UserStatusCallback: Send + Sync + 'static {
  50. /// When the [AuthType] changed, this method will be called. Currently, the auth type
  51. /// will be changed when the user sign in or sign up.
  52. fn auth_type_did_changed(&self, auth_type: AuthType);
  53. /// This will be called after the application launches if the user is already signed in.
  54. /// If the user is not signed in, this method will not be called
  55. fn did_init(&self, user_id: i64, user_workspace: &UserWorkspace) -> Fut<FlowyResult<()>>;
  56. /// Will be called after the user signed in.
  57. fn did_sign_in(&self, user_id: i64, user_workspace: &UserWorkspace) -> Fut<FlowyResult<()>>;
  58. /// Will be called after the user signed up.
  59. fn did_sign_up(
  60. &self,
  61. context: SignUpContext,
  62. user_profile: &UserProfile,
  63. user_workspace: &UserWorkspace,
  64. ) -> Fut<FlowyResult<()>>;
  65. fn did_expired(&self, token: &str, user_id: i64) -> Fut<FlowyResult<()>>;
  66. fn open_workspace(&self, user_id: i64, user_workspace: &UserWorkspace) -> Fut<FlowyResult<()>>;
  67. fn did_update_network(&self, reachable: bool);
  68. }
  69. /// The user cloud service provider.
  70. /// The provider can be supabase, firebase, aws, or any other cloud service.
  71. pub trait UserCloudServiceProvider: Send + Sync + 'static {
  72. fn update_supabase_config(&self, supabase_config: &SupabaseConfiguration);
  73. fn set_auth_type(&self, auth_type: AuthType);
  74. fn get_user_service(&self) -> Result<Arc<dyn UserService>, FlowyError>;
  75. }
  76. impl<T> UserCloudServiceProvider for Arc<T>
  77. where
  78. T: UserCloudServiceProvider,
  79. {
  80. fn update_supabase_config(&self, supabase_config: &SupabaseConfiguration) {
  81. (**self).update_supabase_config(supabase_config)
  82. }
  83. fn set_auth_type(&self, auth_type: AuthType) {
  84. (**self).set_auth_type(auth_type)
  85. }
  86. fn get_user_service(&self) -> Result<Arc<dyn UserService>, FlowyError> {
  87. (**self).get_user_service()
  88. }
  89. }
  90. /// Acts as a placeholder [UserStatusCallback] for the user session, but does not perform any function
  91. pub(crate) struct DefaultUserStatusCallback;
  92. impl UserStatusCallback for DefaultUserStatusCallback {
  93. fn auth_type_did_changed(&self, _auth_type: AuthType) {}
  94. fn did_init(&self, _user_id: i64, _user_workspace: &UserWorkspace) -> Fut<FlowyResult<()>> {
  95. to_fut(async { Ok(()) })
  96. }
  97. fn did_sign_in(&self, _user_id: i64, _user_workspace: &UserWorkspace) -> Fut<FlowyResult<()>> {
  98. to_fut(async { Ok(()) })
  99. }
  100. fn did_sign_up(
  101. &self,
  102. _context: SignUpContext,
  103. _user_profile: &UserProfile,
  104. _user_workspace: &UserWorkspace,
  105. ) -> Fut<FlowyResult<()>> {
  106. to_fut(async { Ok(()) })
  107. }
  108. fn did_expired(&self, _token: &str, _user_id: i64) -> Fut<FlowyResult<()>> {
  109. to_fut(async { Ok(()) })
  110. }
  111. fn open_workspace(&self, _user_id: i64, _user_workspace: &UserWorkspace) -> Fut<FlowyResult<()>> {
  112. to_fut(async { Ok(()) })
  113. }
  114. fn did_update_network(&self, _reachable: bool) {}
  115. }
  116. #[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
  117. #[event_err = "FlowyError"]
  118. pub enum UserEvent {
  119. /// Only use when the [AuthType] is Local or SelfHosted
  120. /// Logging into an account using a register email and password
  121. #[event(input = "SignInPayloadPB", output = "UserProfilePB")]
  122. SignIn = 0,
  123. /// Only use when the [AuthType] is Local or SelfHosted
  124. /// Creating a new account
  125. #[event(input = "SignUpPayloadPB", output = "UserProfilePB")]
  126. SignUp = 1,
  127. /// Logging out fo an account
  128. #[event()]
  129. SignOut = 2,
  130. /// Update the user information
  131. #[event(input = "UpdateUserProfilePayloadPB")]
  132. UpdateUserProfile = 3,
  133. /// Get the user information
  134. #[event(output = "UserProfilePB")]
  135. GetUserProfile = 4,
  136. /// Check the user current session is valid or not
  137. #[event(output = "UserProfilePB")]
  138. CheckUser = 5,
  139. /// Initialize resources for the current user after launching the application
  140. #[event()]
  141. InitUser = 6,
  142. /// Change the visual elements of the interface, such as theme, font and more
  143. #[event(input = "AppearanceSettingsPB")]
  144. SetAppearanceSetting = 7,
  145. /// Get the appearance setting
  146. #[event(output = "AppearanceSettingsPB")]
  147. GetAppearanceSetting = 8,
  148. /// Get the settings of the user, such as the user storage folder
  149. #[event(output = "UserSettingPB")]
  150. GetUserSetting = 9,
  151. #[event(input = "ThirdPartyAuthPB", output = "UserProfilePB")]
  152. ThirdPartyAuth = 10,
  153. /// Set the supabase config. It will be written to the environment variables.
  154. /// Check out the `write_to_env` of [SupabaseConfigPB].
  155. #[event(input = "SupabaseConfigPB")]
  156. SetSupabaseConfig = 13,
  157. #[event(output = "SupabaseConfigPB")]
  158. GetSupabaseConfig = 14,
  159. /// Return the all the workspaces of the user
  160. #[event()]
  161. GetAllUserWorkspaces = 20,
  162. #[event(input = "UserWorkspacePB")]
  163. OpenWorkspace = 21,
  164. #[event(input = "AddWorkspaceUserPB")]
  165. AddUserToWorkspace = 22,
  166. #[event(input = "RemoveWorkspaceUserPB")]
  167. RemoveUserToWorkspace = 23,
  168. #[event(input = "NetworkStatePB")]
  169. UpdateNetworkState = 24,
  170. }