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

geesawra.industries/old-fella

Old people's social media app

0
1
main
118 lines · 5.4 KB Raw History
import Foundation
import SwiftUI

extension AppModel {
    var activeModerationPreferences: ModerationPolicy.Preferences? {
        guard !isSigningOut, let account, let preferences = contentPreferences, preferences.userDID == account.id else {
            return nil
        }
        return preferences
    }

    private var canReadPreparedModeration: Bool {
        preparedDecisionInputs != nil || preparedContentUpdateDepth == 0
    }

    func contentDecision(
        author: Post.Author, text: String = "", labels: [ModerationPolicy.Label] = []
    ) -> ModerationPolicy.Decision? {
        _ = moderationRevision
        let isAuthorOnly = text.isEmpty && labels.isEmpty
        if isAuthorOnly, canReadPreparedModeration,
            let cached = preparedAuthorDecisions[author.id], cached.author == author {
            return cached.decision
        }
        let result = resolveModerationInput(ModerationInput(author: author, text: text, labels: labels))
        if isAuthorOnly, preparedDecisionInputs != nil { preparedAuthorDecisions[author.id] = (author, result) }
        return result
    }

    private func resolveModerationInput(_ source: ModerationInput) -> ModerationPolicy.Decision? {
        guard let preferences = activeModerationPreferences else { return nil }
        let author = source.author
        let input = source.policyPost(
            isFollowing: followResults[author.id] ?? (author.isFollowing || followingActorIDs.contains(author.id)))
        moderationInputConstructionCount += 1
        preparedDecisionInputs?.insert(input)
        if let decision = moderationDecisions[input] { return decision }
        let date = now()
        if preparedDecisionInputs == nil {
            let expiry =
                (preferences.mutedWords.compactMap(\.expiresAt) + author.moderationLabels.compactMap(\.expiresAt)
                + source.labels.compactMap(\.expiresAt)).filter { $0 > date }.min()
            if let expiry {
                moderationExpiryCandidates[input] = expiry
                if expiry < (moderationExpiry ?? .distantFuture) { scheduleModerationExpiry(expiry) }
            }
        }
        let decision = ModerationPolicy.evaluate(post: input, preferences: preferences, now: date)
        moderationEvaluationCount += 1
        moderationDecisions[input] = decision
        return decision
    }

    func contentDecision(for post: Post, stagedPosts: [String: Post] = [:]) -> ModerationPolicy.Decision? {
        _ = moderationRevision
        let fallback = post.quote.flatMap { quote in
            quote.author == nil ? (stagedPosts[quote.postID] ?? self.post(id: quote.postID)) : nil
        }
        let readsCache = stagedPosts.isEmpty && canReadPreparedModeration
        if readsCache, let cached = preparedPostDecisions[post.id], cached.post == post { return cached.decision }
        var result: ModerationPolicy.Decision?
        defer {
            if stagedPosts.isEmpty, preparedDecisionInputs != nil { preparedPostDecisions[post.id] = (post, result) }
        }
        guard var decision = resolveModerationInput(.post(post)) else { return nil }
        if let quote = post.quote, let quoteDecision = quoteContentDecision(quote, fallbackPost: fallback) {
            let mutedWords = quoteDecision.reasons.filter {
                if case .mutedWord = $0 { return true }
                return false
            }
            if !mutedWords.isEmpty {
                decision.action = .hide
                decision.contentList.filter = true
                decision.reasons.append(contentsOf: mutedWords)
            }
        }
        result = decision
        return decision
    }

    func quoteContentDecision(_ quote: Post.Quote, fallbackPost: Post?) -> ModerationPolicy.Decision? {
        _ = moderationRevision
        let fallback = quote.author == nil ? fallbackPost : nil
        if canReadPreparedModeration, let cached = preparedQuoteDecisions[quote.postID],
            cached.quote == quote, cached.fallback == fallback {
            return cached.decision
        }
        let input = quote.author.map { ModerationInput.quote(quote, author: $0) }
            ?? fallback.map(ModerationInput.quotedPost)
        let result = input.flatMap(resolveModerationInput)
        if preparedDecisionInputs != nil {
            preparedQuoteDecisions[quote.postID] = PreparedQuoteDecision(
                quote: quote, fallback: fallback, decision: result)
        }
        return result
    }

    func isRepostAuthorVisible(_ author: Post.Author?) -> Bool {
        guard let author else { return true }
        return !isHidden(author.id) && contentDecision(author: author)?.action == .allow
    }

    func isAccountSummaryVisible(_ author: Post.Author) -> Bool {
        guard let decision = contentDecision(author: author) else { return false }
        return !isHidden(author.id) && decision.action != .hide && !decision.ui(.profileView).blur
    }

    func isHidden(_ actorID: String?) -> Bool {
        guard let actorID, let state = moderationStates[actorID] else { return false }
        return state.isMuted || state.isBlocked
    }

    func isQuoteHidden(_ quote: Post.Quote, fallbackPost: Post?) -> Bool {
        if isHidden(quote.author?.id) || (quote.author == nil && isHidden(fallbackPost?.author.id)) { return true }
        guard let decision = quoteContentDecision(quote, fallbackPost: fallbackPost) else { return true }
        return decision.action == .hide
    }
}