Skip to content

Commit

Permalink
feat: add existing participants to thread upon creation
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyJayJay committed Sep 13, 2024
1 parent 84b039d commit 211b1ac
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/co/gaiwan/compass/routes/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
[co.gaiwan.compass.model.session :as session]
[co.gaiwan.compass.model.user :as user]
[java-time.api :as time]
[co.gaiwan.compass.services.discord :as discord]))
[co.gaiwan.compass.services.discord :as discord]
[co.gaiwan.compass.util :as util]
[clojure.string :as str]))

(defn GET-session-new [req]
(if-not (:identity req)
Expand Down Expand Up @@ -171,8 +173,21 @@
{:status 409
:body "Session thread already exists"}
(if (discord/create-session-thread session)
(do
(let [participant-ids
(db/q '[:find [?id ...]
:in $ ?sid
:where
[?sid :session/participants ?pid]
[?pid :discord/id ?id]]
(db/db) session-eid)
notif-msgs (->> participant-ids
(map discord/user-mention)
(map (partial format " %s "))
(util/partition-with-limit discord/message-limit)
(map str/join))]
(discord/update-session-thread-member session-eid (:discord/id identity) :add)
(doseq [msg notif-msgs]
(discord/send-session-thread-message session-eid msg))
{:location [:session/get {:id session-eid}]
:flash "Thread created! You should have got a notification in Discord."})
{:status 500
Expand Down
2 changes: 2 additions & 0 deletions src/co/gaiwan/compass/services/discord.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
[io.pedestal.log :as log]
[co.gaiwan.compass.db :as db]))

(def message-limit 2000)

(def discord-api-endpoint "https://discord.com/api/v10")

(defn bot-auth-headers []
Expand Down
14 changes: 14 additions & 0 deletions src/co/gaiwan/compass/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,17 @@
(defn expires-in->instant
[expires-in]
(.plusSeconds (Instant/now) (- expires-in 60)))

(defn partition-with-limit
[limit parts]
(loop [result []
current []
length 0
[p & rest] parts]
(let [part-length (count p)
new-length (+ length part-length)]
(cond
(> part-length limit) nil
(nil? p) (cond-> result (seq current) (conj current))
(<= new-length limit) (recur result (conj current p) new-length rest)
:else (recur (conj result current) [] 0 (list* p rest))))))

0 comments on commit 211b1ac

Please sign in to comment.