From 39f2789e13c40000d2bbddd955613433719d3798 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Thu, 27 Jun 2024 17:57:33 +0800 Subject: [PATCH] chore: refactor publish bloc --- .../application/document_share_bloc.dart | 38 +++++++++++-------- .../presentation/share/publish_tab.dart | 27 +++++++++++-- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart b/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart index 01621a64cb..f1303835b0 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart @@ -7,7 +7,6 @@ import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; import 'package:appflowy_result/appflowy_result.dart'; -import 'package:flowy_infra/uuid.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; @@ -51,27 +50,31 @@ class DocumentShareBloc extends Bloc { ), ); }, - publish: (url) async { - // todo: optimize the logic - const prefix = 'appflowy'; - final name = '${view.name}-${uuid()}'.substring(0, 19); - + publish: (nameSpace, publishName) async { // set space name - try { - final nameSpace = - await ViewBackendService.getPublishNameSpace().getOrThrow(); - if (nameSpace.namespace != prefix) { - await ViewBackendService.setPublishNameSpace(prefix).getOrThrow(); - } + final getNameSpaceResult = + await ViewBackendService.getPublishNameSpace(); + final name = await getNameSpaceResult.fold((s) async { + Log.error('get publish namespace success: ${s.namespace}'); + return s.namespace; + }, (f) async { + Log.error('get publish namespace error: $f'); + await ViewBackendService.setPublishNameSpace(nameSpace) + .getOrThrow(); + return nameSpace; + }); - final result = await ViewBackendService.publish(view, name: name); + await ViewBackendService.publish( + view, + name: publishName, + ).getOrThrow(); emit( state.copyWith( isPublished: true, - publishResult: result, - url: '$_url/${nameSpace.namespace}/$name', + publishResult: FlowySuccess(null), + url: '$_url/$name/$publishName', ), ); } catch (e) { @@ -173,7 +176,10 @@ class DocumentShareEvent with _$DocumentShareEvent { DocumentShareType type, String? path, ) = _Share; - const factory DocumentShareEvent.publish(String url) = _Publish; + const factory DocumentShareEvent.publish( + String nameSpace, + String pageId, + ) = _Publish; const factory DocumentShareEvent.unPublish() = _UnPublish; } diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart index 96e5b3e6a0..45cf2642aa 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart @@ -4,11 +4,14 @@ import 'package:appflowy/plugins/document/application/document_share_bloc.dart'; import 'package:appflowy/workspace/presentation/home/toast.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:easy_localization/easy_localization.dart'; +import 'package:flowy_infra/uuid.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flowy_infra_ui/widget/rounded_button.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +final _regExp = RegExp(r'[^\w\-\.@:/]'); + class PublishTab extends StatelessWidget { const PublishTab({super.key}); @@ -36,13 +39,31 @@ class PublishTab extends StatelessWidget { }, ) : _UnPublishWidget( - onPublish: () => context - .read() - .add(const DocumentShareEvent.publish('')), + onPublish: () async { + final publishName = await _generatePublishName(context); + final nameSpace = await _generateNameSpace(); + if (context.mounted) { + context.read().add( + DocumentShareEvent.publish(nameSpace, publishName), + ); + } + }, ); }, ); } + + Future _generateNameSpace() async { + const workspaceName = ''; + final id = uuid().substring(0, 8); + return '$workspaceName$id'.replaceAll(_regExp, '_'); + } + + Future _generatePublishName(BuildContext context) async { + final publishName = context.read().view.name; + final id = uuid().substring(0, 8); + return '$publishName$id'.replaceAll(_regExp, ''); + } } class _PublishedWidget extends StatefulWidget {