WebDeleteGroupRequest.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 WebDeleteGroupRequest: WebAbstractMessage {
  23. var id: Data? = nil
  24. let deleteType: String
  25. override init(message:WebAbstractMessage) {
  26. let idString = message.args!["id"] as! String
  27. id = idString.hexadecimal()
  28. deleteType = message.args!["deleteType"] as! String
  29. super.init(message: message)
  30. }
  31. func deleteOrLeave() {
  32. ack = WebAbstractMessageAcknowledgement.init(requestId, false, nil)
  33. let entityManager = EntityManager()
  34. let conversation = entityManager.entityFetcher.conversation(forGroupId: id)
  35. if conversation == nil {
  36. ack!.success = false
  37. ack!.error = "invalidGroup"
  38. return;
  39. }
  40. if conversation!.isGroup() {
  41. let groupProxy = GroupProxy.init(for: conversation, entityManager: entityManager)
  42. if groupProxy == nil {
  43. ack!.success = false
  44. ack!.error = "invalidGroup"
  45. return;
  46. }
  47. if groupProxy!.didLeaveGroup() {
  48. ack!.success = false
  49. ack!.error = "alreadyLeft"
  50. return;
  51. }
  52. if groupProxy!.isOwnGroup() && deleteType == "delete" {
  53. groupProxy?.adminDeleteGroup()
  54. }
  55. MessageDraftStore.deleteDraft(for: conversation)
  56. ack!.success = true
  57. if deleteType == "delete" {
  58. entityManager.performSyncBlockAndSafe({
  59. entityManager.entityDestroyer.deleteObject(object: conversation!)
  60. })
  61. DispatchQueue.main.async {
  62. NotificationManager.sharedInstance().updateUnreadMessagesCount(false)
  63. let info = [kKeyConversation: conversation!]
  64. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNotificationDeletedConversation), object: nil, userInfo: info)
  65. }
  66. }
  67. else if deleteType == "leave" {
  68. groupProxy?.leaveGroup()
  69. }
  70. else {
  71. ack!.success = false
  72. ack!.error = "badRequest"
  73. return;
  74. }
  75. } else {
  76. ack!.success = false
  77. ack!.error = "invalidGroup"
  78. return;
  79. }
  80. }
  81. }