Skip to content

A peer-to-peer stack for code collaboration

5.4 KB
Raw Blame
(import prelude/basic :unqualified)
(import prelude/bool :unqualified)
(import prelude/dict :unqualified)
(import prelude/set :as 'set)
(import prelude/ref :unqualified)
(import prelude/nonces :unqualified)
(import prelude/validation :as 'validator)
(import prelude/patterns :unqualified)
(import prelude/util :unqualified)
(import prelude/lens :unqualified)
(import prelude/auth '[make-simple-auth add-admin] :unqualified)
(import prelude/machine-remote '[make-updatable-eval] :unqualified)

(def machine-id
  "The name of this machine."
  "monadic/radicle/patch")

(def patches
  "PATCHes are stored in a dict in a ref, keyed by patch-number."
  (ref {}))

(def patch-counter
  "We use a stateful counter for giving patch numbers."
  (make-counter))

(def noncer (make-noncer))

(def auth (make-simple-auth [:admin]))

(def set-patches
  "A helper to set stuff in the patches ref."
  (fn [l v]
    (set-ref patches l v)))

(def input-keys
  {:nonce      (noncer :validator)
   :machine-id   (validator/= machine-id)})

(def validator/time-created
  (validator/and
   [(validator/keys
     {;; NOTE that timestamps are claimed by authors, are not verified, and may
      ;; be incorrect.
      :created-at validator/timestamp
      :modified-at validator/timestamp})
    (validator/pred "`:modified-at` and `:create-at` are equal"
                    (fn [t]
                      (eq? (lookup :created-at t)
                           (lookup :modified-at t))))]))

(def validator/time-modified
  (validator/keys
   {:modified-at validator/timestamp}))

(def validator/input
  "Checks that the input as a valid new *machine input*, that is, it has an unused
  nonce, is marked with this machine name, and is signed."
  (validator/and
   [(validator/keys input-keys)
    validator/signed]))

(def validator/patch-number
  "A validator for numbers which refer to existing patches."
  (validator/pred "Existing patch number" (fn [n] (member? n (read-ref patches)))))

(def comment-keys
  "A comment's body should be text that isn't too long."
  {:body (validator/string-of-max-length 4000)})

(def validator/full-comment
  (validator/keys comment-keys))

(def validator/patch-reference
  "Refers to an existing issue."
  (validator/optional-keys {:issue-number validator/patch-number}))

(def validator/comment-input
  "A comment input should refer to an existing issue."
  (validator/and
   [validator/input
    validator/time-created
    validator/full-comment
    validator/patch-reference]))

(def patch-keys
  {:title       (validator/string-of-max-length 100)
   :description (validator/string-of-max-length 4000)
   :patch       (validator/type :string)
   :state       (validator/member [:pending :retracted :accepted :rejected])
   :comments    (validator/every validator/full-comment)})

(def validator/full-patch (validator/keys patch-keys))

(def validator/patch-input
  "An patch should have all the required fields, be a signed input, etc."
  (validator/and
   [validator/input
    validator/time-created
    validator/full-patch]))

(def edit-keys
  (fn [ks]
    (<> ks
        (<> (keys input-keys)
            [:patch-number :modified-at :author :signature]))))

(def validator/patch-edit-input
  "An input to edit a patch."
  (fn [ed]
    (def auth-validation
      (match (lookup-default :state :nothing ed)
             :nothing   validator/always-valid
             :retracted (validator/pred "edit author == original author"
                          (fn [c]
                            (match c
                                   {:patch-number 'n
                                    :author 'a}
                                   (eq? (view-ref patches (... [(@ n) (@ :author)]))
                                        a))))
             _          (auth [:allowed :admin])))
    ((validator/and
      [validator/input
       validator/time-modified
       validator/patch-reference
       auth-validation
       (validator/contains-only (edit-keys (keys patch-keys)))]) ed)))

(def strip-input
  "After an entity input has been processed/verified, we no longer need some of
  the fields."
  (fn [e]
    (delete-many [:machine-id :signature :nonce :patch-number] e)))

(def create-patch
  "Create a patch from a dict, checking that it is valid and storing it in `patches`."
  (fn [i]
    (validator/patch-input i)
    (def n (patch-counter :next))
    (def i_
      (strip-input
       (<> i
           {:number   n})))
    (set-patches (@ n) i_)
    (noncer [:use (lookup :nonce i)])
    n))

(def over-patches
  "A helper to modify stuff in the patch ref."
  (fn [l f]
    (over-ref patches l f)))

(def add-comment
  "Add a comment to a patch."
  (fn [c]
    (validator/comment-input c)
    (def c_ (strip-input c))
    (over-patches
      (.. (@ (lookup :patch-number c)) (@ :comments))
      (fn [cs] (add-right c_ cs)))
    (noncer [:use (lookup :nonce c)])))

(def merge-new
  (fn [new]
    (fn [old]
      (<> old new))))

(def edit-patch
  "Modify some of the attributes of a patch."
  (fn [i]
    (validator/patch-edit-input i)
    (def i_ (delete :author (strip-input i)))
    (over-patches (@ (lookup :patch-number i)) (merge-new i_))))

(def list-patches
  "List existing patches."
  (fn [] (read-ref patches)))

(def commands
  "The set of allowed machine commands."
  {'create-patch   create-patch
   'list-patches   list-patches
   'edit-patch     edit-patch
   'add-comment    add-comment
   'add-admin      (add-admin auth)})

(def eval (make-updatable-eval commands (auth [:allowed :admin])))