WebConversationUpdate.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 WebConversationUpdate: WebAbstractMessage {
  22. enum ObjectMode: String {
  23. case new = "new"
  24. case modified = "modified"
  25. case removed = "removed"
  26. }
  27. var mode: String
  28. init(conversation: Conversation, objectMode: ObjectMode, session: WCSession) {
  29. mode = objectMode.rawValue
  30. let entityManager = EntityManager()
  31. let allConversations = entityManager.entityFetcher.allConversationsSorted() as? [Conversation]
  32. var index:Int = 0
  33. var found: Bool = false
  34. for conver in allConversations! {
  35. if conver.groupId != nil || conversation.groupId != nil {
  36. if conver.groupId == conversation.groupId {
  37. found = true
  38. }
  39. } else {
  40. if conver.contact != nil && conversation.contact != nil {
  41. if conver.contact.identity == conversation.contact.identity {
  42. found = true
  43. }
  44. }
  45. }
  46. if found == false {
  47. index = index + 1
  48. }
  49. }
  50. let webConversation = WebConversation.init(conversation: conversation, index: index, request: nil, addAvatar: true, entityManager: entityManager, session: session)
  51. let tmpArgs:[AnyHashable:Any?] = ["mode": mode]
  52. let tmpData:[AnyHashable:Any?] = ["type": webConversation.type, "id": webConversation.id, "position": webConversation.position, "messageCount": webConversation.messageCount, "unreadCount": webConversation.unreadCount, "latestMessage": webConversation.latestMessage, "notifications": webConversation.notifications?.objectDict(), "isStarred": webConversation.isStarred]
  53. super.init(messageType: "update", messageSubType: "conversation", requestId: nil, ack: nil, args: tmpArgs, data: tmpData)
  54. }
  55. init(conversation: Conversation, contact: Contact?, objectMode: ObjectMode) {
  56. mode = objectMode.rawValue
  57. let webConversation = WebConversation.init(deletedConversation: conversation, contact: contact)
  58. let tmpArgs:[AnyHashable:Any?] = ["mode": mode]
  59. let tmpData:[AnyHashable:Any?] = ["type": webConversation.type, "id": webConversation.id, "position": webConversation.position, "messageCount": webConversation.messageCount, "unreadCount": webConversation.unreadCount]
  60. super.init(messageType: "update", messageSubType: "conversation", requestId: nil, ack: nil, args: tmpArgs, data: tmpData)
  61. }
  62. }