WebCreateTextMessageRequest.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-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 Foundation
  21. public class WebCreateTextMessageRequest: WebAbstractMessage {
  22. var type: String
  23. var id: String?
  24. var groupId: Data?
  25. var text: String
  26. var quote: WebTextMessageQuote?
  27. var baseMessage: BaseMessage? = nil
  28. var tmpError: String? = nil
  29. override init(message:WebAbstractMessage) {
  30. type = message.args!["type"] as! String
  31. if type == "contact" {
  32. id = message.args!["id"] as? String
  33. } else {
  34. let idString = message.args!["id"] as! String
  35. groupId = idString.hexadecimal()
  36. }
  37. let data = message.data! as! [AnyHashable:Any?]
  38. text = data["text"] as! String
  39. super.init(message: message)
  40. let tmpQuote = data["quote"] as? [AnyHashable:Any]
  41. if tmpQuote != nil {
  42. quote = WebTextMessageQuote.init(identity: tmpQuote!["identity"]! as! String, text: tmpQuote!["text"]! as! String)
  43. let quoteText = makeQuoteWithReply()
  44. text = quoteText + text
  45. }
  46. }
  47. func sendMessage(completion: @escaping () -> ()) {
  48. var conversation: Conversation? = nil
  49. if self.type == "contact" {
  50. // DispatchQueue.main.sync {
  51. let entityManager = EntityManager()
  52. let contact = entityManager.entityFetcher.contact(forId: self.id)
  53. if contact == nil {
  54. self.baseMessage = nil
  55. tmpError = "internalError"
  56. completion()
  57. return
  58. }
  59. conversation = entityManager.entityFetcher.conversation(for: contact)
  60. if conversation == nil {
  61. entityManager.performSyncBlockAndSafe({
  62. conversation = entityManager.entityCreator.conversation()
  63. conversation?.contact = contact
  64. })
  65. }
  66. if conversation != nil {
  67. if !PermissionChecker.init().canSend(in: conversation, entityManager: entityManager) {
  68. self.baseMessage = nil
  69. tmpError = "blocked"
  70. completion()
  71. return
  72. }
  73. MessageSender.sendMessage(self.text, in: conversation, async: true, quickReply: false, requestId:self.requestId, onCompletion: { (message, conv) in
  74. self.baseMessage = message
  75. completion()
  76. return
  77. })
  78. } else {
  79. self.baseMessage = nil
  80. tmpError = "internalError"
  81. completion()
  82. return
  83. }
  84. // }
  85. } else {
  86. // DispatchQueue.main.sync {
  87. let entityManager = EntityManager()
  88. conversation = entityManager.entityFetcher.conversation(forGroupId: self.groupId)
  89. if conversation != nil {
  90. if !PermissionChecker.init().canSend(in: conversation, entityManager: entityManager) {
  91. self.baseMessage = nil
  92. tmpError = "blocked"
  93. completion()
  94. return
  95. }
  96. MessageSender.sendMessage(self.text, in: conversation, async: true, quickReply: false, requestId:self.requestId, onCompletion: { (message, conv) in
  97. self.baseMessage = message
  98. completion()
  99. return
  100. })
  101. } else {
  102. self.baseMessage = nil
  103. tmpError = "internalError"
  104. completion()
  105. return
  106. }
  107. }
  108. // }
  109. }
  110. func makeQuoteWithReply() -> String {
  111. var quoteString: String = "> "
  112. quoteString.append(quote!.identity)
  113. quoteString.append(": ")
  114. let lines = quote!.text.components(separatedBy: "\n")
  115. var i = 0
  116. for line in lines {
  117. if i > 0 {
  118. quoteString.append("\n> ")
  119. }
  120. quoteString.append(line)
  121. i = i+1
  122. }
  123. quoteString.append("\n")
  124. return quoteString
  125. }
  126. }
  127. struct WebTextMessageQuote {
  128. var identity:String
  129. var text:String
  130. }