entities.rs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. use std::str::FromStr;
  2. use chrono::{DateTime, Utc};
  3. use serde::{Deserialize, Serialize};
  4. use serde_repr::*;
  5. use uuid::Uuid;
  6. pub trait UserAuthResponse {
  7. fn user_id(&self) -> i64;
  8. fn user_name(&self) -> &str;
  9. fn latest_workspace(&self) -> &UserWorkspace;
  10. fn user_workspaces(&self) -> &[UserWorkspace];
  11. fn device_id(&self) -> &str;
  12. fn user_token(&self) -> Option<String>;
  13. fn user_email(&self) -> Option<String>;
  14. fn encryption_type(&self) -> EncryptionType;
  15. }
  16. #[derive(Debug, Serialize, Deserialize, Clone)]
  17. pub struct SignInResponse {
  18. pub user_id: i64,
  19. pub name: String,
  20. pub latest_workspace: UserWorkspace,
  21. pub user_workspaces: Vec<UserWorkspace>,
  22. pub email: Option<String>,
  23. pub token: Option<String>,
  24. pub device_id: String,
  25. pub encryption_type: EncryptionType,
  26. }
  27. impl UserAuthResponse for SignInResponse {
  28. fn user_id(&self) -> i64 {
  29. self.user_id
  30. }
  31. fn user_name(&self) -> &str {
  32. &self.name
  33. }
  34. fn latest_workspace(&self) -> &UserWorkspace {
  35. &self.latest_workspace
  36. }
  37. fn user_workspaces(&self) -> &[UserWorkspace] {
  38. &self.user_workspaces
  39. }
  40. fn device_id(&self) -> &str {
  41. &self.device_id
  42. }
  43. fn user_token(&self) -> Option<String> {
  44. self.token.clone()
  45. }
  46. fn user_email(&self) -> Option<String> {
  47. self.email.clone()
  48. }
  49. fn encryption_type(&self) -> EncryptionType {
  50. self.encryption_type.clone()
  51. }
  52. }
  53. #[derive(Default, Serialize, Deserialize, Debug)]
  54. pub struct SignInParams {
  55. pub email: String,
  56. pub password: String,
  57. pub name: String,
  58. pub auth_type: AuthType,
  59. pub device_id: String,
  60. }
  61. #[derive(Serialize, Deserialize, Default, Debug)]
  62. pub struct SignUpParams {
  63. pub email: String,
  64. pub name: String,
  65. pub password: String,
  66. pub auth_type: AuthType,
  67. pub device_id: String,
  68. }
  69. #[derive(Serialize, Deserialize, Debug, Clone)]
  70. pub struct AuthResponse {
  71. pub user_id: i64,
  72. pub name: String,
  73. pub latest_workspace: UserWorkspace,
  74. pub user_workspaces: Vec<UserWorkspace>,
  75. pub is_new_user: bool,
  76. pub email: Option<String>,
  77. pub token: Option<String>,
  78. pub device_id: String,
  79. pub encryption_type: EncryptionType,
  80. }
  81. impl UserAuthResponse for AuthResponse {
  82. fn user_id(&self) -> i64 {
  83. self.user_id
  84. }
  85. fn user_name(&self) -> &str {
  86. &self.name
  87. }
  88. fn latest_workspace(&self) -> &UserWorkspace {
  89. &self.latest_workspace
  90. }
  91. fn user_workspaces(&self) -> &[UserWorkspace] {
  92. &self.user_workspaces
  93. }
  94. fn device_id(&self) -> &str {
  95. &self.device_id
  96. }
  97. fn user_token(&self) -> Option<String> {
  98. self.token.clone()
  99. }
  100. fn user_email(&self) -> Option<String> {
  101. self.email.clone()
  102. }
  103. fn encryption_type(&self) -> EncryptionType {
  104. self.encryption_type.clone()
  105. }
  106. }
  107. #[derive(Clone, Debug)]
  108. pub struct UserCredentials {
  109. /// Currently, the token is only used when the [AuthType] is AFCloud
  110. pub token: Option<String>,
  111. /// The user id
  112. pub uid: Option<i64>,
  113. /// The user id
  114. pub uuid: Option<String>,
  115. }
  116. impl UserCredentials {
  117. pub fn from_uid(uid: i64) -> Self {
  118. Self {
  119. token: None,
  120. uid: Some(uid),
  121. uuid: None,
  122. }
  123. }
  124. pub fn from_uuid(uuid: String) -> Self {
  125. Self {
  126. token: None,
  127. uid: None,
  128. uuid: Some(uuid),
  129. }
  130. }
  131. pub fn new(token: Option<String>, uid: Option<i64>, uuid: Option<String>) -> Self {
  132. Self { token, uid, uuid }
  133. }
  134. }
  135. #[derive(Debug, Serialize, Deserialize, Clone)]
  136. pub struct UserWorkspace {
  137. pub id: String,
  138. pub name: String,
  139. pub created_at: DateTime<Utc>,
  140. /// The database storage id is used indexing all the database in current workspace.
  141. #[serde(rename = "database_storage_id")]
  142. pub database_views_aggregate_id: String,
  143. }
  144. impl UserWorkspace {
  145. pub fn new(workspace_id: &str, _uid: i64) -> Self {
  146. Self {
  147. id: workspace_id.to_string(),
  148. name: "".to_string(),
  149. created_at: Utc::now(),
  150. database_views_aggregate_id: uuid::Uuid::new_v4().to_string(),
  151. }
  152. }
  153. }
  154. #[derive(Serialize, Deserialize, Default, Debug, Clone)]
  155. pub struct UserProfile {
  156. #[serde(rename = "id")]
  157. pub uid: i64,
  158. pub email: String,
  159. pub name: String,
  160. pub token: String,
  161. pub icon_url: String,
  162. pub openai_key: String,
  163. pub workspace_id: String,
  164. pub auth_type: AuthType,
  165. // If the encryption_sign is not empty, which means the user has enabled the encryption.
  166. pub encryption_type: EncryptionType,
  167. }
  168. #[derive(Serialize, Deserialize, Debug, Clone, Default, Eq, PartialEq)]
  169. pub enum EncryptionType {
  170. #[default]
  171. NoEncryption,
  172. SelfEncryption(String),
  173. }
  174. impl EncryptionType {
  175. pub fn from_sign(sign: &str) -> Self {
  176. if sign.is_empty() {
  177. EncryptionType::NoEncryption
  178. } else {
  179. EncryptionType::SelfEncryption(sign.to_owned())
  180. }
  181. }
  182. pub fn is_need_encrypt_secret(&self) -> bool {
  183. match self {
  184. EncryptionType::NoEncryption => false,
  185. EncryptionType::SelfEncryption(sign) => !sign.is_empty(),
  186. }
  187. }
  188. pub fn sign(&self) -> String {
  189. match self {
  190. EncryptionType::NoEncryption => "".to_owned(),
  191. EncryptionType::SelfEncryption(sign) => sign.to_owned(),
  192. }
  193. }
  194. }
  195. impl FromStr for EncryptionType {
  196. type Err = serde_json::Error;
  197. fn from_str(s: &str) -> Result<Self, Self::Err> {
  198. serde_json::from_str(s)
  199. }
  200. }
  201. impl<T> From<(&T, &AuthType)> for UserProfile
  202. where
  203. T: UserAuthResponse,
  204. {
  205. fn from(params: (&T, &AuthType)) -> Self {
  206. let (value, auth_type) = params;
  207. Self {
  208. uid: value.user_id(),
  209. email: value.user_email().unwrap_or_default(),
  210. name: value.user_name().to_owned(),
  211. token: value.user_token().unwrap_or_default(),
  212. icon_url: "".to_owned(),
  213. openai_key: "".to_owned(),
  214. workspace_id: value.latest_workspace().id.to_owned(),
  215. auth_type: auth_type.clone(),
  216. encryption_type: value.encryption_type(),
  217. }
  218. }
  219. }
  220. #[derive(Serialize, Deserialize, Default, Clone, Debug)]
  221. pub struct UpdateUserProfileParams {
  222. pub uid: i64,
  223. pub name: Option<String>,
  224. pub email: Option<String>,
  225. pub password: Option<String>,
  226. pub icon_url: Option<String>,
  227. pub openai_key: Option<String>,
  228. pub encryption_sign: Option<String>,
  229. }
  230. impl UpdateUserProfileParams {
  231. pub fn new(uid: i64) -> Self {
  232. Self {
  233. uid,
  234. ..Default::default()
  235. }
  236. }
  237. pub fn with_name(mut self, name: &str) -> Self {
  238. self.name = Some(name.to_owned());
  239. self
  240. }
  241. pub fn with_email(mut self, email: &str) -> Self {
  242. self.email = Some(email.to_owned());
  243. self
  244. }
  245. pub fn with_password(mut self, password: &str) -> Self {
  246. self.password = Some(password.to_owned());
  247. self
  248. }
  249. pub fn with_icon_url(mut self, icon_url: &str) -> Self {
  250. self.icon_url = Some(icon_url.to_owned());
  251. self
  252. }
  253. pub fn with_openai_key(mut self, openai_key: &str) -> Self {
  254. self.openai_key = Some(openai_key.to_owned());
  255. self
  256. }
  257. pub fn with_encryption_type(mut self, encryption_type: EncryptionType) -> Self {
  258. let sign = match encryption_type {
  259. EncryptionType::NoEncryption => "".to_string(),
  260. EncryptionType::SelfEncryption(sign) => sign,
  261. };
  262. self.encryption_sign = Some(sign);
  263. self
  264. }
  265. pub fn is_empty(&self) -> bool {
  266. self.name.is_none()
  267. && self.email.is_none()
  268. && self.password.is_none()
  269. && self.icon_url.is_none()
  270. && self.openai_key.is_none()
  271. && self.encryption_sign.is_none()
  272. }
  273. }
  274. #[derive(Debug, Clone, Hash, Serialize_repr, Deserialize_repr, Eq, PartialEq)]
  275. #[repr(u8)]
  276. pub enum AuthType {
  277. /// It's a local server, we do fake sign in default.
  278. Local = 0,
  279. /// Currently not supported. It will be supported in the future when the
  280. /// [AppFlowy-Server](https://github.com/AppFlowy-IO/AppFlowy-Server) ready.
  281. AFCloud = 1,
  282. /// It uses Supabase as the backend.
  283. Supabase = 2,
  284. }
  285. impl Default for AuthType {
  286. fn default() -> Self {
  287. Self::Local
  288. }
  289. }
  290. impl AuthType {
  291. pub fn is_local(&self) -> bool {
  292. matches!(self, AuthType::Local)
  293. }
  294. }
  295. impl From<i32> for AuthType {
  296. fn from(value: i32) -> Self {
  297. match value {
  298. 0 => AuthType::Local,
  299. 1 => AuthType::AFCloud,
  300. 2 => AuthType::Supabase,
  301. _ => AuthType::Local,
  302. }
  303. }
  304. }
  305. pub struct SupabaseOAuthParams {
  306. pub uuid: Uuid,
  307. pub email: String,
  308. pub device_id: String,
  309. }
  310. pub struct AFCloudOAuthParams {
  311. pub sign_in_url: String,
  312. pub device_id: String,
  313. }