diff --git a/src/co/gaiwan/compass/routes/sessions.clj b/src/co/gaiwan/compass/routes/sessions.clj index 37cae46..5aea752 100644 --- a/src/co/gaiwan/compass/routes/sessions.clj +++ b/src/co/gaiwan/compass/routes/sessions.clj @@ -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) @@ -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 diff --git a/src/co/gaiwan/compass/services/discord.clj b/src/co/gaiwan/compass/services/discord.clj index 4bb3ae5..16274a6 100644 --- a/src/co/gaiwan/compass/services/discord.clj +++ b/src/co/gaiwan/compass/services/discord.clj @@ -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 [] diff --git a/src/co/gaiwan/compass/util.clj b/src/co/gaiwan/compass/util.clj index 4f4160c..ccea43f 100644 --- a/src/co/gaiwan/compass/util.clj +++ b/src/co/gaiwan/compass/util.clj @@ -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))))))