WebAvatarResponse.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 WebAvatarResponse: WebAbstractMessage {
  22. var type: String
  23. var id: String
  24. var highResolution: Bool
  25. var avatar: Data?
  26. init(request: WebAvatarRequest) {
  27. type = request.type
  28. id = request.id
  29. highResolution = request.highResolution
  30. var size = highResolution ? kWebClientAvatarHiResSize : kWebClientAvatarSize
  31. let quality = highResolution ? kWebClientAvatarHiResQuality : kWebClientAvatarQuality
  32. if request.maxSize != nil {
  33. size = request.maxSize!
  34. }
  35. let entityManager = EntityManager()
  36. if type == "contact" {
  37. let contact = entityManager.entityFetcher.contact(forId: id)
  38. if contact != nil {
  39. if let avatarImage = AvatarMaker.shared().avatar(for: contact!, size: CGFloat(size), masked: false, scaled: false) {
  40. avatar = avatarImage.jpegData(compressionQuality: CGFloat(quality))
  41. }
  42. }
  43. }
  44. else if type == "group" {
  45. let groupId = request.id.hexadecimal()
  46. let conversation = entityManager.entityFetcher.conversation(forGroupId: groupId)
  47. if conversation != nil {
  48. if let avatarImage = AvatarMaker.shared().avatar(for: conversation!, size: CGFloat(size), masked: false, scaled: false) {
  49. avatar = avatarImage.jpegData(compressionQuality: CGFloat(quality))
  50. }
  51. }
  52. }
  53. let tmpArgs:[AnyHashable:Any?] = ["type": type, "id": id, "highResolution": highResolution]
  54. let tmpAck = request.requestId != nil ? WebAbstractMessageAcknowledgement.init(request.requestId, true, nil) : nil
  55. super.init(messageType: "response", messageSubType: "avatar", requestId: nil, ack: tmpAck, args: tmpArgs, data: avatar != nil ? avatar : nil)
  56. }
  57. init(requestId: String?, groupProxy: GroupProxy) {
  58. type = "group"
  59. id = groupProxy.groupId.hexEncodedString()
  60. highResolution = false
  61. let size = highResolution ? kWebClientAvatarHiResSize : kWebClientAvatarSize
  62. let quality = highResolution ? kWebClientAvatarHiResQuality : kWebClientAvatarQuality
  63. if let avatarImage = AvatarMaker.shared().avatar(for: groupProxy.conversation(), size: CGFloat(size), masked: false, scaled: false) {
  64. avatar = avatarImage.jpegData(compressionQuality: CGFloat(quality))
  65. }
  66. let tmpArgs:[AnyHashable:Any?] = ["type": type, "id": id]
  67. let tmpAck = requestId != nil ? WebAbstractMessageAcknowledgement.init(requestId, true, nil) : nil
  68. super.init(messageType: "update", messageSubType: "avatar", requestId: nil, ack: tmpAck, args: tmpArgs, data: avatar != nil ? avatar : nil)
  69. }
  70. init(requestId: String?, contact: Contact) {
  71. type = "contact"
  72. id = contact.identity
  73. highResolution = false
  74. let size = highResolution ? kWebClientAvatarHiResSize : kWebClientAvatarSize
  75. let quality = highResolution ? kWebClientAvatarHiResQuality : kWebClientAvatarQuality
  76. if let avatarImage = AvatarMaker.shared().avatar(for: contact, size: CGFloat(size), masked: false, scaled: false) {
  77. avatar = avatarImage.jpegData(compressionQuality: CGFloat(quality))
  78. }
  79. let tmpArgs:[AnyHashable:Any?] = ["type": type, "id": id]
  80. let tmpAck = requestId != nil ? WebAbstractMessageAcknowledgement.init(requestId, true, nil) : nil
  81. super.init(messageType: "update", messageSubType: "avatar", requestId: nil, ack: tmpAck, args: tmpArgs, data: avatar != nil ? avatar : nil)
  82. }
  83. }