Skip to content

A peer-to-peer stack for code collaboration

Move monadic issues code to monadic dir.

James Henri Haydon6709baac4d65
diff --git a/rad/chains/issue-remote.rad b/rad/chains/issue-remote.rad
deleted file mode 100644
index d251379..0000000
--- a/rad/chains/issue-remote.rad
+++ /dev/null
@@ -1,174 +0,0 @@
-;; Issue chain
-;; Meant to be sent to a chain with send-code!
-
-(def chain-id
- "The name of this chain."
- "monadic/radicle/issues")
-
-(def issues
- "Issues are stored in a dict in a ref."
- (ref {}))
-
-(def issue-counter (lookup :next (make-counter)))
-
-(def used-ids
- "We keep track of the UUIDs which have been used in the chain."
- (ref set/empty))
-
-(def id-free?
- "Check that an ID has not been used yet."
- (fn [id]
- (not (set/member? id (read-ref used-ids)))))
-
-(def mark-used-id
- "Mark a UUID as used."
- (fn [id]
- (modify-ref used-ids (fn [s] (set/insert id s)))))
-
-(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 validator/free-uuid
- "Given a predicate which says if an ID is free, checks input is a free UUID."
- (validator/and
- [validator/uuid
- (validator/pred "ID is free" id-free?)]))
-
-(def validator/text
- (fn [max-len]
- (validator/and
- [(validator/type :string)
- (validator/pred (string-append "< " (show max-len) " chars in length")
- (fn [s] (< (string-length s) max-len)))])))
-
-;; TODO correctly formatted date string
-(def validator/timestamp
- (validator/type :string))
-
-(def validator/entity
- "Checks that the input as a valid new *chain entity*, that is, it has an unused
- UUID, is marked with this chain name, and is signed."
- (validator/and
- [(validator/keys
- {:id validator/free-uuid
- :chain-id (validator/= chain-id)
- ;; NOTE that the timestamp is claimed by the author, is not verified, and
- ;; may be incorrect.
- :created-at validator/timestamp})
- validator/signed]))
-
-(def strip-entity
- "After an entity has been saved to the chain, we no longer need to keep the
- chain-id or the signature, since this has already been checked."
- (fn [e]
- (delete-many [:chain-id :signature] e)))
-
-(def validator/comment
- "Checks a comment is a chain entity which refers to an issue and has a text
- body."
- (validator/and
- [validator/entity
- (validator/keys
- {:issue-id validator/uuid
- :body (validator/text 4000)})]))
-
-(def validator/issue
- "Checks an issue a chain entity with a title, body, etc."
- (validator/and
- [validator/entity
- (validator/keys
- {:title (validator/text 100)
- :body (validator/text 4000)
- :labels (validator/every (validator/text 80))
- :state (validator/member [:open :closed])
- :comments (validator/every validator/comment)})]))
-
-(def strip-comment
- (fn [c]
- (delete :issue-id (strip-entity c))))
-
-(def create-issue
- "Create an issue from a dict, checking it is valid and storing it in `issues`."
- (fn [i]
- (def id (lookup :id i))
- (validator/issue i)
- (def i_
- (strip-entity
- (<> i
- {:number (issue-counter)
- :comments (map strip-comment (lookup :comments i))})))
- (set-issues (@ id) i_)
- (mark-used-id id)))
-
-(def close-issue
- "Set the state of an issue to `:closed`."
- (fn [id]
- (set-issues (.. (@ id) (@ :state)) :closed)))
-
-(def add-comment
- "Add a comment to an issue."
- (fn [c]
- (def id (lookup :id c))
- (validator/comment c)
- (mod-issues (.. (@ id) (@ :comments)) (fn [cs] (add-right (strip-comment c) cs)))
- (mark-used-id id)))
-
-;; These are intended for clients to run locally with eval-in-chain
-
-(def list-issues
- "List existing issues."
- (fn [] (read-ref issues)))
-
-;; General logic
-
-(def commands
- "The set of allowed chain commands."
- (ref
- {'create-issue create-issue
- 'close-issue close-issue
- 'add-comment add-comment
- 'list-issues list-issues}))
-
-(def process-command
- "Function that maps post-eval commands to existing functions"
- (fn [expr]
- (def command (head expr))
- (def args (tail expr))
- (def do-this (lookup command (read-ref commands)))
- (apply do-this args)))
-
-;; Move to eval-fn-app & updatable-eval from chain.rad to a pure file so it may
-;; be imported
-
-(def eval-fn-app
- "Given a state, a function, an argument and a callback, returns
- the result of evaluating the function call on the arg in the given
- state, while also calling the callback on the result."
- (fn [state f arg cb]
- (def exec (base-eval (list f (list 'quote arg)) state))
- (cb (head exec))
- exec))
-
-(def updatable-eval
- "Given an evaluation function `f`, returns a new one which augments `f` with a new
- command `(update expr)` which evaluates arbitrary expression using `base-eval`."
- (fn [sub-eval]
- (fn [expr state]
- (list-with-head
- expr
- (fn [cmd]
- (if (eq? cmd 'update)
- (base-eval (nth 1 expr) state)
- (sub-eval expr state)))
- (fn [] (sub-eval expr state))))))
-
-(def eval (updatable-eval
- (fn [expr state]
- (eval-fn-app state 'process-command expr (fn [x] x)))))
diff --git a/rad/chains/issues.rad b/rad/chains/issues.rad
deleted file mode 100644
index 611b505..0000000
--- a/rad/chains/issues.rad
+++ /dev/null
@@ -1,106 +0,0 @@
-;; Helpers for setting up and interacting with issues chain (as defined in
-;; rad/issues-chain.rad)
-
-;; Create a remote issue chain. Returns a ref for the chain that you
-;; can assign to a variable.
-
-(def radicle-issues-chain-id
- "The name of this chain."
- "monadic/radicle/issues")
-
-(def issues-chain
- (ref (load-chain "http://radicle.xyz/chains/radicle-issues")))
-
-(def create-issues-chain!
- "Create a remote issue chain with the given url."
- (fn []
- (def url (lookup :url (read-ref issues-chain)))
- (send-prelude! url)
- (send-code! url "rad/chains/issue-remote.rad")))
-
-(def sign-entity!
- "Assumes a key pair is stored at `my-keys.rad`. Using that key pair, will sign
- an entity (a dict) by adding `:author` and `:signature` fields, so that it is
- valid according to `validator/signed`."
- (fn [e]
- (def ks (read (read-file! "my-keys.rad")))
- (def sig (gen-signature! (lookup :private-key ks) (show (insert :chain-id radicle-issues-chain-id e))))
- (<> e
- {:author (lookup :public-key ks)
- :signature sig
- :chain-id radicle-issues-chain-id})))
-
-(def simple-issue
- (fn [tit bod]
- {:title tit
- :body bod
- :labels []
- :state :open
- :comments []
- :created-at (now!)}))
-
-(def create-issue!
- "Create a new remote issue. Signs the issue and all it's comments with the key
- stored `my-keys.rad`."
- (fn [i]
- (def issue-id (uuid!))
- (def comment-entities
- (map (fn [c]
- (sign-entity! (<> c {:id (uuid!)
- :issue-id issue-id})))
- (lookup :comments i)))
- (def e
- (sign-entity!
- (<> i {:id issue-id
- :comments comment-entities})))
- (send! (lookup :url (read-ref issues-chain))
- [(list 'create-issue e)])
- ))
-
-(def simple-create-issue!
- (fn [tit bod]
- (create-issue! (simple-issue tit bod))))
-
-(def list-issues
- "Given an issues ref `x`, returns its issues. Does not itself update the chain."
- (fn []
- (update-chain-ref issues-chain)
- (lookup :result (eval-in-chain '(list-issues) (read-ref issues-chain)))))
-
-(def import-github
- (fn []
- (def note-imported
- (fn [x]
- (insert
- :body
- (string-append
- (lookup :body x)
- "\n\n*Imported from GitHub.* Original author: `"
- (lookup :github-username x)
- "`.")
- x)))
- (def note-comments
- (fn [i] (over (@ :comments) (fn [cs] (map note-imported cs)) i)))
- (def issues (read (read-file! "githubissues.rad")))
- (def imported
- (map
- (fn [i] (note-imported (note-comments i)))
- issues))
- (map create-issue! imported)
- ))
-
-(:test
- "creating, manipulating and querying issue chains"
- [:setup
- (do
- (def pk (gen-key-pair! (default-ecc-curve)))
- (def chain-name (uuid!))
- (def chain-ref (create-issues-chain! chain-name))
- (new-issue! chain-ref pk "Ah!" "if thou issueless shall hap to die")
- (update-chain-ref chain-ref)
- (def issues (values (list-issues chain-ref))))
- ]
- [ (length issues) ==> 1 ]
- [ (lookup :title (nth 0 issues)) ==> "Ah!" ]
- [ (lookup :body (nth 0 issues)) ==> "if thou issueless shall hap to die" ]
-)
diff --git a/rad/monadic/issue-remote.rad b/rad/monadic/issue-remote.rad
index da7954b..d251379 100644
--- a/rad/monadic/issue-remote.rad
+++ b/rad/monadic/issue-remote.rad
@@ -6,26 +6,24 @@
"monadic/radicle/issues")
(def issues
- "Issues are stored in a dict in a ref, keyed by issue-number."
+ "Issues are stored in a dict in a ref."
(ref {}))
-(def issue-counter
- "We use a stateful counter for giving issues numbers."
- (make-counter))
+(def issue-counter (lookup :next (make-counter)))
-(def used-nonces
- "We keep track of the UUIDs which have been used as nonces in inputs."
+(def used-ids
+ "We keep track of the UUIDs which have been used in the chain."
(ref set/empty))
-(def unused-nonce?
- "Check that a nonce has not been used already."
- (fn [nonce]
- (not (set/member? nonce (read-ref used-nonces)))))
+(def id-free?
+ "Check that an ID has not been used yet."
+ (fn [id]
+ (not (set/member? id (read-ref used-ids)))))
-(def mark-used-nonce
+(def mark-used-id
"Mark a UUID as used."
- (fn [nonce]
- (modify-ref used-nonces (fn [s] (set/insert nonce s)))))
+ (fn [id]
+ (modify-ref used-ids (fn [s] (set/insert id s)))))
(def set-issues
"A helper to set stuff in the issues ref."
@@ -37,14 +35,13 @@
(fn [l f]
(over-ref issues l f)))
-(def validator/nonce
- "Validator for unused nonces."
+(def validator/free-uuid
+ "Given a predicate which says if an ID is free, checks input is a free UUID."
(validator/and
[validator/uuid
- (validator/pred "Nonce not already used" unused-nonce?)]))
+ (validator/pred "ID is free" id-free?)]))
(def validator/text
- "A validator for text."
(fn [max-len]
(validator/and
[(validator/type :string)
@@ -55,79 +52,73 @@
(def validator/timestamp
(validator/type :string))
-(def validator/input
- "Checks that the input as a valid new *chain input*, that is, it has an unused
- nonce, is marked with this chain name, and is signed."
+(def validator/entity
+ "Checks that the input as a valid new *chain entity*, that is, it has an unused
+ UUID, is marked with this chain name, and is signed."
(validator/and
[(validator/keys
- {:nonce validator/nonce
+ {:id validator/free-uuid
:chain-id (validator/= chain-id)
;; NOTE that the timestamp is claimed by the author, is not verified, and
;; may be incorrect.
:created-at validator/timestamp})
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 strip-entity
+ "After an entity has been saved to the chain, we no longer need to keep the
+ chain-id or the signature, since this has already been checked."
+ (fn [e]
+ (delete-many [:chain-id :signature] e)))
(def validator/comment
- "A comment should have a body."
- (validator/keys
- {:body (validator/text 4000)}))
-
-(def validator/comment-input
- "A comment input should refer to an existing issue."
+ "Checks a comment is a chain entity which refers to an issue and has a text
+ body."
(validator/and
- [validator/input
- validator/comment
- (validator/keys {:issue-number validator/issue-number})]))
+ [validator/entity
+ (validator/keys
+ {:issue-id validator/uuid
+ :body (validator/text 4000)})]))
-(def validator/issue-input
- "An issue should have all the required fields, be a signed input, etc."
+(def validator/issue
+ "Checks an issue a chain entity with a title, body, etc."
(validator/and
- [validator/input
+ [validator/entity
(validator/keys
- {:title (validator/text 100)
- :body (validator/text 4000)
- :labels (validator/every (validator/text 80))
- :state (validator/member [:open :closed])
- :comments (validator/every validator/comment)})]))
-
-(def strip-input
- "After an entity input has been processed/verified, we no longer need some of
- the fields."
- (fn [e]
- (delete-many [:chain-id :signature :nonce :issue-number] e)))
+ {:title (validator/text 100)
+ :body (validator/text 4000)
+ :labels (validator/every (validator/text 80))
+ :state (validator/member [:open :closed])
+ :comments (validator/every validator/comment)})]))
+
+(def strip-comment
+ (fn [c]
+ (delete :issue-id (strip-entity c))))
(def create-issue
- "Create an issue from a dict, checking that it is valid and storing it in `issues`."
+ "Create an issue from a dict, checking it is valid and storing it in `issues`."
(fn [i]
- (validator/issue-input i)
- (def n (issue-counter :next))
+ (def id (lookup :id i))
+ (validator/issue i)
(def i_
- (strip-input
+ (strip-entity
(<> i
- {:number n
- :comments (map strip-input (lookup :comments i))})))
- (set-issues (@ n) i_)
- (mark-used-nonce (lookup :nonce i))))
+ {:number (issue-counter)
+ :comments (map strip-comment (lookup :comments i))})))
+ (set-issues (@ id) i_)
+ (mark-used-id id)))
(def close-issue
- "Given an issue number, sets the state of the issue to `:closed`."
- (fn [n]
- (validator/issue-number n)
- (set-issues (.. (@ n) (@ :state)) :closed)))
+ "Set the state of an issue to `:closed`."
+ (fn [id]
+ (set-issues (.. (@ id) (@ :state)) :closed)))
(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)))
- (mark-used-nonce (lookup :nonce c))))
+ (def id (lookup :id c))
+ (validator/comment c)
+ (mod-issues (.. (@ id) (@ :comments)) (fn [cs] (add-right (strip-comment c) cs)))
+ (mark-used-id id)))
;; These are intended for clients to run locally with eval-in-chain
diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.rad
index 0733f0f..f175ea7 100644
--- a/rad/monadic/issues.rad
+++ b/rad/monadic/issues.rad
@@ -4,8 +4,6 @@
;; Create a remote issue chain. Returns a ref for the chain that you
;; can assign to a variable.
-(load! "rad/prelude.rad")
-
(def radicle-issues-chain-id
"The name of this chain."
"monadic/radicle/issues")
@@ -18,77 +16,58 @@
(fn []
(def url (lookup :url (read-ref issues-chain)))
(send-prelude! url)
- (send-code! url "rad/monadic/issue-remote.rad")))
+ (send-code! url "rad/chains/issue-remote.rad")))
(def sign-entity!
- "Assumes a key pair is stored at `my-keys.rad`. Using that key pair, will sign a
- dict by adding `:author` and `:signature` fields, so that it is valid
- according to `validator/signed`, while also adding a `:nonce`."
+ "Assumes a key pair is stored at `my-keys.rad`. Using that key pair, will sign
+ an entity (a dict) by adding `:author` and `:signature` fields, so that it is
+ valid according to `validator/signed`."
(fn [e]
- (def nonce (uuid!))
- (def e_ (<> e {:chain-id radicle-issues-chain-id
- :nonce nonce}))
(def ks (read (read-file! "my-keys.rad")))
- (def sig
- (gen-signature! (lookup :private-key ks)
- (show e_)))
- (<> e_
+ (def sig (gen-signature! (lookup :private-key ks) (show (insert :chain-id radicle-issues-chain-id e))))
+ (<> e
{:author (lookup :public-key ks)
- :signature sig})))
+ :signature sig
+ :chain-id radicle-issues-chain-id})))
(def simple-issue
- "Create an open issue with no comments or labels."
(fn [tit bod]
- {:title tit
- :body bod
- :labels []
- :state :open
- :comments []
- :created-at (now!)}))
-
-(def simple-comment
- "Create a comment for an issue."
- (fn [issue-number bod]
- {:body bod
- :issue-number issue-number
+ {:title tit
+ :body bod
+ :labels []
+ :state :open
+ :comments []
:created-at (now!)}))
(def create-issue!
- "Create a new remote issue with the keys in `my-keys.rad`."
+ "Create a new remote issue. Signs the issue and all it's comments with the key
+ stored `my-keys.rad`."
(fn [i]
+ (def issue-id (uuid!))
+ (def comment-entities
+ (map (fn [c]
+ (sign-entity! (<> c {:id (uuid!)
+ :issue-id issue-id})))
+ (lookup :comments i)))
+ (def e
+ (sign-entity!
+ (<> i {:id issue-id
+ :comments comment-entities})))
(send! (lookup :url (read-ref issues-chain))
- [(list 'create-issue (sign-entity! i))])))
-
-(def add-comment!
- "Create a remote comment with the keys in `my-keys.rad`."
- (fn [c]
- (send! (lookup :url (read-ref issues-chain))
- [(list 'add-comment (sign-entity! c))])))
+ [(list 'create-issue e)])
+ ))
(def simple-create-issue!
- "Create a remote issue with sensible defaults."
- (fn [tit bod]
- (create-issue! (simple-issue tit bod))))
-
-(def simple-add-comment!
- "Create a remote comment."
- (fn [in b]
- (add-comment! (simple-comment in b))))
+ (fn [tit bod]
+ (create-issue! (simple-issue tit bod))))
(def list-issues
- "Return the full map of issues."
+ "Given an issues ref `x`, returns its issues. Does not itself update the chain."
(fn []
+ (update-chain-ref issues-chain)
(lookup :result (eval-in-chain '(list-issues) (read-ref issues-chain)))))
-(def fetch
- "Update the locally cached state of the chain."
- (fn []
- (update-chain-ref issues-chain)))
-
(def import-github
- "Imports the github issues stored in file `githubissues.rad`. To create this
- file use the `radicle-github-issues` executable. Will add a note to all
- `:body`s that this is an imported entity."
(fn []
(def note-imported
(fn [x]
@@ -107,40 +86,21 @@
(map
(fn [i] (note-imported (note-comments i)))
issues))
- (map create-issue! imported)))
-
-(def short
- "Short render of an issue."
- (fn [i]
- (string-append
- (show (lookup :number i))
- ": "
- (lookup :title i))))
-
-(def list-last
- "Get the last `n` issues."
- (fn [n]
- (def is (sort-by (fn [i] (- 0 (lookup :number i))) (values (list-issues))))
- (take n is)))
-
-(def get-by-id
- (fn [id]
- (lookup id (list-issues))))
+ (map create-issue! imported)
+ ))
(:test
- "The monadic issues chain works."
- [:setup
- (do (create-issues-chain!)
- (simple-create-issue! "title0" "body0")
- (simple-create-issue! "title1" "body1")
- (simple-add-comment! 0 "comment0")
- (simple-add-comment! 1 "comment1")
- (fetch)
- (def is (list-issues)))]
- [ (length (seq is)) ==> 2 ]
- [ (view (.. (@ 0) (@ :title)) is) ==> "title0" ]
- [ (view (.. (@ 1) (@ :title)) is) ==> "title1" ]
- [ (view (... [(@ 1) (@ :comments) (@nth 0) (@ :body)]) is) ==> "comment1" ]
- [ (short (head (values is))) ==> "0: title0" ]
- [ (short (head (list-last 1))) ==> "1: title1" ]
+ "creating, manipulating and querying issue chains"
+ [:setup
+ (do
+ (def pk (gen-key-pair! (default-ecc-curve)))
+ (def chain-name (uuid!))
+ (def chain-ref (create-issues-chain! chain-name))
+ (new-issue! chain-ref pk "Ah!" "if thou issueless shall hap to die")
+ (update-chain-ref chain-ref)
+ (def issues (values (list-issues chain-ref))))
+ ]
+ [ (length issues) ==> 1 ]
+ [ (lookup :title (nth 0 issues)) ==> "Ah!" ]
+ [ (lookup :body (nth 0 issues)) ==> "if thou issueless shall hap to die" ]
)