From a319afcd096030da5091008835d53602e885deb4 Mon Sep 17 00:00:00 2001 From: Laurence Chen Date: Thu, 8 Aug 2024 14:00:20 +0800 Subject: [PATCH] fetch the discord avatar url and show it --- src/co/gaiwan/compass/db/schema.clj | 1 + src/co/gaiwan/compass/html/profiles.clj | 6 +++--- src/co/gaiwan/compass/routes/oauth.clj | 13 ++++++++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/co/gaiwan/compass/db/schema.clj b/src/co/gaiwan/compass/db/schema.clj index ec2ed14..18ff96b 100644 --- a/src/co/gaiwan/compass/db/schema.clj +++ b/src/co/gaiwan/compass/db/schema.clj @@ -14,6 +14,7 @@ [:discord/access-token :string "Discord OAuth2 access-token"] [:discord/expires-at :instant "Expiration timestamp for the OAuth2 token"] [:discord/refresh-token :string "Discord OAuth2 refresh-token"] + [:discord/avatar-url :string "Discord Avatar URL"] [:session/title :string "Title of the talk/workshop/activity"] [:session/subtitle :string "Subtitle of the session, for talks/workshops = speaker names"] diff --git a/src/co/gaiwan/compass/html/profiles.clj b/src/co/gaiwan/compass/html/profiles.clj index 118c180..0e3e05a 100644 --- a/src/co/gaiwan/compass/html/profiles.clj +++ b/src/co/gaiwan/compass/html/profiles.clj @@ -33,11 +33,11 @@ (o/defstyled profile-detail :div#detail [image-frame :w-100px] - ([{:discord/keys [access-token id refresh-token expires-at] - :user/keys [email handle name uuid title image] :as user}] + ([{:discord/keys [access-token id refresh-token expires-at avatar-url] + :user/keys [email handle name uuid title image-url] :as user}] [:<> [image-frame {:profile/image - (if image + (if-let [image (or image-url avatar-url)] (str "url(" image ")") (str "var(--gradient-" (inc (rand-int 7)) ")"))} user] [:div.details diff --git a/src/co/gaiwan/compass/routes/oauth.clj b/src/co/gaiwan/compass/routes/oauth.clj index 868921e..50118a6 100644 --- a/src/co/gaiwan/compass/routes/oauth.clj +++ b/src/co/gaiwan/compass/routes/oauth.clj @@ -42,19 +42,22 @@ [:pre (util/pprint-str body)]] :session {:identity nil}} (let [{:keys [access_token refresh_token expires_in]} body - {:keys [id global_name email username]} (fetch-user-info access_token) + {:keys [id global_name email username] :as user-info} (fetch-user-info access_token) + avatar-url (str "https://cdn.discordapp.com/avatars/" id "/" (:avatar user-info) ".png") + ;;_ (prn :avatar-url avatar-url) user-uuid (:user/uuid (d/entity (db/db) [:user/email email]) (random-uuid)) tx-data [{:user/uuid user-uuid :user/email email :user/name global_name :user/handle username + :discord/avatar-url avatar-url :discord/id id :discord/access-token access_token :discord/refresh-token refresh_token :discord/expires-at (.plusSeconds (java.time.Instant/now) (- expires_in 60))}]] (def tx-data tx-data) - @(db/transact tx-data ) + @(db/transact tx-data) {:status 302 :headers {"Location" (or redirect-url "/")} :flash [:p "You are signed in!"] @@ -74,6 +77,6 @@ ["/logout" {:get {:handler (fn [req] (assoc - (response/redirect "/") - :flash "You were signed out" - :session {}))}}]]) + (response/redirect "/") + :flash "You were signed out" + :session {}))}}]])