WebUpdateContactRequest.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 WebUpdateContactRequest: WebAbstractMessage {
  22. let identity: String
  23. var firstName: String?
  24. var lastName: String?
  25. var avatar: Data?
  26. var deleteAvatar: Bool = false
  27. var contact: Contact?
  28. override init(message:WebAbstractMessage) {
  29. identity = message.args!["identity"] as! String
  30. if let data = message.data as? [AnyHashable:Any?] {
  31. firstName = data["firstName"] as? String
  32. lastName = data["lastName"] as? String
  33. avatar = data["avatar"] as? Data
  34. if data["avatar"] != nil {
  35. if avatar == nil {
  36. deleteAvatar = true
  37. } else {
  38. let image = UIImage.init(data: avatar!)
  39. if image!.size.width >= CGFloat(kContactImageSize) || image!.size.height >= CGFloat(kContactImageSize) {
  40. avatar = MediaConverter.scaleImageData(to: avatar!, toMaxSize: CGFloat(kContactImageSize), useJPEG: false)
  41. }
  42. }
  43. }
  44. }
  45. super.init(message: message)
  46. }
  47. func updateContact() {
  48. ack = WebAbstractMessageAcknowledgement.init(requestId, false, nil)
  49. let entityManager = EntityManager()
  50. let updatedContact = entityManager.entityFetcher.contact(forId: identity)
  51. if firstName != nil {
  52. if firstName!.lengthOfBytes(using: .utf8) > 256 {
  53. ack!.success = false
  54. ack!.error = "valueTooLong"
  55. contact = updatedContact
  56. return
  57. }
  58. }
  59. if lastName != nil {
  60. if lastName!.lengthOfBytes(using: .utf8) > 256 {
  61. ack!.success = false
  62. ack!.error = "valueTooLong"
  63. contact = updatedContact
  64. return
  65. }
  66. }
  67. entityManager.performSyncBlockAndSafe({
  68. updatedContact?.firstName = self.firstName
  69. updatedContact?.lastName = self.lastName
  70. if self.avatar != nil || self.deleteAvatar == true {
  71. updatedContact?.imageData = self.avatar
  72. }
  73. })
  74. contact = updatedContact
  75. ack!.success = true
  76. }
  77. }