Skip to content

Commit

Permalink
fix async handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ginesdt committed Jun 26, 2024
1 parent 921a4c9 commit c3fa59a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
48 changes: 24 additions & 24 deletions src/streamtide/server/business_logic.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
(defn get-roles [current-user]
"Gets the roles of the user sending the request"
(require-auth current-user)
(go
(safe-go
(map #(keyword :role (:role/role %)) (<? (stdb/get-roles current-user)))))

(defn require-role [current-user role]
"Checks the user performing the request has the given role. Throws an error otherwise"
(go
(safe-go
(when-not (contains? (set (<? (get-roles current-user))) role)
(throw (js/Error. "Unauthorized")))))

Expand All @@ -47,13 +47,13 @@

(defn require-not-blacklisted [current-user]
"Checks the user performing the request is not blacklisted. Throws an error otherwise"
(go
(safe-go
(when (<? (stdb/blacklisted? {:user/address current-user}))
(throw (js/Error. "Unauthorized - address blacklisted")))))

(defn require-grant-approved [current-user]
"Checks the grant of the user performing the request has been already approved. Throws an error otherwise"
(go
(safe-go
(when-not (= (name :grant.status/approved) (:grant/status (<? (stdb/get-grant current-user))))
(throw (js/Error. "Unauthorized - require approved grant to perform operation")))))

Expand Down Expand Up @@ -88,13 +88,13 @@

(defn get-users [current-user args]
"Gets all users"
(go
(safe-go
(when (or (:users.order-by/last-seen args)
(:users.order-by/last-modification args))
(require-auth current-user)
(<? (require-admin current-user)))

(stdb/get-users args)))
(<? (stdb/get-users args))))

(defn get-announcements [_current-user args]
"Gets defined announcements"
Expand Down Expand Up @@ -127,7 +127,7 @@
(defn verify-social! [current-user {:keys [:state] :as args}]
"Verify a social network, for example checking the authentication code coming from user authentication is valid.
This returns a channel"
(go
(safe-go
(require-auth current-user)
(<? (require-not-blacklisted current-user))

Expand All @@ -146,13 +146,13 @@
(defn generate-twitter-oauth-url [current-user args]
"Requests a twitter oauth URL"
(require-auth current-user)
(go
(safe-go
(<? (require-not-blacklisted current-user))

(twitter/generate-twitter-oauth-url args)))

(defn- add-notification-types [current-user notification-type-input]
(go
(safe-go
(doall
(for [{:keys [:user/address :notification/user-id :notification/type]}
(map (fn [user-id]
Expand All @@ -165,7 +165,7 @@
(defn add-notification-type [current-user notification-type-input]
"Adds notification details for a given user"
(require-auth current-user)
(go
(safe-go
(<? (require-not-blacklisted current-user))

(<? (add-notification-types current-user notification-type-input))))
Expand Down Expand Up @@ -211,7 +211,7 @@
(defn update-user-info! [current-user {:keys [:user/socials :user/perks :user/photo :user/bg-photo :user/notification-categories :user/notification-types] :as args} config]
"Sets the user info"
(require-auth current-user)
(go
(safe-go
(<? (require-not-blacklisted current-user))

(check-user-urls args)
Expand Down Expand Up @@ -247,15 +247,15 @@

(defn get-notification-categories [current-user address]
(require-auth current-user)
(go
(safe-go
(<? (require-not-blacklisted current-user))
(require-same-user current-user address)

(<? (stdb/get-notification-categories {:user/address address}))))

(defn get-notification-types [current-user address]
(require-auth current-user)
(go
(safe-go
(<? (require-not-blacklisted current-user))
(require-same-user current-user address)

Expand All @@ -278,15 +278,15 @@
(defn request-grant! [current-user]
"Request a grant for a user if she does not requested it already"
(require-auth current-user)
(go
(safe-go
(<? (require-not-blacklisted current-user))

(<? (stdb/upsert-grants! {:user/addresses [current-user] :grant/status (name :grant.status/requested)}))))

(defn review-grants! [current-user {:keys [:user/addresses :grant/status] :as args}]
"Approves or reject a grant request"
(require-auth current-user)
(go
(safe-go
(<? (require-admin current-user))

; Grant approval needs to be done through smart contract, rejection from here
Expand All @@ -304,7 +304,7 @@
(defn get-user-timestamps [current-user user-address]
"Gets the timestamps of a user"
(require-auth current-user)
(go
(safe-go
(<? (require-admin current-user))

(<? (stdb/get-user-timestamps {:user/address user-address}))))
Expand All @@ -315,15 +315,15 @@

(defn unlocked? [current-user user-address]
"Checks if a user is currently blacklisted"
(go
(safe-go
(if current-user
(<? (stdb/has-permission? {:user/source-user current-user :user/target-user user-address}))
false)))

(defn add-announcement! [current-user {:keys [:announcement/text] :as args}]
"Adds an announcement to show to all users"
(require-auth current-user)
(go
(safe-go
(<? (require-admin current-user))

(<? (stdb/add-announcement! (select-keys args [:announcement/text])))
Expand All @@ -332,15 +332,15 @@
(defn remove-announcement! [current-user {:keys [:announcement/id] :as args}]
"Removes an existing announcement"
(require-auth current-user)
(go
(safe-go
(<? (require-admin current-user))

(<? (stdb/remove-announcement! (select-keys args [:announcement/id])))))

(defn add-content! [current-user {:keys [:content/url :content/type :content/public :content/pinned] :as args}]
"Adds (a link to) content for the logged in user"
(require-auth current-user)
(go
(safe-go
(<? (require-grant-approved current-user))
(<? (require-not-blacklisted current-user))

Expand All @@ -349,28 +349,28 @@
(<? (stdb/add-content! (merge {:user/address current-user}
(select-keys args [:content/type :content/url :content/public :content/pinned]))))

(<? (notifiers/notify-new-content (stdb/get-user current-user)))))
(<? (notifiers/notify-new-content (<? (stdb/get-user current-user))))))

(defn remove-content! [current-user {:keys [:content/id] :as args}]
"Removes content of the logged in user"
(require-auth current-user)
(go
(safe-go
(<? (require-not-blacklisted current-user))

(<? (stdb/remove-content! (merge {:user/address current-user} (select-keys args [:content/id]))))))

(defn set-content-visibility! [current-user {:keys [:content/id :content/public] :as args}]
"Updates the visibility (public/private) of a given content"
(require-auth current-user)
(go
(safe-go
(<? (require-not-blacklisted current-user))

(<? (stdb/set-content-visibility! (merge {:user/address current-user} (select-keys args [:content/id :content/public]))))))

(defn set-content-pinned! [current-user {:keys [:content/id :content/pinned] :as args}]
"Set a content as pinned or not"
(require-auth current-user)
(go
(safe-go
(<? (require-not-blacklisted current-user))

(<? (stdb/set-content-pinned! (merge {:user/address current-user} (select-keys args [:content/id :content/pinned]))))))
5 changes: 2 additions & 3 deletions src/streamtide/server/notifiers/notifiers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
(safe-go
(let [notification {:title "New content from supported patron"
:body (gstring/format "The creator you support \"%s\" has added new content" (:user/name creator))}

addresses (map :user/source-user (stdb/get-user-content-permissions {:user/target-user address}))
addresses (map :user/source-user (<? (stdb/get-user-content-permissions {:user/target-user address})))
enabled-notifications (<? (stdb/get-notification-categories {:user/addresses addresses
:notification/category (name :notification-category/patron-publications)
:notification/enable true}))]
Expand All @@ -67,7 +66,7 @@
(defn notify-donation [donation]
"Sends a notification to the receiver of a donation"
(safe-go
(let [sender (stdb/get-user (:donation/sender donation))
(let [sender (<? (stdb/get-user (:donation/sender donation)))
notification {:title "Donation received"
:body (gstring/format "You have received a donation from %s of a value of %s"
(if (string/blank? (:user/name sender)) (:donation/sender donation) (:user/name sender))
Expand Down
2 changes: 1 addition & 1 deletion src/streamtide/server/notifiers/web_push_notifier.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
(.catch (fn [e]
(when (= 410 (.-statusCode e))
(log/info "Web push endpoint-domain is no longer active. Removing it" notification-entry)
(remove-subscription notification-entry))))))
(<? (remove-subscription notification-entry)))))))
(recur (next subscriptions))))))))

(defmethod notify notifier-type-kw [_ users notification]
Expand Down
4 changes: 2 additions & 2 deletions src/streamtide/server/syncer.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
:user/target-user patron-address}))))))))

(defn update-matching-pool [{:keys [:value :round-id :token]}]
(go
(safe-go
(when (bn/> (js/BigNumber. value) (js/BigNumber. 0))
(let [matching-pool (<! (db/get-matching-pool round-id token))
amount (bn/+ (js/BigNumber. (or (:matching-pool/amount matching-pool) 0)) (js/BigNumber. value))]
Expand Down Expand Up @@ -254,7 +254,7 @@
(defn- reload-handler [interval]
(js/setInterval
(fn []
(go
(safe-go
(let [connected? (true? (<! (web3-eth/is-listening? @web3)))]
(when connected?
(do
Expand Down

0 comments on commit c3fa59a

Please sign in to comment.