| 
									
										
										
										
											2021-07-23 16:45:29 +08:00
										 |  |  | import 'package:dartz/dartz.dart'; | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/dispatch/dispatch.dart'; | 
					
						
							| 
									
										
										
										
											2022-03-19 16:52:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 08:41:16 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-04-13 18:53:51 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-document2/entities.pb.dart'; | 
					
						
							| 
									
										
										
										
											2021-07-23 16:45:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 21:02:46 -05:00
										 |  |  | class DocumentService { | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   // unused now.
 | 
					
						
							|  |  |  |   Future<Either<FlowyError, Unit>> createDocument({ | 
					
						
							| 
									
										
										
										
											2022-10-22 21:57:44 +08:00
										 |  |  |     required ViewPB view, | 
					
						
							| 
									
										
										
										
											2022-03-06 21:22:42 +08:00
										 |  |  |   }) async { | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     final canOpen = await openDocument(view: view); | 
					
						
							|  |  |  |     if (canOpen.isRight()) { | 
					
						
							|  |  |  |       return const Right(unit); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-23 16:13:12 +08:00
										 |  |  |     final payload = CreateDocumentPayloadPB()..documentId = view.id; | 
					
						
							|  |  |  |     final result = await DocumentEventCreateDocument(payload).send(); | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     return result.swap(); | 
					
						
							| 
									
										
										
										
											2021-09-11 21:30:58 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-23 16:13:12 +08:00
										 |  |  |   Future<Either<FlowyError, DocumentDataPB>> openDocument({ | 
					
						
							| 
									
										
										
										
											2023-04-13 18:53:51 +08:00
										 |  |  |     required ViewPB view, | 
					
						
							|  |  |  |   }) async { | 
					
						
							| 
									
										
										
										
											2023-05-23 16:13:12 +08:00
										 |  |  |     final payload = OpenDocumentPayloadPB()..documentId = view.id; | 
					
						
							|  |  |  |     final result = await DocumentEventOpenDocument(payload).send(); | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     return result.swap(); | 
					
						
							| 
									
										
										
										
											2023-04-13 18:53:51 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   Future<Either<FlowyError, Unit>> closeDocument({ | 
					
						
							| 
									
										
										
										
											2023-04-13 18:53:51 +08:00
										 |  |  |     required ViewPB view, | 
					
						
							|  |  |  |   }) async { | 
					
						
							| 
									
										
										
										
											2023-05-23 16:13:12 +08:00
										 |  |  |     final payload = CloseDocumentPayloadPB()..documentId = view.id; | 
					
						
							|  |  |  |     final result = await DocumentEventCloseDocument(payload).send(); | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     return result.swap(); | 
					
						
							| 
									
										
										
										
											2023-04-13 18:53:51 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |   Future<Either<FlowyError, Unit>> applyAction({ | 
					
						
							|  |  |  |     required String documentId, | 
					
						
							|  |  |  |     required Iterable<BlockActionPB> actions, | 
					
						
							| 
									
										
										
										
											2023-04-13 18:53:51 +08:00
										 |  |  |   }) async { | 
					
						
							| 
									
										
										
										
											2023-05-23 16:13:12 +08:00
										 |  |  |     final payload = ApplyActionPayloadPB( | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |       documentId: documentId, | 
					
						
							| 
									
										
										
										
											2023-04-13 18:53:51 +08:00
										 |  |  |       actions: actions, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-05-23 16:13:12 +08:00
										 |  |  |     final result = await DocumentEventApplyAction(payload).send(); | 
					
						
							| 
									
										
										
										
											2023-05-16 14:58:24 +08:00
										 |  |  |     return result.swap(); | 
					
						
							| 
									
										
										
										
											2023-04-13 18:53:51 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-07-23 16:45:29 +08:00
										 |  |  | } |