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

geesawra.industries/old-fella

Old people's social media app

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