event_map.rs 8.2 KB

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