add admins to group, clean up some code

This commit is contained in:
Ryan ZHAO 2020-01-22 14:41:34 +11:00
parent efb6295962
commit 8838dfb430
17 changed files with 61 additions and 53 deletions

View file

@ -1580,7 +1580,7 @@ static NSTimeInterval launchStartedAt;
NSString *userDefaultsKey = [@"isRSSFeedSetUp." stringByAppendingString:feed.id];
BOOL isFeedSetUp = [NSUserDefaults.standardUserDefaults boolForKey:userDefaultsKey];
if (!isFeedSetUp || !feed.isDeletable) {
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:feed.displayName memberIds:@[ userHexEncodedPublicKey, feed.server ] image:nil groupId:[LKGroupUtil getEncodedRssFeedGroupIdAsData:feed.id] groupType:RSS_FEED];
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:feed.displayName memberIds:@[ userHexEncodedPublicKey, feed.server ] image:nil groupId:[LKGroupUtil getEncodedRssFeedGroupIdAsData:feed.id] groupType:RSS_FEED adminIds:@[ userHexEncodedPublicKey, feed.server ]];
__block TSGroupThread *thread;
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
thread = [TSGroupThread getOrCreateThreadWithGroupModel:group transaction:transaction];

View file

@ -111,7 +111,7 @@ NS_ASSUME_NONNULL_BEGIN
NSData *groupId = [Randomness generateRandomBytes:16];
//TODO: Figure out if this is correct
TSGroupModel *model =
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL];
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL adminIds:@[ [TSAccountManager localNumber] ]];
TSGroupThread *thread = [TSGroupThread getOrCreateThreadWithGroupModel:model];
[SignalApp.sharedApp presentConversationForThread:thread animated:YES];

View file

@ -3880,7 +3880,7 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac
NSData *groupId = [Randomness generateRandomBytes:kGroupIdLength];
//TODO: Figure out if this is correct
TSGroupModel *groupModel =
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL];
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL adminIds:@[ [TSAccountManager localNumber] ]];
__block TSGroupThread *thread;
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
@ -4387,7 +4387,7 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac
NSData *groupId = [Randomness generateRandomBytes:kGroupIdLength];
//TODO: Figure out if this is correct
TSGroupModel *groupModel =
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL];
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL adminIds:@[ [TSAccountManager localNumber] ]];
TSGroupThread *groupThread =
[TSGroupThread getOrCreateThreadWithGroupModel:groupModel transaction:transaction];
@ -4429,7 +4429,7 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac
NSData *groupId = [Randomness generateRandomBytes:kGroupIdLength];
//TODO: Figure out if this is correct
TSGroupModel *groupModel =
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL];
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL adminIds:@[ [TSAccountManager localNumber] ]];
TSGroupThread *groupThread =
[TSGroupThread getOrCreateThreadWithGroupModel:groupModel transaction:transaction];
@ -4469,7 +4469,7 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac
NSData *groupId = [Randomness generateRandomBytes:kGroupIdLength];
//TODO: Figure out if this is correct
TSGroupModel *groupModel =
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL];
[[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId groupType:SIGNAL adminIds:@[ [TSAccountManager localNumber] ]];
TSGroupThread *groupThread =
[TSGroupThread getOrCreateThreadWithGroupModel:groupModel transaction:transaction];

View file

@ -536,7 +536,8 @@ NS_ASSUME_NONNULL_BEGIN
memberIds:groupThread.groupModel.groupMemberIds
image:groupThread.groupModel.groupImage
groupId:[Randomness generateRandomBytes:kGroupIdLength]
groupType:SIGNAL];
groupType:SIGNAL
adminIds:groupThread.groupModel.groupAdminIds];
thread = [TSGroupThread getOrCreateThreadWithGroupModel:groupModel transaction:transaction];
}];
OWSAssertDebug(thread);
@ -565,7 +566,7 @@ NS_ASSUME_NONNULL_BEGIN
memberIds:recipientIds
image:nil
groupId:[Randomness generateRandomBytes:kGroupIdLength]
groupType:SIGNAL];
groupType:SIGNAL adminIds:@[ self.tsAccountManager.localNumber ]];
thread = [TSGroupThread getOrCreateThreadWithGroupModel:groupModel transaction:transaction];
}];
OWSAssertDebug(thread);

View file

@ -97,6 +97,7 @@ NS_ASSUME_NONNULL_BEGIN
_avatarViewHelper.delegate = self;
self.memberRecipientIds = [NSMutableSet new];
self.adminIds = [NSMutableSet new];
}
#pragma mark - View Lifecycle
@ -529,18 +530,19 @@ NS_ASSUME_NONNULL_BEGIN
{
NSString *groupName = [self.groupNameTextField.text ows_stripped];
NSMutableArray<NSString *> *recipientIds = [self.memberRecipientIds.allObjects mutableCopy];
NSMutableArray<NSString *> *adminIds = [self.adminIds.allObjects mutableCopy];
//Test: Add Ryan to a new group. Should be deleted!!!!!
[recipientIds addObject:@"057fffb55430abb2df5be80fab693ffe4db26a8b76e590c1748a47baef7c483604"];
[recipientIds addObject:@"050c159f0d46c40ec6306bb0b8470972af3f67b5cb1e24b6460a6f692275c8b57f"];
[recipientIds addObject:[self.contactsViewHelper localNumber]];
//Loki - Add the creator as the admin
[self.adminIds addObject:[self.contactsViewHelper localNumber]];
[adminIds addObject:[self.contactsViewHelper localNumber]];
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:groupName
memberIds:recipientIds
image:self.groupAvatar
groupId:self.groupId
groupType:self.groupType];
[group setGroupAdminIds:[self.adminIds.allObjects copy]];
groupType:self.groupType
adminIds:adminIds];
return group;
}

View file

@ -383,7 +383,8 @@ NS_ASSUME_NONNULL_BEGIN
memberIds:self.memberRecipientIds.allObjects
image:self.groupAvatar
groupId:self.thread.groupModel.groupId
groupType:self.thread.groupModel.groupType];
groupType:self.thread.groupModel.groupType
adminIds:self.thread.groupModel.groupAdminIds];
groupModel.removedMembers = self.removedRecipientIds;
[self.conversationSettingsViewDelegate groupWasUpdated:groupModel];
}

View file

@ -398,6 +398,7 @@ message GroupContext {
optional string name = 3;
repeated string members = 4;
optional AttachmentPointer avatar = 5;
repeated string admins = 6;
}
message ContactDetails {

View file

@ -49,7 +49,8 @@ NSString *const TSGroupThread_NotificationKey_UniqueId = @"TSGroupThread_Notific
memberIds:@[ localNumber ]
image:nil
groupId:groupId
groupType:groupType];
groupType:groupType
adminIds:@[ localNumber ]];
self = [self initWithGroupModel:groupModel];
if (!self) {

View file

@ -66,7 +66,7 @@ public final class LokiPublicChatManager : NSObject {
@objc(addChatWithServer:channel:name:)
public func addChat(server: String, channel: UInt64, name: String) -> LokiPublicChat? {
guard let chat = LokiPublicChat(channel: channel, server: server, displayName: name, isDeletable: true) else { return nil }
let model = TSGroupModel(title: chat.displayName, memberIds: [userHexEncodedPublicKey!, chat.server], image: nil, groupId: LKGroupUtil .getEncodedPublichChatGroupId(asData: chat.id), groupType: .PUBLIC_CHAT)
let model = TSGroupModel(title: chat.displayName, memberIds: [userHexEncodedPublicKey!, chat.server], image: nil, groupId: LKGroupUtil .getEncodedPublichChatGroupId(asData: chat.id), groupType: .PUBLIC_CHAT, adminIds: [])
// Store the group chat mapping
self.storage.dbReadWriteConnection.readWrite { transaction in

View file

@ -963,6 +963,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
[groupBuilder setMembers:gThread.groupModel.groupMemberIds];
[groupBuilder setName:gThread.groupModel.groupName];
[groupBuilder setAdmins:gThread.groupModel.groupAdminIds];
}
NSError *error;
SSKProtoGroupContext *_Nullable groupContextProto = [groupBuilder buildAndReturnError:&error];

View file

@ -74,7 +74,6 @@
if ([encodedGroupId componentsSeparatedByString:@"!"].count > 1) {
decodedGroupId =[encodedGroupId componentsSeparatedByString:@"!"][1];
}
OWSLogInfo(@"RYAN: %@", decodedGroupId);
return [decodedGroupId dataUsingEncoding:NSUTF8StringEncoding];
}

View file

@ -581,7 +581,6 @@ NS_ASSUME_NONNULL_BEGIN
// Unknown group.
if (dataMessage.group.type == SSKProtoGroupContextTypeUpdate) {
// Accept group updates for unknown groups.
OWSLogInfo(@"RYAN: Group update message for unknown groups, %@", dataMessage.body);
} else if (dataMessage.group.type == SSKProtoGroupContextTypeDeliver) {
[self sendGroupInfoRequest:dataMessage.group.id envelope:envelope transaction:transaction];
return;
@ -1301,8 +1300,6 @@ NS_ASSUME_NONNULL_BEGIN
OWSLogWarn(@"Ignoring 'Request Group Info' message for group we no longer belong to.");
return;
}
gThread.groupModel.removedMembers = [NSMutableSet setWithArray:dataMessage.group.removedMembers];
NSString *updateGroupInfo =
[gThread.groupModel getInfoStringAboutUpdateTo:gThread.groupModel contactsManager:self.contactsManager];
@ -1366,9 +1363,6 @@ NS_ASSUME_NONNULL_BEGIN
if (groupId.length > 0) {
NSMutableSet *newMemberIds = [NSMutableSet setWithArray:dataMessage.group.members];
NSMutableSet *removedMemberIds = [NSMutableSet new];
if (dataMessage.group.removedMembers) {
removedMemberIds = [NSMutableSet setWithArray:dataMessage.group.removedMembers];
}
//Ryan TODO: validate the recipientId
// for (NSString *recipientId in newMemberIds) {
// if (!recipientId.isValidE164) {
@ -1387,7 +1381,12 @@ NS_ASSUME_NONNULL_BEGIN
// Don't trust other clients; ensure all known group members remain in the
// group unless it is a "quit" message in which case we should only remove
// the quiting member below.
[newMemberIds addObjectsFromArray:oldGroupThread.groupModel.groupMemberIds];
//[newMemberIds addObjectsFromArray:oldGroupThread.groupModel.groupMemberIds];
//Loki - Try to figure out removed members
removedMemberIds = [NSMutableSet setWithArray:oldGroupThread.groupModel.groupMemberIds];
[removedMemberIds minusSet:newMemberIds];
[removedMemberIds removeObject:envelope.source];
}
NSString *hexEncodedPublicKey = ([LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:envelope.source in:transaction] ?: envelope.source);
@ -1398,6 +1397,10 @@ NS_ASSUME_NONNULL_BEGIN
switch (dataMessage.group.type) {
case SSKProtoGroupContextTypeUpdate: {
if (oldGroupThread && ![oldGroupThread.groupModel.groupAdminIds containsObject:envelope.source]) {
OWSLogWarn(@"Loki - Received a group update message from a non-admin user for %@ %@", [LKGroupUtil getEncodedGroupId:groupId], @". Ignoring.");
return nil;
}
// Ensures that the thread exists but doesn't update it.
TSGroupThread *newGroupThread =
[TSGroupThread getOrCreateThreadWithGroupId:groupId groupType:oldGroupThread.groupModel.groupType transaction:transaction];
@ -1406,7 +1409,8 @@ NS_ASSUME_NONNULL_BEGIN
memberIds:newMemberIds.allObjects
image:oldGroupThread.groupModel.groupImage
groupId:dataMessage.group.id
groupType:oldGroupThread.groupModel.groupType];
groupType:oldGroupThread.groupModel.groupType
adminIds:dataMessage.group.admins];
newGroupModel.removedMembers = removedMemberIds;
NSString *updateGroupInfo = [newGroupThread.groupModel getInfoStringAboutUpdateTo:newGroupModel
contactsManager:self.contactsManager];
@ -1710,7 +1714,6 @@ NS_ASSUME_NONNULL_BEGIN
}
- (void)handleFriendRequestMessageIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope data:(SSKProtoDataMessage *)data message:(TSIncomingMessage *)message thread:(TSContactThread *)thread transaction:(YapDatabaseReadWriteTransaction *)transaction {
OWSLogInfo(@"RYAN: handle friend request from %@ %@", envelope.source, message.body);
if (envelope.isGroupChatMessage) {
return NSLog(@"[Loki] Ignoring friend request in group chat.", @"");

View file

@ -33,7 +33,8 @@ extern const int32_t kGroupIdLength;
memberIds:(NSArray<NSString *> *)memberIds
image:(nullable UIImage *)image
groupId:(NSData *)groupId
groupType:(GroupType)groupType;
groupType:(GroupType)groupType
adminIds:(NSArray<NSString *> *)adminIds;
- (BOOL)isEqual:(id)other;
- (BOOL)isEqualToGroupModel:(TSGroupModel *)model;

View file

@ -26,6 +26,7 @@ const int32_t kGroupIdLength = 16;
image:(nullable UIImage *)image
groupId:(NSData *)groupId
groupType:(GroupType)groupType
adminIds:(NSArray<NSString *> *)adminIds
{
OWSAssertDebug(memberIds);
@ -34,6 +35,7 @@ const int32_t kGroupIdLength = 16;
_groupImage = image; // image is stored in DB
_groupType = groupType;
_groupId = groupId;
_groupAdminIds = [adminIds copy];
return self;
}
@ -180,11 +182,6 @@ const int32_t kGroupIdLength = 16;
return _groupName.filterStringForDisplay;
}
- (void)setGroupAdminIds:(NSArray<NSString *> *)groupAdminIds
{
_groupAdminIds = groupAdminIds;
}
- (void)setRemovedMembers:(NSMutableSet<NSString *> *)removedMembers
{
_removedMembers = removedMembers;

View file

@ -5677,7 +5677,7 @@ extension SSKProtoAttachmentPointer.SSKProtoAttachmentPointerBuilder {
if let _value = avatar {
builder.setAvatar(_value)
}
builder.setRemovedMembers(removedMembers)
builder.setAdmins(admins)
return builder
}
@ -5712,23 +5712,18 @@ extension SSKProtoAttachmentPointer.SSKProtoAttachmentPointerBuilder {
proto.members = items
}
@objc public func removeMembers(_ valueParam: String) {
var items = proto.members
var removed = proto.removedMembers
if let index = items.index(of: valueParam) {
items.remove(at: index)
proto.members = items
removed.append(valueParam)
proto.removedMembers = removed
}
@objc public func addAdmins(_ valueParam: String) {
var items = proto.admins
items.append(valueParam)
proto.admins = items
}
@objc public func setMembers(_ wrappedItems: [String]) {
proto.members = wrappedItems
}
@objc public func setRemovedMembers(_ wrappedItems: [String]) {
proto.removedMembers = wrappedItems
@objc public func setAdmins(_ wrappedItems: [String]) {
proto.admins = wrappedItems
}
@objc public func setAvatar(_ valueParam: SSKProtoAttachmentPointer) {
@ -5766,8 +5761,8 @@ extension SSKProtoAttachmentPointer.SSKProtoAttachmentPointerBuilder {
return proto.members
}
@objc public var removedMembers: [String] {
return proto.removedMembers
@objc public var admins: [String] {
return proto.admins
}
private init(proto: SignalServiceProtos_GroupContext,

View file

@ -2312,11 +2312,6 @@ struct SignalServiceProtos_GroupContext {
get {return _storage._members}
set {_uniqueStorage()._members = newValue}
}
var removedMembers: [String] {
get {return _storage._removedMembers}
set {_uniqueStorage()._removedMembers = newValue}
}
var avatar: SignalServiceProtos_AttachmentPointer {
get {return _storage._avatar ?? SignalServiceProtos_AttachmentPointer()}
@ -2326,6 +2321,11 @@ struct SignalServiceProtos_GroupContext {
var hasAvatar: Bool {return _storage._avatar != nil}
/// Clears the value of `avatar`. Subsequent reads from it will return its default value.
mutating func clearAvatar() {_uniqueStorage()._avatar = nil}
var admins: [String] {
get {return _storage._admins}
set {_uniqueStorage()._admins = newValue}
}
var unknownFields = SwiftProtobuf.UnknownStorage()
@ -4945,6 +4945,7 @@ extension SignalServiceProtos_GroupContext: SwiftProtobuf.Message, SwiftProtobuf
3: .same(proto: "name"),
4: .same(proto: "members"),
5: .same(proto: "avatar"),
6: .same(proto: "admins")
]
fileprivate class _StorageClass {
@ -4952,8 +4953,8 @@ extension SignalServiceProtos_GroupContext: SwiftProtobuf.Message, SwiftProtobuf
var _type: SignalServiceProtos_GroupContext.TypeEnum? = nil
var _name: String? = nil
var _members: [String] = []
var _removedMembers: [String] = []
var _avatar: SignalServiceProtos_AttachmentPointer? = nil
var _admins: [String] = []
static let defaultInstance = _StorageClass()
@ -4965,7 +4966,7 @@ extension SignalServiceProtos_GroupContext: SwiftProtobuf.Message, SwiftProtobuf
_name = source._name
_members = source._members
_avatar = source._avatar
_removedMembers = source._removedMembers
_admins = source._admins
}
}
@ -4986,7 +4987,7 @@ extension SignalServiceProtos_GroupContext: SwiftProtobuf.Message, SwiftProtobuf
case 3: try decoder.decodeSingularStringField(value: &_storage._name)
case 4: try decoder.decodeRepeatedStringField(value: &_storage._members)
case 5: try decoder.decodeSingularMessageField(value: &_storage._avatar)
case 6: try decoder.decodeRepeatedStringField(value: &_storage._removedMembers)
case 6: try decoder.decodeRepeatedStringField(value: &_storage._admins)
default: break
}
}
@ -5010,6 +5011,9 @@ extension SignalServiceProtos_GroupContext: SwiftProtobuf.Message, SwiftProtobuf
if let v = _storage._avatar {
try visitor.visitSingularMessageField(value: v, fieldNumber: 5)
}
if !_storage._admins.isEmpty {
try visitor.visitRepeatedStringField(value: _storage._admins, fieldNumber: 6)
}
}
try unknownFields.traverse(visitor: &visitor)
}
@ -5024,6 +5028,7 @@ extension SignalServiceProtos_GroupContext: SwiftProtobuf.Message, SwiftProtobuf
if _storage._name != rhs_storage._name {return false}
if _storage._members != rhs_storage._members {return false}
if _storage._avatar != rhs_storage._avatar {return false}
if _storage._admins != rhs_storage._admins {return false}
return true
}
if !storagesAreEqual {return false}

View file

@ -372,7 +372,8 @@ class GroupThreadFactory: NSObject, Factory {
memberIds: groupThreadFactory.memberIdsBuilder(),
image: groupThreadFactory.imageBuilder(),
groupId: groupThreadFactory.groupIdBuilder(),
groupType: .SIGNAL)
groupType: .SIGNAL,
adminIds: [])
}
@objc