WebUpdateGroupRequest.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 WebUpdateGroupRequest: WebAbstractMessage {
  22. let id: Data
  23. var members: [String]
  24. var name: String?
  25. var avatar: Data?
  26. var deleteName: Bool = false
  27. var deleteAvatar: Bool = false
  28. var groupProxy: GroupProxy?
  29. override init(message:WebAbstractMessage) {
  30. let idString = message.args!["id"] as! String
  31. id = idString.hexadecimal()!
  32. let data = message.data as! [AnyHashable: Any?]
  33. members = data["members"] as! [String]
  34. name = data["name"] as? String
  35. avatar = data["avatar"] as? Data
  36. if data["name"] != nil {
  37. if name == nil {
  38. deleteName = true
  39. }
  40. }
  41. if data["avatar"] != nil {
  42. if avatar == nil {
  43. deleteAvatar = true
  44. } else {
  45. let image = UIImage.init(data: avatar!)
  46. if image!.size.width >= CGFloat(kContactImageSize) || image!.size.height >= CGFloat(kContactImageSize) {
  47. avatar = MediaConverter.scaleImageData(to: avatar!, toMaxSize: CGFloat(kContactImageSize), useJPEG: false)
  48. }
  49. }
  50. }
  51. super.init(message: message)
  52. }
  53. func updateGroup(completion: @escaping () -> ()) {
  54. ack = WebAbstractMessageAcknowledgement.init(requestId, false, nil)
  55. DispatchQueue.main.sync {
  56. let entityManager = EntityManager()
  57. let conversation = entityManager.entityFetcher.conversation(forGroupId: id)
  58. if conversation == nil {
  59. ack!.success = false
  60. ack!.error = "invalidGroup"
  61. completion()
  62. return
  63. }
  64. let groupProxy = GroupProxy.init(for: conversation, entityManager: entityManager)
  65. if groupProxy == nil {
  66. ack!.success = false
  67. ack!.error = "invalidGroup"
  68. completion()
  69. return
  70. }
  71. self.groupProxy = groupProxy
  72. if members.count == 0 {
  73. ack!.success = false
  74. ack!.error = "noMembers"
  75. completion()
  76. return
  77. }
  78. if !groupProxy!.isOwnGroup() {
  79. ack!.success = false
  80. ack!.error = "notAllowed"
  81. completion()
  82. return
  83. }
  84. if self.name != nil {
  85. if self.name!.lengthOfBytes(using: .utf8) > 256 {
  86. self.ack!.success = false
  87. self.ack!.error = "valueTooLong"
  88. completion()
  89. return
  90. }
  91. }
  92. var newMembers = Set<Contact>()
  93. for identity in members {
  94. if let contact = ContactStore.shared().contact(forIdentity: identity) {
  95. newMembers.insert(contact)
  96. }
  97. }
  98. let existingMembers = groupProxy!.members as! Set<Contact>
  99. for member:Contact in existingMembers {
  100. if !newMembers.contains(member) {
  101. groupProxy!.adminRemoveMember(member)
  102. }
  103. }
  104. for member:Contact in newMembers {
  105. if !groupProxy!.members.contains(member) {
  106. groupProxy!.adminAddMember(member)
  107. }
  108. }
  109. if ( self.deleteName || self.name != nil ) && ( !deleteAvatar && avatar == nil ) {
  110. entityManager.performSyncBlockAndSafe({
  111. if self.deleteName || self.name != nil {
  112. conversation?.groupName = self.name
  113. }
  114. })
  115. if self.deleteName || self.name != nil {
  116. DispatchQueue.main.async {
  117. MessageSender.sendGroupRenameMessage(for: conversation, addSystemMessage: true)
  118. }
  119. }
  120. self.ack!.success = true
  121. completion()
  122. return
  123. } else {
  124. let sender = GroupPhotoSender.init()
  125. sender.start(withImageData: self.avatar, in: conversation, toMember: nil, onCompletion: {
  126. entityManager.performSyncBlockAndSafe({
  127. if self.deleteName || self.name != nil {
  128. conversation?.groupName = self.name
  129. }
  130. if conversation?.groupImage != nil {
  131. entityManager.entityDestroyer.deleteObject(object: conversation!.groupImage!)
  132. conversation?.groupImage = nil
  133. }
  134. let dbImage = entityManager.entityCreator.imageData()
  135. dbImage?.data = self.avatar
  136. conversation?.groupImage = dbImage
  137. })
  138. if self.deleteName || self.name != nil {
  139. DispatchQueue.main.async {
  140. MessageSender.sendGroupRenameMessage(for: conversation, addSystemMessage: true)
  141. }
  142. }
  143. self.ack!.success = true
  144. completion()
  145. return
  146. }, onError: { (theError) in
  147. self.ack!.success = false
  148. self.ack!.error = "internalError"
  149. completion()
  150. return
  151. })
  152. }
  153. }
  154. }
  155. }