FeatureMask.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "FeatureMask.h"
  21. #import "MyIdentityStore.h"
  22. #import "ServerAPIConnector.h"
  23. #import "Contact.h"
  24. #import "Conversation.h"
  25. #import "ContactStore.h"
  26. #import "UIDefines.h"
  27. #import "AppGroup.h"
  28. #ifdef DEBUG
  29. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  30. #else
  31. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  32. #endif
  33. #define kTimeTillNextFeatureMaskSet -60*60*24
  34. @implementation FeatureMask
  35. + (void)updateFeatureMask {
  36. MyIdentityStore *myIdentityStore = [MyIdentityStore sharedMyIdentityStore];
  37. NSUserDefaults *defaults = [AppGroup userDefaults];
  38. NSDate *lastFeatureMaskSet = [defaults objectForKey:@"LastFeatureMaskSet"];
  39. NSDate *lastFeatureMaskDate = [NSDate dateWithTimeIntervalSinceNow:kTimeTillNextFeatureMaskSet];
  40. int currentFeatureMask = is64Bit == 1 ? kCurrentFeatureMask : kWithoutVoIPFeatureMask;
  41. if ((!lastFeatureMaskSet || [lastFeatureMaskSet laterDate:lastFeatureMaskDate] == lastFeatureMaskDate) || (myIdentityStore == nil || !myIdentityStore.lastSentFeatureMask || myIdentityStore.lastSentFeatureMask == 0 || myIdentityStore.lastSentFeatureMask != currentFeatureMask)) {
  42. DDLogVerbose(@"Set feature mask %d on server", kCurrentFeatureMask);
  43. ServerAPIConnector *connector = [[ServerAPIConnector alloc] init];
  44. [connector setFeatureMask:[NSNumber numberWithInt:currentFeatureMask] forStore:myIdentityStore onCompletion:^{
  45. myIdentityStore.lastSentFeatureMask = currentFeatureMask;
  46. [defaults setObject:[NSDate date] forKey:@"LastFeatureMaskSet"];
  47. [defaults synchronize];
  48. } onError:^(NSError *error) {
  49. DDLogError(@"Set feature level failed: %@", error);
  50. myIdentityStore.lastSentFeatureMask = 0;
  51. }];
  52. } else {
  53. DDLogVerbose(@"Feature level is up-to-date");
  54. }
  55. }
  56. + (NSSet *)filterContactsWithUnsupportedFeatureMask:(NSInteger)featureMask fromContacts:(NSSet *)inputContacts {
  57. NSMutableSet *unsupportedContacts = [NSMutableSet set];
  58. for (Contact *contact in inputContacts) {
  59. //make sure the object is in sync
  60. [contact.managedObjectContext refreshObject:contact mergeChanges:YES];
  61. if (!(featureMask & [contact.featureMask intValue])) {
  62. [unsupportedContacts addObject: contact];
  63. }
  64. }
  65. return unsupportedContacts;
  66. }
  67. + (void)checkFeatureMask:(NSInteger)featureMask forConversations:(NSSet *)conversations onCompletion:(void (^)(NSArray *unsupportedContacts))onCompletion {
  68. NSMutableSet *contacts = [NSMutableSet set];
  69. for (Conversation *conversation in conversations) {
  70. if ([conversation isGroup]) {
  71. for (Contact *contact in conversation.participants) {
  72. [contacts addObject:contact];
  73. }
  74. } else {
  75. [contacts addObject:conversation.contact];
  76. }
  77. }
  78. [self checkFeatureMask:featureMask forContacts:contacts onCompletion:onCompletion];
  79. }
  80. + (void)checkFeatureMask:(NSInteger)featureMask forContacts:(NSSet *)contacts onCompletion:(void (^)(NSArray *unsupportedContacts))onCompletion {
  81. NSSet *unsupportedContacts = [FeatureMask filterContactsWithUnsupportedFeatureMask:featureMask fromContacts:contacts];
  82. NSInteger count = [unsupportedContacts count];
  83. if (count == 0) {
  84. onCompletion(unsupportedContacts.allObjects);
  85. return;
  86. }
  87. ContactStore *contactStore = [ContactStore sharedContactStore];
  88. [contactStore updateFeatureMasksForContacts:unsupportedContacts.allObjects onCompletion:^{
  89. // reread feature mask
  90. NSSet *unsupportedContacts2Run = [FeatureMask filterContactsWithUnsupportedFeatureMask:featureMask fromContacts:unsupportedContacts];
  91. onCompletion(unsupportedContacts2Run.allObjects);
  92. return;
  93. } onError:^(NSError *error) {
  94. // always run onCompletion
  95. onCompletion(unsupportedContacts.allObjects);
  96. }];
  97. }
  98. + (void)updateFeatureMaskForAllContacts:(NSArray *)allContacts onCompletion:(void(^)(void))onCompletion {
  99. ContactStore *contactStore = [ContactStore sharedContactStore];
  100. [contactStore updateFeatureMasksForContacts:allContacts onCompletion:^{
  101. onCompletion();
  102. } onError:^(NSError *error) {
  103. onCompletion();
  104. }];
  105. }
  106. @end