entities.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. use chrono::{DateTime, Utc};
  2. use serde::{Deserialize, Serialize};
  3. use serde_repr::*;
  4. use uuid::Uuid;
  5. #[derive(Debug, Serialize, Deserialize, Clone)]
  6. pub struct SignInResponse {
  7. pub user_id: i64,
  8. pub name: String,
  9. pub latest_workspace: UserWorkspace,
  10. pub user_workspaces: Vec<UserWorkspace>,
  11. pub email: Option<String>,
  12. pub token: Option<String>,
  13. }
  14. #[derive(Default, Serialize, Deserialize, Debug)]
  15. pub struct SignInParams {
  16. pub email: String,
  17. pub password: String,
  18. pub name: String,
  19. pub auth_type: AuthType,
  20. // Currently, the uid only used in local sign in.
  21. pub uid: Option<i64>,
  22. }
  23. #[derive(Serialize, Deserialize, Default, Debug)]
  24. pub struct SignUpParams {
  25. pub email: String,
  26. pub name: String,
  27. pub password: String,
  28. pub auth_type: AuthType,
  29. }
  30. #[derive(Serialize, Deserialize, Debug, Clone)]
  31. pub struct SignUpResponse {
  32. pub user_id: i64,
  33. pub name: String,
  34. pub latest_workspace: UserWorkspace,
  35. pub user_workspaces: Vec<UserWorkspace>,
  36. pub is_new: bool,
  37. pub email: Option<String>,
  38. pub token: Option<String>,
  39. }
  40. #[derive(Clone, Debug)]
  41. pub struct UserCredentials {
  42. /// Currently, the token is only used when the [AuthType] is SelfHosted
  43. pub token: Option<String>,
  44. /// The user id
  45. pub uid: Option<i64>,
  46. /// The user id
  47. pub uuid: Option<String>,
  48. }
  49. impl UserCredentials {
  50. pub fn from_uid(uid: i64) -> Self {
  51. Self {
  52. token: None,
  53. uid: Some(uid),
  54. uuid: None,
  55. }
  56. }
  57. pub fn from_uuid(uuid: String) -> Self {
  58. Self {
  59. token: None,
  60. uid: None,
  61. uuid: Some(uuid),
  62. }
  63. }
  64. pub fn new(token: Option<String>, uid: Option<i64>, uuid: Option<String>) -> Self {
  65. Self { token, uid, uuid }
  66. }
  67. }
  68. #[derive(Debug, Serialize, Deserialize, Clone)]
  69. pub struct UserWorkspace {
  70. pub id: String,
  71. pub name: String,
  72. pub created_at: DateTime<Utc>,
  73. pub database_storage_id: String,
  74. }
  75. impl UserWorkspace {
  76. pub fn new(workspace_id: &str, _uid: i64) -> Self {
  77. Self {
  78. id: workspace_id.to_string(),
  79. name: "".to_string(),
  80. created_at: Utc::now(),
  81. database_storage_id: uuid::Uuid::new_v4().to_string(),
  82. }
  83. }
  84. }
  85. #[derive(Serialize, Deserialize, Default, Debug, Clone)]
  86. pub struct UserProfile {
  87. pub id: i64,
  88. pub email: String,
  89. pub name: String,
  90. pub token: String,
  91. pub icon_url: String,
  92. pub openai_key: String,
  93. pub workspace_id: String,
  94. pub auth_type: AuthType,
  95. }
  96. #[derive(Serialize, Deserialize, Default, Clone, Debug)]
  97. pub struct UpdateUserProfileParams {
  98. pub id: i64,
  99. pub auth_type: AuthType,
  100. pub name: Option<String>,
  101. pub email: Option<String>,
  102. pub password: Option<String>,
  103. pub icon_url: Option<String>,
  104. pub openai_key: Option<String>,
  105. }
  106. impl UpdateUserProfileParams {
  107. pub fn name(mut self, name: &str) -> Self {
  108. self.name = Some(name.to_owned());
  109. self
  110. }
  111. pub fn email(mut self, email: &str) -> Self {
  112. self.email = Some(email.to_owned());
  113. self
  114. }
  115. pub fn password(mut self, password: &str) -> Self {
  116. self.password = Some(password.to_owned());
  117. self
  118. }
  119. pub fn icon_url(mut self, icon_url: &str) -> Self {
  120. self.icon_url = Some(icon_url.to_owned());
  121. self
  122. }
  123. pub fn openai_key(mut self, openai_key: &str) -> Self {
  124. self.openai_key = Some(openai_key.to_owned());
  125. self
  126. }
  127. pub fn is_empty(&self) -> bool {
  128. self.name.is_none()
  129. && self.email.is_none()
  130. && self.password.is_none()
  131. && self.icon_url.is_none()
  132. && self.openai_key.is_none()
  133. }
  134. }
  135. #[derive(Debug, Clone, Hash, Serialize_repr, Deserialize_repr, Eq, PartialEq)]
  136. #[repr(u8)]
  137. pub enum AuthType {
  138. /// It's a local server, we do fake sign in default.
  139. Local = 0,
  140. /// Currently not supported. It will be supported in the future when the
  141. /// [AppFlowy-Server](https://github.com/AppFlowy-IO/AppFlowy-Server) ready.
  142. SelfHosted = 1,
  143. /// It uses Supabase as the backend.
  144. Supabase = 2,
  145. }
  146. impl Default for AuthType {
  147. fn default() -> Self {
  148. Self::Local
  149. }
  150. }
  151. impl AuthType {
  152. pub fn is_local(&self) -> bool {
  153. matches!(self, AuthType::Local)
  154. }
  155. }
  156. impl From<i32> for AuthType {
  157. fn from(value: i32) -> Self {
  158. match value {
  159. 0 => AuthType::Local,
  160. 1 => AuthType::SelfHosted,
  161. 2 => AuthType::Supabase,
  162. _ => AuthType::Local,
  163. }
  164. }
  165. }
  166. pub struct ThirdPartyParams {
  167. pub uuid: Uuid,
  168. pub email: String,
  169. }