From 092578045e0da9659351191e8e0518c4edc963df Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Mon, 31 Jul 2017 17:40:34 -0400 Subject: [PATCH] [DEBUG-UI] create fake contact threads // FREEBIE --- .../ViewControllers/DebugUI/DebugUIContacts.h | 5 +++ .../ViewControllers/DebugUI/DebugUIContacts.m | 12 ++++++ .../ViewControllers/DebugUI/DebugUIMessages.m | 37 ++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIContacts.h b/Signal/src/ViewControllers/DebugUI/DebugUIContacts.h index 2c82ad7d5..f228bb093 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIContacts.h +++ b/Signal/src/ViewControllers/DebugUI/DebugUIContacts.h @@ -7,9 +7,14 @@ NS_ASSUME_NONNULL_BEGIN @class OWSTableSection; +@class CNContact; @interface DebugUIContacts : DebugUIPage ++ (void)createRandomContacts:(NSUInteger)count + contactHandler: + (nullable void (^)(CNContact *_Nonnull contact, NSUInteger idx, BOOL *_Nonnull stop))contactHandler; + @end NS_ASSUME_NONNULL_END diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIContacts.m b/Signal/src/ViewControllers/DebugUI/DebugUIContacts.m index b83243d3b..665cfeeef 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIContacts.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIContacts.m @@ -1137,13 +1137,20 @@ NS_ASSUME_NONNULL_BEGIN + (void)createRandomContacts:(int)count { OWSAssert(count > 0); + [self createRandomContacts:count contactHandler:nil]; +} ++ (void)createRandomContacts:(NSUInteger)count + contactHandler: + (nullable void (^)(CNContact *_Nonnull contact, NSUInteger idx, BOOL *_Nonnull stop))contactHandler +{ CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) { [OWSAlerts showAlertWithTitle:@"Error" message:@"No contacts access."]; return; } + NSMutableArray *contacts = [NSMutableArray new]; CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts @@ -1166,12 +1173,17 @@ NS_ASSUME_NONNULL_BEGIN value:[CNPhoneNumber phoneNumberWithStringValue:[self randomPhoneNumber]]]; contact.phoneNumbers = @[ homePhone ]; + [contacts addObject:contact]; [request addContact:contact toContainerWithIdentifier:nil]; } NSError *saveError = nil; if (![store executeSaveRequest:request error:&saveError]) { NSLog(@"error = %@", saveError); [OWSAlerts showAlertWithTitle:@"Error" message:saveError.localizedDescription]; + } else { + if (contactHandler) { + [contacts enumerateObjectsUsingBlock:contactHandler]; + } } }]; } diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index 86647fb13..8e2e5e312 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -3,6 +3,7 @@ // #import "DebugUIMessages.h" +#import "DebugUIContacts.h" #import "Environment.h" #import "OWSTableViewController.h" #import "Signal-Swift.h" @@ -82,6 +83,22 @@ NS_ASSUME_NONNULL_BEGIN actionBlock:^{ [DebugUIMessages sendFakeMessages:10 thread:thread]; }], + [OWSTableItem itemWithTitle:@"Create 100 fake threads with 10 messages" + actionBlock:^{ + [DebugUIMessages createFakeThreads:100 withFakeMessages:10]; + }], + [OWSTableItem itemWithTitle:@"Create 10 fake threads with 100 messages" + actionBlock:^{ + [DebugUIMessages createFakeThreads:10 withFakeMessages:100]; + }], + [OWSTableItem itemWithTitle:@"Create 10 fake threads with 10 messages" + actionBlock:^{ + [DebugUIMessages createFakeThreads:10 withFakeMessages:10]; + }], + [OWSTableItem itemWithTitle:@"Create 100 fake threads with 100 messages" + actionBlock:^{ + [DebugUIMessages createFakeThreads:100 withFakeMessages:100]; + }], [OWSTableItem itemWithTitle:@"Create 1k fake messages" actionBlock:^{ [DebugUIMessages sendFakeMessages:1000 thread:thread]; @@ -836,11 +853,27 @@ NS_ASSUME_NONNULL_BEGIN }]; } -+ (void)sendFakeMessages:(int)counter thread:(TSThread *)thread ++ (void)createFakeThreads:(NSUInteger)threadCount withFakeMessages:(NSUInteger)messageCount +{ + [DebugUIContacts + createRandomContacts:threadCount + contactHandler:^(CNContact *_Nonnull contact, NSUInteger idx, BOOL *_Nonnull stop) { + NSString *phoneNumberText = contact.phoneNumbers.firstObject.value.stringValue; + OWSAssert(phoneNumberText); + PhoneNumber *phoneNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:phoneNumberText]; + OWSAssert(phoneNumber); + OWSAssert(phoneNumber.toE164); + + TSContactThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:phoneNumber.toE164]; + [self sendFakeMessages:messageCount thread:contactThread]; + }]; +} + ++ (void)sendFakeMessages:(NSUInteger)counter thread:(TSThread *)thread { [TSStorageManager.sharedManager.dbReadWriteConnection readWriteWithBlock:^( YapDatabaseReadWriteTransaction *transaction) { - for (int i = 0; i < counter; i++) { + for (NSUInteger i = 0; i < counter; i++) { NSString *randomText = [self randomText]; switch (arc4random_uniform(4)) { case 0: {