Skip to content

Commit

Permalink
TF-2316 Fix cannot delete folder with hidden subfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
hieutbui committed Feb 3, 2024
1 parent 5ad800a commit 8bb7350
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import 'dart:collection';
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/mail/mailbox/mailbox.dart';
import 'package:model/extensions/list_presentation_mailbox_extension.dart';
import 'package:model/extensions/mailbox_extension.dart';
import 'package:model/mailbox/presentation_mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/domain/repository/mailbox_repository.dart';
import 'package:tmail_ui_user/features/mailbox/domain/state/delete_multiple_mailbox_state.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/list_mailbox_node_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_node.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_tree.dart';

class DeleteMultipleMailboxInteractor {
final MailboxRepository _mailboxRepository;
Expand All @@ -24,6 +31,70 @@ class DeleteMultipleMailboxInteractor {

final currentMailboxState = await _mailboxRepository.getMailboxState(session, accountId);

final mailboxResponses = await _mailboxRepository.getAllMailbox(session, accountId).toList();

final listUnsubscribedMailbox = mailboxResponses.expand((mailboxResponse) {
final presentationMailboxes = mailboxResponse.mailboxes
?.map((mailbox) => mailbox.toPresentationMailbox()).toList()
?? List<PresentationMailbox>.empty();
return presentationMailboxes.listUnsubscribedMailboxes;
}).toSet();

if (listUnsubscribedMailbox.isNotEmpty) {
Map<MailboxId, MailboxNode> mailboxDictionary = HashMap();
final unsubscribedTree = MailboxTree(MailboxNode.root());

for (var mailbox in listUnsubscribedMailbox) {
if (mailbox.parentId != null) {
mailboxDictionary[mailbox.id] = MailboxNode(mailbox);
}
}

for (var mailbox in listUnsubscribedMailbox) {
final parentId = mailbox.parentId;
if (parentId != null) {
final parentNode = mailboxDictionary[parentId];
final node = mailboxDictionary[mailbox.id];
if (node != null) {
if (parentNode != null) {
parentNode.addChildNode(node);
} else {
mailboxDictionary[parentId] = MailboxNode(PresentationMailbox(parentId));
unsubscribedTree.root.addChildNode(mailboxDictionary[parentId]!);
mailboxDictionary[parentId]!.addChildNode(node);
}
}
}
}

List<MailboxId> visitedUnsubscribedMailboxIds = [];

for (var mailboxId in listMailboxIdToDelete) {
if (visitedUnsubscribedMailboxIds.contains(mailboxId)) {
continue;
} else {
final matchedNode = unsubscribedTree.findNode((node) => node.item.id == mailboxId);

if (matchedNode != null) {
final descendantIds = matchedNode.descendantsAsList().mailboxIds;
final descendantIdsReversed = descendantIds.reversed.toList();
descendantIdsReversed.removeLast();

if (mapMailboxIdToDelete.containsKey(mailboxId)) {
mapMailboxIdToDelete[mailboxId]!.insertAll(mapMailboxIdToDelete[mailboxId]!.length - 1, descendantIdsReversed);
} else {
for (var listIdToDelete in mapMailboxIdToDelete.values) {
if (listIdToDelete.contains(mailboxId)) {
listIdToDelete.insertAll(listIdToDelete.indexOf(mailboxId), descendantIdsReversed);
}
}
}
visitedUnsubscribedMailboxIds.addAll(descendantIdsReversed);
}
}
}
}

final listResult = await Future.wait(
mapMailboxIdToDelete.keys.map((mailboxId) {
final mailboxIdsToDelete = mapMailboxIdToDelete[mailboxId]!;
Expand Down
3 changes: 3 additions & 0 deletions model/lib/extensions/list_presentation_mailbox_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ extension ListPresentationMailboxExtension on List<PresentationMailbox> {

List<PresentationMailbox> get listSubscribedMailboxesAndDefaultMailboxes =>
where((mailbox) => mailbox.isSubscribedMailbox || mailbox.isDefault).toList();

List<PresentationMailbox> get listUnsubscribedMailboxes =>
where((mailbox) => !mailbox.isSubscribedMailbox).toList();

List<PresentationMailbox> get listPersonalMailboxes =>
where((mailbox) => mailbox.isPersonal).toList();
Expand Down

0 comments on commit 8bb7350

Please sign in to comment.