PushSetting.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-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 "PushSetting.h"
  21. #import "Conversation.h"
  22. #import "NSString+Hex.h"
  23. #import "Contact.h"
  24. #import "UserSettings.h"
  25. #import "TextStyleUtils.h"
  26. @implementation PushSetting
  27. + (PushSetting *)findPushSettingForConversation:(Conversation *)conversation {
  28. NSDictionary *dict = nil;
  29. if (conversation.isGroup) {
  30. dict = [PushSetting findPushSettingDictForIdentity:[NSString stringWithHexData:conversation.groupId]];
  31. } else {
  32. dict = [PushSetting findPushSettingDictForIdentity:conversation.contact.identity];
  33. }
  34. if (dict != nil) {
  35. return [[PushSetting alloc] initWithDictionary:dict];
  36. }
  37. return nil;
  38. }
  39. + (NSDictionary *)findPushSettingDictForConversation:(Conversation *)conversation {
  40. if (conversation.isGroup) {
  41. return [PushSetting findPushSettingDictForIdentity:[NSString stringWithHexData:conversation.groupId]];
  42. } else {
  43. return [PushSetting findPushSettingDictForIdentity:conversation.contact.identity];
  44. }
  45. }
  46. + (NSDictionary *)findPushSettingDictForIdentity:(NSString *)identity {
  47. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"identity == %@", identity];
  48. NSOrderedSet *settingsArray = [[[UserSettings sharedUserSettings] pushSettingsList] filteredOrderedSetUsingPredicate:predicate];
  49. if (settingsArray.count > 0) {
  50. return settingsArray.firstObject;
  51. }
  52. return nil;
  53. }
  54. + (PushSetting *)findPushSettingForIdentity:(NSString *)identity {
  55. NSDictionary *dict = [PushSetting findPushSettingDictForIdentity:identity];
  56. if (dict != nil) {
  57. return [[PushSetting alloc] initWithDictionary:dict];
  58. }
  59. return nil;
  60. }
  61. + (PushSetting *)findPushSettingForIdentity:(NSString *)identity pushSettingList:(NSOrderedSet *)pushSettingList {
  62. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"identity == %@", identity];
  63. NSOrderedSet *settingsArray = [pushSettingList filteredOrderedSetUsingPredicate:predicate];
  64. if (settingsArray.count > 0) {
  65. return [[PushSetting alloc] initWithDictionary:settingsArray.firstObject];
  66. }
  67. return nil;
  68. }
  69. + (PushSetting *)findPushSettingForGroupId:(NSData *)groupId {
  70. NSDictionary *dict = [PushSetting findPushSettingDictForIdentity:[NSString stringWithHexData:groupId]];
  71. if (dict != nil) {
  72. return [[PushSetting alloc] initWithDictionary:dict];
  73. }
  74. return nil;
  75. }
  76. - (id)initWithDictionary:(NSDictionary *)dict {
  77. self=[super init];
  78. if (self) {
  79. self.identity = dict[@"identity"];
  80. self.type = [dict[@"type"] integerValue];
  81. self.periodOffTime = [dict[@"periodOffTime"] integerValue];
  82. self.periodOffTillDate = dict[@"periodOffTillDate"];
  83. self.silent = [dict[@"silent"] boolValue];
  84. self.mentions = [dict[@"mentions"] boolValue];
  85. }
  86. return self;
  87. }
  88. - (NSDictionary *)buildDict {
  89. NSMutableDictionary *dict = [NSMutableDictionary new];
  90. if (self.identity != nil){
  91. [dict setObject:self.identity forKey:@"identity"];
  92. }
  93. [dict setObject:[NSNumber numberWithInteger:self.type] forKey:@"type"];
  94. if (self.periodOffTime != 0) {
  95. [dict setObject:[NSNumber numberWithInteger:self.periodOffTime] forKey:@"periodOffTime"];
  96. }
  97. if (self.periodOffTillDate != nil) {
  98. [dict setObject:self.periodOffTillDate forKey:@"periodOffTillDate"];
  99. }
  100. [dict setObject:[NSNumber numberWithBool:self.silent] forKey:@"silent"];
  101. [dict setObject:[NSNumber numberWithBool:self.mentions] forKey:@"mentions"];
  102. return dict;
  103. }
  104. - (BOOL)canSendPushForBaseMessage:(BaseMessage *)baseMessage {
  105. if (self.type == kPushSettingTypeOff) {
  106. if (self.mentions && baseMessage.conversation.isGroup) {
  107. if (![TextStyleUtils isMeOrAllMentionInText:[baseMessage logText]]) {
  108. return NO;
  109. }
  110. } else {
  111. return NO;
  112. }
  113. }
  114. else if (self.type == kPushSettingTypeOffPeriod) {
  115. if ([self.periodOffTillDate compare:[NSDate date]] == NSOrderedDescending) {
  116. if (self.mentions && baseMessage.conversation.isGroup) {
  117. if (![TextStyleUtils isMeOrAllMentionInText:[baseMessage logText]]) {
  118. return NO;
  119. }
  120. } else {
  121. return NO;
  122. }
  123. }
  124. }
  125. return YES;
  126. }
  127. - (BOOL)canSendPush {
  128. if (self.type == kPushSettingTypeOff) {
  129. return NO;
  130. }
  131. else if (self.type == kPushSettingTypeOffPeriod) {
  132. if ([self.periodOffTillDate compare:[NSDate date]] == NSOrderedDescending) {
  133. return NO;
  134. }
  135. }
  136. return YES;
  137. }
  138. - (UIImage *)imageForPushSetting {
  139. UIImage *pushSettingIcon = [self imageForEditedPushSetting];
  140. if (pushSettingIcon == nil) {
  141. pushSettingIcon = [UIImage imageNamed:@"Bell"];
  142. }
  143. return pushSettingIcon;
  144. }
  145. - (UIImage *)imageForEditedPushSetting {
  146. UIImage *pushSettingIcon = nil;
  147. if (self.type == kPushSettingTypeOn && self.silent) {
  148. pushSettingIcon = [UIImage imageNamed:@"BellOff"];
  149. }
  150. else if (self.type == kPushSettingTypeOff && !self.mentions) {
  151. pushSettingIcon = [UIImage imageNamed:@"NotificationOff"];
  152. }
  153. else if (self.type == kPushSettingTypeOff && self.mentions) {
  154. pushSettingIcon = [UIImage imageNamed:@"At"];
  155. }
  156. else if (self.type == kPushSettingTypeOffPeriod && !self.mentions && [self.periodOffTillDate compare:[NSDate date]] == NSOrderedDescending) {
  157. pushSettingIcon = [UIImage imageNamed:@"NotificationOff"];
  158. }
  159. else if (self.type == kPushSettingTypeOffPeriod && self.mentions && [self.periodOffTillDate compare:[NSDate date]] == NSOrderedDescending) {
  160. pushSettingIcon = [UIImage imageNamed:@"At"];
  161. }
  162. return pushSettingIcon;
  163. }
  164. @end