From fcdaef80a27f90851c22c0f37a3f9cf2be98f699 Mon Sep 17 00:00:00 2001 From: DatDang Date: Mon, 12 Aug 2024 10:24:24 +0700 Subject: [PATCH] TF-3043 Dereference public assets before delete identity --- .../identities/identities_controller.dart | 52 +++++++++++++++++-- ...ove_identity_from_public_assets_state.dart | 24 +++++++-- ..._identity_to_public_assets_interactor.dart | 2 +- ...dentity_from_public_assets_interactor.dart | 8 +-- ...lean_up_public_assets_interactor_test.dart | 14 ++--- 5 files changed, 82 insertions(+), 18 deletions(-) diff --git a/lib/features/manage_account/presentation/profiles/identities/identities_controller.dart b/lib/features/manage_account/presentation/profiles/identities/identities_controller.dart index a68f3bde8..155362525 100644 --- a/lib/features/manage_account/presentation/profiles/identities/identities_controller.dart +++ b/lib/features/manage_account/presentation/profiles/identities/identities_controller.dart @@ -5,6 +5,7 @@ import 'package:core/presentation/state/success.dart'; import 'package:core/presentation/views/dialog/confirmation_dialog_builder.dart'; import 'package:core/utils/app_logger.dart'; import 'package:core/utils/platform_info.dart'; +import 'package:dartz/dartz.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:get/get.dart'; @@ -33,8 +34,11 @@ import 'package:tmail_ui_user/features/manage_account/presentation/extensions/id import 'package:tmail_ui_user/features/manage_account/presentation/manage_account_dashboard_controller.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/model/identity_action_type.dart'; import 'package:tmail_ui_user/features/manage_account/presentation/profiles/identities/widgets/delete_identity_dialog_builder.dart'; +import 'package:tmail_ui_user/features/public_asset/domain/extensions/string_to_public_asset_extension.dart'; import 'package:tmail_ui_user/features/public_asset/domain/model/public_assets_in_identity_arguments.dart'; +import 'package:tmail_ui_user/features/public_asset/domain/state/remove_identity_from_public_assets_state.dart'; import 'package:tmail_ui_user/features/public_asset/domain/usecase/clean_up_public_assets_interactor.dart'; +import 'package:tmail_ui_user/features/public_asset/domain/usecase/remove_identity_from_public_assets_interactor.dart'; import 'package:tmail_ui_user/features/public_asset/presentation/clean_up_public_assets_interactor_bindings.dart'; import 'package:tmail_ui_user/main/bindings/network/binding_tag.dart'; import 'package:tmail_ui_user/main/error/capability_validator.dart'; @@ -90,6 +94,8 @@ class IdentitiesController extends BaseController { _editIdentitySuccess(success); } else if (success is TransformHtmlSignatureSuccess) { signatureSelected.value = success.signature; + } else if (success is RemoveIdentityFromPublicAssetsSuccessState) { + _deleteIdentityAction(success.identityId); } } @@ -98,6 +104,10 @@ class IdentitiesController extends BaseController { super.handleFailureViewState(failure); if (failure is DeleteIdentityFailure) { _deleteIdentityFailure(failure); + } else if (failure is RemoveIdentityFromPublicAssetsFailureState) { + _deleteIdentityAction(failure.identityId); + } else if (failure is NotFoundAnyPublicAssetsFailureState) { + _deleteIdentityAction(failure.identityId); } } @@ -213,19 +223,53 @@ class IdentitiesController extends BaseController { DeleteIdentityDialogBuilder( responsiveUtils: responsiveUtils, imagePaths: imagePaths, - onDeleteIdentityAction: () => _deleteIdentityAction(identity), + onDeleteIdentityAction: () => _dereferencePublicAssets(identity), ), barrierColor: AppColor.colorDefaultCupertinoActionSheet, ); } - void _deleteIdentityAction(Identity identity) { + void _deleteIdentityAction(IdentityId identityId) { + final session = accountDashBoardController.sessionCurrent; + final accountId = accountDashBoardController.accountId.value; + if (accountId != null && session != null) { + consumeState(_deleteIdentityInteractor.execute(session, accountId, identityId)); + } else { + consumeState(Stream.value(Left(DeleteIdentityFailure(null)))); + } + } + + Future _dereferencePublicAssets( + Identity identity + ) async { popBack(); final session = accountDashBoardController.sessionCurrent; final accountId = accountDashBoardController.accountId.value; - if (accountId != null && session != null && identity.id != null) { - consumeState(_deleteIdentityInteractor.execute(session, accountId, identity.id!)); + final identityId = identity.id; + + final publicAssetIds = identity + .signatureAsString + .publicAssetIdsFromHtmlContent; + final removeIdentityFromPublicAssetsInteractor = getBinding( + tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag); + + // if there is no identityId, even the delete action will fail + if (identityId == null) return; + + if (session == null + || accountId == null + || removeIdentityFromPublicAssetsInteractor == null + || publicAssetIds.isEmpty) + { + consumeState(Stream.value(Left(RemoveIdentityFromPublicAssetsFailureState(identityId: identityId)))); + } else { + consumeState(removeIdentityFromPublicAssetsInteractor.execute( + session, + accountId, + identityId: identityId, + publicAssetIds: publicAssetIds + )); } } diff --git a/lib/features/public_asset/domain/state/remove_identity_from_public_assets_state.dart b/lib/features/public_asset/domain/state/remove_identity_from_public_assets_state.dart index c55d9001b..8c6497654 100644 --- a/lib/features/public_asset/domain/state/remove_identity_from_public_assets_state.dart +++ b/lib/features/public_asset/domain/state/remove_identity_from_public_assets_state.dart @@ -1,14 +1,32 @@ import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/success.dart'; +import 'package:jmap_dart_client/jmap/identities/identity.dart'; class RemovingIdentityFromPublicAssetsState extends LoadingState {} -class RemoveIdentityFromPublicAssetsSuccessState extends UIState {} +class RemoveIdentityFromPublicAssetsSuccessState extends UIState { + RemoveIdentityFromPublicAssetsSuccessState({required this.identityId}); + + final IdentityId identityId; + + @override + List get props => [identityId]; +} class RemoveIdentityFromPublicAssetsFailureState extends FeatureFailure { - RemoveIdentityFromPublicAssetsFailureState({super.exception}); + RemoveIdentityFromPublicAssetsFailureState({super.exception, required this.identityId}); + + final IdentityId identityId; + + @override + List get props => [...super.props, identityId]; } class NotFoundAnyPublicAssetsFailureState extends FeatureFailure { - NotFoundAnyPublicAssetsFailureState({super.exception}); + NotFoundAnyPublicAssetsFailureState({super.exception, required this.identityId}); + + final IdentityId identityId; + + @override + List get props => [...super.props, identityId]; } \ No newline at end of file diff --git a/lib/features/public_asset/domain/usecase/add_identity_to_public_assets_interactor.dart b/lib/features/public_asset/domain/usecase/add_identity_to_public_assets_interactor.dart index e3b052183..00c38dfb5 100644 --- a/lib/features/public_asset/domain/usecase/add_identity_to_public_assets_interactor.dart +++ b/lib/features/public_asset/domain/usecase/add_identity_to_public_assets_interactor.dart @@ -31,7 +31,7 @@ class AddIdentityToPublicAssetsInteractor { publicAssetIds: publicAssetIds ); if (publicAssets.isEmpty) { - yield Left(NotFoundAnyPublicAssetsFailureState()); + yield Left(NotFoundAnyPublicAssetsFailureState(identityId: identityId)); } else { final publicAssetsWithCurrentIdentity = publicAssets .map((publicAsset) => publicAsset.withAddedIdentityId(identityId)) diff --git a/lib/features/public_asset/domain/usecase/remove_identity_from_public_assets_interactor.dart b/lib/features/public_asset/domain/usecase/remove_identity_from_public_assets_interactor.dart index 99a095d73..09ac3cbba 100644 --- a/lib/features/public_asset/domain/usecase/remove_identity_from_public_assets_interactor.dart +++ b/lib/features/public_asset/domain/usecase/remove_identity_from_public_assets_interactor.dart @@ -30,7 +30,7 @@ class RemoveIdentityFromPublicAssetsInteractor { publicAssetIds: publicAssetIds ); if (publicAssets.isEmpty) { - yield Left(NotFoundAnyPublicAssetsFailureState()); + yield Left(NotFoundAnyPublicAssetsFailureState(identityId: identityId)); } else { final publicAssetsWithCurrentIdentity = publicAssets .map((publicAsset) => publicAsset.withRemovedIdentityId(identityId)) @@ -40,10 +40,12 @@ class RemoveIdentityFromPublicAssetsInteractor { accountId, publicAssets: publicAssetsWithCurrentIdentity ); - yield Right(RemoveIdentityFromPublicAssetsSuccessState()); + yield Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId)); } } catch (exception) { - yield Left(RemoveIdentityFromPublicAssetsFailureState(exception: exception)); + yield Left(RemoveIdentityFromPublicAssetsFailureState( + exception: exception, + identityId: identityId)); } } } \ No newline at end of file diff --git a/test/features/public_asset/domain/usecase/clean_up_public_assets_interactor_test.dart b/test/features/public_asset/domain/usecase/clean_up_public_assets_interactor_test.dart index 4c2e5721e..afbe22579 100644 --- a/test/features/public_asset/domain/usecase/clean_up_public_assets_interactor_test.dart +++ b/test/features/public_asset/domain/usecase/clean_up_public_assets_interactor_test.dart @@ -60,7 +60,7 @@ void main() { .thenAnswer((_) => Stream.value(Right(DeletePublicAssetsSuccessState()))); when(removeIdentityFromPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) - .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState()))); + .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId)))); when(addIdentityToPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) .thenAnswer((_) => Stream.value(Right(AddIdentityToPublicAssetsSuccessState()))); @@ -117,7 +117,7 @@ void main() { .thenAnswer((_) => Stream.value(Right(DeletePublicAssetsSuccessState()))); when(removeIdentityFromPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) - .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState()))); + .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId)))); when(addIdentityToPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) .thenAnswer((_) => Stream.value(Right(AddIdentityToPublicAssetsSuccessState()))); @@ -173,7 +173,7 @@ void main() { .thenAnswer((_) => Stream.value(Right(DeletePublicAssetsSuccessState()))); when(removeIdentityFromPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) - .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState()))); + .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId)))); when(addIdentityToPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) .thenAnswer((_) => Stream.value(Right(AddIdentityToPublicAssetsSuccessState()))); @@ -229,7 +229,7 @@ void main() { .thenAnswer((_) => Stream.value(Right(DeletePublicAssetsSuccessState()))); when(removeIdentityFromPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) - .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState()))); + .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId)))); when(addIdentityToPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) .thenAnswer((_) => Stream.value(Right(AddIdentityToPublicAssetsSuccessState()))); @@ -286,7 +286,7 @@ void main() { .thenAnswer((_) => Stream.value(Right(DeletePublicAssetsSuccessState()))); when(removeIdentityFromPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) - .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState()))); + .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId)))); when(addIdentityToPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) .thenAnswer((_) => Stream.value(Right(AddIdentityToPublicAssetsSuccessState()))); @@ -354,7 +354,7 @@ void main() { .thenAnswer((_) => Stream.value(Right(DeletePublicAssetsSuccessState()))); when(removeIdentityFromPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) - .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState()))); + .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId)))); when(addIdentityToPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) .thenAnswer((_) => Stream.value(Right(AddIdentityToPublicAssetsSuccessState()))); @@ -422,7 +422,7 @@ void main() { .thenAnswer((_) => Stream.value(Right(DeletePublicAssetsSuccessState()))); when(removeIdentityFromPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) - .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState()))); + .thenAnswer((_) => Stream.value(Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId)))); when(addIdentityToPublicAssetsInteractor .execute(any, any, identityId: anyNamed('identityId'), publicAssetIds: anyNamed('publicAssetIds'))) .thenAnswer((_) => Stream.value(Right(AddIdentityToPublicAssetsSuccessState())));