From 8a5c5efd735698370e0cd09c911109513046e9df Mon Sep 17 00:00:00 2001 From: Christine Corbett Date: Sat, 31 Jan 2015 01:37:36 -1000 Subject: [PATCH] Group avatar relationship for deletion. --- .../Messages/TSMessagesManager+attachments.m | 7 ++--- .../textsecure/Messages/TSMessagesManager.m | 5 ++-- .../view controllers/NewGroupViewController.m | 4 +-- Signal/src/view controllers/TSGroupModel.h | 7 ++--- Signal/src/view controllers/TSGroupModel.m | 26 +++++++++++++++---- .../test/textsecure/TSMessageStorageTests.m | 18 ++++++++++--- 6 files changed, 49 insertions(+), 18 deletions(-) diff --git a/Signal/src/textsecure/Messages/TSMessagesManager+attachments.m b/Signal/src/textsecure/Messages/TSMessagesManager+attachments.m index 36079b0d4..2696e1ef6 100644 --- a/Signal/src/textsecure/Messages/TSMessagesManager+attachments.m +++ b/Signal/src/textsecure/Messages/TSMessagesManager+attachments.m @@ -107,6 +107,7 @@ dispatch_queue_t attachmentsQueue() { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { result.pointer.isDownloaded = YES; [result.pointer saveWithTransaction:transaction]; + NSLog(@"finished uploading"); }]; [self sendMessage:outgoingMessage inThread:thread]; @@ -158,13 +159,13 @@ dispatch_queue_t attachmentsQueue() { contentType:attachment.contentType]; [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [stream saveWithTransaction:transaction]; - if([attachment.avatarOfGroupId length]!=0) { - TSGroupModel *emptyModelToFillOutId = [[TSGroupModel alloc] initWithTitle:nil memberIds:nil image:nil groupId:attachment.avatarOfGroupId]; // TODO refactor the TSGroupThread to just take in an ID (as it is all that it uses). Should not take in more than it uses + TSGroupModel *emptyModelToFillOutId = [[TSGroupModel alloc] initWithTitle:nil memberIds:nil image:nil groupId:attachment.avatarOfGroupId associatedAttachmentId:attachment.uniqueId]; // TODO refactor the TSGroupThread to just take in an ID (as it is all that it uses). Should not take in more than it uses TSGroupThread* gThread = [TSGroupThread getOrCreateThreadWithGroupModel:emptyModelToFillOutId transaction:transaction]; gThread.groupModel.groupImage=[stream image]; [gThread saveWithTransaction:transaction]; - } else { + } + else { // Causing message to be reloaded in view. TSMessage *message = [TSMessage fetchObjectWithUniqueID:messageId transaction:transaction]; [message saveWithTransaction:transaction]; diff --git a/Signal/src/textsecure/Messages/TSMessagesManager.m b/Signal/src/textsecure/Messages/TSMessagesManager.m index d854e4ed7..d1afc0f1d 100644 --- a/Signal/src/textsecure/Messages/TSMessagesManager.m +++ b/Signal/src/textsecure/Messages/TSMessagesManager.m @@ -190,7 +190,7 @@ if(content.hasGroup) { __block BOOL ignoreMessage = NO; [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { - TSGroupModel *emptyModelToFillOutId = [[TSGroupModel alloc] initWithTitle:nil memberIds:nil image:nil groupId:content.group.id]; // TODO refactor the TSGroupThread to just take in an ID (as it is all that it uses). Should not take in more than it uses + TSGroupModel *emptyModelToFillOutId = [[TSGroupModel alloc] initWithTitle:nil memberIds:nil image:nil groupId:content.group.id associatedAttachmentId:nil]; // TODO refactor the TSGroupThread to just take in an ID (as it is all that it uses). Should not take in more than it uses TSGroupThread *gThread = [TSGroupThread threadWithGroupModel:emptyModelToFillOutId transaction:transaction]; if(gThread==nil && content.group.type != PushMessageContentGroupContextTypeUpdate) { ignoreMessage = YES; @@ -255,7 +255,7 @@ TSIncomingMessage *incomingMessage; TSThread *thread; if (groupId) { - TSGroupModel *model = [[TSGroupModel alloc] initWithTitle:content.group.name memberIds:[[NSMutableArray alloc ] initWithArray:content.group.members] image:nil groupId:content.group.id]; + TSGroupModel *model = [[TSGroupModel alloc] initWithTitle:content.group.name memberIds:[[NSMutableArray alloc ] initWithArray:content.group.members] image:nil groupId:content.group.id associatedAttachmentId:nil]; TSGroupThread *gThread = [TSGroupThread getOrCreateThreadWithGroupModel:model transaction:transaction]; [gThread saveWithTransaction:transaction]; if(content.group.type==PushMessageContentGroupContextTypeUpdate) { @@ -265,6 +265,7 @@ if ([avatar isKindOfClass:[TSAttachmentStream class]]) { TSAttachmentStream *stream = (TSAttachmentStream*)avatar; if ([stream isImage]) { + model.associatedAttachmentId = stream.uniqueId; model.groupImage = [stream image]; } } diff --git a/Signal/src/view controllers/NewGroupViewController.m b/Signal/src/view controllers/NewGroupViewController.m index 99ed79ea6..47559560c 100644 --- a/Signal/src/view controllers/NewGroupViewController.m +++ b/Signal/src/view controllers/NewGroupViewController.m @@ -126,7 +126,7 @@ static NSString* const kUnwindToMessagesViewSegue = @"UnwindToMessagesViewSegue" [mut addObjectsFromArray:[[contacts objectAtIndex:(NSUInteger)idx.row] textSecureIdentifiers]]; } [mut addObjectsFromArray:_thread.groupModel.groupMemberIds]; - _groupModel = [[TSGroupModel alloc] initWithTitle:_nameGroupTextField.text memberIds:[NSMutableArray arrayWithArray:[[NSSet setWithArray:mut] allObjects]] image:_thread.groupModel.groupImage groupId:_thread.groupModel.groupId]; + _groupModel = [[TSGroupModel alloc] initWithTitle:_nameGroupTextField.text memberIds:[NSMutableArray arrayWithArray:[[NSSet setWithArray:mut] allObjects]] image:_thread.groupModel.groupImage groupId:_thread.groupModel.groupId associatedAttachmentId:nil]; [self performSegueWithIdentifier:kUnwindToMessagesViewSegue sender:self]; } @@ -142,7 +142,7 @@ static NSString* const kUnwindToMessagesViewSegue = @"UnwindToMessagesViewSegue" [mut addObject:[SignalKeyingStorage.localNumber toE164]]; NSData* groupId = [SecurityUtils generateRandomBytes:16]; - return [[TSGroupModel alloc] initWithTitle:title memberIds:mut image:_groupImage groupId:groupId]; + return [[TSGroupModel alloc] initWithTitle:title memberIds:mut image:_groupImage groupId:groupId associatedAttachmentId:nil]; } -(IBAction)addGroupPhoto:(id)sender diff --git a/Signal/src/view controllers/TSGroupModel.h b/Signal/src/view controllers/TSGroupModel.h index 5d98f2539..2054ffdc9 100644 --- a/Signal/src/view controllers/TSGroupModel.h +++ b/Signal/src/view controllers/TSGroupModel.h @@ -9,17 +9,18 @@ #import #import "TSYapDatabaseObject.h" +#import "TSAttachmentAdapter.h" -@interface TSGroupModel : TSYapDatabaseObject +@interface TSGroupModel : TSYapDatabaseObject @property (nonatomic, strong) NSMutableArray *groupMemberIds; @property (nonatomic, strong) UIImage *groupImage; +@property (nonatomic, strong) NSString *associatedAttachmentId; @property (nonatomic, strong) NSString *groupName; @property (nonatomic, strong) NSData* groupId; -- (instancetype)initWithTitle:(NSString*)title memberIds:(NSMutableArray*)members image:(UIImage*)image groupId:(NSData*)groupId; - +-(instancetype)initWithTitle:(NSString*)title memberIds:(NSMutableArray*)memberIds image:(UIImage*)image groupId:(NSData *)groupId associatedAttachmentId:(NSString*)attachmentId; - (BOOL)isEqual:(id)other; - (BOOL)isEqualToGroupModel:(TSGroupModel *)model; - (NSString*) getInfoStringAboutUpdateTo:(TSGroupModel*)model; diff --git a/Signal/src/view controllers/TSGroupModel.m b/Signal/src/view controllers/TSGroupModel.m index dd0eac3df..99bf010f8 100644 --- a/Signal/src/view controllers/TSGroupModel.m +++ b/Signal/src/view controllers/TSGroupModel.m @@ -8,13 +8,16 @@ #import "TSGroupModel.h" +NSString * const TSAttachementGroupAvatarFileRelationshipEdge = @"TSAttachementGroupAvatarFileEdge"; + @implementation TSGroupModel --(instancetype)initWithTitle:(NSString*)title memberIds:(NSMutableArray*)memberIds image:(UIImage*)image groupId:(NSData *)groupId{ - _groupName = title; - _groupMemberIds = [memberIds copy]; - _groupImage = image; - _groupId = groupId; +-(instancetype)initWithTitle:(NSString*)title memberIds:(NSMutableArray*)memberIds image:(UIImage*)image groupId:(NSData *)groupId associatedAttachmentId:(NSString*)attachmentId { + _groupName = title; + _groupMemberIds = [memberIds copy]; + _groupImage = image; + _associatedAttachmentId = attachmentId; + _groupId = groupId; return self; } @@ -84,4 +87,17 @@ return updatedGroupInfoString; } +- (NSArray *)yapDatabaseRelationshipEdges { + if([_associatedAttachmentId length]>0){ + YapDatabaseRelationshipEdge *fileEdge = [[YapDatabaseRelationshipEdge alloc] initWithName:TSAttachementGroupAvatarFileRelationshipEdge + destinationKey:_associatedAttachmentId + collection:[TSAttachment collection] + nodeDeleteRules:YDB_DeleteDestinationIfAllSourcesDeleted]; + return @[fileEdge]; + } + else { + return nil; + } +} + @end diff --git a/Signal/test/textsecure/TSMessageStorageTests.m b/Signal/test/textsecure/TSMessageStorageTests.m index 2386d534f..64e306d29 100644 --- a/Signal/test/textsecure/TSMessageStorageTests.m +++ b/Signal/test/textsecure/TSMessageStorageTests.m @@ -9,6 +9,7 @@ #import #import +#import "Cryptography.h" #import "TSThread.h" #import "TSContactThread.h" #import "TSGroupThread.h" @@ -146,22 +147,28 @@ [self.thread remove]; [[TSStorageManager sharedManager].dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - for (uint64_t i = timestamp; i<1000; i++) { + for (uint64_t i = timestamp; i<100; i++) { TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:[TSInteraction stringFromTimeStamp:timestamp] transaction:transaction]; NSAssert(fetchedMessage == nil, @"Message should be deleted!"); } }]; } + - (void)testGroupMessagesDeletedOnThreadDeletion { uint64_t timestamp = 666; NSString *body = @"A child born today will grow up with no conception of privacy at all. They’ll never know what it means to have a private moment to themselves an unrecorded, unanalyzed thought. And that’s a problem because privacy matters; privacy is what allows us to determine who we are and who we want to be."; + + TSAttachmentStream *pointer = [[TSAttachmentStream alloc] initWithIdentifier:@"helloid" data:[Cryptography generateRandomBytes:16] key:[Cryptography generateRandomBytes:16] contentType:@"data/random"]; + __block TSGroupThread *thread; [[TSStorageManager sharedManager].dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - thread = [TSGroupThread getOrCreateThreadWithGroupModel:[[TSGroupModel alloc] initWithTitle:@"fdsfsd" memberIds:[@[] mutableCopy] image:nil groupId:[NSData data]] transaction:transaction]; + thread = [TSGroupThread getOrCreateThreadWithGroupModel:[[TSGroupModel alloc] initWithTitle:@"fdsfsd" memberIds:[@[] mutableCopy] image:nil groupId:[NSData data] associatedAttachmentId:pointer.uniqueId] transaction:transaction]; [thread saveWithTransaction:transaction]; + [pointer saveWithTransaction:transaction]; + }]; TSStorageManager *manager = [TSStorageManager sharedManager]; @@ -178,6 +185,9 @@ [[TSStorageManager sharedManager].dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { for (uint64_t i = timestamp; i<100; i++) { TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:[TSInteraction stringFromTimeStamp:timestamp] transaction:transaction]; + TSAttachmentStream *fetchedPointer = [TSAttachmentStream fetchObjectWithUniqueID:pointer.uniqueId]; + NSAssert([fetchedPointer.image isEqual:pointer.image], @"attachment pointers not equal"); + NSAssert([fetchedMessage.body isEqualToString:body], @"Body of incoming message recovered"); NSAssert(fetchedMessage.attachments == nil, @"attachments are nil"); @@ -191,8 +201,10 @@ [self.thread remove]; [[TSStorageManager sharedManager].dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - for (uint64_t i = timestamp; i<1000; i++) { + for (uint64_t i = timestamp; i<100; i++) { TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:[TSInteraction stringFromTimeStamp:timestamp] transaction:transaction]; + TSAttachmentStream *fetchedPointer = [TSAttachmentStream fetchObjectWithUniqueID:pointer.uniqueId]; + NSAssert(fetchedPointer == nil, @"Attachment pointer should be deleted"); NSAssert(fetchedMessage == nil, @"Message should be deleted!"); } }];