WebCleanReceiverConversationRequest.swift 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 WebCleanReceiverConversationRequest: WebAbstractMessage {
  22. var identity: String? = nil
  23. var groupId: Data? = nil
  24. let type: String
  25. override init(message:WebAbstractMessage) {
  26. type = message.args!["type"] as! String
  27. if type == "contact" {
  28. identity = message.args!["id"] as? String
  29. } else {
  30. let idString = message.args!["id"] as? String
  31. groupId = idString?.hexadecimal()
  32. }
  33. super.init(message: message)
  34. }
  35. func clean() {
  36. DispatchQueue.main.sync {
  37. let entityManager = EntityManager()
  38. if identity != nil {
  39. let conversation = entityManager.entityFetcher.conversation(forIdentity: identity)
  40. entityManager.performSyncBlockAndSafe({
  41. entityManager.entityDestroyer.deleteObject(object: conversation!)
  42. })
  43. }
  44. else if groupId != nil {
  45. let conversation = entityManager.entityFetcher.conversation(forGroupId: groupId)
  46. let groupProxy = GroupProxy.init(for: conversation, entityManager: entityManager)
  47. if groupProxy != nil {
  48. var tmpConversation: Conversation? = nil
  49. entityManager.performSyncBlockAndSafe({
  50. var imageData: Data? = nil
  51. var imageHeight: NSNumber? = nil
  52. var imageWidth: NSNumber? = nil
  53. if conversation?.groupImage != nil {
  54. imageData = conversation?.groupImage.data
  55. imageHeight = conversation?.groupImage.height
  56. imageWidth = conversation?.groupImage.width
  57. }
  58. entityManager.entityDestroyer.deleteObject(object: conversation!)
  59. tmpConversation = entityManager.entityCreator.conversation()
  60. tmpConversation?.contact = conversation?.contact
  61. tmpConversation?.members = conversation?.members
  62. tmpConversation?.groupId = conversation?.groupId
  63. tmpConversation?.groupName = conversation?.groupName
  64. tmpConversation?.groupMyIdentity = conversation?.groupMyIdentity
  65. if imageData != nil {
  66. let tmpImageData = entityManager.entityCreator.imageData()
  67. tmpImageData?.data = imageData
  68. tmpImageData?.height = imageHeight
  69. tmpImageData?.width = imageWidth
  70. tmpConversation?.groupImage = tmpImageData
  71. tmpConversation?.groupImageSetDate = conversation?.groupImageSetDate
  72. }
  73. })
  74. }
  75. }
  76. }
  77. }
  78. }