WebUpdateProfileRequest.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 WebUpdateProfileRequest: WebAbstractMessage {
  22. var nickName: String?
  23. var avatar: Data?
  24. var deleteNickName: Bool = false
  25. var deleteAvatar: Bool = false
  26. override init(message:WebAbstractMessage) {
  27. let data = message.data as! [AnyHashable: Any?]
  28. nickName = data["publicNickname"] as? String
  29. avatar = data["avatar"] as? Data
  30. if data["publicNickname"] != nil {
  31. if nickName == nil {
  32. deleteNickName = true
  33. }
  34. }
  35. if data["avatar"] != nil {
  36. if avatar == nil {
  37. deleteAvatar = true
  38. } else {
  39. let image = UIImage.init(data: avatar!)
  40. if image!.size.width >= CGFloat(kContactImageSize) || image!.size.height >= CGFloat(kContactImageSize) {
  41. avatar = MediaConverter.scaleImageData(to: avatar!, toMaxSize: CGFloat(kContactImageSize), useJPEG: false)
  42. }
  43. }
  44. }
  45. super.init(message: message)
  46. }
  47. func updateProfile() {
  48. ack = WebAbstractMessageAcknowledgement.init(requestId, false, nil)
  49. if deleteNickName {
  50. MyIdentityStore.shared().pushFromName = nil
  51. LicenseStore.shared().performUpdateWorkInfo()
  52. } else {
  53. if nickName != nil {
  54. if nickName!.lengthOfBytes(using: .utf8) > 32 {
  55. ack!.success = false
  56. ack!.error = "valueTooLong"
  57. return
  58. }
  59. MyIdentityStore.shared().pushFromName = nickName
  60. LicenseStore.shared().performUpdateWorkInfo()
  61. }
  62. }
  63. if avatar == nil && !deleteAvatar {
  64. ack!.success = true
  65. return
  66. }
  67. AvatarMaker.shared().clearCacheForProfilePicture()
  68. var profile = MyIdentityStore.shared().profilePicture
  69. if profile == nil {
  70. profile = [:]
  71. }
  72. if (avatar == profile!["ProfilePicture"] as? Data) {
  73. ack!.success = true
  74. return
  75. }
  76. profile?.setValue(avatar, forKey: "ProfilePicture")
  77. profile?.removeObject(forKey: "LastUpload")
  78. MyIdentityStore.shared().profilePicture = profile
  79. ContactStore.shared().removeProfilePictureFlagForAllContacts()
  80. ack!.success = true
  81. }
  82. }