1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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)
}