From 3b595791bde6666a69d97cbb7faa7aa7c8a5bc33 Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Sat, 7 Sep 2024 17:19:48 +0200 Subject: [PATCH] feat: begin connecting session threads with session form --- src/co/gaiwan/compass/html/sessions.clj | 6 ++++ src/co/gaiwan/compass/routes/sessions.clj | 40 ++++++++++++++++++---- src/co/gaiwan/compass/services/discord.clj | 19 +++++----- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/co/gaiwan/compass/html/sessions.clj b/src/co/gaiwan/compass/html/sessions.clj index 1a6994c..3543add 100644 --- a/src/co/gaiwan/compass/html/sessions.clj +++ b/src/co/gaiwan/compass/html/sessions.clj @@ -277,6 +277,12 @@ (when (or (user/admin? user) (session/organizing? session user)) [:<> + [:button + {:hx-post (url-for :session/create-thread {:id (:db/id session)}) + :title "Create a Discord thread for participants of this session" + :disabled (some? (:session/thread-id session))} + "Create Thread"] + [:br] [:a {:href (url-for :session/edit {:id (:db/id session)})} [:button "Edit"]] [:button {:hx-delete (url-for :session/get {:id (:db/id session)})} "Delete"]])] diff --git a/src/co/gaiwan/compass/routes/sessions.clj b/src/co/gaiwan/compass/routes/sessions.clj index 3b76aa6..13de381 100644 --- a/src/co/gaiwan/compass/routes/sessions.clj +++ b/src/co/gaiwan/compass/routes/sessions.clj @@ -14,7 +14,8 @@ [co.gaiwan.compass.model.assets :as assets] [co.gaiwan.compass.model.session :as session] [co.gaiwan.compass.model.user :as user] - [java-time.api :as time])) + [java-time.api :as time] + [co.gaiwan.compass.services.discord :as discord])) (defn GET-session-new [req] (if-not (:identity req) @@ -105,6 +106,7 @@ @(db/transact [(-> (params->session-data params) (assoc :db/id id))]) + ;; TODO add a @everyone notification via discord/send-session-thread-message if there is a thread and something important changes {:location [:session/get {:id id}] :flash "Successfully edited!"}) {:status 403 @@ -136,24 +138,46 @@ (let [user (:identity req) user-id (:db/id user) session-eid (parse-long (get-in req [:path-params :id])) - session-seletor '[* {:session/type [*] - :session/location [*]}] session (q/session session-eid) capacity (:session/capacity session) signup-cnt (count (:session/participants session))] (cond ;; user leaves the session (session/participating? session user) - (do @(db/transact [[:db/retract session-eid :session/participants user-id]]) - (session-updated-response session-eid)) + (do + @(db/transact [[:db/retract session-eid :session/participants user-id]]) + (when (:session/thread-id session) + (discord/update-session-thread-member session-eid (:discord/id user) :remove)) + (session-updated-response session-eid)) (< (or signup-cnt 0) capacity) ;; user participates the session (do @(db/transact [[:db/add session-eid :session/participants user-id]]) + (when (:session/thread-id session) + (discord/update-session-thread-member session-eid (:discord/id user) :add)) (session-updated-response session-eid)) :else (session-unchanged-response session-eid)))) +(defn POST-create-session-thread + [{:keys [identity path-params]}] + (let [session-eid (parse-long (:id path-params)) + session (q/session session-eid)] + (if (or (user/admin? identity) + (session/organizing? session identity)) + (if (:session/thread-id session) + {:status 409 + :body "Session thread already exists"} + (if (discord/create-session-thread session) + (do + (discord/update-session-thread-member session-eid (:discord/id identity) :add) + {:location [:session/get {:id session-eid}] + :flash "Thread created! You should have got a notification in Discord."}) + {:status 500 + :body "Thread could not be created"})) + {:status 403 + :body "Missing permissions."}))) + (defn GET-sessions [req] (let [filters (-> req :session :session-filters) sessions (q/all-sessions) @@ -194,4 +218,8 @@ :handler POST-participate}}] ["/:id/card" {:name :session/card - :get {:handler GET-session-card}}]]]) + :get {:handler GET-session-card}}] + ["/:id/thread" + {:name :session/create-thread + :post {:middleware [[response/wrap-requires-auth]] + :handler POST-create-session-thread}}]]]) diff --git a/src/co/gaiwan/compass/services/discord.clj b/src/co/gaiwan/compass/services/discord.clj index 0545d66..4bb3ae5 100644 --- a/src/co/gaiwan/compass/services/discord.clj +++ b/src/co/gaiwan/compass/services/discord.clj @@ -86,7 +86,7 @@ (case status 201 (do - (db/transact [[:db/add (:db/id session) :session/thread-id (:id thread)]]) + @(db/transact [[:db/add (:db/id session) :session/thread-id (:id thread)]]) thread) (log/error :discord/thread-create-failed "Failed to create session thread" :session session @@ -136,15 +136,12 @@ :response (dissoc response :request)))) (log/error :discord/missing-session-thread "Tried to send message to session thread that doesn't exist"))) -(defn add-to-session-thread - "Add user with `user-id` to session thread of session with `session-id`. +(defn update-session-thread-member + "Add or remove user with `user-id` to or from session thread of session with `session-id`. - Returns `nil`. In case of failure, an error message is logged." - [session-id user-id] + To add, use `action` `:add`, to remove use `action` `:remove` + Returns whether it was sucessful or not." + [session-id user-id action] (let [session (db/entity session-id) - {:keys [status] :as response} (discord-bot-request :put (str "/channels/" (:session/thread-id session) "/thread-members/" user-id))] - (when-not (= status 204) - (log/error - :discord/thread-member-add-failed "Failed to add member to session thread" - :session session - :response (dissoc response :request))))) + {:keys [status]} (discord-bot-request ({:add :put :remove :delete} action) (str "/channels/" (:session/thread-id session) "/thread-members/" user-id))] + (= status 204)))