TF-3707 Implement clear mailbox method in data and domain layer

Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
dab246
2025-05-06 15:51:59 +07:00
committed by Dat H. Pham
parent 5f6e61bde7
commit e415077738
10 changed files with 190 additions and 0 deletions
@@ -9,6 +9,7 @@ import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
@@ -59,4 +60,6 @@ abstract class MailboxDataSource {
Future<GetMailboxByRoleResponse> getMailboxByRole(Session session, AccountId accountId, Role role);
Future<void> clearAllMailboxCache(AccountId accountId, UserName userName);
Future<UnsignedInt> clearMailbox(Session session, AccountId accountId, MailboxId mailboxId);
}
@@ -161,4 +161,9 @@ class MailboxCacheDataSourceImpl extends MailboxDataSource {
return await _mailboxCacheManager.clearAll(accountId, userName);
}).catchError(_exceptionThrower.throwException);
}
@override
Future<UnsignedInt> clearMailbox(Session session, AccountId accountId, MailboxId mailboxId) {
throw UnimplementedError();
}
}
@@ -154,4 +154,11 @@ class MailboxDataSourceImpl extends MailboxDataSource {
Future<void> clearAllMailboxCache(AccountId accountId, UserName userName) {
throw UnimplementedError();
}
@override
Future<UnsignedInt> clearMailbox(Session session, AccountId accountId, MailboxId mailboxId) {
return Future.sync(() async {
return await mailboxAPI.clearMailbox(session, accountId, mailboxId);
}).catchError(_exceptionThrower.throwException);
}
}
@@ -20,6 +20,8 @@ import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
import 'package:jmap_dart_client/jmap/jmap_request.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/changes/changes_mailbox_method.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/changes/changes_mailbox_response.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/clear/clear_mailbox_method.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/clear/clear_mailbox_response.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_method.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_response.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
@@ -607,4 +609,32 @@ class MailboxAPI with HandleSetErrorMixin {
throw NotFoundMailboxException();
}
}
Future<UnsignedInt> clearMailbox(
Session session,
AccountId accountId,
MailboxId mailboxId,
) async {
final clearMailboxMethod = ClearMailboxMethod(accountId, mailboxId);
final requestBuilder = JmapRequestBuilder(httpClient, ProcessingInvocation());
final invocation = requestBuilder.invocation(clearMailboxMethod);
final response = await (requestBuilder
..usings(clearMailboxMethod.requiredCapabilities))
.build()
.execute();
final clearMailboxResponse = response.parse<ClearMailboxResponse>(
invocation.methodCallId,
ClearMailboxResponse.deserialize,
);
if (clearMailboxResponse?.totalDeletedMessagesCount != null) {
return clearMailboxResponse!.totalDeletedMessagesCount!;
} else if (clearMailboxResponse?.notCleared != null) {
throw clearMailboxResponse!.notCleared!;
} else {
throw NotFoundClearMailboxResponseException();
}
}
}
@@ -300,4 +300,9 @@ class MailboxRepositoryImpl extends MailboxRepository {
Future<GetMailboxByRoleResponse> getMailboxByRole(Session session, AccountId accountId, Role role, {UnsignedInt? limit}) {
return mapDataSource[DataSourceType.network]!.getMailboxByRole(session, accountId, role);
}
@override
Future<UnsignedInt> clearMailbox(Session session, AccountId accountId, MailboxId mailboxId) {
return mapDataSource[DataSourceType.network]!.clearMailbox(session, accountId, mailboxId);
}
}
@@ -2,3 +2,5 @@
class NotFoundInboxMailboxException implements Exception {}
class NotFoundMailboxException implements Exception {}
class NotFoundClearMailboxResponseException implements Exception {}
@@ -54,4 +54,6 @@ abstract class MailboxRepository {
Future<(List<Mailbox> mailboxes, Map<Id, SetError> mapErrors)> setRoleDefaultMailbox(Session session, AccountId accountId, List<Mailbox> listMailbox);
Future<GetMailboxByRoleResponse> getMailboxByRole(Session session, AccountId accountId, Role role, {UnsignedInt? limit});
Future<UnsignedInt> clearMailbox(Session session, AccountId accountId, MailboxId mailboxId);
}
@@ -0,0 +1,19 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
class ClearingMailbox extends LoadingState {}
class ClearMailboxSuccess extends UIState {
final UnsignedInt totalDeletedMessages;
ClearMailboxSuccess(this.totalDeletedMessages);
@override
List<Object> get props => [totalDeletedMessages];
}
class ClearMailboxFailure extends FeatureFailure {
ClearMailboxFailure(dynamic exception) : super(exception: exception);
}
@@ -0,0 +1,32 @@
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/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/domain/repository/mailbox_repository.dart';
import 'package:tmail_ui_user/features/mailbox/domain/state/clear_mailbox_state.dart';
class ClearMailboxInteractor {
final MailboxRepository _mailboxRepository;
ClearMailboxInteractor(this._mailboxRepository);
Stream<Either<Failure, Success>> execute(
Session session,
AccountId accountId,
MailboxId mailboxId,
) async* {
try {
yield Right<Failure, Success>(ClearingMailbox());
final totalDeletedMessages = await _mailboxRepository.clearMailbox(
session,
accountId,
mailboxId,
);
yield Right<Failure, Success>(ClearMailboxSuccess(totalDeletedMessages));
} catch (e) {
yield Left<Failure, Success>(ClearMailboxFailure(e));
}
}
}
@@ -1,5 +1,6 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:jmap_dart_client/http/http_client.dart';
import 'package:jmap_dart_client/jmap/core/error/set_error.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:mockito/annotations.dart';
@@ -389,5 +390,89 @@ void main() {
);
});
});
group('clearMailbox method test', () {
final trashMailboxId = MailboxId(Id('trash-id'));
final notFoundMailboxId = MailboxId(Id('notFoundMailboxId'));
test('Should return totalDeletedMessagesCount when clear mailbox success', () async {
final mapResponseData = {
"methodResponses": [
[
"Mailbox/clear",
{
"accountId": accountId.asString,
"totalDeletedMessagesCount": 10,
},
"c0"
]
],
"sessionState": "0"
};
when(httpClient.post(
'',
data: anyNamed('data'),
cancelToken: anyNamed('cancelToken'),
)).thenAnswer((_) async => mapResponseData);
final result = await mailboxAPI.clearMailbox(
session,
accountId,
trashMailboxId,
);
verify(httpClient.post(
'',
data: anyNamed('data'),
cancelToken: anyNamed('cancelToken'),
));
expect(result.value, 10);
});
test('Should return notCleared when clear mailbox fail with mailbox id not found', () async {
final mapResponseData = {
"methodResponses": [
[
"Mailbox/clear",
{
"accountId": accountId.asString,
"notCleared": {
"type": "notFound",
"description": "${notFoundMailboxId.id.value} can not be found"
}
},
"c0"
]
],
"sessionState": "0"
};
when(httpClient.post(
'',
data: anyNamed('data'),
cancelToken: anyNamed('cancelToken'),
)).thenAnswer((_) async => mapResponseData);
expect(
() => mailboxAPI.clearMailbox(session, accountId, notFoundMailboxId),
throwsA(isA<SetError>()),
);
});
test('Should throw exception when http client throw exception', () async {
when(httpClient.post(
'',
data: anyNamed('data'),
cancelToken: anyNamed('cancelToken'),
)).thenThrow(Exception());
expect(
() => mailboxAPI.clearMailbox(session, accountId, trashMailboxId),
throwsA(isA<Exception>()),
);
});
});
});
}