feat: use default publish name

This commit is contained in:
Lucas.Xu 2024-07-02 17:09:19 +08:00
parent be4d60ca0d
commit 0daabd2f3c
4 changed files with 24 additions and 30 deletions

View File

@ -68,17 +68,8 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
publish: (nameSpace, publishName) async { publish: (nameSpace, publishName) async {
// set space name // set space name
try { try {
final getNameSpaceResult = final result =
await ViewBackendService.getPublishNameSpace(); await ViewBackendService.getPublishNameSpace().getOrThrow();
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;
});
await ViewBackendService.publish( await ViewBackendService.publish(
view, view,
@ -89,7 +80,7 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
state.copyWith( state.copyWith(
isPublished: true, isPublished: true,
publishResult: FlowySuccess(null), publishResult: FlowySuccess(null),
url: '$_url/$name/$publishName', url: '$_url/${result.namespace}/$publishName',
), ),
); );
} catch (e) { } catch (e) {
@ -107,10 +98,15 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
} }
}, },
unPublish: () async { 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( emit(
state.copyWith( state.copyWith(
isPublished: false, isPublished: isPublished,
publishResult: null, publishResult: null,
url: '', url: '',
), ),

View File

@ -1,20 +1,19 @@
import 'dart:math'; String replaceInvalidChars(String input) {
final RegExp invalidCharsRegex = RegExp('[^a-zA-Z0-9-]');
import 'package:flowy_infra/uuid.dart'; return input.replaceAll(invalidCharsRegex, '');
final _regExp = RegExp(r'[^\w\-\.@:/]');
Future<String> generateNameSpace() async {
const workspaceName = '';
final id = uuid().substring(0, 8);
return '$workspaceName$id'.replaceAll(_regExp, '-');
} }
// The backend limits the publish name to a maximum of 50 characters. Future<String> generateNameSpace() async {
// If the combined length of the ID and the name exceeds 50 characters, 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. // we will truncate the name to ensure the final result is within the limit.
// The name should only contain alphanumeric characters and hyphens. // The name should only contain alphanumeric characters and hyphens.
Future<String> generatePublishName(String id, String name) async { Future<String> generatePublishName(String id, String name) async {
final result = '${name.substring(0, min(49 - id.length, name.length))}-$id'; if (name.length >= 120 - id.length) {
return result.replaceAll(_regExp, '-'); name = name.substring(0, 120 - id.length);
}
return replaceInvalidChars('$name-$id');
} }

View File

@ -45,10 +45,9 @@ class PublishTab extends StatelessWidget {
id, id,
state.viewName, state.viewName,
); );
final nameSpace = await generateNameSpace();
if (context.mounted) { if (context.mounted) {
context.read<DocumentShareBloc>().add( context.read<DocumentShareBloc>().add(
DocumentShareEvent.publish(nameSpace, publishName), DocumentShareEvent.publish('', publishName),
); );
} }
}, },