EntityCreator.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "EntityCreator.h"
  21. #import "Conversation.h"
  22. #import "ErrorHandler.h"
  23. #import "DatabaseManager.h"
  24. @interface EntityCreator ()
  25. @property NSManagedObjectContext *managedObjectContext;
  26. @end
  27. @implementation EntityCreator
  28. /*
  29. - (instancetype)init
  30. {
  31. self = [super init];
  32. if (self) {
  33. _managedObjectContext = [[DatabaseManager dbManager] getManagedObjectContext:![NSThread isMainThread]];
  34. }
  35. return self;
  36. }
  37. */
  38. - (instancetype)initWith:(NSManagedObjectContext *) managedObjectContext
  39. {
  40. self = [super init];
  41. if (self) {
  42. _managedObjectContext = managedObjectContext;
  43. }
  44. return self;
  45. }
  46. - (TextMessage *)textMessageFromGroupBox:(GroupTextMessage *)boxMsg {
  47. TextMessage *message = [self createTextMessageFromBox: boxMsg];
  48. message.text = boxMsg.text;
  49. message.quotedMessageId = boxMsg.quotedMessageId;
  50. return message;
  51. }
  52. - (TextMessage *)textMessageFromBox:(BoxTextMessage *)boxMsg {
  53. TextMessage *message = [self createTextMessageFromBox: boxMsg];
  54. message.text = boxMsg.text;
  55. message.quotedMessageId = boxMsg.quotedMessageId;
  56. return message;
  57. }
  58. - (ImageMessage *)imageMessageFromBox:(BoxImageMessage *)boxMsg {
  59. ImageMessage *message = [self createImageMessageFromBox: boxMsg];
  60. message.imageSize = [NSNumber numberWithInt:boxMsg.size];
  61. message.imageBlobId = boxMsg.blobId;
  62. message.imageNonce = boxMsg.imageNonce;
  63. return message;
  64. }
  65. - (ImageMessage *)imageMessageFromGroupBox:(GroupImageMessage *)boxMsg {
  66. ImageMessage *message = [self createImageMessageFromBox: boxMsg];
  67. message.imageSize = [NSNumber numberWithInt:boxMsg.size];
  68. message.imageBlobId = boxMsg.blobId;
  69. message.encryptionKey = boxMsg.encryptionKey;
  70. return message;
  71. }
  72. - (VideoMessage *)videoMessageFromBox:(BoxVideoMessage *)boxMsg {
  73. VideoMessage *message = [self createVideoMessageFromBox: boxMsg];
  74. message.duration = [NSNumber numberWithInt:boxMsg.duration];
  75. message.videoSize = [NSNumber numberWithInt:boxMsg.videoSize];
  76. message.videoBlobId = boxMsg.videoBlobId;
  77. message.encryptionKey = boxMsg.encryptionKey;
  78. return message;
  79. }
  80. - (VideoMessage *)videoMessageFromGroupBox:(GroupVideoMessage *)boxMsg {
  81. VideoMessage *message = [self createVideoMessageFromBox: boxMsg];
  82. message.duration = [NSNumber numberWithInt:boxMsg.duration];
  83. message.videoSize = [NSNumber numberWithInt:boxMsg.videoSize];
  84. message.videoBlobId = boxMsg.videoBlobId;
  85. message.encryptionKey = boxMsg.encryptionKey;
  86. return message;
  87. }
  88. - (AudioMessage *)audioMessageFromBox:(BoxAudioMessage *)boxMsg {
  89. AudioMessage *message = [self createAudioMessageFromBox: boxMsg];
  90. message.duration = [NSNumber numberWithInt:boxMsg.duration];
  91. message.audioSize = [NSNumber numberWithInt:boxMsg.audioSize];
  92. message.audioBlobId = boxMsg.audioBlobId;
  93. message.encryptionKey = boxMsg.encryptionKey;
  94. return message;
  95. }
  96. - (AudioMessage *)audioMessageFromGroupBox:(GroupAudioMessage *)boxMsg {
  97. AudioMessage *message = [self createAudioMessageFromBox: boxMsg];
  98. message.duration = [NSNumber numberWithInt:boxMsg.duration];
  99. message.audioSize = [NSNumber numberWithInt:boxMsg.audioSize];
  100. message.audioBlobId = boxMsg.audioBlobId;
  101. message.encryptionKey = boxMsg.encryptionKey;
  102. return message;
  103. }
  104. - (LocationMessage *)locationMessageFromBox:(BoxLocationMessage *)boxMsg {
  105. LocationMessage *message = [self createLocationMessageFromBox:boxMsg];
  106. message.latitude = [NSNumber numberWithDouble:boxMsg.latitude];
  107. message.longitude = [NSNumber numberWithDouble:boxMsg.longitude];
  108. message.accuracy = [NSNumber numberWithDouble:boxMsg.accuracy];
  109. message.poiName = boxMsg.poiName;
  110. if (boxMsg.poiAddress != nil) {
  111. message.poiName = [message.poiName stringByAppendingFormat:@"\n%@", boxMsg.poiAddress];
  112. }
  113. return message;
  114. }
  115. - (LocationMessage *)locationMessageFromGroupBox:(GroupLocationMessage *)boxMsg {
  116. LocationMessage *message = [self createLocationMessageFromBox:boxMsg];
  117. message.latitude = [NSNumber numberWithDouble:boxMsg.latitude];
  118. message.longitude = [NSNumber numberWithDouble:boxMsg.longitude];
  119. message.accuracy = [NSNumber numberWithDouble:boxMsg.accuracy];
  120. message.poiName = boxMsg.poiName;
  121. if (boxMsg.poiAddress != nil) {
  122. message.poiName = [message.poiName stringByAppendingFormat:@"\n%@", boxMsg.poiAddress];
  123. }
  124. return message;
  125. }
  126. - (BallotMessage *)ballotMessageFromBox:(AbstractMessage *)boxMsg {
  127. BallotMessage *message = (BallotMessage *)[self createBaseMessageFromBox:boxMsg ofType:@"BallotMessage"];
  128. return message;
  129. }
  130. - (FileMessage *)fileMessageFromBox:(AbstractMessage *)boxMsg {
  131. FileMessage *message = (FileMessage *)[self createBaseMessageFromBox:boxMsg ofType:@"FileMessage"];
  132. return message;
  133. }
  134. - (ImageData *)imageData {
  135. return (ImageData *)[self createEntityOfType: @"ImageData"];
  136. }
  137. - (VideoData *)videoData {
  138. return (VideoData *)[self createEntityOfType: @"VideoData"];
  139. }
  140. - (FileData *)fileData {
  141. return (FileData *)[self createEntityOfType: @"FileData"];
  142. }
  143. - (AudioData *)audioData {
  144. return (AudioData *)[self createEntityOfType: @"AudioData"];
  145. }
  146. - (TextMessage *)textMessageForConversation:(Conversation *)conversation {
  147. BaseMessage *message = [self createEntityOfType: @"TextMessage"];
  148. [self setupBasePropertiesForNewMessage: message inConversation: conversation];
  149. conversation.lastMessage = message;
  150. return (TextMessage *)message;
  151. }
  152. - (ImageMessage *)imageMessageForConversation:(Conversation *)conversation {
  153. BaseMessage *message = [self createEntityOfType: @"ImageMessage"];
  154. [self setupBasePropertiesForNewMessage: message inConversation: conversation];
  155. conversation.lastMessage = message;
  156. return (ImageMessage *)message;
  157. }
  158. - (VideoMessage *)videoMessageForConversation:(Conversation *)conversation {
  159. BaseMessage *message = [self createEntityOfType: @"VideoMessage"];
  160. [self setupBasePropertiesForNewMessage: message inConversation: conversation];
  161. conversation.lastMessage = message;
  162. return (VideoMessage *)message;
  163. }
  164. - (FileMessage *)fileMessageForConversation:(Conversation *)conversation {
  165. BaseMessage *message = [self createEntityOfType: @"FileMessage"];
  166. [self setupBasePropertiesForNewMessage: message inConversation: conversation];
  167. conversation.lastMessage = message;
  168. return (FileMessage *)message;
  169. }
  170. - (AudioMessage *)audioMessageForConversation:(Conversation *)conversation {
  171. BaseMessage *message = [self createEntityOfType: @"AudioMessage"];
  172. [self setupBasePropertiesForNewMessage: message inConversation: conversation];
  173. conversation.lastMessage = message;
  174. return (AudioMessage *)message;
  175. }
  176. - (LocationMessage *)locationMessageForConversation:(Conversation *)conversation {
  177. BaseMessage *message = [self createEntityOfType: @"LocationMessage"];
  178. [self setupBasePropertiesForNewMessage: message inConversation: conversation];
  179. conversation.lastMessage = message;
  180. return (LocationMessage *)message;
  181. }
  182. - (SystemMessage *)systemMessageForConversation:(Conversation *)conversation {
  183. BaseMessage *message = [self createEntityOfType: @"SystemMessage"];
  184. [self setupBasePropertiesForNewMessage: message inConversation: conversation];
  185. message.sent = [NSNumber numberWithBool:YES];
  186. return (SystemMessage *)message;
  187. }
  188. - (BallotMessage *)ballotMessageForConversation:(Conversation *)conversation {
  189. BaseMessage *message = [self createEntityOfType: @"BallotMessage"];
  190. [self setupBasePropertiesForNewMessage: message inConversation: conversation];
  191. conversation.lastMessage = message;
  192. return (BallotMessage *)message;
  193. }
  194. - (Contact *)contact {
  195. return (Contact *)[self createEntityOfType: @"Contact"];
  196. }
  197. - (LastGroupSyncRequest *)lastGroupSyncRequest {
  198. return (LastGroupSyncRequest *)[self createEntityOfType: @"LastGroupSyncRequest"];
  199. }
  200. - (Conversation *)conversation {
  201. return (Conversation *)[self createEntityOfType: @"Conversation"];
  202. }
  203. - (Group *)group {
  204. return (Group *)[self createEntityOfType: @"Group"];
  205. }
  206. - (Ballot *)ballot {
  207. return (Ballot *)[self createEntityOfType: @"Ballot"];
  208. }
  209. - (BallotChoice *)ballotChoice {
  210. BallotChoice *choice = [self createEntityOfType: @"BallotChoice"];
  211. choice.id = [NSNumber numberWithInt: arc4random()];
  212. choice.createDate = [NSDate date];
  213. return choice;
  214. }
  215. - (BallotResult *)ballotResult {
  216. BallotResult *result = (BallotResult *)[self createEntityOfType: @"BallotResult"];
  217. result.createDate = [NSDate date];
  218. return result;
  219. }
  220. - (Nonce *)nonceWithData:(NSData *)nonce {
  221. Nonce *result = (Nonce *)[self createEntityOfType: @"Nonce"];
  222. result.nonce = nonce;
  223. return result;
  224. }
  225. - (Tag *)tagWithName:(NSString *)name {
  226. Tag *tag = (Tag *)[self createEntityOfType:@"Tag"];
  227. tag.name = name;
  228. return tag;
  229. }
  230. - (WebClientSession *)webClientSession {
  231. return (WebClientSession *)[self createEntityOfType:@"WebClientSession"];
  232. }
  233. - (RequestedConversation *)requestedConversationWithId:(NSString *)conversationId webClientSession:(WebClientSession*)webClientSession {
  234. RequestedConversation *requestedConversation = (RequestedConversation *)[self createEntityOfType:@"RequestedConversation"];
  235. requestedConversation.conversationId = conversationId;
  236. requestedConversation.webClientSession = webClientSession;
  237. return requestedConversation;
  238. }
  239. - (LastLoadedMessageIndex *)lastLoadedMessageIndexWithBaseMessageId:(NSData *)baseMessageId index:(NSInteger)index webClientSession:(WebClientSession*)webClientSession {
  240. LastLoadedMessageIndex *lastLoadedMessageIndex = (LastLoadedMessageIndex *)[self createEntityOfType:@"LastLoadedMessageIndex"];
  241. lastLoadedMessageIndex.baseMessageId = baseMessageId;
  242. lastLoadedMessageIndex.index = [NSNumber numberWithInteger:index];
  243. lastLoadedMessageIndex.webClientSession = webClientSession;
  244. return lastLoadedMessageIndex;
  245. }
  246. - (RequestedThumbnail *)requestedThumbnailWithMessageId:(NSData *)messageId webClientSession:(WebClientSession*)webClientSession {
  247. RequestedThumbnail *requestedThumbnail = (RequestedThumbnail *)[self createEntityOfType:@"RequestedThumbnail"];
  248. requestedThumbnail.messageId = messageId;
  249. requestedThumbnail.webClientSession = webClientSession;
  250. return requestedThumbnail;
  251. }
  252. #pragma mark - private methods
  253. - (TextMessage *)createTextMessageFromBox:(AbstractMessage *)boxMsg {
  254. return (TextMessage *)[self createBaseMessageFromBox:boxMsg ofType:@"TextMessage"];
  255. }
  256. - (ImageMessage *)createImageMessageFromBox:(AbstractMessage *)boxMsg {
  257. return (ImageMessage *)[self createBaseMessageFromBox:boxMsg ofType:@"ImageMessage"];
  258. }
  259. - (VideoMessage *)createVideoMessageFromBox:(AbstractMessage *)boxMsg {
  260. return (VideoMessage *)[self createBaseMessageFromBox:boxMsg ofType:@"VideoMessage"];
  261. }
  262. - (AudioMessage *)createAudioMessageFromBox:(AbstractMessage *)boxMsg {
  263. return (AudioMessage *)[self createBaseMessageFromBox:boxMsg ofType:@"AudioMessage"];
  264. }
  265. - (LocationMessage *)createLocationMessageFromBox:(AbstractMessage *)boxMsg {
  266. return (LocationMessage *)[self createBaseMessageFromBox:boxMsg ofType:@"LocationMessage"];
  267. }
  268. - (BaseMessage *)createBaseMessageFromBox:(AbstractMessage *)boxMsg ofType:(NSString *)typeName {
  269. BaseMessage *message = [self createEntityOfType: typeName];
  270. [self setupBasePropertiesFor:message withValuesFrom:boxMsg];
  271. return message;
  272. }
  273. - (id)createEntityOfType:(NSString *)typeName {
  274. id object = [NSEntityDescription
  275. insertNewObjectForEntityForName:typeName
  276. inManagedObjectContext:_managedObjectContext];
  277. return object;
  278. }
  279. - (void)setupBasePropertiesForNewMessage:(BaseMessage *)message inConversation:(Conversation *)conversation {
  280. message.id = [AbstractMessage randomMessageId];
  281. message.date = [NSDate date];
  282. message.isOwn = [NSNumber numberWithBool:YES];
  283. message.sent = [NSNumber numberWithBool:NO];
  284. message.delivered = [NSNumber numberWithBool:NO];
  285. message.read = [NSNumber numberWithBool:NO];
  286. message.userack = [NSNumber numberWithBool:NO];
  287. message.conversation = conversation;
  288. }
  289. - (void)setupBasePropertiesFor:(BaseMessage *)dbMessage withValuesFrom:(AbstractMessage *)incomingMsg {
  290. dbMessage.id = incomingMsg.messageId;
  291. dbMessage.date = [NSDate date];
  292. dbMessage.isOwn = [NSNumber numberWithBool:NO];
  293. dbMessage.sent = [NSNumber numberWithBool:NO];
  294. dbMessage.delivered = [NSNumber numberWithBool:NO];
  295. dbMessage.read = [NSNumber numberWithBool:NO];
  296. dbMessage.userack = [NSNumber numberWithBool:NO];
  297. dbMessage.remoteSentDate = incomingMsg.date;
  298. dbMessage.flags = incomingMsg.flags;
  299. }
  300. @end