WebUpdateConversationRequest.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 WebUpdateConversationRequest: WebAbstractMessage {
  22. let type: String
  23. var identity: String? = nil
  24. var groupId: Data? = nil
  25. var isStarred: Bool? = nil
  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. if let data = message.data as? [AnyHashable: Any] {
  35. isStarred = data["isStarred"] as? Bool
  36. }
  37. super.init(message: message)
  38. }
  39. func updateConversation() {
  40. ack = WebAbstractMessageAcknowledgement.init(requestId, false, nil)
  41. DispatchQueue.main.sync {
  42. let entityManager = EntityManager()
  43. if groupId != nil {
  44. let conversation = entityManager.entityFetcher.conversation(forGroupId: groupId)
  45. if conversation == nil {
  46. ack!.success = false
  47. ack!.error = "invalidConversation"
  48. return
  49. }
  50. if isStarred == nil {
  51. ack!.success = false
  52. ack!.error = "badRequest"
  53. return
  54. }
  55. entityManager.performSyncBlockAndSafe({
  56. conversation!.marked = NSNumber.init(value: self.isStarred!)
  57. })
  58. self.ack!.success = true
  59. }
  60. else if identity != nil {
  61. let conversation = entityManager.entityFetcher.conversation(forIdentity: identity!)
  62. if conversation == nil {
  63. ack!.success = false
  64. ack!.error = "invalidConversation"
  65. return
  66. }
  67. if isStarred == nil {
  68. ack!.success = false
  69. ack!.error = "badRequest"
  70. return
  71. }
  72. entityManager.performSyncBlockAndSafe({
  73. conversation!.marked = NSNumber.init(value: self.isStarred!)
  74. })
  75. self.ack!.success = true
  76. } else {
  77. ack!.success = false
  78. ack!.error = "badRequest"
  79. return
  80. }
  81. }
  82. }
  83. }