Skip to content

Commit

Permalink
TF-1718 Implement unsubscribeMail on Jmap server in offline mode
Browse files Browse the repository at this point in the history
Signed-off-by: dab246 <[email protected]>
  • Loading branch information
dab246 committed Nov 16, 2023
1 parent 631ea3a commit 38b418c
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/features/caching/config/cache_version.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

class CacheVersion {
static const int hiveDBVersion = 7;
static const int hiveDBVersion = 8;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ extension EmailRequestExtension on EmailRequest {
identityId: identityId,
mailboxNameRequest: mailboxRequest?.newName,
creationIdRequest: mailboxRequest?.creationId,
sendingState: newState
sendingState: newState,
previousEmailId: previousEmailId
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ extension SendingEmailHiveCacheExtension on SendingEmailHiveCache {
identityId: const IdentityIdNullableConverter().fromJson(identityId),
mailboxNameRequest: const MailboxNameConverter().fromJson(mailboxNameRequest),
creationIdRequest: const IdNullableConverter().fromJson(creationIdRequest),
sendingState: SendingState.values.firstWhere((value) => value.name == sendingState)
sendingState: SendingState.values.firstWhere((value) => value.name == sendingState),
previousEmailId: const EmailIdNullableConverter().fromJson(previousEmailId),
);
}
}
5 changes: 5 additions & 0 deletions lib/features/offline_mode/model/sending_email_hive_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class SendingEmailHiveCache extends HiveObject with EquatableMixin {
@HiveField(10)
final String sendingState;

@HiveField(11)
final String? previousEmailId;

SendingEmailHiveCache(
this.sendingId,
this.email,
Expand All @@ -53,6 +56,7 @@ class SendingEmailHiveCache extends HiveObject with EquatableMixin {
this.mailboxNameRequest,
this.creationIdRequest,
this.sendingState,
this.previousEmailId,
);

@override
Expand All @@ -68,5 +72,6 @@ class SendingEmailHiveCache extends HiveObject with EquatableMixin {
mailboxNameRequest,
creationIdRequest,
sendingState,
previousEmailId,
];
}
34 changes: 33 additions & 1 deletion lib/features/offline_mode/work_manager/sending_email_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import 'package:core/utils/app_logger.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/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:model/email/email_action_type.dart';
import 'package:model/extensions/account_id_extensions.dart';
import 'package:model/extensions/session_extension.dart';
import 'package:model/oidc/token_oidc.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart';
import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
import 'package:tmail_ui_user/features/composer/domain/state/send_email_state.dart';
import 'package:tmail_ui_user/features/composer/domain/usecases/send_email_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/state/unsubscribe_email_state.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/unsubscribe_email_interactor.dart';
import 'package:tmail_ui_user/features/login/data/network/config/authorization_interceptors.dart';
import 'package:tmail_ui_user/features/login/domain/state/get_authenticated_account_state.dart';
import 'package:tmail_ui_user/features/login/domain/state/get_credential_state.dart';
Expand Down Expand Up @@ -43,6 +47,7 @@ class SendingEmailWorker extends Worker {
GetSessionInteractor? _getSessionInteractor;
SendingQueueIsolateManager? _sendingQueueIsolateManager;
SendingEmailCacheManager? _sendingEmailCacheManager;
UnsubscribeEmailInteractor? _unsubscribeEmailInteractor;

late Completer<bool> _completer;
late SendingEmail _sendingEmail;
Expand Down Expand Up @@ -97,6 +102,8 @@ class SendingEmailWorker extends Worker {
failure is GetStoredTokenOidcFailure ||
failure is GetCredentialFailure) {
_handleWorkerTaskToRetry();
} else if (failure is UnsubscribeEmailFailure) {
_handleWorkerTaskSuccess();
}
}

Expand All @@ -111,6 +118,8 @@ class SendingEmailWorker extends Worker {
_handleGetAccountByBasicAuthSuccess(success);
} else if (success is SendEmailSuccess) {
_handleSendEmailSuccess(success);
} else if (success is UnsubscribeEmailSuccess) {
_handleWorkerTaskSuccess();
}
}

Expand All @@ -128,6 +137,7 @@ class SendingEmailWorker extends Worker {
_sendEmailInteractor = getBinding<SendEmailInteractor>();
_sendingQueueIsolateManager = getBinding<SendingQueueIsolateManager>();
_sendingEmailCacheManager = getBinding<SendingEmailCacheManager>();
_unsubscribeEmailInteractor = getBinding<UnsubscribeEmailInteractor>();
}

void _updatingSendingStateToMainUI({String? sendingId, SendingState? sendingState}) {
Expand Down Expand Up @@ -204,7 +214,12 @@ class SendingEmailWorker extends Worker {
}

void _handleSendEmailSuccess(SendEmailSuccess success) {
_handleWorkerTaskSuccess();
if (success.emailRequest.emailActionType == EmailActionType.composeFromUnsubscribeMailtoLink &&
success.emailRequest.previousEmailId != null) {
_unsubscribeMail(success.emailRequest.previousEmailId!);
} else {
_handleWorkerTaskSuccess();
}
}

void _handleSendEmailFailure(SendEmailFailure failure) {
Expand Down Expand Up @@ -246,4 +261,21 @@ class SendingEmailWorker extends Worker {
UserName? userName
}
) => TupleKey(sendingId, sendingState.name, accountId?.asString, userName?.value).toString();

void _unsubscribeMail(EmailId emailId) {
if (_unsubscribeEmailInteractor != null &&
_currentSession != null &&
_currentAccountId != null
) {
consumeState(
_unsubscribeEmailInteractor!.execute(
_currentSession!,
_currentAccountId!,
emailId
)
);
} else {
_handleWorkerTaskSuccess();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extension SendingEmailExtension on SendingEmail {
mailboxNameRequest?.name,
creationIdRequest?.value,
sendingState.name,
previousEmailId?.asString,
);
}

Expand All @@ -34,7 +35,8 @@ extension SendingEmailExtension on SendingEmail {
emailIdDestroyed: emailIdDestroyed,
emailIdAnsweredOrForwarded: emailIdAnsweredOrForwarded,
identityId: identityId,
storedSendingId: sendingId
storedSendingId: sendingId,
previousEmailId: previousEmailId,
);
}

Expand All @@ -51,7 +53,8 @@ extension SendingEmailExtension on SendingEmail {
mailboxNameRequest: mailboxNameRequest,
creationIdRequest: creationIdRequest,
sendingState: sendingState,
selectMode: selectMode == SelectMode.INACTIVE ? SelectMode.ACTIVE : SelectMode.INACTIVE
selectMode: selectMode == SelectMode.INACTIVE ? SelectMode.ACTIVE : SelectMode.INACTIVE,
previousEmailId: previousEmailId,
);
}

Expand All @@ -68,7 +71,8 @@ extension SendingEmailExtension on SendingEmail {
mailboxNameRequest: mailboxNameRequest,
creationIdRequest: creationIdRequest,
sendingState: sendingState,
selectMode: SelectMode.INACTIVE
selectMode: SelectMode.INACTIVE,
previousEmailId: previousEmailId,
);
}

Expand All @@ -85,7 +89,8 @@ extension SendingEmailExtension on SendingEmail {
mailboxNameRequest: mailboxNameRequest,
creationIdRequest: creationIdRequest,
sendingState: newState,
selectMode: selectMode
selectMode: selectMode,
previousEmailId: previousEmailId,
);
}
}
7 changes: 6 additions & 1 deletion lib/features/sending_queue/domain/model/sending_email.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SendingEmail with EquatableMixin {
final DateTime createTime;
final SelectMode selectMode;
final SendingState sendingState;
final EmailId? previousEmailId;

SendingEmail({
required this.sendingId,
Expand All @@ -44,7 +45,8 @@ class SendingEmail with EquatableMixin {
this.mailboxNameRequest,
this.creationIdRequest,
this.selectMode = SelectMode.INACTIVE,
this.sendingState = SendingState.waiting
this.sendingState = SendingState.waiting,
this.previousEmailId,
});

Map<String, dynamic> toJson() {
Expand All @@ -66,6 +68,7 @@ class SendingEmail with EquatableMixin {
writeNotNull('identityId', const IdentityIdNullableConverter().toJson(identityId));
writeNotNull('mailboxNameRequest', mailboxNameRequest?.name);
writeNotNull('creationIdRequest', const IdNullableConverter().toJson(creationIdRequest));
writeNotNull('previousEmailId', const EmailIdNullableConverter().toJson(previousEmailId));

return val;
}
Expand All @@ -88,6 +91,7 @@ class SendingEmail with EquatableMixin {
identityId: const IdentityIdNullableConverter().fromJson(json['identityId'] as String?),
mailboxNameRequest: const MailboxNameConverter().fromJson(json['mailboxNameRequest'] as String?),
creationIdRequest: const IdNullableConverter().fromJson(json['creationIdRequest'] as String?),
previousEmailId: const EmailIdNullableConverter().fromJson(json['previousEmailId'] as String?),
);
}

Expand Down Expand Up @@ -134,5 +138,6 @@ class SendingEmail with EquatableMixin {
creationIdRequest,
selectMode,
sendingState,
previousEmailId,
];
}

0 comments on commit 38b418c

Please sign in to comment.