controller.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. use std::collections::HashMap;
  2. use std::marker::PhantomData;
  3. use std::sync::Arc;
  4. use collab_database::fields::{Field, TypeOptionData};
  5. use collab_database::rows::{Cell, Cells, Row, RowId};
  6. use serde::de::DeserializeOwned;
  7. use serde::Serialize;
  8. use flowy_error::FlowyResult;
  9. use crate::entities::{FieldType, GroupChangesPB, GroupRowsNotificationPB, InsertedRowPB};
  10. use crate::services::cell::{get_cell_protobuf, CellProtobufBlobParser, DecodedCellData};
  11. use crate::services::group::action::{
  12. DidMoveGroupRowResult, DidUpdateGroupRowResult, GroupControllerOperation, GroupCustomize,
  13. };
  14. use crate::services::group::configuration::GroupContext;
  15. use crate::services::group::entities::GroupData;
  16. use crate::services::group::{Group, GroupSettingChangeset};
  17. // use collab_database::views::Group;
  18. /// The [GroupController] trait defines the group actions, including create/delete/move items
  19. /// For example, the group will insert a item if the one of the new [RowRevision]'s [CellRevision]s
  20. /// content match the group filter.
  21. ///
  22. /// Different [FieldType] has a different controller that implements the [GroupController] trait.
  23. /// If the [FieldType] doesn't implement its group controller, then the [DefaultGroupController] will
  24. /// be used.
  25. ///
  26. pub trait GroupController: GroupControllerOperation + Send + Sync {
  27. /// Called when the type option of the [Field] was updated.
  28. fn did_update_field_type_option(&mut self, field: &Arc<Field>);
  29. /// Called before the row was created.
  30. fn will_create_row(&mut self, cells: &mut Cells, field: &Field, group_id: &str);
  31. /// Called after the row was created.
  32. fn did_create_row(&mut self, row: &Row, group_id: &str);
  33. }
  34. /// The [GroupsBuilder] trait is used to generate the groups for different [FieldType]
  35. pub trait GroupsBuilder {
  36. type Context;
  37. type TypeOptionType;
  38. fn build(
  39. field: &Field,
  40. context: &Self::Context,
  41. type_option: &Option<Self::TypeOptionType>,
  42. ) -> GeneratedGroups;
  43. }
  44. pub struct GeneratedGroups {
  45. pub no_status_group: Option<Group>,
  46. pub group_configs: Vec<GeneratedGroupConfig>,
  47. }
  48. pub struct GeneratedGroupConfig {
  49. pub group: Group,
  50. pub filter_content: String,
  51. }
  52. pub struct MoveGroupRowContext<'a> {
  53. pub row: &'a Row,
  54. pub row_changeset: &'a mut RowChangeset,
  55. pub field: &'a Field,
  56. pub to_group_id: &'a str,
  57. pub to_row_id: Option<RowId>,
  58. }
  59. #[derive(Debug, Clone)]
  60. pub struct RowChangeset {
  61. pub row_id: RowId,
  62. pub height: Option<i32>,
  63. pub visibility: Option<bool>,
  64. // Contains the key/value changes represents as the update of the cells. For example,
  65. // if there is one cell was changed, then the `cell_by_field_id` will only have one key/value.
  66. pub cell_by_field_id: HashMap<String, Cell>,
  67. }
  68. impl RowChangeset {
  69. pub fn new(row_id: RowId) -> Self {
  70. Self {
  71. row_id,
  72. height: None,
  73. visibility: None,
  74. cell_by_field_id: Default::default(),
  75. }
  76. }
  77. pub fn is_empty(&self) -> bool {
  78. self.height.is_none() && self.visibility.is_none() && self.cell_by_field_id.is_empty()
  79. }
  80. }
  81. /// C: represents the group configuration that impl [GroupConfigurationSerde]
  82. /// T: the type-option data deserializer that impl [TypeOptionDataDeserializer]
  83. /// G: the group generator, [GroupsBuilder]
  84. /// P: the parser that impl [CellProtobufBlobParser] for the CellBytes
  85. pub struct BaseGroupController<C, T, G, P> {
  86. pub grouping_field_id: String,
  87. pub type_option: Option<T>,
  88. pub context: GroupContext<C>,
  89. group_action_phantom: PhantomData<G>,
  90. cell_parser_phantom: PhantomData<P>,
  91. }
  92. impl<C, T, G, P> BaseGroupController<C, T, G, P>
  93. where
  94. C: Serialize + DeserializeOwned,
  95. T: From<TypeOptionData>,
  96. G: GroupsBuilder<Context = GroupContext<C>, TypeOptionType = T>,
  97. {
  98. pub async fn new(
  99. grouping_field: &Arc<Field>,
  100. mut configuration: GroupContext<C>,
  101. ) -> FlowyResult<Self> {
  102. let field_type = FieldType::from(grouping_field.field_type);
  103. let type_option = grouping_field.get_type_option::<T>(field_type);
  104. let generated_groups = G::build(grouping_field, &configuration, &type_option);
  105. let _ = configuration.init_groups(generated_groups)?;
  106. Ok(Self {
  107. grouping_field_id: grouping_field.id.clone(),
  108. type_option,
  109. context: configuration,
  110. group_action_phantom: PhantomData,
  111. cell_parser_phantom: PhantomData,
  112. })
  113. }
  114. // https://stackoverflow.com/questions/69413164/how-to-fix-this-clippy-warning-needless-collect
  115. #[allow(clippy::needless_collect)]
  116. fn update_no_status_group(
  117. &mut self,
  118. row: &Row,
  119. other_group_changesets: &[GroupRowsNotificationPB],
  120. ) -> Option<GroupRowsNotificationPB> {
  121. let no_status_group = self.context.get_mut_no_status_group()?;
  122. // [other_group_inserted_row] contains all the inserted rows except the default group.
  123. let other_group_inserted_row = other_group_changesets
  124. .iter()
  125. .flat_map(|changeset| &changeset.inserted_rows)
  126. .collect::<Vec<&InsertedRowPB>>();
  127. // Calculate the inserted_rows of the default_group
  128. let no_status_group_rows = other_group_changesets
  129. .iter()
  130. .flat_map(|changeset| &changeset.deleted_rows)
  131. .cloned()
  132. .filter(|row_id| {
  133. // if the [other_group_inserted_row] contains the row_id of the row
  134. // which means the row should not move to the default group.
  135. !other_group_inserted_row
  136. .iter()
  137. .any(|inserted_row| &inserted_row.row.id == row_id)
  138. })
  139. .collect::<Vec<String>>();
  140. let mut changeset = GroupRowsNotificationPB::new(no_status_group.id.clone());
  141. if !no_status_group_rows.is_empty() {
  142. changeset.inserted_rows.push(InsertedRowPB::new(row.into()));
  143. no_status_group.add_row(row.clone());
  144. }
  145. // [other_group_delete_rows] contains all the deleted rows except the default group.
  146. let other_group_delete_rows: Vec<String> = other_group_changesets
  147. .iter()
  148. .flat_map(|changeset| &changeset.deleted_rows)
  149. .cloned()
  150. .collect();
  151. let default_group_deleted_rows = other_group_changesets
  152. .iter()
  153. .flat_map(|changeset| &changeset.inserted_rows)
  154. .filter(|inserted_row| {
  155. // if the [other_group_delete_rows] contain the inserted_row, which means this row should move
  156. // out from the default_group.
  157. !other_group_delete_rows
  158. .iter()
  159. .any(|row_id| &inserted_row.row.id == row_id)
  160. })
  161. .collect::<Vec<&InsertedRowPB>>();
  162. let mut deleted_row_ids = vec![];
  163. for row in &no_status_group.rows {
  164. let row_id = row.id.clone().into_inner();
  165. if default_group_deleted_rows
  166. .iter()
  167. .any(|deleted_row| deleted_row.row.id == row_id)
  168. {
  169. deleted_row_ids.push(row_id);
  170. }
  171. }
  172. no_status_group
  173. .rows
  174. .retain(|row| !deleted_row_ids.contains(&row.id));
  175. changeset.deleted_rows.extend(deleted_row_ids);
  176. Some(changeset)
  177. }
  178. }
  179. impl<C, T, G, P> GroupControllerOperation for BaseGroupController<C, T, G, P>
  180. where
  181. P: CellProtobufBlobParser,
  182. C: Serialize + DeserializeOwned,
  183. T: From<TypeOptionData>,
  184. G: GroupsBuilder<Context = GroupContext<C>, TypeOptionType = T>,
  185. Self: GroupCustomize<CellData = P::Object>,
  186. {
  187. fn field_id(&self) -> &str {
  188. &self.grouping_field_id
  189. }
  190. fn groups(&self) -> Vec<&GroupData> {
  191. self.context.groups()
  192. }
  193. fn get_group(&self, group_id: &str) -> Option<(usize, GroupData)> {
  194. let group = self.context.get_group(group_id)?;
  195. Some((group.0, group.1.clone()))
  196. }
  197. #[tracing::instrument(level = "trace", skip_all, fields(row_count=%rows.len(), group_result))]
  198. fn fill_groups(&mut self, rows: &[&Row], field: &Field) -> FlowyResult<()> {
  199. for row in rows {
  200. let cell = match row.cells.get(&self.grouping_field_id) {
  201. None => self.placeholder_cell(),
  202. Some(cell) => Some(cell.clone()),
  203. };
  204. if let Some(cell) = cell {
  205. let mut grouped_rows: Vec<GroupedRow> = vec![];
  206. let cell_bytes = get_cell_protobuf(&cell, field, None);
  207. let cell_data = cell_bytes.parser::<P>()?;
  208. for group in self.context.groups() {
  209. if self.can_group(&group.filter_content, &cell_data) {
  210. grouped_rows.push(GroupedRow {
  211. row: (*row).clone(),
  212. group_id: group.id.clone(),
  213. });
  214. }
  215. }
  216. if !grouped_rows.is_empty() {
  217. for group_row in grouped_rows {
  218. if let Some(group) = self.context.get_mut_group(&group_row.group_id) {
  219. group.add_row(group_row.row);
  220. }
  221. }
  222. continue;
  223. }
  224. }
  225. match self.context.get_mut_no_status_group() {
  226. None => {},
  227. Some(no_status_group) => no_status_group.add_row((*row).clone()),
  228. }
  229. }
  230. tracing::Span::current().record("group_result", format!("{},", self.context,).as_str());
  231. Ok(())
  232. }
  233. fn move_group(&mut self, from_group_id: &str, to_group_id: &str) -> FlowyResult<()> {
  234. self.context.move_group(from_group_id, to_group_id)
  235. }
  236. fn did_update_group_row(
  237. &mut self,
  238. old_row: &Option<Row>,
  239. row: &Row,
  240. field: &Field,
  241. ) -> FlowyResult<DidUpdateGroupRowResult> {
  242. // let cell_data = row_rev.cells.get(&self.field_id).and_then(|cell_rev| {
  243. // let cell_data: Option<P> = get_type_cell_data(cell_rev, field_rev, None);
  244. // cell_data
  245. // });
  246. let mut result = DidUpdateGroupRowResult {
  247. inserted_group: None,
  248. deleted_group: None,
  249. row_changesets: vec![],
  250. };
  251. if let Some(cell_data) = get_cell_data_from_row::<P>(Some(row), field) {
  252. let old_row = old_row.as_ref();
  253. let old_cell_data = get_cell_data_from_row::<P>(old_row, field);
  254. if let Ok((insert, delete)) =
  255. self.create_or_delete_group_when_cell_changed(row, old_cell_data.as_ref(), &cell_data)
  256. {
  257. result.inserted_group = insert;
  258. result.deleted_group = delete;
  259. }
  260. let mut changesets = self.add_or_remove_row_when_cell_changed(row, &cell_data);
  261. if let Some(changeset) = self.update_no_status_group(row, &changesets) {
  262. if !changeset.is_empty() {
  263. changesets.push(changeset);
  264. }
  265. }
  266. result.row_changesets = changesets;
  267. }
  268. Ok(result)
  269. }
  270. fn did_delete_delete_row(
  271. &mut self,
  272. row: &Row,
  273. field: &Field,
  274. ) -> FlowyResult<DidMoveGroupRowResult> {
  275. // if the cell_rev is none, then the row must in the default group.
  276. let mut result = DidMoveGroupRowResult {
  277. deleted_group: None,
  278. row_changesets: vec![],
  279. };
  280. if let Some(cell) = row.cells.get(&self.grouping_field_id) {
  281. let cell_bytes = get_cell_protobuf(cell, field, None);
  282. let cell_data = cell_bytes.parser::<P>()?;
  283. if !cell_data.is_empty() {
  284. tracing::error!("did_delete_delete_row {:?}", cell);
  285. result.row_changesets = self.delete_row(row, &cell_data);
  286. return Ok(result);
  287. }
  288. }
  289. match self.context.get_no_status_group() {
  290. None => {
  291. tracing::error!("Unexpected None value. It should have the no status group");
  292. },
  293. Some(no_status_group) => {
  294. if !no_status_group.contains_row(&row.id) {
  295. tracing::error!("The row: {:?} should be in the no status group", row.id);
  296. }
  297. result.row_changesets = vec![GroupRowsNotificationPB::delete(
  298. no_status_group.id.clone(),
  299. vec![row.id.clone().into_inner()],
  300. )];
  301. },
  302. }
  303. Ok(result)
  304. }
  305. #[tracing::instrument(level = "trace", skip_all, err)]
  306. fn move_group_row(&mut self, context: MoveGroupRowContext) -> FlowyResult<DidMoveGroupRowResult> {
  307. let mut result = DidMoveGroupRowResult {
  308. deleted_group: None,
  309. row_changesets: vec![],
  310. };
  311. let cell = match context.row.cells.get(&self.grouping_field_id) {
  312. Some(cell) => Some(cell.clone()),
  313. None => self.placeholder_cell(),
  314. };
  315. if let Some(cell) = cell {
  316. let cell_bytes = get_cell_protobuf(&cell, context.field, None);
  317. let cell_data = cell_bytes.parser::<P>()?;
  318. result.deleted_group = self.delete_group_when_move_row(context.row, &cell_data);
  319. result.row_changesets = self.move_row(&cell_data, context);
  320. } else {
  321. tracing::warn!("Unexpected moving group row, changes should not be empty");
  322. }
  323. Ok(result)
  324. }
  325. fn did_update_group_field(&mut self, _field: &Field) -> FlowyResult<Option<GroupChangesPB>> {
  326. Ok(None)
  327. }
  328. fn apply_group_setting_changeset(&mut self, changeset: GroupSettingChangeset) -> FlowyResult<()> {
  329. for group_changeset in changeset.update_groups {
  330. if let Err(e) = self.context.update_group(group_changeset) {
  331. tracing::error!("Failed to update group: {:?}", e);
  332. }
  333. }
  334. Ok(())
  335. }
  336. }
  337. struct GroupedRow {
  338. row: Row,
  339. group_id: String,
  340. }
  341. fn get_cell_data_from_row<P: CellProtobufBlobParser>(
  342. row: Option<&Row>,
  343. field: &Field,
  344. ) -> Option<P::Object> {
  345. let cell = row.and_then(|row| row.cells.get(&field.id))?;
  346. let cell_bytes = get_cell_protobuf(cell, field, None);
  347. cell_bytes.parser::<P>().ok()
  348. }