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

geesawra.industries/old-fella

Old people's social media app

0
1
main
73 lines · 2.3 KB Raw History
import Foundation

struct EditableProfile: Equatable, Sendable {
    let accountID: String
    let recordCID: String?
    let avatarURL: URL?
    let draft: ProfileEdit
}

struct ProfileEditResult {
    let account: Account
    let linkIDs: [String]
}

struct ProfileEdit: Equatable, Sendable {
    enum Avatar: Equatable, Sendable {
        case keep
        case remove
        case replace(Data)
    }

    struct Link: Equatable, Identifiable, Sendable {
        var id = UUID()
        var title: String
        var address: String
        let editedAt = Date()
        let revision = UUID()

        var label: String {
            if !title.isEmpty { return title }
            return URL(string: address)?.host() ?? (address.isEmpty ? "Unavailable link" : address)
        }

        var validationError: String? {
            guard title.count <= 40, title.utf8.count <= 320 else { return "Use a shorter link title." }
            guard let url = URL(string: address, encodingInvalidCharacters: false),
                url.scheme?.lowercased() == "https", let host = url.host(), !host.isEmpty,
                url.user() == nil, url.password() == nil
            else { return "Enter an address that starts with https://." }
            return nil
        }
    }

    static let linkLimit = 10

    var displayName: String
    var bio: String
    var links: [Link]
    var avatar: Avatar = .keep

    func validationError(comparedTo original: Self) -> String? {
        if displayName != original.displayName, displayName.count > 64 || displayName.utf8.count > 640 {
            return "Use a shorter display name."
        }
        if bio != original.bio, bio.count > 256 || bio.utf8.count > 2560 {
            return "Use a shorter bio."
        }
        if links.count > Self.linkLimit, links != original.links { return "Use up to 10 profile links." }
        return links.lazy.filter { !original.links.contains($0) }.compactMap(\.validationError).first
    }
}

enum ProfileEditError: LocalizedError {
    case changedOnServer
    case invalidInput(String)

    var errorDescription: String? {
        switch self {
        case .changedOnServer: "Your profile changed on Bluesky. Reopen the editor to load the latest version."
        case .invalidInput(let message): message
        }
    }
}