97 lines
3.5 KiB
Swift
97 lines
3.5 KiB
Swift
import Foundation
|
|
|
|
struct ProfileDataPayload: Decodable {
|
|
let userId: UUID
|
|
let login: String
|
|
let fullName: String?
|
|
let bio: String?
|
|
let balances: [WalletBalancePayload]
|
|
let createdAt: Date?
|
|
let stories: [JSONValue]
|
|
let profilePermissions: ProfilePermissionsPayload
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case userId
|
|
case login
|
|
case fullName
|
|
case bio
|
|
case balances
|
|
case createdAt
|
|
case stories
|
|
case profilePermissions
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
self.userId = try container.decode(UUID.self, forKey: .userId)
|
|
self.login = try container.decode(String.self, forKey: .login)
|
|
self.fullName = try container.decodeIfPresent(String.self, forKey: .fullName)
|
|
self.bio = try container.decodeIfPresent(String.self, forKey: .bio)
|
|
self.balances = try container.decodeIfPresent([WalletBalancePayload].self, forKey: .balances) ?? []
|
|
self.createdAt = try container.decodeIfPresent(Date.self, forKey: .createdAt)
|
|
self.stories = try container.decodeIfPresent([JSONValue].self, forKey: .stories) ?? []
|
|
self.profilePermissions = try container.decode(ProfilePermissionsPayload.self, forKey: .profilePermissions)
|
|
}
|
|
}
|
|
|
|
struct WalletBalancePayload: Decodable {
|
|
let currency: String
|
|
let balance: Decimal
|
|
let displayBalance: Double?
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case currency
|
|
case balance
|
|
case displayBalance
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
self.currency = try container.decode(String.self, forKey: .currency)
|
|
self.balance = try Self.decodeDecimal(from: container, forKey: .balance)
|
|
if let doubleValue = try? container.decode(Double.self, forKey: .displayBalance) {
|
|
self.displayBalance = doubleValue
|
|
} else if let stringValue = try? container.decode(String.self, forKey: .displayBalance),
|
|
let doubleValue = Double(stringValue) {
|
|
self.displayBalance = doubleValue
|
|
} else {
|
|
self.displayBalance = nil
|
|
}
|
|
}
|
|
|
|
private static func decodeDecimal(from container: KeyedDecodingContainer<CodingKeys>, forKey key: CodingKeys) throws -> Decimal {
|
|
if let decimalValue = try? container.decode(Decimal.self, forKey: key) {
|
|
return decimalValue
|
|
}
|
|
if let stringValue = try? container.decode(String.self, forKey: key),
|
|
let decimal = Decimal(string: stringValue) {
|
|
return decimal
|
|
}
|
|
if let doubleValue = try? container.decode(Double.self, forKey: key) {
|
|
return Decimal(doubleValue)
|
|
}
|
|
throw DecodingError.dataCorruptedError(
|
|
forKey: key,
|
|
in: container,
|
|
debugDescription: "Unable to decode decimal value for key \(key)"
|
|
)
|
|
}
|
|
}
|
|
|
|
struct ProfilePermissionsPayload: Decodable {
|
|
let isSearchable: Bool
|
|
let allowMessageForwarding: Bool
|
|
let allowMessagesFromNonContacts: Bool
|
|
let showProfilePhotoToNonContacts: Bool
|
|
let lastSeenVisibility: Int
|
|
let showBioToNonContacts: Bool
|
|
let showStoriesToNonContacts: Bool
|
|
let allowServerChats: Bool
|
|
let publicInvitePermission: Int
|
|
let groupInvitePermission: Int
|
|
let callPermission: Int
|
|
let forceAutoDeleteMessagesInPrivate: Bool
|
|
let maxMessageAutoDeleteSeconds: Int?
|
|
let autoDeleteAfterDays: Int?
|
|
}
|