entities.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. use collab::core::collab_state::SyncState;
  2. use collab_document::blocks::{BlockAction, DocumentData};
  3. use std::collections::HashMap;
  4. use crate::parse::{NotEmptyStr, NotEmptyVec};
  5. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  6. use flowy_error::ErrorCode;
  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. }
  139. #[derive(Default, ProtoBuf, Debug)]
  140. pub struct MetaPB {
  141. #[pb(index = 1)]
  142. pub children_map: HashMap<String, ChildrenPB>,
  143. }
  144. #[derive(Default, ProtoBuf, Debug)]
  145. pub struct ChildrenPB {
  146. #[pb(index = 1)]
  147. pub children: Vec<String>,
  148. }
  149. // Actions
  150. #[derive(Default, ProtoBuf, Debug)]
  151. pub struct BlockActionPB {
  152. #[pb(index = 1)]
  153. pub action: BlockActionTypePB,
  154. #[pb(index = 2)]
  155. pub payload: BlockActionPayloadPB,
  156. }
  157. #[derive(Default, ProtoBuf, Debug)]
  158. pub struct BlockActionPayloadPB {
  159. #[pb(index = 1)]
  160. pub block: BlockPB,
  161. #[pb(index = 2, one_of)]
  162. pub prev_id: Option<String>,
  163. #[pb(index = 3, one_of)]
  164. pub parent_id: Option<String>,
  165. }
  166. #[derive(ProtoBuf_Enum, Debug)]
  167. pub enum BlockActionTypePB {
  168. Insert = 0,
  169. Update = 1,
  170. Delete = 2,
  171. Move = 3,
  172. }
  173. impl Default for BlockActionTypePB {
  174. fn default() -> Self {
  175. Self::Insert
  176. }
  177. }
  178. #[derive(ProtoBuf_Enum)]
  179. pub enum DeltaTypePB {
  180. Inserted = 0,
  181. Updated = 1,
  182. Removed = 2,
  183. }
  184. impl Default for DeltaTypePB {
  185. fn default() -> Self {
  186. Self::Inserted
  187. }
  188. }
  189. #[derive(Default, ProtoBuf)]
  190. pub struct DocEventPB {
  191. #[pb(index = 1)]
  192. pub events: Vec<BlockEventPB>,
  193. #[pb(index = 2)]
  194. pub is_remote: bool,
  195. }
  196. #[derive(Default, ProtoBuf)]
  197. pub struct BlockEventPB {
  198. #[pb(index = 1)]
  199. pub event: Vec<BlockEventPayloadPB>,
  200. }
  201. #[derive(Default, ProtoBuf)]
  202. pub struct BlockEventPayloadPB {
  203. #[pb(index = 1)]
  204. pub command: DeltaTypePB,
  205. #[pb(index = 2)]
  206. pub path: Vec<String>,
  207. #[pb(index = 3)]
  208. pub id: String,
  209. #[pb(index = 4)]
  210. pub value: String,
  211. }
  212. #[derive(PartialEq, Eq, Debug, ProtoBuf_Enum, Clone, Default)]
  213. pub enum ExportType {
  214. #[default]
  215. Text = 0,
  216. Markdown = 1,
  217. Link = 2,
  218. }
  219. impl From<i32> for ExportType {
  220. fn from(val: i32) -> Self {
  221. match val {
  222. 0 => ExportType::Text,
  223. 1 => ExportType::Markdown,
  224. 2 => ExportType::Link,
  225. _ => {
  226. tracing::error!("Invalid export type: {}", val);
  227. ExportType::Text
  228. },
  229. }
  230. }
  231. }
  232. #[derive(Default, ProtoBuf)]
  233. pub struct EditPayloadPB {
  234. #[pb(index = 1)]
  235. pub doc_id: String,
  236. // Encode in JSON format
  237. #[pb(index = 2)]
  238. pub operations: String,
  239. }
  240. #[derive(Default, ProtoBuf)]
  241. pub struct ExportDataPB {
  242. #[pb(index = 1)]
  243. pub data: String,
  244. #[pb(index = 2)]
  245. pub export_type: ExportType,
  246. }
  247. #[derive(PartialEq, Eq, Debug, ProtoBuf_Enum, Clone, Default)]
  248. pub enum ConvertType {
  249. #[default]
  250. Json = 0,
  251. }
  252. impl From<i32> for ConvertType {
  253. fn from(val: i32) -> Self {
  254. match val {
  255. 0 => ConvertType::Json,
  256. _ => {
  257. tracing::error!("Invalid export type: {}", val);
  258. ConvertType::Json
  259. },
  260. }
  261. }
  262. }
  263. /// for the json type
  264. /// the data is the json string
  265. #[derive(Default, ProtoBuf, Debug)]
  266. pub struct ConvertDataPayloadPB {
  267. #[pb(index = 1)]
  268. pub convert_type: ConvertType,
  269. #[pb(index = 2)]
  270. pub data: Vec<u8>,
  271. }
  272. pub struct ConvertDataParams {
  273. pub convert_type: ConvertType,
  274. pub data: Vec<u8>,
  275. }
  276. impl TryInto<ConvertDataParams> for ConvertDataPayloadPB {
  277. type Error = ErrorCode;
  278. fn try_into(self) -> Result<ConvertDataParams, Self::Error> {
  279. let convert_type = self.convert_type;
  280. let data = self.data;
  281. Ok(ConvertDataParams { convert_type, data })
  282. }
  283. }
  284. #[derive(Debug, Default, ProtoBuf)]
  285. pub struct RepeatedDocumentSnapshotPB {
  286. #[pb(index = 1)]
  287. pub items: Vec<DocumentSnapshotPB>,
  288. }
  289. #[derive(Debug, Default, ProtoBuf)]
  290. pub struct DocumentSnapshotPB {
  291. #[pb(index = 1)]
  292. pub snapshot_id: i64,
  293. #[pb(index = 2)]
  294. pub snapshot_desc: String,
  295. #[pb(index = 3)]
  296. pub created_at: i64,
  297. #[pb(index = 4)]
  298. pub data: Vec<u8>,
  299. }
  300. #[derive(Debug, Default, ProtoBuf)]
  301. pub struct DocumentSnapshotStatePB {
  302. #[pb(index = 1)]
  303. pub new_snapshot_id: i64,
  304. }
  305. #[derive(Debug, Default, ProtoBuf)]
  306. pub struct DocumentSyncStatePB {
  307. #[pb(index = 1)]
  308. pub is_syncing: bool,
  309. #[pb(index = 2)]
  310. pub is_finish: bool,
  311. }
  312. impl From<SyncState> for DocumentSyncStatePB {
  313. fn from(value: SyncState) -> Self {
  314. Self {
  315. is_syncing: value.is_syncing(),
  316. is_finish: value.is_sync_finished(),
  317. }
  318. }
  319. }