Conversation.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-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 "Conversation.h"
  21. #import "Contact.h"
  22. #import "BaseMessage.h"
  23. #import "ImageData.h"
  24. #import "UserSettings.h"
  25. #import "BundleUtil.h"
  26. #import "EntityFetcher.h"
  27. @implementation Conversation
  28. @dynamic groupId;
  29. @dynamic groupImageSetDate;
  30. @dynamic groupMyIdentity;
  31. @dynamic groupName;
  32. @dynamic lastTypingStart;
  33. @dynamic typing;
  34. @dynamic unreadMessageCount;
  35. @dynamic marked;
  36. @dynamic ballots;
  37. @dynamic contact;
  38. @dynamic groupImage;
  39. @dynamic lastMessage;
  40. @dynamic members;
  41. @dynamic messages;
  42. @dynamic tags;
  43. - (void)setTyping:(NSNumber *)typing {
  44. [self willChangeValueForKey:@"typing"];
  45. [self setPrimitiveValue:typing forKey:@"typing"];
  46. if (typing.boolValue) {
  47. [self willChangeValueForKey:@"lastTypingStart"];
  48. [self setPrimitiveValue:[NSDate date] forKey:@"lastTypingStart"];
  49. [self didChangeValueForKey:@"lastTypingStart"];
  50. }
  51. [self didChangeValueForKey:@"typing"];
  52. }
  53. - (NSString*)displayName {
  54. if (self.groupId != nil) {
  55. if (self.groupName.length > 0) {
  56. return self.groupName;
  57. } else {
  58. return [self sortedMemberNames];
  59. }
  60. } else {
  61. return self.contact.displayName;
  62. }
  63. }
  64. - (NSString *)sortedMemberNames {
  65. NSMutableString *namesString = [NSMutableString string];
  66. if (self.contact == nil) {
  67. [namesString appendString:[BundleUtil localizedStringForKey:@"me"]];
  68. } else {
  69. [namesString appendString:[self memberNameForContact:self.contact]];
  70. }
  71. [namesString appendString:@": "];
  72. NSMutableArray *memberNames = [NSMutableArray array];
  73. for (Contact *member in self.sortedMembers) {
  74. if (member.state.intValue == kStateInvalid) {
  75. continue;
  76. }
  77. [memberNames addObject:[self memberNameForContact:member]];
  78. }
  79. // add self to end of list if not creator
  80. if (self.contact) {
  81. [memberNames addObject:[BundleUtil localizedStringForKey:@"me"]];
  82. }
  83. if (memberNames.count == 0) {
  84. [namesString appendString:[BundleUtil localizedStringForKey:@"nobody"]];
  85. } else {
  86. BOOL addComma = NO;
  87. for (NSString *name in memberNames) {
  88. if (addComma) {
  89. [namesString appendString:@", "];
  90. }
  91. [namesString appendString:name];
  92. addComma = YES;
  93. }
  94. }
  95. return namesString;
  96. }
  97. - (NSString *)memberNameForContact:(Contact *)member {
  98. if (member.firstName.length > 0) {
  99. return member.firstName;
  100. }
  101. return member.displayName;
  102. }
  103. + (NSSet *)keyPathsForValuesAffectingDisplayName {
  104. return [NSSet setWithObjects:@"groupName", @"members", @"contact.displayName", nil];
  105. }
  106. - (NSArray *)sortedMembers {
  107. /* Extract members without first or last name, and put them at the end of the list
  108. (otherwise they would appear on top) */
  109. NSMutableArray *namedMembers = [NSMutableArray array];
  110. NSMutableArray *unnamedMembers = [NSMutableArray array];
  111. for (Contact *contact in self.members) {
  112. if (contact.firstName.length == 0 && contact.lastName.length == 0)
  113. [unnamedMembers addObject:contact];
  114. else
  115. [namedMembers addObject:contact];
  116. }
  117. NSArray *sortedNamedMembers;
  118. if ([UserSettings sharedUserSettings].sortOrderFirstName) {
  119. sortedNamedMembers = [namedMembers sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES],
  120. [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES],
  121. [NSSortDescriptor sortDescriptorWithKey:@"identity" ascending:YES]]];
  122. } else {
  123. sortedNamedMembers = [namedMembers sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES],
  124. [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES],
  125. [NSSortDescriptor sortDescriptorWithKey:@"identity" ascending:YES]]];
  126. }
  127. NSArray *sortedUnnamedMembers = [unnamedMembers sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"identity" ascending:YES]]];
  128. return [sortedNamedMembers arrayByAddingObjectsFromArray:sortedUnnamedMembers];
  129. }
  130. + (NSSet *)keyPathsForValuesAffectingSortedMembers {
  131. return [NSSet setWithObjects:@"members", nil];
  132. }
  133. - (void)setGroupImage:(ImageData *)groupImage {
  134. [self willChangeValueForKey:@"groupImage"];
  135. [self setPrimitiveValue:groupImage forKey:@"groupImage"];
  136. [[NSNotificationCenter defaultCenter] postNotificationName:@"ThreemaGroupConversationImageChanged" object:self];
  137. [self didChangeValueForKey:@"groupImage"];
  138. }
  139. - (BOOL)wasDeleted {
  140. return self.managedObjectContext == nil;
  141. }
  142. - (BOOL)isGroup {
  143. return self.groupId != nil;
  144. }
  145. - (NSSet *)participants {
  146. if ([self isGroup]) {
  147. return [NSSet setWithSet:self.members];
  148. } else {
  149. return [NSSet setWithObject:self.contact];
  150. }
  151. }
  152. @end