TF-3083 PublicAsset Implement partial update
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/identities/identity.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/public_asset.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/repository/public_asset_repository.dart';
|
||||
|
||||
abstract class PublicAssetDatasource {
|
||||
const PublicAssetDatasource();
|
||||
@@ -33,4 +34,10 @@ abstract class PublicAssetDatasource {
|
||||
AccountId accountId,
|
||||
{required List<PublicAsset> publicAssets}
|
||||
);
|
||||
|
||||
Future<void> partialUpdatePublicAssets(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
{required Map<Id, UpdatingIdentityIds> mapPublicAssetIdToUpdatingIdentityIds}
|
||||
);
|
||||
}
|
||||
+14
@@ -5,6 +5,7 @@ import 'package:jmap_dart_client/jmap/identities/identity.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/public_asset.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/data/datasource/public_asset_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/data/network/public_asset_api.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/repository/public_asset_repository.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
|
||||
|
||||
class RemotePublicAssetDatasourceImpl implements PublicAssetDatasource {
|
||||
@@ -67,4 +68,17 @@ class RemotePublicAssetDatasourceImpl implements PublicAssetDatasource {
|
||||
publicAssets: publicAssets
|
||||
);
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
|
||||
@override
|
||||
Future<void> partialUpdatePublicAssets(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
{required Map<Id, UpdatingIdentityIds> mapPublicAssetIdToUpdatingIdentityIds}
|
||||
) => Future.sync(() async {
|
||||
return await _publicAssetApi.partialUpdatePublicAssets(
|
||||
session,
|
||||
accountId,
|
||||
mapPublicAssetIdToUpdatingIdentityIds: mapPublicAssetIdToUpdatingIdentityIds
|
||||
);
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/public_asset.
|
||||
import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/set/set_public_asset_method.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/set/set_public_asset_response.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/exceptions/public_asset_exceptions.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/domain/repository/public_asset_repository.dart';
|
||||
import 'package:tmail_ui_user/features/public_asset/presentation/public_asset_controller.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class PublicAssetApi {
|
||||
@@ -29,6 +31,20 @@ class PublicAssetApi {
|
||||
return MapEntry(publicAsset.id!, patchObject);
|
||||
}
|
||||
|
||||
MapEntry<PublicAssetId, PatchObject> _toPartialPatchObjectMapEntry(
|
||||
PublicAssetId publicAssetId,
|
||||
UpdatingIdentityIds updatingIdentityIds,
|
||||
) {
|
||||
assert(
|
||||
updatingIdentityIds.values.every((value) => value != false),
|
||||
'All updating identity id values must be true or null'
|
||||
);
|
||||
final patchObject = PatchObject(updatingIdentityIds.map(
|
||||
(key, value) => MapEntry('${PatchObject.identityIdsProperty}/${key.id.value}', value)));
|
||||
|
||||
return MapEntry(publicAssetId, patchObject);
|
||||
}
|
||||
|
||||
Future<List<PublicAsset>> getPublicAssets(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
@@ -134,4 +150,29 @@ class PublicAssetApi {
|
||||
throw const CannotUpdatePublicAssetException();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> partialUpdatePublicAssets(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
{required Map<PublicAssetId, UpdatingIdentityIds> mapPublicAssetIdToUpdatingIdentityIds}
|
||||
) async {
|
||||
final requestBuilder = JmapRequestBuilder(_httpClient, ProcessingInvocation());
|
||||
final method = SetPublicAssetMethod(accountId);
|
||||
method.addUpdates(
|
||||
mapPublicAssetIdToUpdatingIdentityIds.map(_toPartialPatchObjectMapEntry)
|
||||
);
|
||||
final invocation = requestBuilder.invocation(method);
|
||||
final response = await (requestBuilder..usings(method.requiredCapabilities))
|
||||
.build()
|
||||
.execute();
|
||||
|
||||
final notUpdatedPublicAssetIds = response.parse<SetPublicAssetResponse>(
|
||||
invocation.methodCallId,
|
||||
SetPublicAssetResponse.deserialize
|
||||
)?.notUpdated;
|
||||
|
||||
if (notUpdatedPublicAssetIds?.isNotEmpty == true) {
|
||||
throw const CannotUpdatePublicAssetException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,4 +54,14 @@ class PublicAssetRepositoryImpl implements PublicAssetRepository {
|
||||
session,
|
||||
accountId,
|
||||
publicAssets: publicAssets);
|
||||
|
||||
@override
|
||||
Future<void> partialUpdatePublicAssets(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
{required Map<Id, UpdatingIdentityIds> mapPublicAssetIdToUpdatingIdentityIds}
|
||||
) => _publicAssetDatasource.partialUpdatePublicAssets(
|
||||
session,
|
||||
accountId,
|
||||
mapPublicAssetIdToUpdatingIdentityIds: mapPublicAssetIdToUpdatingIdentityIds);
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/identities/identity.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/public_asset.dart';
|
||||
|
||||
typedef UpdatingIdentityIds = Map<IdentityId, bool?>;
|
||||
|
||||
abstract class PublicAssetRepository {
|
||||
Future<List<PublicAsset>> getPublicAssetsFromIds(
|
||||
Session session,
|
||||
@@ -31,4 +33,10 @@ abstract class PublicAssetRepository {
|
||||
AccountId accountId,
|
||||
{required List<PublicAsset> publicAssets}
|
||||
);
|
||||
|
||||
Future<void> partialUpdatePublicAssets(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
{required Map<Id, UpdatingIdentityIds> mapPublicAssetIdToUpdatingIdentityIds}
|
||||
);
|
||||
}
|
||||
+5
-17
@@ -4,10 +4,8 @@ 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 {
|
||||
@@ -25,24 +23,14 @@ class AddIdentityToPublicAssetsInteractor {
|
||||
) async* {
|
||||
try {
|
||||
yield Right(AddingIdentityToPublicAssetsState());
|
||||
final publicAssets = await _publicAssetRepository.getPublicAssetsFromIds(
|
||||
await _publicAssetRepository.partialUpdatePublicAssets(
|
||||
session,
|
||||
accountId,
|
||||
publicAssetIds: publicAssetIds
|
||||
mapPublicAssetIdToUpdatingIdentityIds: Map.fromEntries(publicAssetIds.map(
|
||||
(publicAssetId) => MapEntry(publicAssetId, {identityId: true})
|
||||
))
|
||||
);
|
||||
if (publicAssets.isEmpty) {
|
||||
yield Left(NotFoundAnyPublicAssetsFailureState(identityId: identityId));
|
||||
} else {
|
||||
final publicAssetsWithCurrentIdentity = publicAssets
|
||||
.map((publicAsset) => publicAsset.withAddedIdentityId(identityId))
|
||||
.toList();
|
||||
await _publicAssetRepository.updatePublicAssets(
|
||||
session,
|
||||
accountId,
|
||||
publicAssets: publicAssetsWithCurrentIdentity
|
||||
);
|
||||
yield Right(AddIdentityToPublicAssetsSuccessState());
|
||||
}
|
||||
yield Right(AddIdentityToPublicAssetsSuccessState());
|
||||
} catch (exception) {
|
||||
yield Left(AddIdentityToPublicAssetsFailureState(exception: exception));
|
||||
}
|
||||
|
||||
+5
-16
@@ -4,7 +4,6 @@ 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';
|
||||
@@ -24,24 +23,14 @@ class RemoveIdentityFromPublicAssetsInteractor {
|
||||
) async* {
|
||||
try {
|
||||
yield Right(RemovingIdentityFromPublicAssetsState());
|
||||
final publicAssets = await _publicAssetRepository.getPublicAssetsFromIds(
|
||||
await _publicAssetRepository.partialUpdatePublicAssets(
|
||||
session,
|
||||
accountId,
|
||||
publicAssetIds: publicAssetIds
|
||||
mapPublicAssetIdToUpdatingIdentityIds: Map.fromEntries(publicAssetIds.map(
|
||||
(publicAssetId) => MapEntry(publicAssetId, {identityId: null}),
|
||||
))
|
||||
);
|
||||
if (publicAssets.isEmpty) {
|
||||
yield Left(NotFoundAnyPublicAssetsFailureState(identityId: identityId));
|
||||
} else {
|
||||
final publicAssetsWithCurrentIdentity = publicAssets
|
||||
.map((publicAsset) => publicAsset.withRemovedIdentityId(identityId))
|
||||
.toList();
|
||||
await _publicAssetRepository.updatePublicAssets(
|
||||
session,
|
||||
accountId,
|
||||
publicAssets: publicAssetsWithCurrentIdentity
|
||||
);
|
||||
yield Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId));
|
||||
}
|
||||
yield Right(RemoveIdentityFromPublicAssetsSuccessState(identityId: identityId));
|
||||
} catch (exception) {
|
||||
yield Left(RemoveIdentityFromPublicAssetsFailureState(
|
||||
exception: exception,
|
||||
|
||||
Reference in New Issue
Block a user