session-ios/SessionMessagingKit/Threads/TSGroupModel.m

162 lines
5.4 KiB
Mathematica
Raw Normal View History

2020-11-11 00:58:56 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "TSGroupModel.h"
2020-11-25 06:15:16 +01:00
#import <SessionMessagingKit/SessionMessagingKit-Swift.h>
#import <SignalCoreKit/NSString+OWS.h>
2020-11-11 00:58:56 +01:00
NS_ASSUME_NONNULL_BEGIN
const int32_t kGroupIdLength = 16;
@interface TSGroupModel ()
@property (nullable, nonatomic) NSString *groupName;
@end
#pragma mark -
@implementation TSGroupModel
2021-05-05 02:38:09 +02:00
- (nullable NSString *)groupName
{
return _groupName.filterStringForDisplay;
}
2020-11-11 00:58:56 +01:00
#if TARGET_OS_IOS
- (instancetype)initWithTitle:(nullable NSString *)title
memberIds:(NSArray<NSString *> *)memberIds
image:(nullable UIImage *)image
groupId:(NSData *)groupId
groupType:(GroupType)groupType
adminIds:(NSArray<NSString *> *)adminIds
{
_groupName = title;
_groupMemberIds = [memberIds copy];
2021-05-05 02:38:09 +02:00
_groupImage = image;
2020-11-11 00:58:56 +01:00
_groupType = groupType;
_groupId = groupId;
_groupAdminIds = [adminIds copy];
return self;
}
- (nullable instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (!self) {
return self;
}
// Occasionally seeing this as nil in legacy data,
// which causes crashes.
if (_groupMemberIds == nil) {
_groupMemberIds = [NSArray new];
}
if (_groupAdminIds == nil) {
_groupAdminIds = [NSArray new];
}
return self;
}
- (BOOL)isEqual:(id)other {
if (other == self) {
return YES;
}
if (!other || ![other isKindOfClass:[self class]]) {
return NO;
}
return [self isEqualToGroupModel:other];
}
- (BOOL)isEqualToGroupModel:(TSGroupModel *)other {
if (self == other)
return YES;
if (![_groupId isEqualToData:other.groupId]) {
return NO;
}
if (![_groupName isEqual:other.groupName]) {
return NO;
}
if (!(_groupImage != nil && other.groupImage != nil &&
[UIImagePNGRepresentation(_groupImage) isEqualToData:UIImagePNGRepresentation(other.groupImage)])) {
return NO;
}
if (_groupType != other.groupType) {
return NO;
}
NSMutableArray *compareMyGroupMemberIds = [NSMutableArray arrayWithArray:_groupMemberIds];
[compareMyGroupMemberIds removeObjectsInArray:other.groupMemberIds];
if ([compareMyGroupMemberIds count] > 0) {
return NO;
}
return YES;
}
2020-11-16 00:34:47 +01:00
- (NSString *)getInfoStringAboutUpdateTo:(TSGroupModel *)newModel {
2021-05-05 02:38:09 +02:00
// This is only invoked for group * changes *, i.e. not when a group is created.
NSString *userPublicKey = [SNGeneralUtilities getUserPublicKey];
2020-11-11 00:58:56 +01:00
NSString *updatedGroupInfoString = @"";
if (self == newModel) {
return NSLocalizedString(@"GROUP_UPDATED", @"");
}
2021-05-05 02:38:09 +02:00
// Name change
2020-11-11 00:58:56 +01:00
if (![_groupName isEqual:newModel.groupName]) {
2021-05-05 02:38:09 +02:00
updatedGroupInfoString = [updatedGroupInfoString stringByAppendingString:[NSString stringWithFormat:NSLocalizedString(@"GROUP_TITLE_CHANGED", @""), newModel.groupName]];
2020-11-11 00:58:56 +01:00
}
2021-05-05 02:38:09 +02:00
// Added & removed members
2020-11-11 00:58:56 +01:00
NSSet *oldMembers = [NSSet setWithArray:_groupMemberIds];
NSSet *newMembers = [NSSet setWithArray:newModel.groupMemberIds];
2021-05-05 02:38:09 +02:00
NSMutableSet *addedMembers = newMembers.mutableCopy;
[addedMembers minusSet:oldMembers];
2020-11-11 00:58:56 +01:00
2021-05-05 02:38:09 +02:00
NSMutableSet *removedMembers = oldMembers.mutableCopy;
[removedMembers minusSet:newMembers];
2020-11-11 00:58:56 +01:00
2021-05-05 02:38:09 +02:00
NSMutableSet *removedMembersMinusSelf = removedMembers.mutableCopy;
[removedMembersMinusSelf minusSet:[NSSet setWithObject:userPublicKey]];
2020-11-11 00:58:56 +01:00
2021-05-05 02:38:09 +02:00
if (removedMembersMinusSelf.count > 0) {
NSArray *removedMemberNames = [removedMembers.allObjects map:^NSString *(NSString *publicKey) {
SNContact *contact = [LKStorage.shared getContactWithSessionID:publicKey];
return [contact displayNameFor:SNContactContextRegular] ?: publicKey;
2020-11-11 00:58:56 +01:00
}];
2021-05-05 02:38:09 +02:00
NSString *format = removedMembers.count > 1 ? NSLocalizedString(@"GROUP_MEMBERS_REMOVED", @"") : NSLocalizedString(@"GROUP_MEMBER_REMOVED", @"");
2020-11-11 00:58:56 +01:00
updatedGroupInfoString = [updatedGroupInfoString
stringByAppendingString:[NSString
2021-05-05 02:38:09 +02:00
stringWithFormat: format,
[removedMemberNames componentsJoinedByString:@", "]]];
2020-11-11 00:58:56 +01:00
}
2021-05-05 02:38:09 +02:00
if (addedMembers.count > 0) {
NSArray *addedMemberNames = [[addedMembers allObjects] map:^NSString*(NSString* publicKey) {
SNContact *contact = [LKStorage.shared getContactWithSessionID:publicKey];
return [contact displayNameFor:SNContactContextRegular] ?: publicKey;
2021-01-27 01:45:46 +01:00
}];
updatedGroupInfoString = [updatedGroupInfoString
stringByAppendingString:[NSString
stringWithFormat:NSLocalizedString(@"GROUP_MEMBER_JOINED", @""),
2021-05-05 02:38:09 +02:00
[addedMemberNames componentsJoinedByString:@", "]]];
2020-11-11 00:58:56 +01:00
}
2021-05-05 02:38:09 +02:00
if ([removedMembers containsObject:userPublicKey]) {
updatedGroupInfoString = [updatedGroupInfoString stringByAppendingString:NSLocalizedString(@"YOU_WERE_REMOVED", @"")];
2020-11-11 00:58:56 +01:00
}
2021-05-05 02:38:09 +02:00
// Return
2020-11-11 00:58:56 +01:00
if ([updatedGroupInfoString length] == 0) {
updatedGroupInfoString = NSLocalizedString(@"GROUP_UPDATED", @"");
}
return updatedGroupInfoString;
}
#endif
@end
NS_ASSUME_NONNULL_END