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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import Foundation
/// A network-independent moderation decision for a post in a content list.
enum ModerationPolicy {
static let defaultLabelerDID = "did:plc:ar7c4by46qjdydhdevvrndac"
enum Action: Int, Comparable, Sendable {
case allow, warn, hide
static func < (lhs: Self, rhs: Self) -> Bool { lhs.rawValue < rhs.rawValue }
}
enum LabelSetting: Equatable, Sendable {
case ignore, warn, hide
}
struct MutedWord: Equatable, Sendable {
var value: String
var targets: Set<Target> = [.content, .tag]
var excludeFollowing = false
var expiresAt: Date?
}
struct Label: Hashable, Sendable {
var value: String
var sourceDID: String
var expiresAt: Date?
var isNegated = false
}
enum WarningBehavior: String, Sendable { case blur, blurMedia, alert, none }
enum Severity: String, Sendable { case alert, inform, none }
enum Blur: String, Sendable { case content, media, none }
struct LabelDefinition: Equatable, Sendable {
var value: String
/// Set this for a custom definition from one labeler.
var sourceDID: String?
var defaultSetting: LabelSetting = .ignore
var configurable = true
var adult = false
var noSelf = false
var unauthenticatedOnly = false
var noOverride = false
var warningBehavior: WarningBehavior = .blur
var severity: Severity = .alert
var blur: Blur = .content
}
// Based on atproto moderation/const/labels.ts at cea5189c0ca0e95cce819dcd557cc16d30f408b0.
static let builtInDefinitions: [LabelDefinition] =
[
.init(value: "!hide", defaultSetting: .hide, configurable: false, noOverride: true),
.init(value: "!no-promote", defaultSetting: .hide, configurable: false, warningBehavior: .none),
.init(value: "!warn", defaultSetting: .warn, configurable: false),
.init(
value: "!no-unauthenticated", defaultSetting: .hide, configurable: false, unauthenticatedOnly: true,
noOverride: true),
.init(value: "dmca-violation", defaultSetting: .hide, configurable: false, noOverride: true),
.init(value: "doxxing", defaultSetting: .hide, configurable: false, noOverride: true)
]
+ ["porn", "sexual", "nudity", "graphic-media", "nsfl", "corpse", "gore", "self-harm"].map {
.init(value: $0, adult: true, warningBehavior: .blurMedia)
} + [.init(value: "torture", adult: true)]
+ [
"intolerant-race", "intolerant-gender", "intolerant-sexual-orientation", "intolerant-religion",
"intolerant", "threat", "spoiler", "spam", "account-security", "net-abuse"
].map {
.init(value: $0)
} + [.init(value: "icon-intolerant", warningBehavior: .blurMedia)]
+ ["impersonation", "scam", "misleading"].map { .init(value: $0, warningBehavior: .alert) }
struct Labeler: Equatable, Sendable {
var did: String
var labels: [String: LabelSetting] = [:]
}
struct Preferences: Equatable, Sendable {
var userDID: String?
var adultContentEnabled = false
var labels: [String: LabelSetting] = [:]
var labelers: [Labeler] = []
var labelDefinitions: [LabelDefinition] = []
var mutedWords: [MutedWord] = []
}
struct Author: Hashable, Sendable {
var did: String
var isFollowing = false
var labels: [Label] = []
}
struct Post: Hashable, Sendable {
var author: Author
var text: String
var tags: [String] = []
var languages: [String] = []
var imageAltTexts: [String] = []
var embeddedTexts: [String] = []
var labels: [Label] = []
}
enum Reason: Equatable, Sendable {
case mutedWord(String)
case label(value: String, sourceDID: String, noOverride: Bool)
}
enum Context { case contentList, contentView, contentMedia, avatar, profileView }
struct ContextDecision: Equatable, Sendable {
var filter = false
var blur = false
var alert = false
var inform = false
var noOverride = false
}
struct Decision: Equatable, Sendable {
var action: Action
var reasons: [Reason]
var contentList = ContextDecision()
var contentView = ContextDecision()
var contentMedia = ContextDecision()
var avatar = ContextDecision()
var profileView = ContextDecision()
var hasAdultMediaLabel = false
var allMediaBlursAreAdultWarnings = false
func ui(_ context: Context) -> ContextDecision {
switch context {
case .contentList: contentList
case .contentView: contentView
case .contentMedia: contentMedia
case .avatar: avatar
case .profileView: profileView
}
}
}
static func isCustomLabelIdentifier(_ value: String) -> Bool {
!value.isEmpty && value.utf8.allSatisfy { (97...122).contains($0) || $0 == 45 }
}
}
extension ModerationPolicy.MutedWord {
enum Target: Hashable, Sendable { case content, tag }
}