From b3909f4e60df3bb2b6ac305e3406023929fdb9bb Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Thu, 19 Jan 2017 18:59:05 +0200 Subject: [PATCH] Fix saving of users' email Previously the user's email did not get saved to the db unless it was defined as public in the profile. This commit adds an additional oauth scope for accessing the user's emails, and the user's primary email address is saved to the db. --- src/clj/commiteth/github/core.clj | 10 +++++++- src/clj/commiteth/routes/qrcodes.clj | 34 +++++++++++++++++---------- src/clj/commiteth/routes/redirect.clj | 17 ++++++++++---- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/clj/commiteth/github/core.clj b/src/clj/commiteth/github/core.clj index 0ad0d05..ce2c327 100644 --- a/src/clj/commiteth/github/core.clj +++ b/src/clj/commiteth/github/core.clj @@ -26,7 +26,7 @@ (defn authorize-url [] (let [params (codec/form-encode {:client_id (client-id) :redirect_uri (redirect-uri) - :scope "admin:repo_hook repo admin:org_hook" + :scope "admin:repo_hook user:email repo admin:org_hook" :allow_signup true :state (str (UUID/randomUUID))})] (str "https://github.com/login/oauth/authorize" "?" params))) @@ -82,6 +82,14 @@ [token] (users/me (auth-params token))) +(defn get-user-email + [token] + (let [emails (users/emails (auth-params token))] + (-> + (filter :primary emails) + first + :email))) + (defn add-webhook [full-repo token] (log/debug "adding webhook" full-repo token) diff --git a/src/clj/commiteth/routes/qrcodes.clj b/src/clj/commiteth/routes/qrcodes.clj index 933b143..89c420b 100644 --- a/src/clj/commiteth/routes/qrcodes.clj +++ b/src/clj/commiteth/routes/qrcodes.clj @@ -24,20 +24,28 @@ (defn generate-image [address balance issue-url width height] (let [qr-code-image (ImageIO/read (generate-qr-code address)) - comment-image (html->image (generate-html address balance issue-url) width height)] + comment-image (html->image + (generate-html address balance issue-url) width height)] (combine-images qr-code-image comment-image))) (defapi qr-routes (context "/qr" [] - (GET "/:user/:repo/bounty/:issue{[0-9]{1,9}}/:hash/qr.png" [user repo issue hash] - (if (= hash (github/github-comment-hash user repo issue)) - (let [{address :contract_address - login :login - repo :repo - issue-number :issue_number} (bounties/get-bounty-address user repo (Integer/parseInt issue))] - (if address - (let [balance (eth/get-balance-eth address 8) - issue-url (str login "/" repo "/issues/" issue-number)] - (ok (generate-image address balance issue-url 768 256))) - (bad-request))) - (bad-request))))) + ;; user may be an organization here + (GET "/:user/:repo/bounty/:issue{[0-9]{1,9}}/:hash/qr.png" [user repo issue hash] + (log/debug "qr PNG GET") + (let [{address :contract_address + login :login + repo :repo + issue-number :issue_number} + (bounties/get-bounty-address user + repo + (Integer/parseInt issue)) + balance (eth/get-balance-eth address 8)] + (log/debug "address:" address "balance:" balance) + + (if (and address + (= hash (github/github-comment-hash user repo issue balance))) + (let [issue-url (str login "/" repo "/issues/" issue-number)] + (log/debug "balance:" address) + (ok (generate-image address balance issue-url 768 256))) + (bad-request)))))) diff --git a/src/clj/commiteth/routes/redirect.clj b/src/clj/commiteth/routes/redirect.clj index 431bed7..ab3ba17 100644 --- a/src/clj/commiteth/routes/redirect.clj +++ b/src/clj/commiteth/routes/redirect.clj @@ -8,18 +8,27 @@ [ring.util.http-response :refer [content-type ok]] [ring.util.response :as response] [commiteth.layout :refer [render]] - [cheshire.core :refer [generate-string]])) + [cheshire.core :refer [generate-string]] + [clojure.tools.logging :as log])) + + + +(defn- create-user [token user] + (let [{name :name + login :login + user-id :id} user + email (github/get-user-email token)] + (users/create-user user-id login name email token))) (defn- get-or-create-user [token] (let [user (github/get-user token) {email :email - name :name - login :login user-id :id} user] + (log/debug "get-or-create-user" user) (or (users/update-user-token user-id token) - (users/create-user user-id login name email token)))) + (create-user token user)))) (defroutes redirect-routes (GET "/callback" [code state]