DeleteContactAction.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "DeleteContactAction.h"
  21. #import "EntityManager.h"
  22. #import "UserSettings.h"
  23. #import "AppDelegate.h"
  24. #import "NotificationManager.h"
  25. #import "ContactStore.h"
  26. @interface DeleteContactAction ()
  27. @property EntityManager *entityManager;
  28. @property Contact *contact;
  29. @property UIAlertController *alertController;
  30. @property (copy) void (^onCompletion)(BOOL didCancel);
  31. @property NSString *idToExclude;
  32. @end
  33. @implementation DeleteContactAction
  34. + (instancetype)deleteActionForContact:(Contact *)contact {
  35. DeleteContactAction *action = [[DeleteContactAction alloc] init];
  36. action.contact = contact;
  37. return action;
  38. }
  39. - (void)executeOnCompletion:(void (^)(BOOL didCancel))onCompletion {
  40. _onCompletion = onCompletion;
  41. _entityManager = [[EntityManager alloc] init];
  42. /* Check that there are no more group conversations where this contact is a member. */
  43. NSArray *groups = [_entityManager.entityFetcher groupConversationsForContact:_contact];
  44. if ([groups count] > 0) {
  45. [UIAlertTemplate showAlertWithOwner:[[AppDelegate sharedAppDelegate] currentTopViewController] title:NSLocalizedString(@"delete_contact_group_exists_title", nil) message:NSLocalizedString(@"delete_contact_group_exists_message", nil) actionOk:^(UIAlertAction * _Nonnull okAction) {
  46. _onCompletion(NO);
  47. }];
  48. return;
  49. }
  50. /* any conversations for this contact? If so, prompt before delete */
  51. if (_contact.conversations.count > 0) {
  52. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"delete_contact_warn_title", nil) message:NSLocalizedString(@"delete_contact_warn_message", nil) preferredStyle:UIAlertControllerStyleAlert];
  53. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  54. [self deleteContactInDB];
  55. }]];
  56. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
  57. if (_onCompletion) {
  58. _onCompletion(NO);
  59. }
  60. }]];
  61. [[[AppDelegate sharedAppDelegate] currentTopViewController] presentViewController:alertController animated:YES completion:nil];
  62. } else {
  63. [self deleteContactInDB];
  64. }
  65. }
  66. - (void)deleteContactInDB {
  67. __block BOOL contactWasLinked = NO;
  68. [_entityManager performSyncBlockAndSafe:^{
  69. _idToExclude = _contact.identity;
  70. if (_contact.cnContactId != nil) {
  71. contactWasLinked = YES;
  72. }
  73. [[_entityManager entityDestroyer] deleteObjectWithObject:_contact];
  74. }];
  75. [[NotificationManager sharedInstance] updateUnreadMessagesCount:NO];
  76. /* remove from blacklist, if present */
  77. if ([[UserSettings sharedUserSettings].blacklist containsObject:_idToExclude]) {
  78. NSMutableOrderedSet *blacklist = [NSMutableOrderedSet orderedSetWithOrderedSet:[UserSettings sharedUserSettings].blacklist];
  79. [blacklist removeObject:_idToExclude];
  80. [UserSettings sharedUserSettings].blacklist = blacklist;
  81. }
  82. /* remove from profile picture receiver list */
  83. if ([[UserSettings sharedUserSettings].profilePictureContactList containsObject:_idToExclude]) {
  84. NSMutableSet *profilePictureContactList = [NSMutableSet setWithArray:[UserSettings sharedUserSettings].profilePictureContactList];
  85. [profilePictureContactList removeObject:_idToExclude];
  86. [UserSettings sharedUserSettings].profilePictureContactList = profilePictureContactList.allObjects;
  87. }
  88. /* remove from profile picture request list */
  89. [[ContactStore sharedContactStore] removeProfilePictureRequest:_idToExclude];
  90. NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
  91. _contact, kKeyContact,
  92. nil];
  93. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationDeletedContact object:nil userInfo:info];
  94. if (contactWasLinked && ![[UserSettings sharedUserSettings].syncExclusionList containsObject:_idToExclude]) {
  95. /* ask the user if he wants to add this contact to the exclusion list */
  96. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"exclude_deleted_id_title", nil) message:NSLocalizedString(@"exclude_deleted_id_message", nil) preferredStyle:UIAlertControllerStyleAlert];
  97. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"no", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  98. }]];
  99. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"yes", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  100. NSMutableArray *exclusionList = [NSMutableArray arrayWithArray:[UserSettings sharedUserSettings].syncExclusionList];
  101. [exclusionList addObject:_idToExclude];
  102. [exclusionList sortUsingSelector:@selector(caseInsensitiveCompare:)];
  103. [UserSettings sharedUserSettings].syncExclusionList = exclusionList;
  104. }]];
  105. [[[AppDelegate sharedAppDelegate] currentTopViewController] presentViewController:alertController animated:YES completion:nil];
  106. }
  107. if (_onCompletion) {
  108. _onCompletion(YES);
  109. }
  110. }
  111. @end