MessageForwarder.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-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 "MessageForwarder.h"
  21. #import "EntityManager.h"
  22. #import "MessageSender.h"
  23. #import "TextMessage.h"
  24. #import "ImageMessage.h"
  25. #import "VideoMessage.h"
  26. #import "AudioMessage.h"
  27. #import "LocationMessage.h"
  28. #import "FileMessageSender.h"
  29. #import "UTIConverter.h"
  30. #import "AudioMessageSender.h"
  31. #ifdef DEBUG
  32. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  33. #else
  34. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  35. #endif
  36. @implementation MessageForwarder
  37. + (void)forwardMessage:(BaseMessage *)message toContact:(Contact *)contact
  38. {
  39. EntityManager *entityManager = [[EntityManager alloc] init];
  40. Conversation *conversation = [entityManager conversationForContact:contact createIfNotExisting:YES];
  41. [self forwardMessage:message toConversation:conversation];
  42. }
  43. + (void)forwardMessage:(BaseMessage *)message toGroup:(GroupProxy *)group {
  44. Conversation *conversation = group.conversation;
  45. [self forwardMessage:message toConversation:conversation];
  46. }
  47. + (void)forwardMessage:(BaseMessage *)message toConversation:(Conversation *)conversation
  48. {
  49. if (conversation == nil) {
  50. DDLogError(@"MessageForwarder: Cannot send message to nil conversation.");
  51. return;
  52. }
  53. if ([message isKindOfClass: [TextMessage class]]) {
  54. TextMessage *textMessage = (TextMessage *)message;
  55. [MessageSender sendMessage:textMessage.text inConversation:conversation async:YES quickReply:NO requestId:nil onCompletion:^(TextMessage *message, Conversation *conv) {
  56. ;//nop
  57. }];
  58. } else if ([message isKindOfClass: [AudioMessage class]]) {
  59. // AudioMessage *audioMessage = (AudioMessage *)message;
  60. // NSData *data = [audioMessage.audio.data copy];
  61. //
  62. // URLSenderItem *item = [URLSenderItem itemWithData:data
  63. // fileName:@"AudioMessage"
  64. // type:UTTYPE_AUDIO
  65. // renderType:@0
  66. // sendAsFile:true];
  67. //
  68. // FileMessageSender *sender = [[FileMessageSender alloc] init];
  69. // [sender sendItem:item inConversation:conversation requestId:nil];
  70. AudioMessage *audioMessage = (AudioMessage *)message;
  71. AudioMessageSender *sender = [[AudioMessageSender alloc] init];
  72. NSData *data = [audioMessage.audio.data copy];
  73. [sender startWithAudioData:data duration:audioMessage.duration inConversation:conversation requestId:nil];
  74. } else if ([message isKindOfClass: [ImageMessage class]]) {
  75. ImageMessage *imageMessage = (ImageMessage *)message;
  76. ImageURLSenderItemCreator *imageSender = [[ImageURLSenderItemCreator alloc] init];
  77. URLSenderItem *item = [imageSender senderItemFromImage:imageMessage.image.uiImage];
  78. FileMessageSender *sender = [[FileMessageSender alloc] init];
  79. [sender sendItem:item inConversation:conversation];
  80. } else if ([message isKindOfClass: [VideoMessage class]]) {
  81. VideoMessage *videoMessage = (VideoMessage *)message;
  82. VideoURLSenderItemCreator *senderCreator = [[VideoURLSenderItemCreator alloc] init];
  83. NSURL *videoURL = [VideoURLSenderItemCreator writeToTemporaryDirectoryWithData:videoMessage.video.data];
  84. if (videoURL == nil) {
  85. DDLogError(@"VideoURL was nil.");
  86. return;
  87. }
  88. URLSenderItem *senderItem = [senderCreator senderItemFrom:videoURL];
  89. FileMessageSender *sender = [[FileMessageSender alloc] init];
  90. [sender sendItem:senderItem inConversation:conversation requestId:nil];
  91. [VideoURLSenderItemCreator cleanTemporaryDirectory];
  92. } else if ([message isKindOfClass: [LocationMessage class]]) {
  93. LocationMessage *locationMessage = (LocationMessage *)message;
  94. CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(locationMessage.latitude.doubleValue, locationMessage.longitude.doubleValue);
  95. double accurracy = locationMessage.accuracy.doubleValue;
  96. [MessageSender sendLocation:coordinates accuracy:accurracy poiName:locationMessage.poiName poiAddress:nil inConversation:conversation onCompletion:^(NSData *messageId) {
  97. ;//nop
  98. }];
  99. }
  100. }
  101. @end