SafeJsonParser.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 SafeJsonParser {
  22. struct SafeBackupData : Codable {
  23. let info: Info
  24. struct Info : Codable {
  25. let version: Int
  26. let device: String?
  27. }
  28. var user: User?
  29. var contacts: [Contact]?
  30. var groups: [Group]?
  31. var settings: Settings?
  32. class User : Codable {
  33. var privatekey: String
  34. var nickname: String?
  35. var profilePic: String?
  36. var profilePicRelease: [String?]?
  37. var links: [Link]?
  38. init(privatekey: String) {
  39. self.privatekey = privatekey
  40. }
  41. struct Link : Codable {
  42. let type: String?
  43. let name: String? = ""
  44. let value: String?
  45. }
  46. }
  47. class Contact : Codable {
  48. var identity: String?
  49. var publickey: String?
  50. var createdAt: UInt64? = 0
  51. var verification: Int? = 0
  52. var workVerified: Bool? = false
  53. var hidden: Bool? = false
  54. var firstname: String? = ""
  55. var lastname: String? = ""
  56. var nickname: String? = ""
  57. init(identity: String, verification: Int) {
  58. self.identity = identity
  59. self.verification = verification
  60. }
  61. }
  62. struct Group : Codable {
  63. let id: String?
  64. let creator: String?
  65. let groupname: String?
  66. let createdAt: UInt64? = 0
  67. let members: [String]?
  68. let deleted: Bool?
  69. }
  70. class Settings : Codable {
  71. var syncContacts: Bool = false
  72. var blockUnknown: Bool? = false
  73. var readReceipts: Bool? = true
  74. var sendTyping: Bool? = true
  75. var threemaCalls: Bool? = true
  76. var relayThreemaCalls: Bool? = false
  77. var blockedContacts: [String]?
  78. var syncExcludedIds: [String]?
  79. }
  80. }
  81. struct SafeServerConfig : Codable {
  82. let maxBackupBytes: Int
  83. let retentionDays: Int
  84. }
  85. func getSafeBackupData() -> SafeBackupData {
  86. return SafeBackupData(info: SafeBackupData.Info(version: 1, device: "ios"), user: nil, contacts: nil, groups: nil, settings: nil)
  87. }
  88. func getSafeBackupData(from: Data) throws -> SafeBackupData {
  89. let decoder = JSONDecoder()
  90. decoder.dataDecodingStrategy = .deferredToData
  91. decoder.keyDecodingStrategy = .useDefaultKeys
  92. return try decoder.decode(SafeBackupData.self, from: from)
  93. }
  94. func getJsonAsBytes(from: SafeBackupData) -> [UInt8]? {
  95. let encoder = JSONEncoder()
  96. if let data = try? encoder.encode(from) {
  97. return data.withUnsafeBytes {
  98. return Array($0)
  99. }
  100. }
  101. return nil
  102. }
  103. func getJsonAsString(from: SafeBackupData) -> String? {
  104. let encoder = JSONEncoder()
  105. if let data = try? encoder.encode(from) {
  106. return String(data: data, encoding: .utf8)!
  107. }
  108. return nil
  109. }
  110. func getSafeServerConfig(from: Data) -> SafeServerConfig? {
  111. let decoder = JSONDecoder()
  112. return try? decoder.decode(SafeServerConfig.self, from: from)
  113. }
  114. }