Skip to content

Commit

Permalink
feat: begin connecting session threads with session form
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyJayJay committed Sep 7, 2024
1 parent e8e99e5 commit 3b59579
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/co/gaiwan/compass/html/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]])]
Expand Down
40 changes: 34 additions & 6 deletions src/co/gaiwan/compass/routes/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}}]]])
19 changes: 8 additions & 11 deletions src/co/gaiwan/compass/services/discord.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))

0 comments on commit 3b59579

Please sign in to comment.