Skip to content

Commit

Permalink
Adds link to download ics file for session
Browse files Browse the repository at this point in the history
  • Loading branch information
MTrost committed Sep 17, 2024
1 parent acd4c8b commit 689d700
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
26 changes: 16 additions & 10 deletions src/co/gaiwan/compass/html/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,22 @@
[:h4 (session/subtitle session)]]]
[:div.event-at
#_[:p "Event scheduled at"]
[:div.datetime
(when time
(str
(subs (str/capitalize (str (time/day-of-week time))) 0 3)
" "
(time/format "dd.MM" time)
", "
(time/truncate-to (time/local-time time) :minutes)))
""
(fmt-dur duration)]]
[:div
[:div.datetime
(when time
(str
(subs (str/capitalize (str (time/day-of-week time))) 0 3)
" "
(time/format "dd.MM" time)
", "
(time/truncate-to (time/local-time time) :minutes)))
""
(fmt-dur duration)]]
(when (and duration time)
[:a {:hx-boost "false"
:href (url-for :session/add-to-calendar {:id (:db/id session)})
:download (str (str/replace title #"\s+" "_") ".ics")}
"Add to calendar (downloads ICS file)"])]

[:div.description.site-copy
[:div (m/component (m/md->hiccup description))]]
Expand Down
42 changes: 40 additions & 2 deletions src/co/gaiwan/compass/routes/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
session
(:identity req)]
:html/head [session-html/session-metas
session ]}))
session]}))

(defn GET-session-card [req]
(let [session-eid (parse-long (get-in req [:path-params :id]))]
Expand Down Expand Up @@ -209,6 +209,41 @@
:user user
:sessions (session/apply-filters sessions user filters)}]}))

(defn format-datetime [time]
(time/format "yyyyMMdd'T'HHmmss" time))

(defn generate-icalendar [event]
(let [{:keys [title description location start-time end-time]} event]
(str/join "\r\n"
["BEGIN:VCALENDAR"
"VERSION:2.0"
"BEGIN:VEVENT"
(str "SUMMARY:" title)
(str "DESCRIPTION:" description)
(str "LOCATION:" location)
(str "DTSTART:" start-time)
(str "DTEND:" end-time)
"END:VEVENT"
"END:VCALENDAR"])))

(defn create-icalendar-response [event]
{:headers {"content-type" "text/calendar"
"content-disposition" (str "attachment; filename=\"" (:title event) ".ics\"")}
:body (generate-icalendar event)})

(defn GET-add-to-calendar-handler [req]
(let [session-eid (parse-long (get-in req [:path-params :id]))
{:session/keys [title description
location time duration]} (q/session session-eid)
event {:title title
:description (when description (subs description 0 (min (count description) 50)))
:location (:location/name location)
:start-time (format-datetime time)
:end-time (-> time
(time/+ (time/duration duration))
format-datetime)}]
(create-icalendar-response event)))

(defn routes []
[[""
["/" {:name :sessions/index
Expand Down Expand Up @@ -243,4 +278,7 @@
["/:id/thread"
{:name :session/create-thread
:post {:middleware [[response/wrap-requires-auth]]
:handler POST-create-session-thread}}]]])
:handler POST-create-session-thread}}]
["/:id/add-to-calendar"
{:name :session/add-to-calendar
:get {:handler GET-add-to-calendar-handler}}]]])

0 comments on commit 689d700

Please sign in to comment.