From 0daabd2f3ca03ce8f8c4a567663cd7f192c1dda3 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 2 Jul 2024 17:09:19 +0800 Subject: [PATCH] feat: use default publish name --- .../application/document_share_bloc.dart | 24 +++++++---------- .../presentation/share/publish_bloc.dart | 0 .../share/publish_name_generator.dart | 27 +++++++++---------- .../presentation/share/publish_tab.dart | 3 +-- 4 files changed, 24 insertions(+), 30 deletions(-) delete mode 100644 frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_bloc.dart 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 cbc1a23dab..85fd02ec5a 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 @@ -68,17 +68,8 @@ class DocumentShareBloc extends Bloc { publish: (nameSpace, publishName) async { // set space name try { - 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.getPublishNameSpace().getOrThrow(); await ViewBackendService.publish( view, @@ -89,7 +80,7 @@ class DocumentShareBloc extends Bloc { state.copyWith( isPublished: true, publishResult: FlowySuccess(null), - url: '$_url/$name/$publishName', + url: '$_url/${result.namespace}/$publishName', ), ); } catch (e) { @@ -107,10 +98,15 @@ class DocumentShareBloc extends Bloc { } }, unPublish: () async { - await ViewBackendService.unpublish(view); + final result = await ViewBackendService.unpublish(view); + final isPublished = !result.isSuccess; + result.onFailure((f) { + Log.error('unpublish error: $f'); + }); + emit( state.copyWith( - isPublished: false, + isPublished: isPublished, publishResult: null, url: '', ), diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_bloc.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_bloc.dart deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_name_generator.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_name_generator.dart index e22205627d..cb44271ee9 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_name_generator.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_name_generator.dart @@ -1,20 +1,19 @@ -import 'dart:math'; - -import 'package:flowy_infra/uuid.dart'; - -final _regExp = RegExp(r'[^\w\-\.@:/]'); - -Future generateNameSpace() async { - const workspaceName = ''; - final id = uuid().substring(0, 8); - return '$workspaceName$id'.replaceAll(_regExp, '-'); +String replaceInvalidChars(String input) { + final RegExp invalidCharsRegex = RegExp('[^a-zA-Z0-9-]'); + return input.replaceAll(invalidCharsRegex, ''); } -// The backend limits the publish name to a maximum of 50 characters. -// If the combined length of the ID and the name exceeds 50 characters, +Future generateNameSpace() async { + return ''; +} + +// The backend limits the publish name to a maximum of 120 characters. +// If the combined length of the ID and the name exceeds 120 characters, // we will truncate the name to ensure the final result is within the limit. // The name should only contain alphanumeric characters and hyphens. Future generatePublishName(String id, String name) async { - final result = '${name.substring(0, min(49 - id.length, name.length))}-$id'; - return result.replaceAll(_regExp, '-'); + if (name.length >= 120 - id.length) { + name = name.substring(0, 120 - id.length); + } + return replaceInvalidChars('$name-$id'); } 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 e007675262..ca52ae733c 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 @@ -45,10 +45,9 @@ class PublishTab extends StatelessWidget { id, state.viewName, ); - final nameSpace = await generateNameSpace(); if (context.mounted) { context.read().add( - DocumentShareEvent.publish(nameSpace, publishName), + DocumentShareEvent.publish('', publishName), ); } },