2023-01-08 12:10:53 +08:00
|
|
|
import 'package:appflowy_backend/dispatch/dispatch.dart';
|
2023-12-31 07:29:40 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart';
|
2023-09-12 20:49:03 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
2023-12-31 07:29:40 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
|
2023-09-12 20:49:03 +08:00
|
|
|
import 'package:dartz/dartz.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-10-26 03:41:03 +02:00
|
|
|
final canOpen = await openDocument(viewId: view.id);
|
2023-05-16 14:58:24 +08:00
|
|
|
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-10-26 03:41:03 +02:00
|
|
|
required String viewId,
|
2023-04-13 18:53:51 +08:00
|
|
|
}) async {
|
2023-10-26 03:41:03 +02:00
|
|
|
final payload = OpenDocumentPayloadPB()..documentId = viewId;
|
2023-05-23 16:13:12 +08:00
|
|
|
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-10-26 03:41:03 +02:00
|
|
|
Future<Either<FlowyError, BlockPB>> getBlockFromDocument({
|
|
|
|
required DocumentDataPB document,
|
|
|
|
required String blockId,
|
|
|
|
}) async {
|
|
|
|
final block = document.blocks[blockId];
|
|
|
|
|
|
|
|
if (block != null) {
|
|
|
|
return right(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
return left(
|
|
|
|
FlowyError(
|
|
|
|
msg: 'Block($blockId) not found in Document(${document.pageId})',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-09-12 20:49:03 +08:00
|
|
|
|
|
|
|
/// Creates a new external text.
|
|
|
|
///
|
|
|
|
/// Normally, it's used to the block that needs sync long text.
|
|
|
|
///
|
|
|
|
/// the delta parameter is the json representation of the delta.
|
|
|
|
Future<Either<FlowyError, Unit>> createExternalText({
|
|
|
|
required String documentId,
|
|
|
|
required String textId,
|
|
|
|
String? delta,
|
|
|
|
}) async {
|
|
|
|
final payload = TextDeltaPayloadPB(
|
|
|
|
documentId: documentId,
|
|
|
|
textId: textId,
|
|
|
|
delta: delta,
|
|
|
|
);
|
|
|
|
final result = await DocumentEventCreateText(payload).send();
|
|
|
|
return result.swap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the external text.
|
|
|
|
///
|
|
|
|
/// this function is compatible with the [createExternalText] function.
|
|
|
|
///
|
|
|
|
/// the delta parameter is the json representation of the delta too.
|
|
|
|
Future<Either<FlowyError, Unit>> updateExternalText({
|
|
|
|
required String documentId,
|
|
|
|
required String textId,
|
|
|
|
String? delta,
|
|
|
|
}) async {
|
|
|
|
final payload = TextDeltaPayloadPB(
|
|
|
|
documentId: documentId,
|
|
|
|
textId: textId,
|
|
|
|
delta: delta,
|
|
|
|
);
|
|
|
|
final result = await DocumentEventApplyTextDeltaEvent(payload).send();
|
|
|
|
return result.swap();
|
|
|
|
}
|
2024-01-20 23:16:18 +08:00
|
|
|
|
|
|
|
/// Upload a file to the cloud storage.
|
|
|
|
Future<Either<FlowyError, UploadedFilePB>> uploadFile({
|
|
|
|
required String localFilePath,
|
2024-02-19 16:24:47 +07:00
|
|
|
bool isAsync = true,
|
2024-01-20 23:16:18 +08:00
|
|
|
}) async {
|
|
|
|
final workspace = await FolderEventReadCurrentWorkspace().send();
|
|
|
|
return workspace.fold((l) async {
|
|
|
|
final payload = UploadFileParamsPB(
|
|
|
|
workspaceId: l.id,
|
|
|
|
localFilePath: localFilePath,
|
2024-02-19 16:24:47 +07:00
|
|
|
isAsync: isAsync,
|
2024-01-20 23:16:18 +08:00
|
|
|
);
|
|
|
|
final result = await DocumentEventUploadFile(payload).send();
|
|
|
|
return result.swap();
|
|
|
|
}, (r) async {
|
|
|
|
return left(FlowyError(msg: 'Workspace not found'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Download a file from the cloud storage.
|
|
|
|
Future<Either<FlowyError, Unit>> downloadFile({
|
|
|
|
required String url,
|
|
|
|
}) async {
|
|
|
|
final workspace = await FolderEventReadCurrentWorkspace().send();
|
|
|
|
return workspace.fold((l) async {
|
|
|
|
final payload = UploadedFilePB(
|
|
|
|
url: url,
|
|
|
|
);
|
|
|
|
final result = await DocumentEventDownloadFile(payload).send();
|
|
|
|
return result.swap();
|
|
|
|
}, (r) async {
|
|
|
|
return left(FlowyError(msg: 'Workspace not found'));
|
|
|
|
});
|
|
|
|
}
|
2021-07-23 16:45:29 +08:00
|
|
|
}
|