user_event.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. use std::sync::Arc;
  2. use nanoid::nanoid;
  3. use flowy_user::entities::{AuthTypePB, SignUpPayloadPB, UserProfilePB};
  4. use flowy_user::errors::FlowyError;
  5. use flowy_user::event_map::UserEvent::*;
  6. use lib_dispatch::prelude::{AFPluginDispatcher, AFPluginRequest, ToBytes};
  7. pub fn random_email() -> String {
  8. format!("{}@appflowy.io", nanoid!(20))
  9. }
  10. pub fn login_email() -> String {
  11. "[email protected]".to_string()
  12. }
  13. pub fn login_password() -> String {
  14. "HelloWorld!123".to_string()
  15. }
  16. pub struct SignUpContext {
  17. pub user_profile: UserProfilePB,
  18. pub password: String,
  19. }
  20. pub fn sign_up(dispatch: Arc<AFPluginDispatcher>) -> SignUpContext {
  21. let password = login_password();
  22. let payload = SignUpPayloadPB {
  23. email: random_email(),
  24. name: "app flowy".to_string(),
  25. password: password.clone(),
  26. auth_type: AuthTypePB::Local,
  27. device_id: uuid::Uuid::new_v4().to_string(),
  28. }
  29. .into_bytes()
  30. .unwrap();
  31. let request = AFPluginRequest::new(SignUp).payload(payload);
  32. let user_profile = AFPluginDispatcher::sync_send(dispatch, request)
  33. .parse::<UserProfilePB, FlowyError>()
  34. .unwrap()
  35. .unwrap();
  36. SignUpContext {
  37. user_profile,
  38. password,
  39. }
  40. }
  41. pub async fn async_sign_up(
  42. dispatch: Arc<AFPluginDispatcher>,
  43. auth_type: AuthTypePB,
  44. ) -> SignUpContext {
  45. let password = login_password();
  46. let email = random_email();
  47. let payload = SignUpPayloadPB {
  48. email,
  49. name: "appflowy".to_string(),
  50. password: password.clone(),
  51. auth_type,
  52. device_id: uuid::Uuid::new_v4().to_string(),
  53. }
  54. .into_bytes()
  55. .unwrap();
  56. let request = AFPluginRequest::new(SignUp).payload(payload);
  57. let user_profile = AFPluginDispatcher::async_send(dispatch.clone(), request)
  58. .await
  59. .parse::<UserProfilePB, FlowyError>()
  60. .unwrap()
  61. .unwrap();
  62. // let _ = create_default_workspace_if_need(dispatch.clone(), &user_profile.id);
  63. SignUpContext {
  64. user_profile,
  65. password,
  66. }
  67. }
  68. pub async fn init_user_setting(dispatch: Arc<AFPluginDispatcher>) {
  69. let request = AFPluginRequest::new(InitUser);
  70. let _ = AFPluginDispatcher::async_send(dispatch.clone(), request).await;
  71. }