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

geesawra.industries/old-fella

Old people's social media app

0
1
main
47 lines · 2.2 KB Raw History
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
    }
}