A peer-to-peer stack for code collaboration
;; Issue machine
;; Meant to be sent to a machine with send-code!
(import prelude/basic :unqualified)
(import prelude/bool :unqualified)
(import prelude/dict :unqualified)
(import prelude/set :as 'set)
(import prelude/ref :unqualified)
(import prelude/validation :as 'validator)
(import prelude/patterns :unqualified)
(import prelude/util :unqualified)
(import prelude/lens :unqualified)
(import prelude/nonces :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/issue")
(def issues
"Issues are stored in a dict in a ref, keyed by issue-number."
(ref {}))
(def issue-counter
"We use a stateful counter for giving issues numbers."
(make-counter))
(def noncer (make-noncer))
(def auth (make-simple-auth [:admin]))
(def set-issues
"A helper to set stuff in the issues ref."
(fn [l v]
(set-ref issues l v)))
(def over-issues
"A helper to modify stuff in the issues ref."
(fn [l f]
(over-ref issues l f)))
(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/issue-number
"A validator for numbers which refer to existing issues."
(validator/pred "Existing issue number" (fn [n] (member? n (read-ref issues)))))
(def comment-keys
"A comment's body should be text that isn't too long."
{:body (validator/string-of-max-length 4000)})
(def validator/sub-comment
(validator/optional-keys comment-keys))
(def validator/full-comment
(validator/keys comment-keys))
(def validator/issue-reference
"Refers to an existing issue."
(validator/optional-keys {:issue-number validator/issue-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/issue-reference]))
(def issue-keys
{:title (validator/string-of-max-length 100)
:body (validator/string-of-max-length 4000)
:labels (validator/every (validator/string-of-max-length 80))
:state (validator/member [:open :closed])
:comments (validator/every (validator/and [validator/full-comment
validator/time-created]))})
(def validator/full-issue (validator/keys issue-keys))
(def validator/sub-issue (validator/optional-keys issue-keys))
(def validator/issue-input
"An issue should have all the required fields, be a signed input, etc."
(validator/and
[;; NOTE that we don't check `validator/input` at this point, because some of
;; the data is fixed before validation.
validator/time-created
validator/full-issue]))
(def edit-keys
(fn [ks]
(<> ks
(<> (keys input-keys)
[:issue-number :modified-at :author :signature]))))
(def validator/issue-edit-input
"An input to edit an issue."
(validator/and
[validator/input
validator/time-modified
validator/sub-issue
validator/issue-reference
(validator/or
[(validator/pred
"edit author == original author"
(fn [c]
(match c
{:issue-number 'n
:author 'a}
(eq? (view-ref issues (... [(@ n) (@ :author)]))
a))))
(auth [:allowed :admin])])
(validator/contains-only (edit-keys (keys issue-keys)))]))
(def validator/comment-edit-input
"An input which edits a comment. In particular it should refer to an existing
issue, to a valid comment-index in that issue, and the author of the edit
should be the same as the original author of the comment."
(validator/and
[validator/input
validator/time-modified
validator/sub-comment
validator/issue-reference
(validator/contains-only (<> (edit-keys (keys comment-keys))
[:comment-index]))
(validator/keys
{:comment-index validator/integral})
(validator/pred "comment index exists"
(fn [c]
(match c
{:comment-index 'i
:issue-number 'n}
(< i (length (view-ref issues (.. (@ n) (@ :comments))))))))
(validator/or
[(validator/pred
"edit author == original author"
(fn [c]
(match c
{:issue-number 'n
:comment-index 'i
:author 'a}
(eq? (view-ref issues (... [(@ n) (@ :comments) (@nth i) (@ :author)]))
a))))
(auth [:allowed :admin])])]))
(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 :issue-number] e)))
(def fix-modified-at
"Some inputs come in with a missing `:modified-at` key. If this is the case, we
add it as equal to the `:created-at` key."
(fn [x]
(if (member? :modified-at x)
x
(insert :modified-at (lookup :created-at x) x))))
(def add-issue
(fn [i]
(def fixed
(insert :comments
(map fix-modified-at (lookup :comments i))
(fix-modified-at i)))
(validator/issue-input fixed)
(def n (issue-counter :next))
(def i_
(strip-input
(<> fixed
{:number n
:comments (map strip-input (lookup :comments fixed))})))
(set-issues (@ n) i_)
(noncer [:use (lookup :nonce i)])
n))
(def create-issue
"Create an issue from a dict, checking that it is valid and storing it in `issues`."
(fn [i]
(validator/input i)
(add-issue i)))
(def anonymous-create-issue
"Anonymously create an issue from a dict, checking that it is valid and storing it in `issues`."
(fn [i]
((validator/keys input-keys) i)
(add-issue i)))
(def add-comment
"Add a comment to an issue."
(fn [c]
(validator/comment-input c)
(def c_ (strip-input c))
(over-issues
(.. (@ (lookup :issue-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-issue
"Modify some of the attributes of an issue."
(fn [i]
(validator/issue-edit-input i)
(def i_ (delete :author (strip-input i)))
(over-issues (@ (lookup :issue-number i)) (merge-new i_))))
(def edit-comment
"Modify a comment of an issue."
(fn [c]
(match c
{:issue-number 'n
:comment-index 'i}
(do (validator/comment-edit-input c)
(def c_ (strip-input c))
(over-issues (... [(@ n) (@ :comments) (@nth i)]) (merge-new c_))))))
;; HTML frontend
(def html (ref "QmcENMqxFaYifTCGpHwureKnyEAEfgQXZ8fW35kn17uQPP"))
(def get-html
"The frontend."
(fn [] (read-ref html)))
(def set-html
"Change frontend. The new HTML should be an IPFS CID."
(fn [new-html]
((validator/and [(auth [:allowed :admin]) validator/signed]) new-html)
(write-ref html new-html)))
;; These are intended for clients to run locally with eval-in-machine
(def list-issues
"List existing issues."
(fn [] (read-ref issues)))
;; General logic
(def commands
"The set of allowed machine commands."
{'create-issue create-issue
'anonymous-create-issue anonymous-create-issue
'edit-issue edit-issue
'add-comment add-comment
'edit-comment edit-comment
'list-issues list-issues
'get-html get-html
'set-html set-html
'add-admin (add-admin auth)})
(def eval (make-updatable-eval commands (auth [:allowed :admin])))