event_map.rs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. use crate::entities::UserProfilePB;
  2. use crate::event_handler::*;
  3. use crate::{errors::FlowyError, services::UserSession};
  4. use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
  5. use flowy_error::FlowyResult;
  6. use lib_dispatch::prelude::*;
  7. use lib_infra::future::{Fut, FutureResult};
  8. use std::sync::Arc;
  9. use strum_macros::Display;
  10. use user_model::{
  11. SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserProfileParams, UserProfile,
  12. };
  13. pub fn init(user_session: Arc<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. }
  28. pub trait UserStatusCallback: Send + Sync + 'static {
  29. fn did_sign_in(&self, token: &str, user_id: i64) -> Fut<FlowyResult<()>>;
  30. fn did_sign_up(&self, user_profile: &UserProfile) -> Fut<FlowyResult<()>>;
  31. fn did_expired(&self, token: &str, user_id: i64) -> Fut<FlowyResult<()>>;
  32. fn will_migrated(&self, token: &str, old_user_id: &str, user_id: i64) -> Fut<FlowyResult<()>>;
  33. }
  34. pub trait UserCloudService: Send + Sync {
  35. fn sign_up(&self, params: SignUpParams) -> FutureResult<SignUpResponse, FlowyError>;
  36. fn sign_in(&self, params: SignInParams) -> FutureResult<SignInResponse, FlowyError>;
  37. fn sign_out(&self, token: &str) -> FutureResult<(), FlowyError>;
  38. fn update_user(
  39. &self,
  40. token: &str,
  41. params: UpdateUserProfileParams,
  42. ) -> FutureResult<(), FlowyError>;
  43. fn get_user(&self, token: &str) -> FutureResult<UserProfilePB, FlowyError>;
  44. fn ws_addr(&self) -> String;
  45. }
  46. #[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
  47. #[event_err = "FlowyError"]
  48. pub enum UserEvent {
  49. /// Logging into an account using a register email and password
  50. #[event(input = "SignInPayloadPB", output = "UserProfilePB")]
  51. SignIn = 0,
  52. /// Creating a new account
  53. #[event(input = "SignUpPayloadPB", output = "UserProfilePB")]
  54. SignUp = 1,
  55. /// Logging out fo an account
  56. #[event(passthrough)]
  57. SignOut = 2,
  58. /// Update the user information
  59. #[event(input = "UpdateUserProfilePayloadPB")]
  60. UpdateUserProfile = 3,
  61. /// Get the user information
  62. #[event(output = "UserProfilePB")]
  63. GetUserProfile = 4,
  64. /// Check the user current session is valid or not
  65. #[event(output = "UserProfilePB")]
  66. CheckUser = 5,
  67. /// Initialize resources for the current user after launching the application
  68. #[event()]
  69. InitUser = 6,
  70. /// Change the visual elements of the interface, such as theme, font and more
  71. #[event(input = "AppearanceSettingsPB")]
  72. SetAppearanceSetting = 7,
  73. /// Get the appearance setting
  74. #[event(output = "AppearanceSettingsPB")]
  75. GetAppearanceSetting = 8,
  76. /// Get the settings of the user, such as the user storage folder
  77. #[event(output = "UserSettingPB")]
  78. GetUserSetting = 9,
  79. }