entities.rs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. use std::collections::HashMap;
  2. use collab::core::collab_state::SyncState;
  3. use collab_document::blocks::{BlockAction, DocumentData};
  4. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  5. use flowy_error::ErrorCode;
  6. use crate::parse::{NotEmptyStr, NotEmptyVec};
  7. #[derive(Default, ProtoBuf)]
  8. pub struct OpenDocumentPayloadPB {
  9. #[pb(index = 1)]
  10. pub document_id: String,
  11. }
  12. pub struct OpenDocumentParams {
  13. pub document_id: String,
  14. }
  15. impl TryInto<OpenDocumentParams> for OpenDocumentPayloadPB {
  16. type Error = ErrorCode;
  17. fn try_into(self) -> Result<OpenDocumentParams, Self::Error> {
  18. let document_id =
  19. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  20. Ok(OpenDocumentParams {
  21. document_id: document_id.0,
  22. })
  23. }
  24. }
  25. #[derive(Default, ProtoBuf)]
  26. pub struct DocumentRedoUndoPayloadPB {
  27. #[pb(index = 1)]
  28. pub document_id: String,
  29. }
  30. pub struct DocumentRedoUndoParams {
  31. pub document_id: String,
  32. }
  33. impl TryInto<DocumentRedoUndoParams> for DocumentRedoUndoPayloadPB {
  34. type Error = ErrorCode;
  35. fn try_into(self) -> Result<DocumentRedoUndoParams, Self::Error> {
  36. let document_id =
  37. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  38. Ok(DocumentRedoUndoParams {
  39. document_id: document_id.0,
  40. })
  41. }
  42. }
  43. #[derive(Default, Debug, ProtoBuf)]
  44. pub struct DocumentRedoUndoResponsePB {
  45. #[pb(index = 1)]
  46. pub can_undo: bool,
  47. #[pb(index = 2)]
  48. pub can_redo: bool,
  49. #[pb(index = 3)]
  50. pub is_success: bool,
  51. }
  52. #[derive(Default, ProtoBuf)]
  53. pub struct CreateDocumentPayloadPB {
  54. #[pb(index = 1)]
  55. pub document_id: String,
  56. #[pb(index = 2, one_of)]
  57. pub initial_data: Option<DocumentDataPB>,
  58. }
  59. pub struct CreateDocumentParams {
  60. pub document_id: String,
  61. pub initial_data: Option<DocumentData>,
  62. }
  63. impl TryInto<CreateDocumentParams> for CreateDocumentPayloadPB {
  64. type Error = ErrorCode;
  65. fn try_into(self) -> Result<CreateDocumentParams, Self::Error> {
  66. let document_id =
  67. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  68. let initial_data = self.initial_data.map(|data| data.into());
  69. Ok(CreateDocumentParams {
  70. document_id: document_id.0,
  71. initial_data,
  72. })
  73. }
  74. }
  75. #[derive(Default, ProtoBuf)]
  76. pub struct CloseDocumentPayloadPB {
  77. #[pb(index = 1)]
  78. pub document_id: String,
  79. }
  80. pub struct CloseDocumentParams {
  81. pub document_id: String,
  82. }
  83. impl TryInto<CloseDocumentParams> for CloseDocumentPayloadPB {
  84. type Error = ErrorCode;
  85. fn try_into(self) -> Result<CloseDocumentParams, Self::Error> {
  86. let document_id =
  87. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  88. Ok(CloseDocumentParams {
  89. document_id: document_id.0,
  90. })
  91. }
  92. }
  93. #[derive(Default, ProtoBuf, Debug)]
  94. pub struct ApplyActionPayloadPB {
  95. #[pb(index = 1)]
  96. pub document_id: String,
  97. #[pb(index = 2)]
  98. pub actions: Vec<BlockActionPB>,
  99. }
  100. pub struct ApplyActionParams {
  101. pub document_id: String,
  102. pub actions: Vec<BlockAction>,
  103. }
  104. impl TryInto<ApplyActionParams> for ApplyActionPayloadPB {
  105. type Error = ErrorCode;
  106. fn try_into(self) -> Result<ApplyActionParams, Self::Error> {
  107. let document_id =
  108. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  109. let actions = NotEmptyVec::parse(self.actions).map_err(|_| ErrorCode::ApplyActionsIsEmpty)?;
  110. let actions = actions.0.into_iter().map(BlockAction::from).collect();
  111. Ok(ApplyActionParams {
  112. document_id: document_id.0,
  113. actions,
  114. })
  115. }
  116. }
  117. #[derive(Default, Debug, ProtoBuf)]
  118. pub struct DocumentDataPB {
  119. #[pb(index = 1)]
  120. pub page_id: String,
  121. #[pb(index = 2)]
  122. pub blocks: HashMap<String, BlockPB>,
  123. #[pb(index = 3)]
  124. pub meta: MetaPB,
  125. }
  126. #[derive(Default, ProtoBuf, Debug, Clone)]
  127. pub struct BlockPB {
  128. #[pb(index = 1)]
  129. pub id: String,
  130. #[pb(index = 2)]
  131. pub ty: String,
  132. #[pb(index = 3)]
  133. pub data: String,
  134. #[pb(index = 4)]
  135. pub parent_id: String,
  136. #[pb(index = 5)]
  137. pub children_id: String,
  138. #[pb(index = 6, one_of)]
  139. pub external_id: Option<String>,
  140. #[pb(index = 7, one_of)]
  141. pub external_type: Option<String>,
  142. }
  143. #[derive(Default, ProtoBuf, Debug)]
  144. pub struct MetaPB {
  145. #[pb(index = 1)]
  146. pub children_map: HashMap<String, ChildrenPB>,
  147. #[pb(index = 2)]
  148. pub text_map: HashMap<String, String>,
  149. }
  150. #[derive(Default, ProtoBuf, Debug)]
  151. pub struct ChildrenPB {
  152. #[pb(index = 1)]
  153. pub children: Vec<String>,
  154. }
  155. // Actions
  156. #[derive(Default, ProtoBuf, Debug)]
  157. pub struct BlockActionPB {
  158. #[pb(index = 1)]
  159. pub action: BlockActionTypePB,
  160. #[pb(index = 2)]
  161. pub payload: BlockActionPayloadPB,
  162. }
  163. #[derive(Default, ProtoBuf, Debug)]
  164. pub struct BlockActionPayloadPB {
  165. // When action = Insert, Update, Delete or Move, block needs to be passed.
  166. #[pb(index = 1, one_of)]
  167. pub block: Option<BlockPB>,
  168. // When action = Insert or Move, prev_id needs to be passed.
  169. #[pb(index = 2, one_of)]
  170. pub prev_id: Option<String>,
  171. // When action = Insert or Move, parent_id needs to be passed.
  172. #[pb(index = 3, one_of)]
  173. pub parent_id: Option<String>,
  174. // When action = InsertText or ApplyTextDelta, text_id needs to be passed.
  175. #[pb(index = 4, one_of)]
  176. pub text_id: Option<String>,
  177. // When action = InsertText or ApplyTextDelta, delta needs to be passed.
  178. // The format of delta is a JSON string, similar to the serialization result of [{ "insert": "Hello World" }].
  179. #[pb(index = 5, one_of)]
  180. pub delta: Option<String>,
  181. }
  182. #[derive(ProtoBuf_Enum, Debug)]
  183. pub enum BlockActionTypePB {
  184. Insert = 0,
  185. Update = 1,
  186. Delete = 2,
  187. Move = 3,
  188. InsertText = 4,
  189. ApplyTextDelta = 5,
  190. }
  191. impl Default for BlockActionTypePB {
  192. fn default() -> Self {
  193. Self::Insert
  194. }
  195. }
  196. #[derive(ProtoBuf_Enum)]
  197. pub enum DeltaTypePB {
  198. Inserted = 0,
  199. Updated = 1,
  200. Removed = 2,
  201. }
  202. impl Default for DeltaTypePB {
  203. fn default() -> Self {
  204. Self::Inserted
  205. }
  206. }
  207. #[derive(Default, ProtoBuf)]
  208. pub struct DocEventPB {
  209. #[pb(index = 1)]
  210. pub events: Vec<BlockEventPB>,
  211. #[pb(index = 2)]
  212. pub is_remote: bool,
  213. }
  214. #[derive(Default, ProtoBuf)]
  215. pub struct BlockEventPB {
  216. #[pb(index = 1)]
  217. pub event: Vec<BlockEventPayloadPB>,
  218. }
  219. #[derive(Default, ProtoBuf)]
  220. pub struct BlockEventPayloadPB {
  221. #[pb(index = 1)]
  222. pub command: DeltaTypePB,
  223. #[pb(index = 2)]
  224. pub path: Vec<String>,
  225. #[pb(index = 3)]
  226. pub id: String,
  227. #[pb(index = 4)]
  228. pub value: String,
  229. }
  230. #[derive(PartialEq, Eq, Debug, ProtoBuf_Enum, Clone, Default)]
  231. pub enum ExportType {
  232. #[default]
  233. Text = 0,
  234. Markdown = 1,
  235. Link = 2,
  236. }
  237. impl From<i32> for ExportType {
  238. fn from(val: i32) -> Self {
  239. match val {
  240. 0 => ExportType::Text,
  241. 1 => ExportType::Markdown,
  242. 2 => ExportType::Link,
  243. _ => {
  244. tracing::error!("🔴Invalid export type: {}", val);
  245. ExportType::Text
  246. },
  247. }
  248. }
  249. }
  250. #[derive(Default, ProtoBuf)]
  251. pub struct EditPayloadPB {
  252. #[pb(index = 1)]
  253. pub doc_id: String,
  254. // Encode in JSON format
  255. #[pb(index = 2)]
  256. pub operations: String,
  257. }
  258. #[derive(Default, ProtoBuf)]
  259. pub struct ExportDataPB {
  260. #[pb(index = 1)]
  261. pub data: String,
  262. #[pb(index = 2)]
  263. pub export_type: ExportType,
  264. }
  265. #[derive(PartialEq, Eq, Debug, ProtoBuf_Enum, Clone, Default)]
  266. pub enum ConvertType {
  267. #[default]
  268. Json = 0,
  269. }
  270. impl From<i32> for ConvertType {
  271. fn from(val: i32) -> Self {
  272. match val {
  273. 0 => ConvertType::Json,
  274. _ => {
  275. tracing::error!("🔴Invalid export type: {}", val);
  276. ConvertType::Json
  277. },
  278. }
  279. }
  280. }
  281. /// for the json type
  282. /// the data is the json string
  283. #[derive(Default, ProtoBuf, Debug)]
  284. pub struct ConvertDataPayloadPB {
  285. #[pb(index = 1)]
  286. pub convert_type: ConvertType,
  287. #[pb(index = 2)]
  288. pub data: Vec<u8>,
  289. }
  290. pub struct ConvertDataParams {
  291. pub convert_type: ConvertType,
  292. pub data: Vec<u8>,
  293. }
  294. impl TryInto<ConvertDataParams> for ConvertDataPayloadPB {
  295. type Error = ErrorCode;
  296. fn try_into(self) -> Result<ConvertDataParams, Self::Error> {
  297. let convert_type = self.convert_type;
  298. let data = self.data;
  299. Ok(ConvertDataParams { convert_type, data })
  300. }
  301. }
  302. #[derive(Debug, Default, ProtoBuf)]
  303. pub struct RepeatedDocumentSnapshotPB {
  304. #[pb(index = 1)]
  305. pub items: Vec<DocumentSnapshotPB>,
  306. }
  307. #[derive(Debug, Default, ProtoBuf)]
  308. pub struct DocumentSnapshotPB {
  309. #[pb(index = 1)]
  310. pub snapshot_id: i64,
  311. #[pb(index = 2)]
  312. pub snapshot_desc: String,
  313. #[pb(index = 3)]
  314. pub created_at: i64,
  315. #[pb(index = 4)]
  316. pub data: Vec<u8>,
  317. }
  318. #[derive(Debug, Default, ProtoBuf)]
  319. pub struct DocumentSnapshotStatePB {
  320. #[pb(index = 1)]
  321. pub new_snapshot_id: i64,
  322. }
  323. #[derive(Debug, Default, ProtoBuf)]
  324. pub struct DocumentSyncStatePB {
  325. #[pb(index = 1)]
  326. pub is_syncing: bool,
  327. #[pb(index = 2)]
  328. pub is_finish: bool,
  329. }
  330. impl From<SyncState> for DocumentSyncStatePB {
  331. fn from(value: SyncState) -> Self {
  332. Self {
  333. is_syncing: value.is_syncing(),
  334. is_finish: value.is_sync_finished(),
  335. }
  336. }
  337. }
  338. #[derive(Default, ProtoBuf, Debug)]
  339. pub struct TextDeltaPayloadPB {
  340. #[pb(index = 1)]
  341. pub document_id: String,
  342. #[pb(index = 2)]
  343. pub text_id: String,
  344. #[pb(index = 3, one_of)]
  345. pub delta: Option<String>,
  346. }
  347. pub struct TextDeltaParams {
  348. pub document_id: String,
  349. pub text_id: String,
  350. pub delta: String,
  351. }
  352. impl TryInto<TextDeltaParams> for TextDeltaPayloadPB {
  353. type Error = ErrorCode;
  354. fn try_into(self) -> Result<TextDeltaParams, Self::Error> {
  355. let document_id =
  356. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  357. let text_id = NotEmptyStr::parse(self.text_id).map_err(|_| ErrorCode::TextIdIsEmpty)?;
  358. let delta = self.delta.map_or_else(|| "".to_string(), |delta| delta);
  359. Ok(TextDeltaParams {
  360. document_id: document_id.0,
  361. text_id: text_id.0,
  362. delta,
  363. })
  364. }
  365. }