entities.rs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. use collab_document::blocks::{BlockAction, DocumentData};
  2. use std::collections::HashMap;
  3. use crate::parse::{NotEmptyStr, NotEmptyVec};
  4. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  5. use flowy_error::ErrorCode;
  6. #[derive(Default, ProtoBuf)]
  7. pub struct OpenDocumentPayloadPB {
  8. #[pb(index = 1)]
  9. pub document_id: String,
  10. }
  11. pub struct OpenDocumentParams {
  12. pub document_id: String,
  13. }
  14. impl TryInto<OpenDocumentParams> for OpenDocumentPayloadPB {
  15. type Error = ErrorCode;
  16. fn try_into(self) -> Result<OpenDocumentParams, Self::Error> {
  17. let document_id =
  18. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  19. Ok(OpenDocumentParams {
  20. document_id: document_id.0,
  21. })
  22. }
  23. }
  24. #[derive(Default, ProtoBuf)]
  25. pub struct DocumentRedoUndoPayloadPB {
  26. #[pb(index = 1)]
  27. pub document_id: String,
  28. }
  29. pub struct DocumentRedoUndoParams {
  30. pub document_id: String,
  31. }
  32. impl TryInto<DocumentRedoUndoParams> for DocumentRedoUndoPayloadPB {
  33. type Error = ErrorCode;
  34. fn try_into(self) -> Result<DocumentRedoUndoParams, Self::Error> {
  35. let document_id =
  36. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  37. Ok(DocumentRedoUndoParams {
  38. document_id: document_id.0,
  39. })
  40. }
  41. }
  42. #[derive(Default, Debug, ProtoBuf)]
  43. pub struct DocumentRedoUndoResponsePB {
  44. #[pb(index = 1)]
  45. pub can_undo: bool,
  46. #[pb(index = 2)]
  47. pub can_redo: bool,
  48. #[pb(index = 3)]
  49. pub is_success: bool,
  50. }
  51. #[derive(Default, ProtoBuf)]
  52. pub struct CreateDocumentPayloadPB {
  53. #[pb(index = 1)]
  54. pub document_id: String,
  55. #[pb(index = 2, one_of)]
  56. pub initial_data: Option<DocumentDataPB>,
  57. }
  58. pub struct CreateDocumentParams {
  59. pub document_id: String,
  60. pub initial_data: Option<DocumentData>,
  61. }
  62. impl TryInto<CreateDocumentParams> for CreateDocumentPayloadPB {
  63. type Error = ErrorCode;
  64. fn try_into(self) -> Result<CreateDocumentParams, Self::Error> {
  65. let document_id =
  66. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  67. let initial_data = self.initial_data.map(|data| data.into());
  68. Ok(CreateDocumentParams {
  69. document_id: document_id.0,
  70. initial_data,
  71. })
  72. }
  73. }
  74. #[derive(Default, ProtoBuf)]
  75. pub struct CloseDocumentPayloadPB {
  76. #[pb(index = 1)]
  77. pub document_id: String,
  78. }
  79. pub struct CloseDocumentParams {
  80. pub document_id: String,
  81. }
  82. impl TryInto<CloseDocumentParams> for CloseDocumentPayloadPB {
  83. type Error = ErrorCode;
  84. fn try_into(self) -> Result<CloseDocumentParams, Self::Error> {
  85. let document_id =
  86. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  87. Ok(CloseDocumentParams {
  88. document_id: document_id.0,
  89. })
  90. }
  91. }
  92. #[derive(Default, ProtoBuf, Debug)]
  93. pub struct ApplyActionPayloadPB {
  94. #[pb(index = 1)]
  95. pub document_id: String,
  96. #[pb(index = 2)]
  97. pub actions: Vec<BlockActionPB>,
  98. }
  99. pub struct ApplyActionParams {
  100. pub document_id: String,
  101. pub actions: Vec<BlockAction>,
  102. }
  103. impl TryInto<ApplyActionParams> for ApplyActionPayloadPB {
  104. type Error = ErrorCode;
  105. fn try_into(self) -> Result<ApplyActionParams, Self::Error> {
  106. let document_id =
  107. NotEmptyStr::parse(self.document_id).map_err(|_| ErrorCode::DocumentIdIsEmpty)?;
  108. let actions = NotEmptyVec::parse(self.actions).map_err(|_| ErrorCode::ApplyActionsIsEmpty)?;
  109. let actions = actions.0.into_iter().map(BlockAction::from).collect();
  110. Ok(ApplyActionParams {
  111. document_id: document_id.0,
  112. actions,
  113. })
  114. }
  115. }
  116. #[derive(Default, Debug, ProtoBuf)]
  117. pub struct DocumentDataPB {
  118. #[pb(index = 1)]
  119. pub page_id: String,
  120. #[pb(index = 2)]
  121. pub blocks: HashMap<String, BlockPB>,
  122. #[pb(index = 3)]
  123. pub meta: MetaPB,
  124. }
  125. #[derive(Default, ProtoBuf, Debug)]
  126. pub struct BlockPB {
  127. #[pb(index = 1)]
  128. pub id: String,
  129. #[pb(index = 2)]
  130. pub ty: String,
  131. #[pb(index = 3)]
  132. pub data: String,
  133. #[pb(index = 4)]
  134. pub parent_id: String,
  135. #[pb(index = 5)]
  136. pub children_id: String,
  137. }
  138. #[derive(Default, ProtoBuf, Debug)]
  139. pub struct MetaPB {
  140. #[pb(index = 1)]
  141. pub children_map: HashMap<String, ChildrenPB>,
  142. }
  143. #[derive(Default, ProtoBuf, Debug)]
  144. pub struct ChildrenPB {
  145. #[pb(index = 1)]
  146. pub children: Vec<String>,
  147. }
  148. // Actions
  149. #[derive(Default, ProtoBuf, Debug)]
  150. pub struct BlockActionPB {
  151. #[pb(index = 1)]
  152. pub action: BlockActionTypePB,
  153. #[pb(index = 2)]
  154. pub payload: BlockActionPayloadPB,
  155. }
  156. #[derive(Default, ProtoBuf, Debug)]
  157. pub struct BlockActionPayloadPB {
  158. #[pb(index = 1)]
  159. pub block: BlockPB,
  160. #[pb(index = 2, one_of)]
  161. pub prev_id: Option<String>,
  162. #[pb(index = 3, one_of)]
  163. pub parent_id: Option<String>,
  164. }
  165. #[derive(ProtoBuf_Enum, Debug)]
  166. pub enum BlockActionTypePB {
  167. Insert = 0,
  168. Update = 1,
  169. Delete = 2,
  170. Move = 3,
  171. }
  172. impl Default for BlockActionTypePB {
  173. fn default() -> Self {
  174. Self::Insert
  175. }
  176. }
  177. #[derive(ProtoBuf_Enum)]
  178. pub enum DeltaTypePB {
  179. Inserted = 0,
  180. Updated = 1,
  181. Removed = 2,
  182. }
  183. impl Default for DeltaTypePB {
  184. fn default() -> Self {
  185. Self::Inserted
  186. }
  187. }
  188. #[derive(Default, ProtoBuf)]
  189. pub struct DocEventPB {
  190. #[pb(index = 1)]
  191. pub events: Vec<BlockEventPB>,
  192. #[pb(index = 2)]
  193. pub is_remote: bool,
  194. }
  195. #[derive(Default, ProtoBuf)]
  196. pub struct BlockEventPB {
  197. #[pb(index = 1)]
  198. pub event: Vec<BlockEventPayloadPB>,
  199. }
  200. #[derive(Default, ProtoBuf)]
  201. pub struct BlockEventPayloadPB {
  202. #[pb(index = 1)]
  203. pub command: DeltaTypePB,
  204. #[pb(index = 2)]
  205. pub path: Vec<String>,
  206. #[pb(index = 3)]
  207. pub id: String,
  208. #[pb(index = 4)]
  209. pub value: String,
  210. }
  211. #[derive(PartialEq, Eq, Debug, ProtoBuf_Enum, Clone, Default)]
  212. pub enum ExportType {
  213. #[default]
  214. Text = 0,
  215. Markdown = 1,
  216. Link = 2,
  217. }
  218. impl From<i32> for ExportType {
  219. fn from(val: i32) -> Self {
  220. match val {
  221. 0 => ExportType::Text,
  222. 1 => ExportType::Markdown,
  223. 2 => ExportType::Link,
  224. _ => {
  225. tracing::error!("Invalid export type: {}", val);
  226. ExportType::Text
  227. },
  228. }
  229. }
  230. }
  231. #[derive(Default, ProtoBuf)]
  232. pub struct EditPayloadPB {
  233. #[pb(index = 1)]
  234. pub doc_id: String,
  235. // Encode in JSON format
  236. #[pb(index = 2)]
  237. pub operations: String,
  238. }
  239. #[derive(Default, ProtoBuf)]
  240. pub struct ExportDataPB {
  241. #[pb(index = 1)]
  242. pub data: String,
  243. #[pb(index = 2)]
  244. pub export_type: ExportType,
  245. }
  246. #[derive(PartialEq, Eq, Debug, ProtoBuf_Enum, Clone, Default)]
  247. pub enum ConvertType {
  248. #[default]
  249. Json = 0,
  250. }
  251. impl From<i32> for ConvertType {
  252. fn from(val: i32) -> Self {
  253. match val {
  254. 0 => ConvertType::Json,
  255. _ => {
  256. tracing::error!("Invalid export type: {}", val);
  257. ConvertType::Json
  258. },
  259. }
  260. }
  261. }
  262. /// for the json type
  263. /// the data is the json string
  264. #[derive(Default, ProtoBuf, Debug)]
  265. pub struct ConvertDataPayloadPB {
  266. #[pb(index = 1)]
  267. pub convert_type: ConvertType,
  268. #[pb(index = 2)]
  269. pub data: Vec<u8>,
  270. }
  271. pub struct ConvertDataParams {
  272. pub convert_type: ConvertType,
  273. pub data: Vec<u8>,
  274. }
  275. impl TryInto<ConvertDataParams> for ConvertDataPayloadPB {
  276. type Error = ErrorCode;
  277. fn try_into(self) -> Result<ConvertDataParams, Self::Error> {
  278. let convert_type = self.convert_type;
  279. let data = self.data;
  280. Ok(ConvertDataParams { convert_type, data })
  281. }
  282. }