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
import Foundation
extension ModerationPolicy {
static func matches(_ word: MutedWord, post: Post, now: Date) -> Bool {
guard !word.value.isEmpty, word.expiresAt.map({ $0 > now }) ?? true,
!(word.excludeFollowing && post.author.isFollowing)
else { return false }
let value = word.value.lowercased()
// Bluesky treats a content target as also applying to tags.
if !word.targets.isDisjoint(with: [.content, .tag]),
post.tags.contains(where: { $0.lowercased() == value }) {
return true
}
guard word.targets.contains(.content) else { return false }
return ([post.text] + post.imageAltTexts + post.embeddedTexts).contains {
matchesText(value, text: $0.lowercased(), languages: post.languages)
}
}
private static func matchesText(_ value: String, text: String, languages: [String]) -> Bool {
let exceptions = ["ja", "zh", "ko", "th", "vi"]
if value.utf16.count == 1 || languages.first.map(exceptions.contains) == true, text.contains(value) {
return true
}
if value == text { return true }
let punctuation = CharacterSet.punctuationCharacters
if value.unicodeScalars.contains(where: {
punctuation.contains($0) || CharacterSet.whitespacesAndNewlines.contains($0)
}),
text.contains(value) {
return true
}
for part in text.components(separatedBy: .whitespacesAndNewlines) {
if part == value { return true }
let trimmed = part.trimmingCharacters(in: punctuation)
if trimmed == value { return true }
guard value.utf16.count <= trimmed.utf16.count else { continue }
if trimmed.unicodeScalars.contains(where: punctuation.contains) {
// The reference stops checking this muted word when a token contains a slash.
if trimmed.contains("/") { return false }
let parts = trimmed.components(separatedBy: punctuation).filter { !$0.isEmpty }
if parts.joined() == value || parts.contains(value) { return true }
}
}
return false
}
}