Impressia/Vernissage/Models/RelationshipModel.swift

153 lines
4.6 KiB
Swift
Raw Normal View History

2023-03-27 14:52:53 +02:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-03-27 14:52:53 +02:00
//
import SwiftUI
import Foundation
import PixelfedKit
2023-10-19 13:24:02 +02:00
@Observable public class RelationshipModel {
2023-04-25 15:47:21 +02:00
enum RelationshipAction {
case follow
case unfollow
case requestFollow
case cancelRequestFollow
}
2023-03-27 14:52:53 +02:00
/// The account ID.
2023-10-19 13:24:02 +02:00
public var id: EntityId
2023-03-27 14:52:53 +02:00
/// Are you followed by this user?
2023-10-19 13:24:02 +02:00
public var followedBy: Bool
2023-03-27 14:52:53 +02:00
/// Is this user blocking you?
2023-10-19 13:24:02 +02:00
public var blockedBy: Bool
2023-03-27 14:52:53 +02:00
/// Are you muting notifications from this user?
2023-10-19 13:24:02 +02:00
public var mutingNotifications: Bool
2023-03-27 14:52:53 +02:00
/// Do you have a pending follow request for this user?
2023-10-19 13:24:02 +02:00
public var requested: Bool
2023-03-27 14:52:53 +02:00
/// Are you receiving this users boosts in your home timeline?
2023-10-19 13:24:02 +02:00
public var showingReblogs: Bool
2023-03-27 14:52:53 +02:00
/// Have you enabled notifications for this user?
2023-10-19 13:24:02 +02:00
public var notifying: Bool
2023-03-27 14:52:53 +02:00
/// Are you blocking this users domain?
2023-10-19 13:24:02 +02:00
public var domainBlocking: Bool
2023-03-27 14:52:53 +02:00
/// Are you featuring this user on your profile?
2023-10-19 13:24:02 +02:00
public var endorsed: Bool
2023-03-27 14:52:53 +02:00
/// Which languages are you following from this user? Array of String (ISO 639-1 language two-letter code).
2023-10-19 13:24:02 +02:00
public var languages: [String]?
2023-03-27 14:52:53 +02:00
/// This users profile bio.
2023-10-19 13:24:02 +02:00
public var note: String?
2023-03-27 14:52:53 +02:00
/// Are you following this user?
2023-10-19 13:24:02 +02:00
public var following: Bool
2023-03-27 14:52:53 +02:00
/// Are you blocking this user?
2023-10-19 13:24:02 +02:00
public var blocking: Bool
2023-03-27 14:52:53 +02:00
/// Are you muting this user?
2023-10-19 13:24:02 +02:00
public var muting: Bool
2023-03-27 14:52:53 +02:00
public init() {
self.id = ""
self.following = false
self.followedBy = false
self.blocking = false
self.blockedBy = false
self.muting = false
self.mutingNotifications = false
self.requested = false
self.showingReblogs = false
self.notifying = false
self.domainBlocking = false
self.endorsed = false
self.note = nil
self.languages = []
}
2023-03-27 14:52:53 +02:00
public init(relationship: Relationship) {
self.id = relationship.id
self.following = relationship.following
self.followedBy = relationship.followedBy
self.blocking = relationship.blocking
self.blockedBy = relationship.blockedBy
self.muting = relationship.muting
self.mutingNotifications = relationship.mutingNotifications
self.requested = relationship.requested
self.showingReblogs = relationship.showingReblogs
self.notifying = relationship.notifying
self.domainBlocking = relationship.domainBlocking
self.endorsed = relationship.endorsed
self.languages = relationship.languages
self.note = relationship.note
}
}
extension RelationshipModel {
public func update(relationship: Relationship) {
self.id = relationship.id
self.following = relationship.following
self.followedBy = relationship.followedBy
self.blocking = relationship.blocking
self.blockedBy = relationship.blockedBy
self.muting = relationship.muting
self.mutingNotifications = relationship.mutingNotifications
self.requested = relationship.requested
self.showingReblogs = relationship.showingReblogs
self.notifying = relationship.notifying
self.domainBlocking = relationship.domainBlocking
self.endorsed = relationship.endorsed
self.languages = relationship.languages
self.note = relationship.note
}
2023-03-27 14:52:53 +02:00
public func update(relationship: RelationshipModel) {
self.id = relationship.id
self.following = relationship.following
self.followedBy = relationship.followedBy
self.blocking = relationship.blocking
self.blockedBy = relationship.blockedBy
self.muting = relationship.muting
self.mutingNotifications = relationship.mutingNotifications
self.requested = relationship.requested
self.showingReblogs = relationship.showingReblogs
self.notifying = relationship.notifying
self.domainBlocking = relationship.domainBlocking
self.endorsed = relationship.endorsed
self.languages = relationship.languages
self.note = relationship.note
}
}
2023-04-25 15:47:21 +02:00
extension RelationshipModel {
func haveAccessToPhotos(account: Account) -> Bool {
return !account.locked || (account.locked && self.following)
}
func getRelationshipAction(account: Account) -> RelationshipAction {
if self.following {
return .unfollow
}
if self.requested {
return .cancelRequestFollow
}
if account.locked {
return .requestFollow
}
return .follow
}
}