Skip to content

A peer-to-peer stack for code collaboration

wip

James Henri Haydond08b02f0e42c
diff --git a/rad/chains/issue-remote.rad b/rad/chains/issue-remote.rad
new file mode 100644
index 0000000..84585e6
--- /dev/null
+++ b/rad/chains/issue-remote.rad
@@ -0,0 +1,126 @@
+;; Issue chain
+;; Meant to be sent to a chain with send-code!
+
+(def issues
+ "Issues are stored in a dict in a ref."
+ (ref {}))
+
+(def mod-issues
+ "A helper to apply a function to `issues`"
+ (fn [f] (modify-ref issues f)))
+
+(def validator/free-uuid
+ (validator/and
+ [(validator/type :string)
+ (validator/pred "valid UUID" uuid?)
+ (validator/pred "ID is free"
+ (fn [i] (not (member? i (read-ref issues)))))]))
+
+(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/date-string
+ (validator/type :string))
+
+(def validator/comment
+ "Comment validator. Checks format, free ID and signature."
+ (validator/and
+ [(validator/keys
+ {:id validator/free-uuid
+ :body (validator/text 4000)
+ :created-at validator/date-string})
+ validator/signed]))
+
+(def validator/issue
+ "Issue validator. Checks format, free ID, and signature."
+ (validator/and
+ [(validator/keys
+ {:id validator/free-uuid
+ :author (validator/pred "valid public key" public-key?)
+ :title (validator/text 100)
+ :body (validator/text 4000)
+ :labels (validator/every (validator/text 80))
+ :state (validator/member [:open :closed])
+ :created-at validator/date-string})
+ validator/signed]))
+
+(def new-issue
+ "Create an issue from a dict, checking it is valid and storing it in `issues`."
+ (fn [i]
+ (validator/issue i)
+ (mod-issues
+ (fn [is] (insert (lookup :id i) i is)))))
+
+(def close-issue
+ "Set the state of an issue to `:closed`."
+ (fn [id]
+ (set-ref issues (.. (@ id) (@ :state)) :closed)))
+
+(def add-comment
+ "Add a comment to an issue."
+ (fn [issue-id comment]
+ (mod-issues
+ (fn [is]
+ (modify-map issue-id
+ (fn [i]
+ (modify-map :comments
+ (fn [cs] (add-right comment cs))
+ i))
+ is)))))
+
+;; 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 new-issue
+ 'close-issue close-issue
+ '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
new file mode 100644
index 0000000..eda9d2f
--- /dev/null
+++ b/rad/chains/issues.rad
@@ -0,0 +1,58 @@
+;; 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 create-issues-chain!
+ "Create a remote issue chain with the given url. Returns a ref with the chain."
+ (fn [chain-name]
+ (send-prelude! chain-name)
+ (send-code! chain-name "rad/chains/issue-remote.rad")
+ (ref (new-chain chain-name))))
+
+(def new-issue!
+ "Create a new remote issue. Takes a chain ref, a keypair, an issue title and an issue body."
+ (fn [chain-ref keys title body]
+ (def chain (read-ref chain-ref))
+ (def id (uuid!))
+ (def msg
+ (string-append (lookup :url chain)
+ id
+ title
+ body))
+ (def arg
+ {:id id
+ :author (lookup :public-key keys)
+ :title title
+ :body body
+ :signature (gen-signature! (lookup :private-key keys) msg)})
+ (send! (lookup :url chain)
+ [(list 'create-issue arg)])))
+
+(def list-issues
+ "Given an issues ref `x`, returns its issues. Does not itself update the chain."
+ (fn [chain-ref]
+ (lookup :result (eval-in-chain '(list-issues) (read-ref chain-ref)))))
+
+(def import-issues-github
+ (fn [chain-ref file]
+ (def is (read (read-file! file)))
+ (map (fn [i] (new-issue! chain-ref keys _ _)) is)
+ ))
+
+(: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" ]
+)