WebCreateContactResponse.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. import ThreemaFramework
  22. class WebCreateContactResponse: WebAbstractMessage {
  23. var identity: String
  24. var contact:Contact? = nil
  25. init(request: WebCreateContactRequest) {
  26. identity = request.identity.uppercased()
  27. let tmpAck = WebAbstractMessageAcknowledgement.init(request.requestId, true, nil)
  28. super.init(messageType: "create", messageSubType: "contact", requestId: nil, ack: tmpAck, args: nil, data: nil)
  29. }
  30. func addContact(completion: @escaping () -> ()) {
  31. let mdmSetup = MDMSetup(setup: false)!
  32. if mdmSetup.disableAddContact() {
  33. self.ack!.success = false
  34. self.ack!.error = "disabledByPolicy"
  35. self.args = ["identity": self.identity]
  36. self.data = nil
  37. completion()
  38. return
  39. }
  40. if identity.count != kIdentityLen {
  41. self.ack!.success = false
  42. self.ack!.error = "invalidIdentity"
  43. self.args = ["identity": self.identity]
  44. self.data = nil
  45. completion()
  46. return
  47. }
  48. ContactStore.shared().addContact(withIdentity: identity, verificationLevel: Int32(kVerificationLevelUnverified), onCompletion: { (theContact, alreadyExisting) in
  49. if MyIdentityStore.shared().isProvisioned() && self.identity == MyIdentityStore.shared().identity {
  50. self.ack!.success = false
  51. self.ack!.error = "invalidIdentity"
  52. self.args = ["identity": self.identity]
  53. self.data = nil
  54. completion()
  55. return
  56. }
  57. if theContact == nil {
  58. self.ack!.success = false
  59. self.ack!.error = "internalError"
  60. self.args = ["identity": self.identity]
  61. self.data = nil
  62. completion()
  63. return
  64. }
  65. self.contact = theContact!
  66. self.ack!.success = true
  67. self.args = ["identity": self.identity]
  68. let webContact = WebContact.init(self.contact!)
  69. self.data = ["receiver": webContact.objectDict()]
  70. completion()
  71. return
  72. }) { (theError) in
  73. self.ack!.success = false
  74. self.ack!.error = "invalidIdentity"
  75. self.args = ["identity": self.identity]
  76. self.data = nil
  77. completion()
  78. return
  79. }
  80. }
  81. }