~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
106 lines · 3.3 KB Raw History
import Foundation

struct NotificationPage: Equatable {
    let notifications: [FeedNotification]
    let cursor: String?
}

struct FeedNotification: Identifiable, Equatable {
    enum Reason: String, CaseIterable, Hashable {
        case like
        case repost
        case follow
        case mention
        case reply
        case quote
        case unknown

        var filterLabel: String {
            switch self {
            case .like: "Likes"
            case .repost: "Reposts"
            case .follow: "Follows"
            case .mention: "Mentions"
            case .reply: "Replies"
            case .quote: "Quotes"
            case .unknown: "Other"
            }
        }

        var message: String {
            switch self {
            case .like: "liked your post"
            case .repost: "reposted your post"
            case .follow: "followed you"
            case .mention: "mentioned you"
            case .reply: "replied to your post"
            case .quote: "quoted your post"
            case .unknown: "interacted with you"
            }
        }

        var includesPost: Bool {
            switch self {
            case .like, .repost, .mention, .reply, .quote: true
            case .follow, .unknown: false
            }
        }

        var symbol: String {
            switch self {
            case .like: "heart.fill"
            case .repost: "arrow.2.squarepath"
            case .follow: "person.badge.plus"
            case .mention, .reply: "bubble.left"
            case .quote: "quote.bubble"
            case .unknown: "bell"
            }
        }
    }

    let id: String
    let author: Post.Author
    let reason: Reason
    let indexedAt: Date
    var isRead: Bool
    let postID: String?
    var relatedPost: Post?
    var parentPost: Post?
    var isFollowedBy: Bool = false

    func replyIsToAccount(_ accountID: String?) -> Bool {
        guard let accountID, let parentPost else { return false }
        return parentPost.author.id == accountID
    }
}

struct NotificationFilter {
    var mutualsOnly: Bool
    var excludedTypes: Set<FeedNotification.Reason>

    init(mutualsOnly: Bool, excludedTypesRaw: String) {
        self.mutualsOnly = mutualsOnly
        let excluded = Set(
            excludedTypesRaw.split(separator: ",").compactMap { FeedNotification.Reason(rawValue: String($0)) })
        self.excludedTypes = excluded.count == FeedNotification.Reason.allCases.count ? [] : excluded
    }

    var isActive: Bool { mutualsOnly || !excludedTypes.isEmpty }
    var showsAllTypes: Bool { excludedTypes.isEmpty }

    func typeIsSelected(_ reason: FeedNotification.Reason) -> Bool {
        !showsAllTypes && !excludedTypes.contains(reason)
    }

    func settingType(_ reason: FeedNotification.Reason, enabled: Bool) -> String {
        let all = Set(FeedNotification.Reason.allCases)
        var excluded = showsAllTypes && enabled ? all : excludedTypes
        if enabled { excluded.remove(reason) } else { excluded.insert(reason) }
        guard excluded.count < all.count else { return excludedTypes.map(\.rawValue).sorted().joined(separator: ",") }
        return excluded.map(\.rawValue).sorted().joined(separator: ",")
    }

    func includes(_ notification: FeedNotification, follows: Bool) -> Bool {
        !excludedTypes.contains(notification.reason) && (!mutualsOnly || (follows && notification.isFollowedBy))
    }
}