view.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. use crate::{
  2. entities::trash::{Trash, TrashType},
  3. errors::ErrorCode,
  4. impl_def_and_def_mut,
  5. parser::{
  6. app::AppIdentify,
  7. view::{ViewDesc, ViewIdentify, ViewName, ViewThumbnail},
  8. },
  9. };
  10. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  11. use serde::{Deserialize, Serialize};
  12. use std::convert::TryInto;
  13. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone, Serialize, Deserialize)]
  14. pub struct View {
  15. #[pb(index = 1)]
  16. pub id: String,
  17. #[pb(index = 2)]
  18. pub belong_to_id: String,
  19. #[pb(index = 3)]
  20. pub name: String,
  21. #[pb(index = 4)]
  22. pub desc: String,
  23. #[pb(index = 5)]
  24. pub view_type: ViewType,
  25. #[pb(index = 6)]
  26. pub version: i64,
  27. #[pb(index = 7)]
  28. pub belongings: RepeatedView,
  29. #[pb(index = 8)]
  30. pub modified_time: i64,
  31. #[pb(index = 9)]
  32. pub create_time: i64,
  33. }
  34. #[derive(PartialEq, Debug, Default, ProtoBuf, Clone, Serialize, Deserialize)]
  35. #[serde(transparent)]
  36. pub struct RepeatedView {
  37. #[pb(index = 1)]
  38. pub items: Vec<View>,
  39. }
  40. impl_def_and_def_mut!(RepeatedView, View);
  41. impl std::convert::From<View> for Trash {
  42. fn from(view: View) -> Self {
  43. Trash {
  44. id: view.id,
  45. name: view.name,
  46. modified_time: view.modified_time,
  47. create_time: view.create_time,
  48. ty: TrashType::View,
  49. }
  50. }
  51. }
  52. #[derive(PartialEq, Debug, ProtoBuf_Enum, Clone, Serialize, Deserialize)]
  53. pub enum ViewType {
  54. Blank = 0,
  55. Doc = 1,
  56. }
  57. impl std::default::Default for ViewType {
  58. fn default() -> Self { ViewType::Blank }
  59. }
  60. impl std::convert::From<i32> for ViewType {
  61. fn from(val: i32) -> Self {
  62. match val {
  63. 1 => ViewType::Doc,
  64. 0 => ViewType::Blank,
  65. _ => {
  66. log::error!("Invalid view type: {}", val);
  67. ViewType::Blank
  68. },
  69. }
  70. }
  71. }
  72. #[derive(Default, ProtoBuf)]
  73. pub struct CreateViewRequest {
  74. #[pb(index = 1)]
  75. pub belong_to_id: String,
  76. #[pb(index = 2)]
  77. pub name: String,
  78. #[pb(index = 3)]
  79. pub desc: String,
  80. #[pb(index = 4, one_of)]
  81. pub thumbnail: Option<String>,
  82. #[pb(index = 5)]
  83. pub view_type: ViewType,
  84. }
  85. #[derive(Default, ProtoBuf, Debug, Clone)]
  86. pub struct CreateViewParams {
  87. #[pb(index = 1)]
  88. pub belong_to_id: String,
  89. #[pb(index = 2)]
  90. pub name: String,
  91. #[pb(index = 3)]
  92. pub desc: String,
  93. #[pb(index = 4)]
  94. pub thumbnail: String,
  95. #[pb(index = 5)]
  96. pub view_type: ViewType,
  97. // ViewType::Doc -> Delta string
  98. #[pb(index = 6)]
  99. pub view_data: String,
  100. #[pb(index = 7)]
  101. pub view_id: String,
  102. }
  103. impl CreateViewParams {
  104. pub fn new(
  105. belong_to_id: String,
  106. name: String,
  107. desc: String,
  108. view_type: ViewType,
  109. thumbnail: String,
  110. view_data: String,
  111. view_id: String,
  112. ) -> Self {
  113. Self {
  114. belong_to_id,
  115. name,
  116. desc,
  117. thumbnail,
  118. view_type,
  119. view_data,
  120. view_id,
  121. }
  122. }
  123. }
  124. impl TryInto<CreateViewParams> for CreateViewRequest {
  125. type Error = ErrorCode;
  126. fn try_into(self) -> Result<CreateViewParams, Self::Error> {
  127. let name = ViewName::parse(self.name)?.0;
  128. let belong_to_id = AppIdentify::parse(self.belong_to_id)?.0;
  129. let view_data = "".to_string();
  130. let view_id = uuid::Uuid::new_v4().to_string();
  131. let thumbnail = match self.thumbnail {
  132. None => "".to_string(),
  133. Some(thumbnail) => ViewThumbnail::parse(thumbnail)?.0,
  134. };
  135. Ok(CreateViewParams::new(
  136. belong_to_id,
  137. name,
  138. self.desc,
  139. self.view_type,
  140. thumbnail,
  141. view_data,
  142. view_id,
  143. ))
  144. }
  145. }
  146. #[derive(Default, ProtoBuf)]
  147. pub struct QueryViewRequest {
  148. #[pb(index = 1)]
  149. pub view_ids: Vec<String>,
  150. }
  151. #[derive(Default, ProtoBuf, Clone, Debug)]
  152. pub struct ViewId {
  153. #[pb(index = 1)]
  154. pub view_id: String,
  155. }
  156. impl std::convert::From<String> for ViewId {
  157. fn from(view_id: String) -> Self { ViewId { view_id } }
  158. }
  159. impl TryInto<ViewId> for QueryViewRequest {
  160. type Error = ErrorCode;
  161. fn try_into(self) -> Result<ViewId, Self::Error> {
  162. debug_assert!(self.view_ids.len() == 1);
  163. if self.view_ids.len() != 1 {
  164. log::error!("The len of view_ids should be equal to 1");
  165. return Err(ErrorCode::ViewIdInvalid);
  166. }
  167. let view_id = self.view_ids.first().unwrap().clone();
  168. let view_id = ViewIdentify::parse(view_id)?.0;
  169. Ok(ViewId { view_id })
  170. }
  171. }
  172. #[derive(Default, ProtoBuf)]
  173. pub struct RepeatedViewId {
  174. #[pb(index = 1)]
  175. pub items: Vec<String>,
  176. }
  177. impl TryInto<RepeatedViewId> for QueryViewRequest {
  178. type Error = ErrorCode;
  179. fn try_into(self) -> Result<RepeatedViewId, Self::Error> {
  180. let mut view_ids = vec![];
  181. for view_id in self.view_ids {
  182. let view_id = ViewIdentify::parse(view_id)?.0;
  183. view_ids.push(view_id);
  184. }
  185. Ok(RepeatedViewId { items: view_ids })
  186. }
  187. }
  188. #[derive(Default, ProtoBuf)]
  189. pub struct UpdateViewRequest {
  190. #[pb(index = 1)]
  191. pub view_id: String,
  192. #[pb(index = 2, one_of)]
  193. pub name: Option<String>,
  194. #[pb(index = 3, one_of)]
  195. pub desc: Option<String>,
  196. #[pb(index = 4, one_of)]
  197. pub thumbnail: Option<String>,
  198. }
  199. #[derive(Default, ProtoBuf, Clone, Debug)]
  200. pub struct UpdateViewParams {
  201. #[pb(index = 1)]
  202. pub view_id: String,
  203. #[pb(index = 2, one_of)]
  204. pub name: Option<String>,
  205. #[pb(index = 3, one_of)]
  206. pub desc: Option<String>,
  207. #[pb(index = 4, one_of)]
  208. pub thumbnail: Option<String>,
  209. }
  210. impl UpdateViewParams {
  211. pub fn new(view_id: &str) -> Self {
  212. Self {
  213. view_id: view_id.to_owned(),
  214. ..Default::default()
  215. }
  216. }
  217. pub fn name(mut self, name: &str) -> Self {
  218. self.name = Some(name.to_owned());
  219. self
  220. }
  221. pub fn desc(mut self, desc: &str) -> Self {
  222. self.desc = Some(desc.to_owned());
  223. self
  224. }
  225. }
  226. impl TryInto<UpdateViewParams> for UpdateViewRequest {
  227. type Error = ErrorCode;
  228. fn try_into(self) -> Result<UpdateViewParams, Self::Error> {
  229. let view_id = ViewIdentify::parse(self.view_id)?.0;
  230. let name = match self.name {
  231. None => None,
  232. Some(name) => Some(ViewName::parse(name)?.0),
  233. };
  234. let desc = match self.desc {
  235. None => None,
  236. Some(desc) => Some(ViewDesc::parse(desc)?.0),
  237. };
  238. let thumbnail = match self.thumbnail {
  239. None => None,
  240. Some(thumbnail) => Some(ViewThumbnail::parse(thumbnail)?.0),
  241. };
  242. Ok(UpdateViewParams {
  243. view_id,
  244. name,
  245. desc,
  246. thumbnail,
  247. })
  248. }
  249. }