event_map.rs 8.5 KB

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