Skip to content

Commit

Permalink
Group avatar relationship for deletion.
Browse files Browse the repository at this point in the history
  • Loading branch information
corbett authored and FredericJacobs committed Jan 31, 2015
1 parent 585079d commit 8a5c5ef
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ - (void)sendAttachment:(NSData*)attachmentData contentType:(NSString*)contentTyp
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
result.pointer.isDownloaded = YES;
[result.pointer saveWithTransaction:transaction];

NSLog(@"finished uploading");
}];
[self sendMessage:outgoingMessage inThread:thread];
Expand Down Expand Up @@ -158,13 +159,13 @@ - (void)decryptedAndSaveAttachment:(TSAttachmentPointer*)attachment data:(NSData
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];
Expand Down
5 changes: 3 additions & 2 deletions Signal/src/textsecure/Messages/TSMessagesManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ - (void)handleIncomingMessage:(IncomingPushMessageSignal*)incomingMessage withPu
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;
Expand Down Expand Up @@ -255,7 +255,7 @@ - (void)handleReceivedMessage:(IncomingPushMessageSignal*)message withContent:(P
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) {
Expand All @@ -265,6 +265,7 @@ - (void)handleReceivedMessage:(IncomingPushMessageSignal*)message withContent:(P
if ([avatar isKindOfClass:[TSAttachmentStream class]]) {
TSAttachmentStream *stream = (TSAttachmentStream*)avatar;
if ([stream isImage]) {
model.associatedAttachmentId = stream.uniqueId;
model.groupImage = [stream image];
}
}
Expand Down
4 changes: 2 additions & 2 deletions Signal/src/view controllers/NewGroupViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ -(void)updateGroup {
[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];
}
Expand All @@ -142,7 +142,7 @@ -(TSGroupModel*)makeGroup {
[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
Expand Down
7 changes: 4 additions & 3 deletions Signal/src/view controllers/TSGroupModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
#import <Foundation/Foundation.h>
#import "TSYapDatabaseObject.h"

#import "TSAttachmentAdapter.h"


@interface TSGroupModel : TSYapDatabaseObject
@interface TSGroupModel : TSYapDatabaseObject <YapDatabaseRelationshipNode>

@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;
Expand Down
26 changes: 21 additions & 5 deletions Signal/src/view controllers/TSGroupModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -84,4 +87,17 @@ - (NSString*) getInfoStringAboutUpdateTo:(TSGroupModel*)newModel {
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
18 changes: 15 additions & 3 deletions Signal/test/textsecure/TSMessageStorageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>

#import "Cryptography.h"
#import "TSThread.h"
#import "TSContactThread.h"
#import "TSGroupThread.h"
Expand Down Expand Up @@ -146,22 +147,28 @@ - (void)testMessagesDeletedOnThreadDeletion {
[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];
Expand All @@ -178,6 +185,9 @@ - (void)testGroupMessagesDeletedOnThreadDeletion {
[[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");
Expand All @@ -191,8 +201,10 @@ - (void)testGroupMessagesDeletedOnThreadDeletion {
[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!");
}
}];
Expand Down

0 comments on commit 8a5c5ef

Please sign in to comment.