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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
}
}