Skip to content

Commit 8a5c5ef

Browse files
corbettFredericJacobs
authored andcommitted
Group avatar relationship for deletion.
1 parent 585079d commit 8a5c5ef

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

Signal/src/textsecure/Messages/TSMessagesManager+attachments.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ - (void)sendAttachment:(NSData*)attachmentData contentType:(NSString*)contentTyp
107107
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
108108
result.pointer.isDownloaded = YES;
109109
[result.pointer saveWithTransaction:transaction];
110+
110111
NSLog(@"finished uploading");
111112
}];
112113
[self sendMessage:outgoingMessage inThread:thread];
@@ -158,13 +159,13 @@ - (void)decryptedAndSaveAttachment:(TSAttachmentPointer*)attachment data:(NSData
158159
contentType:attachment.contentType];
159160
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
160161
[stream saveWithTransaction:transaction];
161-
162162
if([attachment.avatarOfGroupId length]!=0) {
163-
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
163+
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
164164
TSGroupThread* gThread = [TSGroupThread getOrCreateThreadWithGroupModel:emptyModelToFillOutId transaction:transaction];
165165
gThread.groupModel.groupImage=[stream image];
166166
[gThread saveWithTransaction:transaction];
167-
} else {
167+
}
168+
else {
168169
// Causing message to be reloaded in view.
169170
TSMessage *message = [TSMessage fetchObjectWithUniqueID:messageId transaction:transaction];
170171
[message saveWithTransaction:transaction];

Signal/src/textsecure/Messages/TSMessagesManager.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ - (void)handleIncomingMessage:(IncomingPushMessageSignal*)incomingMessage withPu
190190
if(content.hasGroup) {
191191
__block BOOL ignoreMessage = NO;
192192
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
193-
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
193+
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
194194
TSGroupThread *gThread = [TSGroupThread threadWithGroupModel:emptyModelToFillOutId transaction:transaction];
195195
if(gThread==nil && content.group.type != PushMessageContentGroupContextTypeUpdate) {
196196
ignoreMessage = YES;
@@ -255,7 +255,7 @@ - (void)handleReceivedMessage:(IncomingPushMessageSignal*)message withContent:(P
255255
TSIncomingMessage *incomingMessage;
256256
TSThread *thread;
257257
if (groupId) {
258-
TSGroupModel *model = [[TSGroupModel alloc] initWithTitle:content.group.name memberIds:[[NSMutableArray alloc ] initWithArray:content.group.members] image:nil groupId:content.group.id];
258+
TSGroupModel *model = [[TSGroupModel alloc] initWithTitle:content.group.name memberIds:[[NSMutableArray alloc ] initWithArray:content.group.members] image:nil groupId:content.group.id associatedAttachmentId:nil];
259259
TSGroupThread *gThread = [TSGroupThread getOrCreateThreadWithGroupModel:model transaction:transaction];
260260
[gThread saveWithTransaction:transaction];
261261
if(content.group.type==PushMessageContentGroupContextTypeUpdate) {
@@ -265,6 +265,7 @@ - (void)handleReceivedMessage:(IncomingPushMessageSignal*)message withContent:(P
265265
if ([avatar isKindOfClass:[TSAttachmentStream class]]) {
266266
TSAttachmentStream *stream = (TSAttachmentStream*)avatar;
267267
if ([stream isImage]) {
268+
model.associatedAttachmentId = stream.uniqueId;
268269
model.groupImage = [stream image];
269270
}
270271
}

Signal/src/view controllers/NewGroupViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ -(void)updateGroup {
126126
[mut addObjectsFromArray:[[contacts objectAtIndex:(NSUInteger)idx.row] textSecureIdentifiers]];
127127
}
128128
[mut addObjectsFromArray:_thread.groupModel.groupMemberIds];
129-
_groupModel = [[TSGroupModel alloc] initWithTitle:_nameGroupTextField.text memberIds:[NSMutableArray arrayWithArray:[[NSSet setWithArray:mut] allObjects]] image:_thread.groupModel.groupImage groupId:_thread.groupModel.groupId];
129+
_groupModel = [[TSGroupModel alloc] initWithTitle:_nameGroupTextField.text memberIds:[NSMutableArray arrayWithArray:[[NSSet setWithArray:mut] allObjects]] image:_thread.groupModel.groupImage groupId:_thread.groupModel.groupId associatedAttachmentId:nil];
130130

131131
[self performSegueWithIdentifier:kUnwindToMessagesViewSegue sender:self];
132132
}
@@ -142,7 +142,7 @@ -(TSGroupModel*)makeGroup {
142142
[mut addObject:[SignalKeyingStorage.localNumber toE164]];
143143
NSData* groupId = [SecurityUtils generateRandomBytes:16];
144144

145-
return [[TSGroupModel alloc] initWithTitle:title memberIds:mut image:_groupImage groupId:groupId];
145+
return [[TSGroupModel alloc] initWithTitle:title memberIds:mut image:_groupImage groupId:groupId associatedAttachmentId:nil];
146146
}
147147

148148
-(IBAction)addGroupPhoto:(id)sender

Signal/src/view controllers/TSGroupModel.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
#import <Foundation/Foundation.h>
1010
#import "TSYapDatabaseObject.h"
1111

12+
#import "TSAttachmentAdapter.h"
1213

1314

14-
@interface TSGroupModel : TSYapDatabaseObject
15+
@interface TSGroupModel : TSYapDatabaseObject <YapDatabaseRelationshipNode>
1516

1617
@property (nonatomic, strong) NSMutableArray *groupMemberIds;
1718
@property (nonatomic, strong) UIImage *groupImage;
19+
@property (nonatomic, strong) NSString *associatedAttachmentId;
1820
@property (nonatomic, strong) NSString *groupName;
1921
@property (nonatomic, strong) NSData* groupId;
2022

21-
- (instancetype)initWithTitle:(NSString*)title memberIds:(NSMutableArray*)members image:(UIImage*)image groupId:(NSData*)groupId;
22-
23+
-(instancetype)initWithTitle:(NSString*)title memberIds:(NSMutableArray*)memberIds image:(UIImage*)image groupId:(NSData *)groupId associatedAttachmentId:(NSString*)attachmentId;
2324
- (BOOL)isEqual:(id)other;
2425
- (BOOL)isEqualToGroupModel:(TSGroupModel *)model;
2526
- (NSString*) getInfoStringAboutUpdateTo:(TSGroupModel*)model;

Signal/src/view controllers/TSGroupModel.m

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
#import "TSGroupModel.h"
1010

11+
NSString * const TSAttachementGroupAvatarFileRelationshipEdge = @"TSAttachementGroupAvatarFileEdge";
12+
1113
@implementation TSGroupModel
1214

13-
-(instancetype)initWithTitle:(NSString*)title memberIds:(NSMutableArray*)memberIds image:(UIImage*)image groupId:(NSData *)groupId{
14-
_groupName = title;
15-
_groupMemberIds = [memberIds copy];
16-
_groupImage = image;
17-
_groupId = groupId;
15+
-(instancetype)initWithTitle:(NSString*)title memberIds:(NSMutableArray*)memberIds image:(UIImage*)image groupId:(NSData *)groupId associatedAttachmentId:(NSString*)attachmentId {
16+
_groupName = title;
17+
_groupMemberIds = [memberIds copy];
18+
_groupImage = image;
19+
_associatedAttachmentId = attachmentId;
20+
_groupId = groupId;
1821

1922
return self;
2023
}
@@ -84,4 +87,17 @@ - (NSString*) getInfoStringAboutUpdateTo:(TSGroupModel*)newModel {
8487
return updatedGroupInfoString;
8588
}
8689

90+
- (NSArray *)yapDatabaseRelationshipEdges {
91+
if([_associatedAttachmentId length]>0){
92+
YapDatabaseRelationshipEdge *fileEdge = [[YapDatabaseRelationshipEdge alloc] initWithName:TSAttachementGroupAvatarFileRelationshipEdge
93+
destinationKey:_associatedAttachmentId
94+
collection:[TSAttachment collection]
95+
nodeDeleteRules:YDB_DeleteDestinationIfAllSourcesDeleted];
96+
return @[fileEdge];
97+
}
98+
else {
99+
return nil;
100+
}
101+
}
102+
87103
@end

Signal/test/textsecure/TSMessageStorageTests.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import <UIKit/UIKit.h>
1010
#import <XCTest/XCTest.h>
1111

12+
#import "Cryptography.h"
1213
#import "TSThread.h"
1314
#import "TSContactThread.h"
1415
#import "TSGroupThread.h"
@@ -146,22 +147,28 @@ - (void)testMessagesDeletedOnThreadDeletion {
146147
[self.thread remove];
147148

148149
[[TSStorageManager sharedManager].dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
149-
for (uint64_t i = timestamp; i<1000; i++) {
150+
for (uint64_t i = timestamp; i<100; i++) {
150151
TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:[TSInteraction stringFromTimeStamp:timestamp] transaction:transaction];
151152
NSAssert(fetchedMessage == nil, @"Message should be deleted!");
152153
}
153154
}];
154155
}
155156

157+
156158
- (void)testGroupMessagesDeletedOnThreadDeletion {
157159
uint64_t timestamp = 666;
158160
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.";
159161

162+
163+
TSAttachmentStream *pointer = [[TSAttachmentStream alloc] initWithIdentifier:@"helloid" data:[Cryptography generateRandomBytes:16] key:[Cryptography generateRandomBytes:16] contentType:@"data/random"];
164+
160165
__block TSGroupThread *thread;
161166
[[TSStorageManager sharedManager].dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
162-
thread = [TSGroupThread getOrCreateThreadWithGroupModel:[[TSGroupModel alloc] initWithTitle:@"fdsfsd" memberIds:[@[] mutableCopy] image:nil groupId:[NSData data]] transaction:transaction];
167+
thread = [TSGroupThread getOrCreateThreadWithGroupModel:[[TSGroupModel alloc] initWithTitle:@"fdsfsd" memberIds:[@[] mutableCopy] image:nil groupId:[NSData data] associatedAttachmentId:pointer.uniqueId] transaction:transaction];
163168

164169
[thread saveWithTransaction:transaction];
170+
[pointer saveWithTransaction:transaction];
171+
165172
}];
166173

167174
TSStorageManager *manager = [TSStorageManager sharedManager];
@@ -178,6 +185,9 @@ - (void)testGroupMessagesDeletedOnThreadDeletion {
178185
[[TSStorageManager sharedManager].dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
179186
for (uint64_t i = timestamp; i<100; i++) {
180187
TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:[TSInteraction stringFromTimeStamp:timestamp] transaction:transaction];
188+
TSAttachmentStream *fetchedPointer = [TSAttachmentStream fetchObjectWithUniqueID:pointer.uniqueId];
189+
NSAssert([fetchedPointer.image isEqual:pointer.image], @"attachment pointers not equal");
190+
181191

182192
NSAssert([fetchedMessage.body isEqualToString:body], @"Body of incoming message recovered");
183193
NSAssert(fetchedMessage.attachments == nil, @"attachments are nil");
@@ -191,8 +201,10 @@ - (void)testGroupMessagesDeletedOnThreadDeletion {
191201
[self.thread remove];
192202

193203
[[TSStorageManager sharedManager].dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
194-
for (uint64_t i = timestamp; i<1000; i++) {
204+
for (uint64_t i = timestamp; i<100; i++) {
195205
TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:[TSInteraction stringFromTimeStamp:timestamp] transaction:transaction];
206+
TSAttachmentStream *fetchedPointer = [TSAttachmentStream fetchObjectWithUniqueID:pointer.uniqueId];
207+
NSAssert(fetchedPointer == nil, @"Attachment pointer should be deleted");
196208
NSAssert(fetchedMessage == nil, @"Message should be deleted!");
197209
}
198210
}];

0 commit comments

Comments
 (0)