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

johnandersen777.bsky.social/knotless

git that lives in your Atmosphere account

Forked from chadtmiller.com/knotless

0
0
126 lines · 5.1 KB Raw History
import Foundation

/// A write the app makes: a procedure, and what it's given. They're all here so
/// that `ios/check-models.sh` can hold each to its lexicon, with no server.
struct Write: Sendable {
    let method: String
    let input: [String: String]
    var numbers: [String: Int] = [:]
    var notifications: NotificationChoices?

    /// What it's given, as it's sent.
    var body: [String: Any] {
        var all: [String: Any] = input
        for (name, number) in numbers { all[name] = number }
        if let notifications { all["preferences"] = [notifications.written] }
        return all
    }
}

enum Writes {
    /// `verdict` makes it a review: `approve` or `changes`.
    static func comment(repo: RepoRef, subject: String, body: String, verdict: String?) -> Write {
        var input = ["repo": repo.uri, "subject": subject, "body": body]
        if let verdict { input["verdict"] = verdict }
        return Write(method: "createComment", input: input)
    }

    static func issue(repo: RepoRef, title: String, body: String) -> Write {
        var input = ["repo": repo.uri, "title": title]
        if !body.isEmpty { input["body"] = body }
        return Write(method: "createIssue", input: input)
    }

    static func react(repo: RepoRef, subject: String, content: String, undo: Bool) -> Write {
        Write(method: undo ? "removeReaction" : "addReaction", input: ["repo": repo.uri, "subject": subject, "content": content])
    }

    /// `open` or `closed`.
    static func setState(repo: RepoRef, subject: String, state: String) -> Write {
        Write(method: "setItemState", input: ["repo": repo.uri, "subject": subject, "state": state])
    }

    static func merge(repo: RepoRef, pull: String) -> Write {
        Write(method: "mergePull", input: ["repo": repo.uri, "pull": pull])
    }

    static func queue(repo: RepoRef, pull: String) -> Write {
        Write(method: "queuePull", input: ["repo": repo.uri, "pull": pull])
    }

    static func leaveQueue(repo: RepoRef, pull: String) -> Write {
        Write(method: "leaveQueue", input: ["repo": repo.uri, "pull": pull])
    }

    static func star(repo: RepoRef, undo: Bool) -> Write {
        Write(method: undo ? "unstarRepo" : "starRepo", input: ["repo": repo.uri])
    }

    static func fork(repo: RepoRef) -> Write {
        Write(method: "forkRepo", input: ["repo": repo.uri])
    }

    /// `all`, `releases` or `ignore`, or `participating` to forget the choice.
    static func hear(repo: RepoRef, level: String) -> Write {
        Write(method: "putSubscription", input: ["subject": repo.uri, "level": level])
    }

    static func follow(did: String, undo: Bool) -> Write {
        Write(method: undo ? "unfollowActor" : "followActor", input: ["subject": did])
    }

    /// `subject` is a DID, a repository's knotless:// URL or a record's at:// URI.
    static func report(subject: String, reason: String, text: String) -> Write {
        var input = ["subject": subject, "reason": reason]
        if !text.isEmpty { input["text"] = text }
        return Write(method: "createReport", input: input)
    }

    /// From all the account's repositories, or the one `repo` names.
    static func block(subject: String, repo: String? = nil, undo: Bool) -> Write {
        var input = ["subject": subject]
        if let repo { input["repo"] = repo }
        return Write(method: undo ? "unblockActor" : "blockActor", input: input)
    }

    /// One part of it: asked again, it carries on.
    static func deleteData(did: String, limit: Int) -> Write {
        Write(method: "deleteAccountData", input: ["did": did], numbers: ["limit": limit])
    }

    /// All of them, as the server takes them whole.
    static func notifications(_ choices: NotificationChoices) -> Write {
        Write(method: "putPreferences", input: [:], notifications: choices)
    }

    /// A pull's, an issue's or a comment's body, which only who wrote it may change.
    static func edit(repo: RepoRef, subject: String, body: String) -> Write {
        Write(method: "editItem", input: ["repo": repo.uri, "subject": subject, "body": body])
    }

    /// Takes back one's own comment or review.
    static func deleteComment(repo: RepoRef, subject: String) -> Write {
        Write(method: "deleteComment", input: ["repo": repo.uri, "subject": subject])
    }

    /// The runners run a pull's head, or a ref, again: all its workflows, or `workflow`.
    static func rerun(repo: RepoRef, pull: String?, ref: String?, workflow: String?) -> Write {
        var input = ["repo": repo.uri]
        if let pull { input["pull"] = pull } else if let ref { input["ref"] = ref }
        if let workflow { input["workflow"] = workflow }
        return Write(method: "requestRerun", input: input)
    }

    static func seen(at: String) -> Write {
        Write(method: "updateSeen", input: ["seenAt": at])
    }

    /// `environment` is `production` or `sandbox`.
    static func registerPush(token: String, environment: String) -> Write {
        Write(method: "registerPush", input: ["token": token, "environment": environment])
    }

    static func unregisterPush(token: String) -> Write {
        Write(method: "unregisterPush", input: ["token": token])
    }
}