Skip to content

A peer-to-peer stack for code collaboration

Add editing of issues and comments. (#329)

James Haydonbff5d7682df8
diff --git a/rad/monadic/issue-remote.rad b/rad/monadic/issue-remote.rad
index 0eda35d..302f7c5 100644
--- a/rad/monadic/issue-remote.rad
+++ b/rad/monadic/issue-remote.rad
@@ -1,6 +1,7 @@
;; Issue chain
;; Meant to be sent to a chain with send-code!
+(import prelude/basic :unqualified)
(import prelude/bool :unqualified)
(import prelude/dict :unqualified)
(import prelude/set :as 'set)
@@ -64,44 +65,119 @@
(def validator/timestamp
(validator/type :string))
+(def input-keys
+ {:nonce validator/nonce
+ :chain-id (validator/= chain-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 *chain input*, that is, it has an unused
nonce, is marked with this chain name, and is signed."
(validator/and
- [(validator/keys
- {:nonce validator/nonce
- :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/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 validator/comment
- "A comment should have a body."
- (validator/keys
- {:body (validator/text 4000)}))
+(def comment-keys
+ "A comment's body should be text that isn't too long."
+ {:body (validator/text 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/comment
- (validator/keys {:issue-number validator/issue-number})]))
+ validator/time-created
+ validator/full-comment
+ validator/issue-reference]))
+
+(def issue-keys
+ {:title (validator/text 100)
+ :body (validator/text 4000)
+ :labels (validator/every (validator/text 80))
+ :state (validator/member [:open :closed])
+ :comments (validator/every validator/full-comment)})
+
+(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
[validator/input
+ 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/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
- {:title (validator/text 100)
- :body (validator/text 4000)
- :labels (validator/every (validator/text 80))
- :state (validator/member [:open :closed])
- :comments (validator/every validator/comment)})]))
+ {: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/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))))]))
(def strip-input
"After an entity input has been processed/verified, we no longer need some of
@@ -122,12 +198,6 @@
(set-issues (@ n) i_)
(mark-used-nonce (lookup :nonce i))))
-(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)))
-
(def add-comment
"Add a comment to an issue."
(fn [c]
@@ -138,6 +208,28 @@
(fn [cs] (add-right c_ cs)))
(mark-used-nonce (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_))))))
+
;; These are intended for clients to run locally with eval-in-chain
(def list-issues
@@ -150,8 +242,9 @@
"The set of allowed chain commands."
(ref
{'create-issue create-issue
- 'close-issue close-issue
+ 'edit-issue edit-issue
'add-comment add-comment
+ 'edit-comment edit-comment
'list-issues list-issues}))
(def process-command
diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.rad
index 56b3494..c660bf9 100644
--- a/rad/monadic/issues.rad
+++ b/rad/monadic/issues.rad
@@ -40,31 +40,49 @@
(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 t (now!))
+ {:title tit
+ :body bod
+ :labels []
+ :state :open
+ :comments []
+ :created-at t
+ :modified-at t}))
(def simple-comment
"Create a comment for an issue."
(fn [issue-number bod]
- {:body bod
+ (def t (now!))
+ {:body bod
:issue-number issue-number
- :created-at (now!)}))
+ :created-at t
+ :modified-at t}))
+
+(def send-signed-command!
+ "Send a command signed by the keys in `my-keys.rad`."
+ (fn [chain cmd payload]
+ (send! (lookup :url (read-ref chain))
+ [(list cmd (sign-entity! payload))])))
(def create-issue!
"Create a new remote issue with the keys in `my-keys.rad`."
(fn [chain i]
- (send! (lookup :url (read-ref chain))
- [(list 'create-issue (sign-entity! i))])))
+ (send-signed-command! chain 'create-issue i)))
(def add-comment!
"Create a remote comment with the keys in `my-keys.rad`."
(fn [chain c]
- (send! (lookup :url (read-ref chain))
- [(list 'add-comment (sign-entity! c))])))
+ (send-signed-command! chain 'add-comment c)))
+
+(def edit-issue!
+ "Edit a remote issue with the keys in `my-keys.rad`."
+ (fn [chain i]
+ (send-signed-command! chain 'edit-issue i)))
+
+(def edit-comment!
+ "Edit a remote comment with the keys in `my-keys.rad`."
+ (fn [chain c]
+ (send-signed-command! chain 'edit-comment c)))
(def simple-create-issue!
"Create a remote issue with sensible defaults."
@@ -76,6 +94,21 @@
(fn [chain in b]
(add-comment! chain (simple-comment in b))))
+(def simple-edit-issue!
+ (fn [chain n i]
+ (edit-issue! chain
+ (<> {:issue-number n
+ :modified-at (now!)}
+ i))))
+
+(def simple-edit-comment!
+ (fn [chain n i c]
+ (edit-comment! chain
+ (<> {:issue-number n
+ :comment-index i
+ :modified-at (now!)}
+ c))))
+
(def list-issues
"Return the full map of issues."
(fn [chain]
@@ -108,7 +141,7 @@
(map
(fn [i] (note-imported (note-comments i)))
issues))
- (map (fn [i] (create-issue! chain i) imported))))
+ (map (fn [i] (create-issue! chain i)) imported)))
(def short
"Short render of an issue."
@@ -125,7 +158,7 @@
(take n is)))
(def get-by-id
- (fn [id]
+ (fn [chain id]
(lookup id (list-issues chain))))
(:test
@@ -138,12 +171,16 @@
(simple-create-issue! chain "title1" "body1")
(simple-add-comment! chain 0 "comment0")
(simple-add-comment! chain 1 "comment1")
+ (simple-edit-issue! chain 0 {:state :closed})
+ (simple-edit-comment! chain 0 0 {:body "comment0-edit"})
(fetch! chain)
(def is (list-issues chain)))]
[ (length (seq is)) ==> 2 ]
[ (view (.. (@ 0) (@ :title)) is) ==> "title0" ]
[ (view (.. (@ 1) (@ :title)) is) ==> "title1" ]
[ (view (... [(@ 1) (@ :comments) (@nth 0) (@ :body)]) is) ==> "comment1" ]
+ [ (view (... [(@ 0) (@ :comments) (@nth 0) (@ :body)]) is) ==> "comment0-edit" ]
[ (short (first (values is))) ==> "0: title0" ]
[ (short (first (list-last chain 1))) ==> "1: title1" ]
+ [ (view (.. (@ 0) (@ :state)) is) ==> :closed ]
)