entities.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 SignUpResponse {
  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 SignUpResponse {
  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 SelfHosted
  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. pub database_storage_id: String,
  142. }
  143. impl UserWorkspace {
  144. pub fn new(workspace_id: &str, _uid: i64) -> Self {
  145. Self {
  146. id: workspace_id.to_string(),
  147. name: "".to_string(),
  148. created_at: Utc::now(),
  149. database_storage_id: uuid::Uuid::new_v4().to_string(),
  150. }
  151. }
  152. }
  153. #[derive(Serialize, Deserialize, Default, Debug, Clone)]
  154. pub struct UserProfile {
  155. #[serde(rename = "id")]
  156. pub uid: i64,
  157. pub email: String,
  158. pub name: String,
  159. pub token: String,
  160. pub icon_url: String,
  161. pub openai_key: String,
  162. pub workspace_id: String,
  163. pub auth_type: AuthType,
  164. // If the encryption_sign is not empty, which means the user has enabled the encryption.
  165. pub encryption_type: EncryptionType,
  166. }
  167. #[derive(Serialize, Deserialize, Debug, Clone, Default, Eq, PartialEq)]
  168. pub enum EncryptionType {
  169. #[default]
  170. NoEncryption,
  171. SelfEncryption(String),
  172. }
  173. impl EncryptionType {
  174. pub fn from_sign(sign: &str) -> Self {
  175. if sign.is_empty() {
  176. EncryptionType::NoEncryption
  177. } else {
  178. EncryptionType::SelfEncryption(sign.to_owned())
  179. }
  180. }
  181. pub fn is_need_encrypt_secret(&self) -> bool {
  182. match self {
  183. EncryptionType::NoEncryption => false,
  184. EncryptionType::SelfEncryption(sign) => !sign.is_empty(),
  185. }
  186. }
  187. pub fn sign(&self) -> String {
  188. match self {
  189. EncryptionType::NoEncryption => "".to_owned(),
  190. EncryptionType::SelfEncryption(sign) => sign.to_owned(),
  191. }
  192. }
  193. }
  194. impl FromStr for EncryptionType {
  195. type Err = serde_json::Error;
  196. fn from_str(s: &str) -> Result<Self, Self::Err> {
  197. serde_json::from_str(s)
  198. }
  199. }
  200. impl<T> From<(&T, &AuthType)> for UserProfile
  201. where
  202. T: UserAuthResponse,
  203. {
  204. fn from(params: (&T, &AuthType)) -> Self {
  205. let (value, auth_type) = params;
  206. Self {
  207. uid: value.user_id(),
  208. email: value.user_email().unwrap_or_default(),
  209. name: value.user_name().to_owned(),
  210. token: value.user_token().unwrap_or_default(),
  211. icon_url: "".to_owned(),
  212. openai_key: "".to_owned(),
  213. workspace_id: value.latest_workspace().id.to_owned(),
  214. auth_type: auth_type.clone(),
  215. encryption_type: value.encryption_type(),
  216. }
  217. }
  218. }
  219. #[derive(Serialize, Deserialize, Default, Clone, Debug)]
  220. pub struct UpdateUserProfileParams {
  221. pub uid: i64,
  222. pub name: Option<String>,
  223. pub email: Option<String>,
  224. pub password: Option<String>,
  225. pub icon_url: Option<String>,
  226. pub openai_key: Option<String>,
  227. pub encryption_sign: Option<String>,
  228. }
  229. impl UpdateUserProfileParams {
  230. pub fn new(uid: i64) -> Self {
  231. Self {
  232. uid,
  233. ..Default::default()
  234. }
  235. }
  236. pub fn with_name(mut self, name: &str) -> Self {
  237. self.name = Some(name.to_owned());
  238. self
  239. }
  240. pub fn with_email(mut self, email: &str) -> Self {
  241. self.email = Some(email.to_owned());
  242. self
  243. }
  244. pub fn with_password(mut self, password: &str) -> Self {
  245. self.password = Some(password.to_owned());
  246. self
  247. }
  248. pub fn with_icon_url(mut self, icon_url: &str) -> Self {
  249. self.icon_url = Some(icon_url.to_owned());
  250. self
  251. }
  252. pub fn with_openai_key(mut self, openai_key: &str) -> Self {
  253. self.openai_key = Some(openai_key.to_owned());
  254. self
  255. }
  256. pub fn with_encryption_type(mut self, encryption_type: EncryptionType) -> Self {
  257. let sign = match encryption_type {
  258. EncryptionType::NoEncryption => "".to_string(),
  259. EncryptionType::SelfEncryption(sign) => sign,
  260. };
  261. self.encryption_sign = Some(sign);
  262. self
  263. }
  264. pub fn is_empty(&self) -> bool {
  265. self.name.is_none()
  266. && self.email.is_none()
  267. && self.password.is_none()
  268. && self.icon_url.is_none()
  269. && self.openai_key.is_none()
  270. && self.encryption_sign.is_none()
  271. }
  272. }
  273. #[derive(Debug, Clone, Hash, Serialize_repr, Deserialize_repr, Eq, PartialEq)]
  274. #[repr(u8)]
  275. pub enum AuthType {
  276. /// It's a local server, we do fake sign in default.
  277. Local = 0,
  278. /// Currently not supported. It will be supported in the future when the
  279. /// [AppFlowy-Server](https://github.com/AppFlowy-IO/AppFlowy-Server) ready.
  280. SelfHosted = 1,
  281. /// It uses Supabase as the backend.
  282. Supabase = 2,
  283. }
  284. impl Default for AuthType {
  285. fn default() -> Self {
  286. Self::Local
  287. }
  288. }
  289. impl AuthType {
  290. pub fn is_local(&self) -> bool {
  291. matches!(self, AuthType::Local)
  292. }
  293. }
  294. impl From<i32> for AuthType {
  295. fn from(value: i32) -> Self {
  296. match value {
  297. 0 => AuthType::Local,
  298. 1 => AuthType::SelfHosted,
  299. 2 => AuthType::Supabase,
  300. _ => AuthType::Local,
  301. }
  302. }
  303. }
  304. pub struct ThirdPartyParams {
  305. pub uuid: Uuid,
  306. pub email: String,
  307. pub device_id: String,
  308. }