This repository has been archived by the owner on Apr 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
services.clj
235 lines (203 loc) · 9.17 KB
/
services.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
(ns commiteth.routes.services
(:require [ring.util.http-response :refer :all]
[compojure.api.sweet :refer :all]
[schema.core :as s]
[compojure.api.meta :refer [restructure-param]]
[buddy.auth.accessrules :refer [restrict]]
[buddy.auth :refer [authenticated?]]
[commiteth.db.core :as db]
[commiteth.db.users :as users]
[commiteth.db.usage-metrics :as usage-metrics]
[commiteth.db.repositories :as repositories]
[commiteth.db.bounties :as bounties-db]
[commiteth.bounties :as bounties]
[commiteth.eth.core :as eth]
[commiteth.github.core :as github]
[clojure.tools.logging :as log]
[commiteth.config :refer [env]]
[commiteth.util.util :refer [usd-decimal->str
eth-decimal->str]]
[crypto.random :as random]
[clojure.set :refer [rename-keys]]
[clojure.string :as str]))
(defn add-bounties-for-existing-issues? []
(env :add-bounties-for-existing-issues false))
(defn access-error [_ _]
(unauthorized {:error "unauthorized"}))
(defn wrap-restricted [handler rule]
(restrict handler {:handler rule
:on-error access-error}))
(defmethod restructure-param :auth-rules
[_ rule acc]
(update-in acc [:middleware] conj [wrap-restricted rule]))
(defmethod restructure-param :current-user
[_ binding acc]
(update-in acc [:letks] into [binding `(:identity ~'+compojure-api-request+)]))
(defn status-team-member? [req]
(let [token (get-in req [:params :token])
member? (github/status-team-member? token)]
(log/debug "token" token "member?" member?)
member?))
(defn in? [coll elem]
(some #(= elem %) coll))
(defn handle-get-user-repos [user token]
(log/debug "handle-get-user-repos")
(let [github-repos (github/get-user-repos token)
enabled-repos (vec (repositories/get-enabled (:id user)))
repo-enabled? (fn [repo] (in? enabled-repos (:id repo)))
update-enabled (fn [repo] (assoc repo :enabled (repo-enabled? repo)))]
(into {}
(map (fn [[group repos]] {group
(map update-enabled repos)})
github-repos))))
(defn ^:private enrich-owner-bounties [owner-bounty]
(let [claims (map
#(update % :value_usd usd-decimal->str)
(bounties-db/bounty-claims (:issue_id owner-bounty)))
with-claims (assoc owner-bounty :claims claims)]
(-> with-claims
(update :value_usd usd-decimal->str)
(assoc :state (bounties/bounty-state with-claims)))))
(defn user-bounties [user]
(let [owner-bounties (bounties-db/owner-bounties (:id user))]
(->> owner-bounties
(map enrich-owner-bounties)
(map (juxt :issue_id identity))
(into {}))))
(defn top-hunters []
(let [renames {:user_name :display-name
:avatar_url :avatar-url
:total_usd :total-usd}]
(map #(-> %
(rename-keys renames)
(update :total-usd usd-decimal->str))
(bounties-db/top-hunters))))
(defn prettify-bounty-items [bounty-items]
(let [format-float (fn [bounty balance]
(try
(format "%.2f" (double balance))
(catch Throwable ex
(log/error (str (:repo-owner bounty)
"/"
(:repo-name bounty)
"/"
(:issue-number bounty))
"Failed to convert token value:" balance)
"0.00")))
update-token-values (fn [bounty]
(->> bounty
:tokens
(map (fn [[tla balance]]
[tla (format-float bounty balance)]))
(into {})
(assoc bounty :tokens)))
renames {:user_name :display-name
:user_avatar_url :avatar-url
:issue_title :issue-title
:pr_title :pr-title
:pr_number :pr-number
:pr_id :pr-id
:type :item-type
:repo_name :repo-name
:repo_owner :repo-owner
:issue_number :issue-number
:issue_id :issue-id
:value_usd :value-usd
:claim_count :claim-count
:balance_eth :balance-eth
:user_has_address :user-has-address}]
(map #(-> %
(rename-keys renames)
(update :value-usd usd-decimal->str)
(update :balance-eth eth-decimal->str)
update-token-values)
bounty-items)))
(defn activity-feed []
(prettify-bounty-items (bounties-db/bounty-activity)))
(defn open-bounties []
(prettify-bounty-items (bounties-db/open-bounties)))
(defn current-user-token [user]
(some identity (map #(% user) [:token :admin-token])))
(defn handle-get-user [user admin-token]
(let [status-member? (github/status-team-member? admin-token)]
{:user
(-> (users/get-user (:id user))
(dissoc :email)
(assoc :status-team-member? status-member?))}))
(defn user-whitelisted? [user]
(let [whitelist (env :user-whitelist #{})]
(whitelist user)))
(defapi service-routes
(when (:dev env)
{:swagger {:ui "/swagger-ui"
:spec "/swagger.json"
:data {:info {:version "0.1"
:title "commitETH API"
:description "commitETH API"}}}})
(context "/api" []
(GET "/top-hunters" []
(log/debug "/top-hunters")
(ok (top-hunters)))
(GET "/activity-feed" []
(log/debug "/activity-feed")
(ok (activity-feed)))
(GET "/open-bounties" []
(log/debug "/open-bounties")
(ok (open-bounties)))
(GET "/usage-metrics" []
:query-params [token :- String]
:auth-rules status-team-member?
:current-user user
(do
(log/debug "/usage-metrics" user)
(ok (usage-metrics/usage-metrics-by-day))))
(context "/user" []
(GET "/" {:keys [params]}
:auth-rules authenticated?
:current-user user
(ok (handle-get-user user (:token params))))
(POST "/" []
:auth-rules authenticated?
:current-user user
:body [body {:address s/Str
:is_hidden_in_hunters s/Bool}]
:summary "Updates user's fields."
(let [user-id (:id user)
{:keys [address]} body]
(when-not (eth/valid-address? address)
(log/debugf "POST /user: Wrong address %s" address)
(bad-request! (format "Invalid Ethereum address: %s" address)))
(db/with-tx
(when-not (db/user-exists? {:id user-id})
(not-found! "No such a user."))
(db/update! :users body ["id = ?" user-id]))
(ok)))
(GET "/repositories" {:keys [params]}
:auth-rules authenticated?
:current-user user
(ok {:repositories (handle-get-user-repos
user
(:token params))}))
(GET "/enabled-repositories" []
:auth-rules authenticated?
:current-user user
(ok (repositories/get-enabled (:id user))))
(POST "/bounty/:issue{[0-9]{1,9}}/payout" {:keys [params]}
:auth-rules authenticated?
:current-user user
(do
(log/debug "/bounty/X/payout" params)
(let [{issue :issue
payout-hash :payout-hash} params
result (bounties-db/update-payout-hash
(Integer/parseInt issue)
payout-hash)]
(log/debug "result" result)
(if (= 1 result)
(ok)
(internal-server-error)))))
(GET "/bounties" []
:auth-rules authenticated?
:current-user user
(log/debug "/user/bounties")
(ok (user-bounties user))))))