Fake media send

// FREEBIE
This commit is contained in:
Michael Kirk 2018-03-16 10:31:26 -04:00
parent 966660fa2d
commit e5b1c0c9b4
2 changed files with 159 additions and 1 deletions

View File

@ -25,6 +25,13 @@
NS_ASSUME_NONNULL_BEGIN
@interface TSOutgoingMessage (PostDatingDebug)
- (void)setReceivedAtTimestamp:(uint64_t)value;
@end
@implementation DebugUIMessages
#pragma mark - Factory Methods
@ -115,6 +122,14 @@ NS_ASSUME_NONNULL_BEGIN
actionBlock:^{
[DebugUIMessages createFakeThreads:1000 withFakeMessages:1];
}],
[OWSTableItem itemWithTitle:@"🖼 fake media messages: 100"
actionBlock:^{
[DebugUIMessages sendFakeMediaMessages:100 thread:thread];
}],
[OWSTableItem itemWithTitle:@"🖼 fake media messages: 1k"
actionBlock:^{
[DebugUIMessages sendFakeMediaMessages:1000 thread:thread];
}],
[OWSTableItem itemWithTitle:@"Create 1k fake messages"
actionBlock:^{
[DebugUIMessages sendFakeMessages:1000 thread:thread];
@ -1108,6 +1123,149 @@ NS_ASSUME_NONNULL_BEGIN
}
}
+ (void)sendFakeMediaMessages:(NSUInteger)counter thread:(TSThread *)thread
{
// Download media
void (^failureBlock)(void) = ^void(void) {
OWSFail(@"%@ Failed to download example media.", self.logTag);
return;
};
[self ensureRandomGifWithSuccess:^(NSString *_Nonnull gifFilePath) {
[self ensureRandomJpegWithSuccess:^(NSString *_Nonnull jpegFilePath) {
[self ensureRandomMp4WithSuccess:^(NSString *_Nonnull mp4FilePath) {
DataSource *gifDataSource = [DataSourcePath dataSourceWithFilePath:gifFilePath];
DataSource *jpegDataSource = [DataSourcePath dataSourceWithFilePath:jpegFilePath];
DataSource *mp4DataSource = [DataSourcePath dataSourceWithFilePath:mp4FilePath];
[self sendFakeMediaMessages:counter
thread:thread
gifDataSource:gifDataSource
jpegDataSource:jpegDataSource
mp4DataSource:mp4DataSource];
}
failure:failureBlock];
}
failure:failureBlock];
}
failure:failureBlock];
}
+ (void)sendFakeMediaMessages:(NSUInteger)counter
thread:(TSThread *)thread
gifDataSource:(DataSource *)gifDataSource
jpegDataSource:(DataSource *)jpegDataSource
mp4DataSource:(DataSource *)mp4DataSource
{
const NSUInteger kMaxBatchSize = 2500;
if (counter < kMaxBatchSize) {
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self sendFakeMediaMessages:counter
thread:thread
gifDataSource:gifDataSource
jpegDataSource:jpegDataSource
mp4DataSource:mp4DataSource
transaction:transaction];
}];
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSUInteger remainder = counter;
while (remainder > 0) {
NSUInteger batchSize = MIN(kMaxBatchSize, remainder);
[OWSPrimaryStorage.dbReadWriteConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self sendFakeMediaMessages:batchSize
thread:thread
gifDataSource:gifDataSource
jpegDataSource:jpegDataSource
mp4DataSource:mp4DataSource
transaction:transaction];
}];
remainder -= batchSize;
DDLogInfo(@"%@ sendFakeMediaMessages %zd / %zd", self.logTag, counter - remainder, counter);
}
});
}
}
+ (void)sendFakeMediaMessages:(NSUInteger)counter
thread:(TSThread *)thread
gifDataSource:(DataSource *)gifDataSource
jpegDataSource:(DataSource *)jpegDataSource
mp4DataSource:(DataSource *)mp4DataSource
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
DDLogInfo(@"%@ sendFakeMediaMessages: %zd", self.logTag, counter);
for (NSUInteger i = 0; i < counter; i++) {
// Random time within last n years. Helpful for filling out a media gallery over time.
double yearsMillis = 4.0 * 365.0 * 24.0 * 60.0 * 60.0 * 1000.0;
uint64_t millisAgo = (uint64_t)(((double)arc4random() / ((double)0xffffffff)) * yearsMillis);
uint64_t timestamp = [NSDate ows_millisecondTimeStamp] - millisAgo;
NSString *_Nullable randomCaption;
if (arc4random() % 2 == 2) {
randomCaption = [self randomText];
}
TSOutgoingMessage *message = [[TSOutgoingMessage alloc] initWithTimestamp:timestamp
inThread:thread
messageBody:randomCaption
isVoiceMessage:NO
expiresInSeconds:0];
[message setReceivedAtTimestamp:timestamp];
TSAttachmentStream *attachmentStream;
NSString *filename;
switch (arc4random_uniform(3)) {
case 0: {
DataSource *dataSource = gifDataSource;
filename = dataSource.sourceFilename;
attachmentStream = [[TSAttachmentStream alloc] initWithContentType:@"image/gif"
byteCount:(UInt32)dataSource.dataLength
sourceFilename:filename];
NSError *error;
BOOL success = [attachmentStream writeData:dataSource.data error:&error];
OWSAssert(success && !error);
break;
}
case 1: {
DataSource *dataSource = jpegDataSource;
filename = dataSource.sourceFilename;
attachmentStream = [[TSAttachmentStream alloc] initWithContentType:@"image/jpeg"
byteCount:(UInt32)dataSource.dataLength
sourceFilename:filename];
NSError *error;
BOOL success = [attachmentStream writeData:dataSource.data error:&error];
OWSAssert(success && !error);
break;
}
case 2: {
DataSource *dataSource = mp4DataSource;
filename = dataSource.sourceFilename;
attachmentStream = [[TSAttachmentStream alloc] initWithContentType:@"video/mp4"
byteCount:(UInt32)dataSource.dataLength
sourceFilename:filename];
NSError *error;
BOOL success = [attachmentStream writeData:dataSource.data error:&error];
OWSAssert(success && !error);
break;
}
}
[attachmentStream saveWithTransaction:transaction];
[message.attachmentIds addObject:attachmentStream.uniqueId];
if (filename) {
message.attachmentFilenameMap[attachmentStream.uniqueId] = filename;
}
[message saveWithTransaction:transaction];
[message updateWithMessageState:TSOutgoingMessageStateSentToService transaction:transaction];
}
}
+ (void)createFakeLargeOutgoingAttachments:(int)counter thread:(TSThread *)thread
{
if (counter < 1) {

View File

@ -46,7 +46,7 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
//
// We typically want to order messages locally by when
// they were received & decrypted, not by when they were sent.
@property (nonatomic, readonly) uint64_t receivedAtTimestamp;
@property (nonatomic) uint64_t receivedAtTimestamp;
@end