TF-3042 Clean up public assets after identity is saved
This commit is contained in:
@@ -128,7 +128,7 @@ class PublicAssetApi {
|
||||
final notUpdatedPublicAssetIds = response.parse<SetPublicAssetResponse>(
|
||||
invocation.methodCallId,
|
||||
SetPublicAssetResponse.deserialize
|
||||
)?.updated;
|
||||
)?.notUpdated;
|
||||
|
||||
if (notUpdatedPublicAssetIds?.isNotEmpty == true) {
|
||||
throw const CannotUpdatePublicAssetException();
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:jmap_dart_client/jmap/identities/identity.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/public_asset.dart';
|
||||
|
||||
extension PublicAssetExtension on PublicAsset {
|
||||
PublicAsset withRemovedIdentityId(IdentityId toBeRemovedIdentityId) {
|
||||
return PublicAsset(
|
||||
id: id,
|
||||
publicURI: publicURI,
|
||||
size: size,
|
||||
contentType: contentType,
|
||||
blobId: blobId,
|
||||
identityIds: identityIds?..remove(toBeRemovedIdentityId),
|
||||
);
|
||||
}
|
||||
|
||||
PublicAsset withAddedIdentityId(IdentityId toBeAddedIdentityId) {
|
||||
return PublicAsset(
|
||||
id: id,
|
||||
publicURI: publicURI,
|
||||
size: size,
|
||||
contentType: contentType,
|
||||
blobId: blobId,
|
||||
identityIds: identityIds?..addAll({toBeAddedIdentityId: true}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/presentation/public_asset_controller.dart';
|
||||
|
||||
class PublicAssetsInIdentityArguments with EquatableMixin {
|
||||
final String htmlSignature;
|
||||
final List<PublicAssetId> preExistingPublicAssetIds;
|
||||
final List<PublicAssetId> newlyPickedPublicAssetIds;
|
||||
|
||||
PublicAssetsInIdentityArguments({
|
||||
required this.htmlSignature,
|
||||
required this.preExistingPublicAssetIds,
|
||||
required this.newlyPickedPublicAssetIds,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
htmlSignature,
|
||||
preExistingPublicAssetIds,
|
||||
newlyPickedPublicAssetIds,
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
|
||||
class AddingIdentityToPublicAssetsState extends LoadingState {}
|
||||
|
||||
class AddIdentityToPublicAssetsSuccessState extends UIState {}
|
||||
|
||||
class AddIdentityToPublicAssetsFailureState extends FeatureFailure {
|
||||
AddIdentityToPublicAssetsFailureState({super.exception});
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
|
||||
class CleaningUpPublicAssetsState extends LoadingState {}
|
||||
|
||||
class CleanUpPublicAssetsSuccessState extends UIState {}
|
||||
|
||||
class CleanUpPublicAssetsFailureState extends FeatureFailure {
|
||||
CleanUpPublicAssetsFailureState({super.exception});
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
|
||||
class RemovingIdentityFromPublicAssetsState extends LoadingState {}
|
||||
|
||||
class RemoveIdentityFromPublicAssetsSuccessState extends UIState {}
|
||||
|
||||
class RemoveIdentityFromPublicAssetsFailureState extends FeatureFailure {
|
||||
RemoveIdentityFromPublicAssetsFailureState({super.exception});
|
||||
}
|
||||
|
||||
class NotFoundAnyPublicAssetsFailureState extends FeatureFailure {
|
||||
NotFoundAnyPublicAssetsFailureState({super.exception});
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/identities/identity.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/extensions/public_asset_extension.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/repository/public_asset_repository.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/state/add_identity_to_public_assets_state.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/presentation/public_asset_controller.dart';
|
||||
|
||||
class AddIdentityToPublicAssetsInteractor {
|
||||
AddIdentityToPublicAssetsInteractor(this._publicAssetRepository);
|
||||
|
||||
final PublicAssetRepository _publicAssetRepository;
|
||||
|
||||
Stream<Either<Failure, Success>> execute(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
{
|
||||
required IdentityId identityId,
|
||||
required List<PublicAssetId> publicAssetIds
|
||||
}
|
||||
) async* {
|
||||
try {
|
||||
yield Right(AddingIdentityToPublicAssetsState());
|
||||
final publicAssets = await _publicAssetRepository.getPublicAssetsFromIds(
|
||||
session,
|
||||
accountId,
|
||||
publicAssetIds: publicAssetIds
|
||||
);
|
||||
if (publicAssets.isEmpty) {
|
||||
yield Left(NotFoundAnyPublicAssetsFailureState());
|
||||
} else {
|
||||
final publicAssetsWithCurrentIdentity = publicAssets
|
||||
.map((publicAsset) => publicAsset.withAddedIdentityId(identityId))
|
||||
.toList();
|
||||
await _publicAssetRepository.updatePublicAssets(
|
||||
session,
|
||||
accountId,
|
||||
publicAssets: publicAssetsWithCurrentIdentity
|
||||
);
|
||||
yield Right(AddIdentityToPublicAssetsSuccessState());
|
||||
}
|
||||
} catch (exception) {
|
||||
yield Left(AddIdentityToPublicAssetsFailureState(exception: exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/identities/identity.dart';
|
||||
import 'package:model/extensions/list_extension.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/model/identity_action_type.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/clean_up_public_assets_state.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/usecase/add_identity_to_public_assets_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/usecase/delete_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/public_asset_controller.dart';
|
||||
|
||||
class CleanUpPublicAssetsInteractor {
|
||||
CleanUpPublicAssetsInteractor(
|
||||
this.removeIdentityFromPublicAssetsInteractor,
|
||||
this.deletePublicAssetsInteractor,
|
||||
this.addIdentityToPublicAssetsInteractor);
|
||||
|
||||
final RemoveIdentityFromPublicAssetsInteractor removeIdentityFromPublicAssetsInteractor;
|
||||
final DeletePublicAssetsInteractor deletePublicAssetsInteractor;
|
||||
final AddIdentityToPublicAssetsInteractor addIdentityToPublicAssetsInteractor;
|
||||
|
||||
Stream<Either<Failure, Success>> execute(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
{
|
||||
required IdentityId identityId,
|
||||
required IdentityActionType identityActionType,
|
||||
required PublicAssetsInIdentityArguments publicAssetsInIdentityArguments,
|
||||
}
|
||||
) async* {
|
||||
try {
|
||||
yield Right(CleaningUpPublicAssetsState());
|
||||
|
||||
final publicAssetIds = _filterPublicAssetsIdsForCleanUp(
|
||||
identityId,
|
||||
identityActionType,
|
||||
publicAssetsInIdentityArguments);
|
||||
|
||||
await Rx.merge<Either<Failure, Success>>([
|
||||
if (publicAssetIds.publicAssetsIdsToBeDereferenced.isNotEmpty)
|
||||
removeIdentityFromPublicAssetsInteractor.execute(
|
||||
session,
|
||||
accountId,
|
||||
identityId: identityId,
|
||||
publicAssetIds: publicAssetIds.publicAssetsIdsToBeDereferenced),
|
||||
if (publicAssetIds.publicAssetsIdsToBeDestroyed.isNotEmpty)
|
||||
deletePublicAssetsInteractor.execute(
|
||||
session,
|
||||
accountId,
|
||||
publicAssetIds: publicAssetIds.publicAssetsIdsToBeDestroyed),
|
||||
if (publicAssetIds.publicAssetsIdsToBeReferenced.isNotEmpty)
|
||||
addIdentityToPublicAssetsInteractor.execute(
|
||||
session,
|
||||
accountId,
|
||||
identityId: identityId,
|
||||
publicAssetIds: publicAssetIds.publicAssetsIdsToBeReferenced),
|
||||
]).last;
|
||||
|
||||
yield Right(CleanUpPublicAssetsSuccessState());
|
||||
|
||||
} catch (exception) {
|
||||
logError('CleanUpPublicAssetsInteractor::execute():error: $exception');
|
||||
yield Left(CleanUpPublicAssetsFailureState(exception: exception));
|
||||
}
|
||||
}
|
||||
|
||||
({
|
||||
List<PublicAssetId> publicAssetsIdsToBeDestroyed,
|
||||
List<PublicAssetId> publicAssetsIdsToBeReferenced,
|
||||
List<PublicAssetId> publicAssetsIdsToBeDereferenced,
|
||||
}) _filterPublicAssetsIdsForCleanUp(
|
||||
IdentityId identityId,
|
||||
IdentityActionType identityActionType,
|
||||
PublicAssetsInIdentityArguments publicAssetsInIdentityArguments,
|
||||
) {
|
||||
final htmlSignature = publicAssetsInIdentityArguments.htmlSignature;
|
||||
final publicAssetIdsInSignature = htmlSignature.publicAssetIdsFromHtmlContent;
|
||||
log('CleanUpPublicAssetsInteractor::publicAssetIdsInSignature: $publicAssetIdsInSignature');
|
||||
final preExistingPublicAssetIds = publicAssetsInIdentityArguments.preExistingPublicAssetIds;
|
||||
log('CleanUpPublicAssetsInteractor::preExistingPublicAssetIds: $preExistingPublicAssetIds');
|
||||
final newlyPickedPublicAssetIds = publicAssetsInIdentityArguments.newlyPickedPublicAssetIds;
|
||||
log('CleanUpPublicAssetsInteractor::newlyPickedPublicAssetIds: $newlyPickedPublicAssetIds');
|
||||
|
||||
final deletedPreExistingPublicAssetIds = List<PublicAssetId>.from(preExistingPublicAssetIds);
|
||||
final notIncludedNewlyPickedPublicAssetIds = List<PublicAssetId>.from(newlyPickedPublicAssetIds);
|
||||
final pastedPublicAssetIds = <PublicAssetId>[];
|
||||
final newPublicAssetsIdsInSignature = <PublicAssetId>[];
|
||||
|
||||
for (var id in publicAssetIdsInSignature) {
|
||||
deletedPreExistingPublicAssetIds.remove(id);
|
||||
notIncludedNewlyPickedPublicAssetIds.remove(id);
|
||||
|
||||
if (preExistingPublicAssetIds.notContains(id)
|
||||
&& newlyPickedPublicAssetIds.notContains(id)
|
||||
) {
|
||||
pastedPublicAssetIds.add(id);
|
||||
}
|
||||
|
||||
if (identityActionType == IdentityActionType.create
|
||||
&& newlyPickedPublicAssetIds.contains(id)
|
||||
) {
|
||||
newPublicAssetsIdsInSignature.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
log('CleanUpPublicAssetsInteractor::deletedpreExistingPublicAssetIds: $deletedPreExistingPublicAssetIds');
|
||||
log('CleanUpPublicAssetsInteractor::notIncludednewlyPickedPublicAssetIds: $notIncludedNewlyPickedPublicAssetIds');
|
||||
log('CleanUpPublicAssetsInteractor::pastedPublicAssetIds: $pastedPublicAssetIds');
|
||||
log('CleanUpPublicAssetsInteractor::newPublicAssetsIdsInSignature: $newPublicAssetsIdsInSignature');
|
||||
|
||||
return (
|
||||
publicAssetsIdsToBeDestroyed: notIncludedNewlyPickedPublicAssetIds,
|
||||
publicAssetsIdsToBeReferenced: pastedPublicAssetIds + newPublicAssetsIdsInSignature,
|
||||
publicAssetsIdsToBeDereferenced: deletedPreExistingPublicAssetIds,
|
||||
);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/identities/identity.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/extensions/public_asset_extension.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/repository/public_asset_repository.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/presentation/public_asset_controller.dart';
|
||||
|
||||
class RemoveIdentityFromPublicAssetsInteractor {
|
||||
RemoveIdentityFromPublicAssetsInteractor(this._publicAssetRepository);
|
||||
|
||||
final PublicAssetRepository _publicAssetRepository;
|
||||
|
||||
Stream<Either<Failure, Success>> execute(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
{
|
||||
required IdentityId identityId,
|
||||
required List<PublicAssetId> publicAssetIds
|
||||
}
|
||||
) async* {
|
||||
try {
|
||||
yield Right(RemovingIdentityFromPublicAssetsState());
|
||||
final publicAssets = await _publicAssetRepository.getPublicAssetsFromIds(
|
||||
session,
|
||||
accountId,
|
||||
publicAssetIds: publicAssetIds
|
||||
);
|
||||
if (publicAssets.isEmpty) {
|
||||
yield Left(NotFoundAnyPublicAssetsFailureState());
|
||||
} else {
|
||||
final publicAssetsWithCurrentIdentity = publicAssets
|
||||
.map((publicAsset) => publicAsset.withRemovedIdentityId(identityId))
|
||||
.toList();
|
||||
await _publicAssetRepository.updatePublicAssets(
|
||||
session,
|
||||
accountId,
|
||||
publicAssets: publicAssetsWithCurrentIdentity
|
||||
);
|
||||
yield Right(RemoveIdentityFromPublicAssetsSuccessState());
|
||||
}
|
||||
} catch (exception) {
|
||||
yield Left(RemoveIdentityFromPublicAssetsFailureState(exception: exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:jmap_dart_client/http/http_client.dart';
|
||||
import 'package:tmail_ui_user/features/base/interactors_bindings.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/data/datasource/public_asset_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/data/datasource_impl/remote_public_asset_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/data/network/public_asset_api.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/data/repository/public_asset_repository_impl.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/repository/public_asset_repository.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/usecase/add_identity_to_public_assets_interactor.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/delete_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/main/bindings/network/binding_tag.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class CleanUpPublicAssetsInteractorBindings extends InteractorsBindings {
|
||||
@override
|
||||
void dependencies() {
|
||||
Get.lazyPut(
|
||||
() => PublicAssetApi(Get.find<HttpClient>(), Get.find<Uuid>()),
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
|
||||
super.dependencies();
|
||||
}
|
||||
|
||||
@override
|
||||
void bindingsDataSource() {
|
||||
Get.lazyPut<PublicAssetDatasource>(
|
||||
() => Get.find<RemotePublicAssetDatasourceImpl>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag),
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
}
|
||||
|
||||
@override
|
||||
void bindingsDataSourceImpl() {
|
||||
Get.lazyPut(
|
||||
() => RemotePublicAssetDatasourceImpl(
|
||||
Get.find<PublicAssetApi>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag),
|
||||
Get.find<RemoteExceptionThrower>()),
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
}
|
||||
|
||||
@override
|
||||
void bindingsInteractor() {
|
||||
Get.lazyPut(
|
||||
() => CleanUpPublicAssetsInteractor(
|
||||
Get.find<RemoveIdentityFromPublicAssetsInteractor>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag),
|
||||
Get.find<DeletePublicAssetsInteractor>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag),
|
||||
Get.find<AddIdentityToPublicAssetsInteractor>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag)),
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.lazyPut(
|
||||
() => RemoveIdentityFromPublicAssetsInteractor(Get.find<PublicAssetRepository>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag)),
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.lazyPut(
|
||||
() => DeletePublicAssetsInteractor(Get.find<PublicAssetRepository>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag)),
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.lazyPut(
|
||||
() => AddIdentityToPublicAssetsInteractor(Get.find<PublicAssetRepository>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag)),
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
}
|
||||
|
||||
@override
|
||||
void bindingsRepository() {
|
||||
Get.lazyPut<PublicAssetRepository>(
|
||||
() => Get.find<PublicAssetRepositoryImpl>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag),
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
}
|
||||
|
||||
@override
|
||||
void bindingsRepositoryImpl() {
|
||||
Get.lazyPut(
|
||||
() => PublicAssetRepositoryImpl(Get.find<PublicAssetDatasource>(
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag)),
|
||||
tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
}
|
||||
|
||||
void close() {
|
||||
Get.delete<CleanUpPublicAssetsInteractor>(tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.delete<RemoveIdentityFromPublicAssetsInteractor>(tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.delete<DeletePublicAssetsInteractor>(tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.delete<AddIdentityToPublicAssetsInteractor>(tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.delete<PublicAssetRepository>(tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.delete<PublicAssetRepositoryImpl>(tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.delete<PublicAssetDatasource>(tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.delete<RemotePublicAssetDatasourceImpl>(tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
Get.delete<PublicAssetApi>(tag: BindingTag.cleanUpPublicAssetsInteractorBindingsTag);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user