Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #5 #16

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions repl-sessions/user_group.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(ns repl-sessions.user-group
"Prepare some testing data to transact into db for testing"
(:require
[co.gaiwan.compass.db :as db]
[datomic.api :as d]))

(defn find-user-eid [email]
(db/q
'[:find ?e .
:in $ ?m
:where
[?e :user/email ?m]]
(db/db) email))

;; Init the orga group
;; Add orga user by email
(let [{:keys [tempids]} @(db/transact [{:db/id "user-group"
:user-group/orga true
:user-group/user-count 0}])
ug-eid (get tempids "user-group")
u-eid (find-user-eid "humorless@gmail.com")]
@(db/transact [[:db/cas ug-eid :user-group/user-count 0 1]
[:db/add ug-eid :user-group/users u-eid]]))

;; Double check the user-group just created
(db/q
'[:find [(pull ?e [*]) ...]
:where
[?e :user-group/users]]
(db/db))

(def user-entity
(db/entity
(find-user-eid "humorless@gmail.com")))

(type user-entity)

(->
(:user-group/_users user-entity)
first
:user-group/orga)
4 changes: 4 additions & 0 deletions src/co/gaiwan/compass/db/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
[:discord/refresh-token :string "Discord OAuth2 refresh-token"]
[:discord/avatar-url :string "Discord Avatar URL"]

[:user-group/orga :boolean "If this group is orga group or not"]
[:user-group/user-count :long "Number of people in this group"]
[:user-group/users :ref "Reference points to the user" :many]

[:session/code :string "Corresponding Pretalx code, to prevent the import from creating duplicates" :identity]
[:session/title :string "Title of the talk/workshop/activity"]
[:session/subtitle :string "Subtitle of the session, for talks/workshops = speaker names"]
Expand Down
12 changes: 6 additions & 6 deletions src/co/gaiwan/compass/html/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@
m (.toMinutesPart d)]
(str
(when (< 0 h)
(str h " hrs ")
)
(str h " hrs "))
(when (< 0 m)
(str m " min")))))

Expand Down Expand Up @@ -206,7 +205,7 @@
[:div.capacity
[:div "Spots available:"]
[:div (- (or capacity 0) (or signup-count 0))]]
(when (session/organizing? organized user)
(when (session/organizing? session user)
;; Only show the participants' list to organizer.
[:div.participants
[:div "Participants:"]
Expand All @@ -215,11 +214,11 @@
[:p "Ticket Required"])
[:div.actions
[participate-btn session user]
(when (session/organizing? organized user)
(when (session/organizing? session user)
;; Only allow the event organizer to edit this event
[:<>
[:button {:hx-get (str "/sessions/" (:db/id session) "/edit")} "Edit"]
[:button {:hx-delete (str "/sessions/" (:db/id session))}"Delete"]])]
[:button {:hx-delete (str "/sessions/" (:db/id session))} "Delete"]])]
#_[:p.host "Organized by " organized]
#_[:ol (map attendee participants)]
#_[:p (pr-str user)]
Expand Down Expand Up @@ -266,11 +265,12 @@
:flex
:gap-3]]
[:div.date-time :flex :gap-2]
([params]
([user]
[:<>
[:h2 "Create Activity"]
[:form {:method "POST" :action "/sessions"
:enctype "multipart/form-data"}
[:input {:type "hidden" :name "organizer-id" :value (:db/id user)}]
[:label {:for "title"} "Name of Your Activity"]
[:input {:id "title" :name "title" :type "text"
:required true :min-length 2}]
Expand Down
10 changes: 5 additions & 5 deletions src/co/gaiwan/compass/http/middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
(handler
(if-let [uuid (get-in req [:session :identity])]
(assoc req :identity
(db/q '[:find (pull ?u [*]) .
:in $ ?uid
:where [?u :user/uuid ?uid]]
(db/db)
uuid))
(db/entity (db/q '[:find ?u .
:in $ ?uid
:where [?u :user/uuid ?uid]]
(db/db)
uuid)))
req))))
28 changes: 21 additions & 7 deletions src/co/gaiwan/compass/model/session.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@
(:require
[java-time.api :as time]))

(defn participating? [session user]
(defn participating?
"If user participates this session"
[session user]
(some (comp #{(:db/id user)} :db/id)
(:session/participants session)))

(defn organizing? [organized user]
(and
(some? organized)
(= (:db/id user) (:db/id organized))))
(defn organizing?
"If user organizes this session"
[session user]
(let [organized (:session/organized session)]
(and
;; first make sure that user is already login
(some? user)
(or
;; Condition 1: organized property record the user's :db/id
(= (:db/id user)
(:db/id organized))
;; Condition 2: organized property record the user's group :db/id
(some (comp #{(:db/id user)} :db/id)
(:user-group/users organized))
;; Condition 3: The user belongs to orga group
(some :user-group/orga
(:user-group/_users user))))))

;; => {:day :today,
;; :type :all-types,
Expand Down Expand Up @@ -90,5 +105,4 @@
(fn [sessions [k v]]
(apply-filter sessions user k v))
sessions
filters)
)
filters))
16 changes: 8 additions & 8 deletions src/co/gaiwan/compass/routes/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{:status 200
:headers {"HX-Trigger" "login-required"}} #_(util/redirect)
{:html/head [:title "Create new session"]
:html/body [session-html/session-form {}]}))
:html/body [session-html/session-form (:identity req)]}))

(defn GET-session [req]
(let [session-eid (parse-long (get-in req [:path-params :id]))]
Expand All @@ -42,10 +42,9 @@
"convert the Http Post Params to data ready for DB transaction"
[{:keys [title subtitle start-date start-time duration-time description
type location
capacity
capacity organizer-id
ticket-required? published?]
:or {type "activity"}}
identity]
:or {type "activity"}}]
(let [local-date (time/local-date start-date)
local-time (time/local-time start-time)
local-date-time (time/local-date-time local-date local-time)
Expand All @@ -55,13 +54,13 @@
_ (prn :debug-duration duration)]
(cond-> {:db/id "session"
:session/title title
:session/organized (:db/id identity)
:session/subtitle subtitle
:session/time start
:session/duration duration
:session/description description
:session/type (keyword "session.type" type)
:session/location (keyword "location.type" location)
:session/organized (parse-long organizer-id)
:session/signup-count 0
:session/capacity (parse-long capacity)}
(= ticket-required? "on")
Expand All @@ -73,15 +72,16 @@
"Create new session, save to Datomic

The typical params is:
{:name \"dsafa\",
{:organizer-id \"455\"
:name \"dsafa\",
:description \"dsafa\",
:type \"activity\",
:location \"depot-main-stage\",
:capacity \"34\",
:ticket-required? \"on\"
:published? \"on\"}"
[{:keys [params identity]}]
(let [{:keys [tempids]} @(db/transact [(params->session-data params identity)])]
[{:keys [params]}]
(let [{:keys [tempids]} @(db/transact [(params->session-data params)])]
(when (:image params)
(let [{:keys [filename tempfile]} (:image params)
session-eid (get tempids "session")
Expand Down
Loading