about summary refs log tree commit diff
path: root/users/grfn
diff options
context:
space:
mode:
Diffstat (limited to 'users/grfn')
-rw-r--r--users/grfn/bbbg/src/bbbg/db/user.clj9
-rw-r--r--users/grfn/bbbg/src/bbbg/handlers/home.clj2
2 files changed, 10 insertions, 1 deletions
diff --git a/users/grfn/bbbg/src/bbbg/db/user.clj b/users/grfn/bbbg/src/bbbg/db/user.clj
index 7db73e378d91..700105ef6350 100644
--- a/users/grfn/bbbg/src/bbbg/db/user.clj
+++ b/users/grfn/bbbg/src/bbbg/db/user.clj
@@ -8,3 +8,12 @@
               (select-keys attrs [::user/id
                                   ::user/username
                                   ::user/discord-user-id])))
+
+(defn find-or-create! [db attrs]
+  (or
+   (db/fetch db {:select [:*]
+                 :from [:public.user]
+                 :where [:=
+                         :discord-user-id
+                         (::user/discord-user-id attrs)]})
+   (create! db attrs)))
diff --git a/users/grfn/bbbg/src/bbbg/handlers/home.clj b/users/grfn/bbbg/src/bbbg/handlers/home.clj
index 480706574569..4b46675e4c34 100644
--- a/users/grfn/bbbg/src/bbbg/handlers/home.clj
+++ b/users/grfn/bbbg/src/bbbg/handlers/home.clj
@@ -35,7 +35,7 @@
      (let [token (get-in request [:oauth2/access-tokens :discord])]
        (if (discord.auth/check-discord-auth env token)
          (let [discord-user (discord/me token)
-               user (db.user/create!
+               user (db.user/find-or-create!
                      db
                      #::user{:username (:username discord-user)
                              :discord-user-id (:id discord-user)})]