WebDeleteMessageRequest.swift 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. class WebDeleteMessageRequest: WebAbstractMessage {
  22. let type: String
  23. var identity: String? = nil
  24. var groupId: Data? = nil
  25. let messageId: Data
  26. override init(message:WebAbstractMessage) {
  27. type = message.args!["type"] as! String
  28. if type == "contact" {
  29. identity = message.args!["id"] as? String
  30. } else {
  31. let idString = message.args!["id"] as! String
  32. groupId = idString.hexadecimal()
  33. }
  34. let messageIdBase64 = message.args!["messageId"] as! String
  35. messageId = Data(base64Encoded: messageIdBase64, options: .ignoreUnknownCharacters)!
  36. super.init(message: message)
  37. }
  38. func delete() {
  39. ack = WebAbstractMessageAcknowledgement.init(requestId, false, nil)
  40. let entityManager = EntityManager()
  41. let message = entityManager.entityFetcher.message(withId: messageId)
  42. var chatViewController: ChatViewController? = nil
  43. if message != nil {
  44. let groupId = message!.conversation.groupId
  45. var identity: String? = nil
  46. if message!.conversation.contact != nil {
  47. identity = message!.conversation.contact.identity
  48. }
  49. if message != nil {
  50. entityManager.performSyncBlockAndSafe({
  51. if message!.isKind(of: BaseMessage.self) {
  52. message?.conversation = nil
  53. entityManager.entityDestroyer.deleteObject(object: message!)
  54. }
  55. if message!.isKind(of: SystemMessage.self) {
  56. entityManager.entityDestroyer.deleteObject(object: message!)
  57. }
  58. var conversation: Conversation? = nil
  59. if groupId != nil {
  60. conversation = entityManager.entityFetcher.conversation(forGroupId: groupId)
  61. }
  62. else if identity != nil {
  63. conversation = entityManager.entityFetcher.conversation(forIdentity: identity)
  64. }
  65. if conversation != nil {
  66. let messageFetcher = MessageFetcher.init(for: conversation, with: entityManager.entityFetcher)
  67. conversation?.lastMessage = messageFetcher?.lastMessage()
  68. chatViewController = ChatViewControllerCache.controller(for: conversation)
  69. chatViewController?.updateConversationLastMessage()
  70. }
  71. })
  72. DispatchQueue.main.async {
  73. chatViewController?.updateConversation()
  74. }
  75. ack!.success = true
  76. } else {
  77. ack!.success = false
  78. ack!.error = "invalidMessage"
  79. }
  80. }
  81. }
  82. }