WebCreateGroupResponse.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 WebCreateGroupResponse: WebAbstractMessage {
  23. var groupRequest: WebCreateGroupRequest
  24. init(request: WebCreateGroupRequest) {
  25. groupRequest = request
  26. let tmpAck = WebAbstractMessageAcknowledgement.init(request.requestId, true, nil)
  27. super.init(messageType: "create", messageSubType: "group", requestId: nil, ack: tmpAck, args: nil, data: nil)
  28. }
  29. func addGroup(completion: @escaping () -> ()) {
  30. let mdmSetup = MDMSetup(setup: false)!
  31. if mdmSetup.disableCreateGroup() {
  32. self.ack!.success = false
  33. self.ack!.error = "disabledByPolicy"
  34. self.args = nil
  35. self.data = nil
  36. completion()
  37. return
  38. }
  39. var conversation: Conversation?
  40. var entityManager: EntityManager?
  41. DispatchQueue.main.sync {
  42. entityManager = EntityManager()
  43. entityManager?.performSyncBlockAndSafe({
  44. conversation = entityManager?.entityCreator.conversation()
  45. conversation?.groupId = NaClCrypto.shared().randomBytes(kGroupIdLen)
  46. conversation?.groupMyIdentity = MyIdentityStore.shared().identity
  47. conversation?.groupName = self.groupRequest.name
  48. for identity in self.groupRequest.members {
  49. let contact = ContactStore.shared().contact(forIdentity: identity)
  50. conversation?.addMembersObject(contact)
  51. }
  52. if self.groupRequest.avatar != nil {
  53. let dbImage = entityManager?.entityCreator.imageData()
  54. dbImage?.data = self.groupRequest.avatar
  55. conversation?.groupImage = dbImage
  56. }
  57. })
  58. }
  59. var groupProxy: GroupProxy? = nil
  60. /* send group create messages to all members */
  61. if (conversation?.isGroup())! {
  62. DispatchQueue.main.sync {
  63. groupProxy = GroupProxy.init(for: conversation!, entityManager: entityManager)
  64. }
  65. groupProxy?.syncGroupInfoToAll()
  66. }
  67. if groupRequest.name != nil {
  68. MessageSender.sendGroupRenameMessage(for: conversation, addSystemMessage: true)
  69. }
  70. if groupRequest.avatar != nil {
  71. sendGroupPhotoMessage(conversation: conversation!)
  72. }
  73. self.ack!.success = true
  74. self.args = nil
  75. let webGroup = WebGroup.init(group: groupProxy!)
  76. self.data = ["receiver": webGroup.objectDict()]
  77. completion()
  78. return
  79. }
  80. func sendGroupPhotoMessage(conversation: Conversation) {
  81. let sender = GroupPhotoSender.init()
  82. sender.start(withImageData: groupRequest.avatar, in: conversation, toMember: nil, onCompletion: {
  83. // do nothing
  84. }) { (theError) in
  85. // do nothing
  86. }
  87. }
  88. }