Vernissage/PixelfedKit/Sources/PixelfedKit/Entities/Relationship.swift

113 lines
4.3 KiB
Swift
Raw Normal View History

2023-01-20 16:57:25 +01:00
//
// https://mczachurski.dev
// Copyright © 2022 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-20 16:57:25 +01:00
//
2023-01-10 08:04:25 +01:00
import Foundation
2023-01-22 13:49:19 +01:00
/// Represents the relationship between accounts, such as following / blocking / muting / etc.
2023-01-10 08:04:25 +01:00
public struct Relationship: Codable {
2023-01-22 13:49:19 +01:00
/// The account ID.
public let id: EntityId
/// Are you following this user?
2023-01-10 08:04:25 +01:00
public let following: Bool
2023-01-22 13:49:19 +01:00
/// Are you followed by this user?
2023-01-10 08:04:25 +01:00
public let followedBy: Bool
2023-01-22 13:49:19 +01:00
/// Are you blocking this user?
2023-01-10 08:04:25 +01:00
public let blocking: Bool
2023-01-22 13:49:19 +01:00
/// Is this user blocking you?
2023-01-10 08:04:25 +01:00
public let blockedBy: Bool
2023-01-22 13:49:19 +01:00
/// Are you muting this user?
2023-01-10 08:04:25 +01:00
public let muting: Bool
2023-01-22 13:49:19 +01:00
/// Are you muting notifications from this user?
2023-01-10 08:04:25 +01:00
public let mutingNotifications: Bool
2023-01-22 13:49:19 +01:00
/// Do you have a pending follow request for this user?
2023-01-10 08:04:25 +01:00
public let requested: Bool
2023-01-22 13:49:19 +01:00
/// Are you receiving this users boosts in your home timeline?
2023-01-10 08:04:25 +01:00
public let showingReblogs: Bool
2023-01-22 13:49:19 +01:00
/// Have you enabled notifications for this user?
2023-01-10 08:04:25 +01:00
public let notifying: Bool
2023-01-22 13:49:19 +01:00
/// Are you blocking this users domain?
2023-01-10 08:04:25 +01:00
public let domainBlocking: Bool
2023-01-22 13:49:19 +01:00
/// Are you featuring this user on your profile?
2023-01-10 08:04:25 +01:00
public let endorsed: Bool
2023-01-22 13:49:19 +01:00
/// Which languages are you following from this user? Array of String (ISO 639-1 language two-letter code).
public let languages: [String]?
/// This users profile bio.
public let note: String?
2023-01-10 08:04:25 +01:00
private enum CodingKeys: String, CodingKey {
case id
case following
case followedBy = "followed_by"
case blocking
case blockedBy = "blocked_by"
case muting
case mutingNotifications = "muting_notifications"
case requested
case showingReblogs = "showing_reblogs"
case notifying
case domainBlocking = "domain_blocking"
case endorsed
2023-01-22 13:49:19 +01:00
case languages
case note
2023-01-10 08:04:25 +01:00
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.following = (try? container.decode(Bool.self, forKey: .following)) ?? false
self.followedBy = (try? container.decode(Bool.self, forKey: .followedBy)) ?? false
self.blocking = (try? container.decode(Bool.self, forKey: .blocking)) ?? false
self.blockedBy = (try? container.decode(Bool.self, forKey: .blockedBy)) ?? false
self.muting = (try? container.decode(Bool.self, forKey: .muting)) ?? false
self.mutingNotifications = (try? container.decode(Bool.self, forKey: .mutingNotifications)) ?? false
self.requested = (try? container.decode(Bool.self, forKey: .requested)) ?? false
self.showingReblogs = (try? container.decode(Bool.self, forKey: .showingReblogs)) ?? false
self.notifying = (try? container.decode(Bool.self, forKey: .notifying)) ?? false
self.domainBlocking = (try? container.decode(Bool.self, forKey: .domainBlocking)) ?? false
self.endorsed = (try? container.decode(Bool.self, forKey: .endorsed)) ?? false
2023-01-22 13:49:19 +01:00
self.languages = try? container.decodeIfPresent([String].self, forKey: .languages)
self.note = try? container.decodeIfPresent(String.self, forKey: .note)
2023-01-10 08:04:25 +01:00
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(following, forKey: .following)
try container.encode(followedBy, forKey: .followedBy)
try container.encode(blocking, forKey: .blocking)
try container.encode(blockedBy, forKey: .blockedBy)
try container.encode(muting, forKey: .muting)
try container.encode(mutingNotifications, forKey: .mutingNotifications)
try container.encode(requested, forKey: .requested)
try container.encode(showingReblogs, forKey: .showingReblogs)
try container.encode(notifying, forKey: .notifying)
try container.encode(domainBlocking, forKey: .domainBlocking)
try container.encode(endorsed, forKey: .endorsed)
2023-01-22 13:49:19 +01:00
if let languages {
try container.encode(languages, forKey: .languages)
}
if let note {
try container.encode(note, forKey: .note)
}
2023-01-10 08:04:25 +01:00
}
}