Skip to content

A peer-to-peer stack for code collaboration

wip

James Henri Haydon54aa20200e70
diff --git a/package.yaml b/package.yaml
index d54641e..ae0d198 100644
--- a/package.yaml
+++ b/package.yaml
@@ -11,7 +11,9 @@ extra-source-files:
dependencies:
- base >=4 && <5
- protolude
-
+when:
+ - condition: impl(ghcjs)
+ dependencies: ghcjs-dom
ghc-options:
-Wall -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints
diff --git a/rad/chains/issue-remote.rad b/rad/chains/issue-remote.rad
index 84585e6..d251379 100644
--- a/rad/chains/issue-remote.rad
+++ b/rad/chains/issue-remote.rad
@@ -1,20 +1,45 @@
;; 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 mod-issues
- "A helper to apply a function to `issues`"
- (fn [f] (modify-ref issues f)))
+(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/type :string)
- (validator/pred "valid UUID" uuid?)
- (validator/pred "ID is free"
- (fn [i] (not (member? i (read-ref issues)))))]))
+ [validator/uuid
+ (validator/pred "ID is free" id-free?)]))
(def validator/text
(fn [max-len]
@@ -24,54 +49,76 @@
(fn [s] (< (string-length s) max-len)))])))
;; TODO correctly formatted date string
-(def validator/date-string
+(def validator/timestamp
(validator/type :string))
-(def validator/comment
- "Comment validator. Checks format, free ID and signature."
+(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
- :body (validator/text 4000)
- :created-at validator/date-string})
+ :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
- "Issue validator. Checks format, free ID, and signature."
+ "Checks an issue a chain entity with a title, body, etc."
(validator/and
- [(validator/keys
- {:id validator/free-uuid
- :author (validator/pred "valid public key" public-key?)
- :title (validator/text 100)
+ [validator/entity
+ (validator/keys
+ {: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]))
+ :comments (validator/every validator/comment)})]))
+
+(def strip-comment
+ (fn [c]
+ (delete :issue-id (strip-entity c))))
-(def new-issue
+(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)
- (mod-issues
- (fn [is] (insert (lookup :id i) i is)))))
+ (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-ref issues (.. (@ id) (@ :state)) :closed)))
+ (set-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)))))
+ (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
@@ -84,9 +131,10 @@
(def commands
"The set of allowed chain commands."
(ref
- {'create-issue new-issue
- 'close-issue close-issue
- 'list-issues list-issues}))
+ {'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"
diff --git a/rad/chains/issues.rad b/rad/chains/issues.rad
index eda9d2f..611b505 100644
--- a/rad/chains/issues.rad
+++ b/rad/chains/issues.rad
@@ -4,41 +4,89 @@
;; 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. 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)])))
+ "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 [chain-ref]
- (lookup :result (eval-in-chain '(list-issues) (read-ref chain-ref)))))
+ (fn []
+ (update-chain-ref issues-chain)
+ (lookup :result (eval-in-chain '(list-issues) (read-ref issues-chain)))))
-(def import-issues-github
- (fn [chain-ref file]
- (def is (read (read-file! file)))
- (map (fn [i] (new-issue! chain-ref keys _ _)) is)
+(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