DummyDataCreator.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import <UIKit/UIKit.h>
  21. #import "DummyDataCreator.h"
  22. #import "EntityManager.h"
  23. #import "TestObjectFactory.h"
  24. @interface DummyDataCreator ()
  25. @property EntityManager *entityManager;
  26. @end
  27. @implementation DummyDataCreator
  28. - (instancetype)init
  29. {
  30. self = [super init];
  31. if (self) {
  32. self.entityManager = [[EntityManager alloc] init];
  33. self.doSave = NO;
  34. }
  35. return self;
  36. }
  37. - (Conversation *)getConversationForThreemaId:(NSString *)threemaId {
  38. Contact *contact = [_entityManager.entityFetcher contactForId:threemaId];
  39. if (contact == nil && [threemaId isEqualToString:@"ECHOECHO"]) {
  40. TestObjectFactory *factory = [TestObjectFactory testObjectFactory];
  41. contact = [factory contactWithIdentity:@"ECHOECHO" publicKey:[[NSData alloc] initWithBase64EncodedString:@"4a6a1b34 dcef15d4 3cb74de2 fd36091b e99fbbaf 126d099d 47d83d91 9712c72b" options:NSDataBase64DecodingIgnoreUnknownCharacters]];
  42. }
  43. NSAssert(contact != nil, @"contact not found");
  44. Conversation *conversation = [_entityManager conversationForContact:contact createIfNotExisting:YES];
  45. NSAssert(conversation != nil, @"conversation not found");
  46. return conversation;
  47. }
  48. - (void)createDummyMessages {
  49. Conversation *conversation = [self getConversationForThreemaId:@"ECHOECHO"];
  50. [self createDummyTextMessagesForConversation:conversation count:10000 messageFormat:@"dummy msg: %ld"];
  51. [self createDummyImageMessagesForConversation:conversation count:100];
  52. sleep(2);
  53. }
  54. - (TextMessage *)createTextMessageForConversation:(Conversation *)conversation text:(NSString *)text {
  55. TextMessage *msg = [_entityManager.entityCreator textMessageForConversation:conversation];
  56. msg.text = text;
  57. msg.conversation = conversation;
  58. conversation.lastMessage = msg;
  59. return msg;
  60. }
  61. - (NSArray *)createDummyTextMessagesForConversation:(Conversation *)conversation count:(NSInteger)count messageFormat:(NSString *)format {
  62. NSInteger saveCount = 0;
  63. NSMutableArray *messages = [NSMutableArray arrayWithCapacity:count];
  64. for (NSInteger i=0; i<count; i++) {
  65. NSString *text = [NSString stringWithFormat:format, (long)i];
  66. TextMessage *msg = [self createTextMessageForConversation:conversation text:text];
  67. [messages addObject:msg];
  68. if (saveCount == 1000) {
  69. saveCount = 1;
  70. if (_doSave) {
  71. [_entityManager performSyncBlockAndSafe:nil];
  72. }
  73. NSLog(@"added %ld of %ld", (long)i, (long)count);
  74. } else {
  75. saveCount++;
  76. }
  77. }
  78. if (_doSave) {
  79. [_entityManager performSyncBlockAndSafe:nil];
  80. }
  81. return messages;
  82. }
  83. - (NSArray *)createDummyImageMessagesForConversation:(Conversation *)conversation count:(NSInteger)count {
  84. NSInteger saveCount = 0;
  85. NSMutableArray *messages = [NSMutableArray arrayWithCapacity:count];
  86. for (NSInteger i=0; i<count; i++) {
  87. ImageMessage *msg = [self dummyImageMessageForConversation:conversation];
  88. [messages addObject:msg];
  89. conversation.lastMessage = msg;
  90. if (saveCount > 100) {
  91. saveCount = 0;
  92. if (_doSave) {
  93. [_entityManager performSyncBlockAndSafe:nil];
  94. }
  95. } else {
  96. saveCount++;
  97. }
  98. }
  99. if (_doSave) {
  100. [_entityManager performSyncBlockAndSafe:nil];
  101. }
  102. return messages;
  103. }
  104. - (ImageMessage *)dummyImageMessageForConversation:(Conversation *)conversation {
  105. NSArray *messages = [_entityManager.entityFetcher imageMessagesForConversation:conversation];
  106. if ([messages count] < 1) {
  107. return nil;
  108. }
  109. ImageMessage *templateMessage = [messages objectAtIndex:0];
  110. ImageMessage *imageMessage = [_entityManager.entityCreator imageMessageForConversation:conversation];
  111. imageMessage.image = [self cloneFrom:templateMessage.image];
  112. imageMessage.thumbnail = [self cloneFrom:templateMessage.thumbnail];
  113. imageMessage.imageBlobId = [templateMessage.imageBlobId copy];
  114. imageMessage.imageNonce = [templateMessage.imageNonce copy];
  115. imageMessage.imageSize = [templateMessage.imageSize copy];
  116. imageMessage.progress = [NSNumber numberWithFloat:1.0];
  117. imageMessage.sent = [NSNumber numberWithBool:YES];
  118. imageMessage.delivered = [NSNumber numberWithBool:YES];
  119. imageMessage.read = [NSNumber numberWithBool:YES];
  120. return imageMessage;
  121. }
  122. - (ImageData *)cloneFrom:(ImageData *)imageData {
  123. EntityManager *entityManager = [[EntityManager alloc] init];
  124. ImageData *newImage = [entityManager.entityCreator imageData];
  125. newImage.data = imageData.data;
  126. newImage.width = [NSNumber numberWithInt:imageData.uiImage.size.width];
  127. newImage.height = [NSNumber numberWithInt:imageData.uiImage.size.height];
  128. return newImage;
  129. }
  130. @end