WebMessageUpdate.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 WebMessagesUpdate: WebAbstractMessage {
  22. enum ObjectMode: String {
  23. case new = "new"
  24. case modified = "modified"
  25. case removed = "removed"
  26. }
  27. var type: String
  28. var id: String
  29. var mode: String
  30. var message: [AnyHashable:Any]
  31. init(_ requestId: String? = nil, baseMessage: BaseMessage, conversation: Conversation, objectMode: ObjectMode, session: WCSession) {
  32. if conversation.isGroup() {
  33. type = "group"
  34. id = conversation.groupId.hexEncodedString()
  35. } else {
  36. type = "contact"
  37. if baseMessage.sender != nil {
  38. id = baseMessage.sender.identity
  39. } else {
  40. if conversation.contact != nil {
  41. id = conversation.contact.identity
  42. }
  43. else {
  44. id = MyIdentityStore.shared().identity
  45. }
  46. }
  47. }
  48. mode = objectMode.rawValue
  49. if objectMode == .removed {
  50. let messageObject = WebMessageObject.init(message: baseMessage, conversation:conversation)
  51. message = messageObject.removedObjectDict()
  52. } else {
  53. let messageObject = WebMessageObject.init(message: baseMessage, conversation: conversation, forConversationsRequest: false, session: session)
  54. message = messageObject.objectDict()
  55. }
  56. let tmpArgs:[AnyHashable:Any?] = ["type": type, "id": id, "mode": mode]
  57. let tmpAck = requestId != nil ? WebAbstractMessageAcknowledgement.init(requestId, true, nil) : nil
  58. super.init(messageType: "update", messageSubType: "messages", requestId: nil, ack: tmpAck, args: tmpArgs, data: [message])
  59. }
  60. }