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

geesawra.industries/old-fella

Old people's social media app

0
1
main
90 lines · 3.7 KB Raw History
import Foundation

struct FeedModerationSnapshot: Sendable {
    let date: Date
    let preferences: ModerationPolicy.Preferences?
    let followResults: [String: Bool]
    let followingActorIDs: Set<String>
    let decisions: [ModerationPolicy.Post: ModerationPolicy.Decision]
    let expiry: Date?

    struct Batch: Sendable {
        var decisions: [ModerationPolicy.Post: ModerationPolicy.Decision] = [:]
        var expiry: Date?
        var evaluationCount = 0
    }

    func prepare(_ page: FeedPage) -> Batch {
        var batch = Batch(expiry: expiry)
        guard let preferences else { return batch }
        func add(_ input: ModerationInput) {
            let author = input.author
            let isFollowing = followResults[author.id] ?? (author.isFollowing || followingActorIDs.contains(author.id))
            let post = input.policyPost(isFollowing: isFollowing)
            guard batch.decisions[post] == nil else { return }
            for label in input.labels + author.moderationLabels {
                if let expiresAt = label.expiresAt, expiresAt > date {
                    batch.expiry = min(batch.expiry ?? expiresAt, expiresAt)
                }
            }
            if let known = decisions[post] {
                batch.decisions[post] = known
            } else {
                batch.decisions[post] = ModerationPolicy.evaluate(post: post, preferences: preferences, now: date)
                batch.evaluationCount += 1
            }
        }

        for author in page.repostedBy.values { add(ModerationInput(author: author)) }
        for post in page.posts + page.replyContexts.values.flatMap({ $0 }) {
            add(.post(post))
            if !post.pendingImages.isEmpty { add(.quotedPost(post)) }
            add(ModerationInput(author: post.author))
            if let quote = post.quote, let author = quote.author {
                add(.quote(quote, author: author))
                add(ModerationInput(author: author))
            }
        }
        return batch
    }
}

extension AppModel {
    func prepareFeedPage(_ page: FeedPage, isCurrent: () -> Bool) async throws {
        var text: PreparedPostTextCache.Batch?
        while isCurrent() {
            refreshExpiredModeration()
            let revision = moderationRevision
            let date = now()
            let snapshot = FeedModerationSnapshot(
                date: date, preferences: activeModerationPreferences, followResults: followResults,
                followingActorIDs: followingActorIDs, decisions: moderationDecisions, expiry: moderationExpiry)
            let needsText = text == nil
            let result = await prepareIncomingFeedPage(page, snapshot: snapshot, needsText: needsText)
            try requireCurrentFeedRequest(isCurrent)
            if let preparedText = result.text { text = preparedText }
            let currentDate = now()
            guard moderationRevision == revision, currentDate >= date,
                result.moderation.expiry.map({ currentDate < $0 }) ?? true
            else { continue }

            moderationDecisions.merge(result.moderation.decisions) { current, _ in current }
            moderationEvaluationCount += result.moderation.evaluationCount
            if let text { preparedPostText.insert(text) }
            return
        }
        throw CancellationError()
    }

    func requireCurrentFeedRequest(_ isCurrent: () -> Bool) throws {
        guard isCurrent() else { throw CancellationError() }
    }
}

@concurrent
private func prepareIncomingFeedPage(
    _ page: FeedPage, snapshot: FeedModerationSnapshot, needsText: Bool
) async -> (moderation: FeedModerationSnapshot.Batch, text: PreparedPostTextCache.Batch?) {
    let text = needsText ? PreparedPostTextCache.prepare(page: page) : nil
    return (snapshot.prepare(page), text)
}