view_create.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. use crate::{
  2. entities::trash::{Trash, TrashType},
  3. errors::ErrorCode,
  4. impl_def_and_def_mut,
  5. parser::{
  6. app::AppIdentify,
  7. view::{ViewName, ViewThumbnail},
  8. },
  9. };
  10. use flowy_collaboration::document::default::initial_delta_string;
  11. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  12. use std::convert::TryInto;
  13. #[derive(PartialEq, Debug, ProtoBuf_Enum, Clone)]
  14. pub enum ViewType {
  15. Blank = 0,
  16. Doc = 1,
  17. }
  18. impl std::default::Default for ViewType {
  19. fn default() -> Self { ViewType::Blank }
  20. }
  21. impl std::convert::From<i32> for ViewType {
  22. fn from(val: i32) -> Self {
  23. match val {
  24. 1 => ViewType::Doc,
  25. 0 => ViewType::Blank,
  26. _ => {
  27. log::error!("Invalid view type: {}", val);
  28. ViewType::Blank
  29. },
  30. }
  31. }
  32. }
  33. #[derive(Default, ProtoBuf)]
  34. pub struct CreateViewRequest {
  35. #[pb(index = 1)]
  36. pub belong_to_id: String,
  37. #[pb(index = 2)]
  38. pub name: String,
  39. #[pb(index = 3)]
  40. pub desc: String,
  41. #[pb(index = 4, one_of)]
  42. pub thumbnail: Option<String>,
  43. #[pb(index = 5)]
  44. pub view_type: ViewType,
  45. }
  46. #[derive(Default, ProtoBuf, Debug, Clone)]
  47. pub struct CreateViewParams {
  48. #[pb(index = 1)]
  49. pub belong_to_id: String,
  50. #[pb(index = 2)]
  51. pub name: String,
  52. #[pb(index = 3)]
  53. pub desc: String,
  54. #[pb(index = 4)]
  55. pub thumbnail: String,
  56. #[pb(index = 5)]
  57. pub view_type: ViewType,
  58. // ViewType::Doc -> Delta string
  59. #[pb(index = 6)]
  60. pub view_data: String,
  61. #[pb(index = 7)]
  62. pub view_id: String,
  63. }
  64. impl CreateViewParams {
  65. pub fn new(
  66. belong_to_id: String,
  67. name: String,
  68. desc: String,
  69. view_type: ViewType,
  70. thumbnail: String,
  71. view_data: String,
  72. view_id: String,
  73. ) -> Self {
  74. Self {
  75. belong_to_id,
  76. name,
  77. desc,
  78. thumbnail,
  79. view_type,
  80. view_data,
  81. view_id,
  82. }
  83. }
  84. pub fn take_view_data(&mut self) -> String { std::mem::take(&mut self.view_data) }
  85. }
  86. impl TryInto<CreateViewParams> for CreateViewRequest {
  87. type Error = ErrorCode;
  88. fn try_into(self) -> Result<CreateViewParams, Self::Error> {
  89. let name = ViewName::parse(self.name)?.0;
  90. let belong_to_id = AppIdentify::parse(self.belong_to_id)?.0;
  91. let view_data = initial_delta_string();
  92. let view_id = uuid::Uuid::new_v4().to_string();
  93. let thumbnail = match self.thumbnail {
  94. None => "".to_string(),
  95. Some(thumbnail) => ViewThumbnail::parse(thumbnail)?.0,
  96. };
  97. Ok(CreateViewParams::new(
  98. belong_to_id,
  99. name,
  100. self.desc,
  101. self.view_type,
  102. thumbnail,
  103. view_data,
  104. view_id,
  105. ))
  106. }
  107. }
  108. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
  109. pub struct View {
  110. #[pb(index = 1)]
  111. pub id: String,
  112. #[pb(index = 2)]
  113. pub belong_to_id: String,
  114. #[pb(index = 3)]
  115. pub name: String,
  116. #[pb(index = 4)]
  117. pub desc: String,
  118. #[pb(index = 5)]
  119. pub view_type: ViewType,
  120. #[pb(index = 6)]
  121. pub version: i64,
  122. #[pb(index = 7)]
  123. pub belongings: RepeatedView,
  124. #[pb(index = 8)]
  125. pub modified_time: i64,
  126. #[pb(index = 9)]
  127. pub create_time: i64,
  128. }
  129. #[derive(PartialEq, Debug, Default, ProtoBuf, Clone)]
  130. pub struct RepeatedView {
  131. #[pb(index = 1)]
  132. pub items: Vec<View>,
  133. }
  134. impl_def_and_def_mut!(RepeatedView, View);
  135. impl std::convert::From<View> for Trash {
  136. fn from(view: View) -> Self {
  137. Trash {
  138. id: view.id,
  139. name: view.name,
  140. modified_time: view.modified_time,
  141. create_time: view.create_time,
  142. ty: TrashType::View,
  143. }
  144. }
  145. }