diff --git a/src/cljs/commiteth/interceptors.cljs b/src/cljs/commiteth/interceptors.cljs index 6602e96..a3988af 100644 --- a/src/cljs/commiteth/interceptors.cljs +++ b/src/cljs/commiteth/interceptors.cljs @@ -3,22 +3,12 @@ [re-frame.core :as rf] [clojure.data :as data])) -(defn get-bounty-field [owner-bounties field-name] - "for app-db representation of owner bounties, return a list of just the relevant field" - (-> owner-bounties - vals - (->> (map #(field-name %))))) - -(defn filter-updated-bounties [field-name old-bounties new-bounties] - "filters collection of bounties to only those with a field that has been recently set" - (filter (fn [[issue-id owner-bounty]] - (let [new-field-value (field-name owner-bounty) - old-field-value (get-in old-bounties [issue-id field-name])] - (when (and (nil? old-field-value) (some? new-field-value)) - (println "old value for " field-name " is " old-field-value) - (println "new value for " field-name "is " new-field-value) - true))) - new-bounties)) +(defn get-confirming-issue-id [owner pending-revocations] + "returns the issue id for the current revocation matching the desired owner type" + (some (fn [[issue-id revocation]] + (when (= owner (:confirming-account revocation)) + issue-id)) + pending-revocations)) (defn dispatch-confirm-payout [bounty] "dispatches a bounty via reframe dispatch" @@ -36,9 +26,9 @@ (rf/dispatch [:remove-pending-revocation (:issue-id bounty)])) (def watch-confirm-hash - "An interceptor which exaimines the event diff for `:load-owner-bounties` - and dispatches a `confirm-payout` event when one of the owner bounties has - its confirm_hash updated. + "revocations move through 2 states, confirmation by commiteth and then the repo owner + if a commiteth revocation is detected, check to see if its confirm hash is set, and, if it is + dispatch a confirm payout event and update the confirming account to owner *Warning* this inteceptor is only intended for use with the `:load-owner-bounties` event @@ -51,36 +41,21 @@ :after (fn confirm-hash-update-after [context] (println "watch confirm hash interceptor...") - (let [event-name (-> context - :coeffects - :event - first) - pending-revocations (get-in context [:coeffects :db ::db/pending-revocations]) - start-ob (get-in context [:coeffects :db :owner-bounties]) - end-ob (get-in context [:effects :db :owner-bounties])] - ;; proceed when the change isn't caused by page load - (when (not-empty start-ob) - (let [[only-before only-after both] (data/diff - (get-bounty-field start-ob :confirm_hash) - (get-bounty-field end-ob :confirm_hash))] - ;; proceed when confirm hashes have changed and there is a pending revocation - (when (and only-after (not-empty pending-revocations)) - (println "pending revocations are " pending-revocations) - (let [updated-bounties (->> end-ob - (filter-updated-bounties :confirm_hash start-ob) - vals)] - ;; for bounties which just had confirm hash set: perform - ;; dispatch side effect but interceptor must return context - (doseq [bounty updated-bounties] - (dispatch-confirm-payout bounty) - (dispatch-set-pending-revocation bounty)))))) + (let [pending-revocations (get-in context [:effects :db ::db/pending-revocations]) + updated-bounties (get-in context [:effects :db :owner-bounties]) + confirming-issue-id (get-confirming-issue-id :commiteth pending-revocations)] + (when-let [revoking-bounty (get updated-bounties confirming-issue-id)] + (if (:confirm_hash revoking-bounty) + (do (dispatch-confirm-payout revoking-bounty) + (dispatch-set-pending-revocation revoking-bounty)) + (println (str "currently revoking " confirming-issue-id " but confirm hash has not been set yet.")))) + ;; interceptor must return context context)))) (def watch-payout-receipt - "An interceptor which exaimines the event diff for `:load-owner-bounties` - and dispatches a `remove-pending-revocation` event when one of them has - its payout_receipt set. + "examine pending revocations with their currently confirming account set to owner + when one of them has its payout_receipt set, dispatch `remove-pending-revocation` *Warning* this inteceptor is only intended for use with the `:load-owner-bounties` event @@ -93,26 +68,12 @@ :after (fn payout-receipt-update-after [context] (println "watch payout receipt interceptor...") - (let [event-name (-> context - :coeffects - :event - first) - pending-revocations (get-in context [:coeffects :db ::db/pending-revocations]) - start-ob (get-in context [:coeffects :db :owner-bounties]) - end-ob (get-in context [:effects :db :owner-bounties])] - ;; proceed when the change isn't caused by page load - (when (not-empty start-ob) - (let [[only-before only-after both] (data/diff - (get-bounty-field start-ob :payout_receipt) - (get-bounty-field end-ob :payout_receipt))] - ;; proceed when payout_receipt has been updated and there is a pending revocation - (when (and only-after (not-empty pending-revocations)) - (println "pending revocations are " pending-revocations) - (let [updated-bounties (->> end-ob - (filter-updated-bounties :payout_receipt start-ob) - vals)] - ;; for bounties which just had confirm hash set: perform - ;; dispatch side effect but interceptor must return context - (doseq [bounty updated-bounties] - (dispatch-remove-pending-revocation bounty)))))) + (let [pending-revocations (get-in context [:effects :db ::db/pending-revocations]) + updated-bounties (get-in context [:effects :db :owner-bounties]) + confirming-issue-id (get-confirming-issue-id :owner pending-revocations)] + (when-let [revoking-bounty (get updated-bounties confirming-issue-id)] + (if (:payout_receipt revoking-bounty) + (dispatch-remove-pending-revocation revoking-bounty) + (println (str "currently revoking " confirming-issue-id " but confirm hash has not been set yet.")))) + ;; interceptor must return context context))))