ThreemaPushNotification.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 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. /// Errors for `ThreemaPushNotification` parsing
  22. enum ThreemaPushNotificationError: Error, Equatable {
  23. case unknownCommand(String)
  24. case keyNotFoundOrTypeMissmatch(ThreemaPushNotificationDictionary)
  25. }
  26. /// Represents the payload in a push notifcation keyed with "threema"
  27. public class ThreemaPushNotification: NSObject {
  28. /// A command that is part of of the push notification payload
  29. enum Command: String, Codable {
  30. /// A new message in a chat with a single person
  31. case newMessage = "newmsg"
  32. /// A new message in a group chat
  33. case newGroupMessage = "newgroupmsg"
  34. /// Creates a new instance depending on the string
  35. /// - Parameter string: String representing a command
  36. /// - Throws: `ThreemaPushNotificationError` if command string is unknown
  37. init(from string: String) throws {
  38. switch string {
  39. case ThreemaPushNotificationDictionary.Command.newMessage.rawValue:
  40. self = .newMessage
  41. case ThreemaPushNotificationDictionary.Command.newGroupMessage.rawValue:
  42. self = .newGroupMessage
  43. default:
  44. throw ThreemaPushNotificationError.unknownCommand(string)
  45. }
  46. }
  47. }
  48. let command: Command
  49. /// Message sender
  50. ///
  51. /// Needed to open the correct chat
  52. let from: String
  53. /// Nickname set by sender (for themself)
  54. let nickname: String?
  55. let messageId: String
  56. /// Indicates if the push notification is related to an incoming voip call
  57. let voip: Bool?
  58. /// Parse an incoming push payload dictionary
  59. /// - Parameter dictionary: Dictionary to parse
  60. /// - Throws: `ThreemaPushNotificationError` if a required key is missing or a value cannot be parsed
  61. init(from dictionary: [String: Any]) throws {
  62. let commandString = try ThreemaPushNotification.decode(String.self, forKey: .commandKey, in: dictionary)
  63. command = try Command(from: commandString)
  64. from = try ThreemaPushNotification.decode(String.self, forKey: .fromKey, in: dictionary)
  65. nickname = try? ThreemaPushNotification.decode(String.self, forKey: .nicknameKey, in: dictionary)
  66. messageId = try ThreemaPushNotification.decode(String.self, forKey: .messageIdKey, in: dictionary)
  67. // For backwards compatiblity the voip key is also a string,
  68. // but in the future it could be a bool
  69. if let voipString = try? ThreemaPushNotification.decode(String.self, forKey: .voipKey, in: dictionary) {
  70. if voipString == ThreemaPushNotificationDictionary.Bool.true.rawValue {
  71. voip = true
  72. } else if voipString == ThreemaPushNotificationDictionary.Bool.false.rawValue {
  73. voip = false
  74. } else {
  75. voip = nil
  76. }
  77. } else {
  78. voip = try? ThreemaPushNotification.decode(Bool.self, forKey: .voipKey, in: dictionary)
  79. }
  80. }
  81. private static func decode<T>(_ type: T.Type, forKey key: ThreemaPushNotificationDictionary, in dictionary: [String: Any]) throws -> T {
  82. if let decodedValue = dictionary[key.rawValue] as? T {
  83. return decodedValue
  84. } else {
  85. throw ThreemaPushNotificationError.keyNotFoundOrTypeMissmatch(key)
  86. }
  87. }
  88. /// Initalizer for `NSCoding`
  89. /// - Parameter coder: Coder to decode from
  90. public required init?(coder: NSCoder) {
  91. guard let commandString = coder.decodeObject(forKey: ThreemaPushNotificationDictionary.commandKey.rawValue) as? String,
  92. let command = Command(rawValue: commandString) else {
  93. return nil
  94. }
  95. self.command = command
  96. guard let from = coder.decodeObject(forKey: ThreemaPushNotificationDictionary.fromKey.rawValue) as? String else {
  97. return nil
  98. }
  99. self.from = from
  100. self.nickname = coder.decodeObject(forKey: ThreemaPushNotificationDictionary.nicknameKey.rawValue) as? String
  101. guard let messageId = coder.decodeObject(forKey: ThreemaPushNotificationDictionary.messageIdKey.rawValue) as? String else {
  102. return nil
  103. }
  104. self.messageId = messageId
  105. self.voip = coder.decodeObject(forKey: ThreemaPushNotificationDictionary.voipKey.rawValue) as? Bool
  106. }
  107. }
  108. // MARK: - NSCoding
  109. extension ThreemaPushNotification: NSCoding {
  110. // We still need to use NSCoding (instead of Codable), because there are dependencies
  111. // used in `PendingMessage` that are in Obj-C.
  112. // When we can remove this, `ThreemaPushNotification` can become a struct.
  113. public func encode(with coder: NSCoder) {
  114. coder.encode(command.rawValue, forKey: ThreemaPushNotificationDictionary.commandKey.rawValue)
  115. coder.encode(from, forKey: ThreemaPushNotificationDictionary.fromKey.rawValue)
  116. coder.encode(nickname, forKey: ThreemaPushNotificationDictionary.nicknameKey.rawValue)
  117. coder.encode(messageId, forKey: ThreemaPushNotificationDictionary.messageIdKey.rawValue)
  118. coder.encode(voip, forKey: ThreemaPushNotificationDictionary.voipKey.rawValue)
  119. }
  120. }