-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.cljc
146 lines (128 loc) · 6.26 KB
/
data.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(ns sb.app.member.data
(:require #?(:clj [java-time.api :as time])
[re-db.api :as db]
[sb.app.entity.data :as entity.data]
[sb.authorize :as az]
[sb.query :as q]
[sb.schema :as sch :refer [? s-]]
[sb.server.datalevin :as dl])
#?(:clj (:import [java.util Date])))
(sch/register!
{:roles/as-map {s- :member/as-map}
:member/last-visited (merge sch/instant
{s- 'inst?})
:member/entity+account (merge {:db/tupleAttrs [:member/entity :member/account]}
sch/unique-value)
:member/_entity {s- [:or
:member/as-map
[:sequential
:member/as-map]]}
:member/role {s- [:enum
:role/admin
:role/editor
:role/member]}
:member/roles (merge sch/keyword
sch/many
{s- [:set :member/role]})
:member/entity (sch/ref :one)
:member/account (sch/ref :one)
:member/tags (sch/ref :many :tag/as-map)
:member/ad-hoc-tags {s- [:sequential [:map {:closed true} :tag/label]]}
;; TODO: move/remove. this should simply be a field that an organizer adds.
:member/newsletter-subscription? {s- :boolean},
;; TODO: move email-frequency to a separate place (focused on notifications)
:member/email-frequency {s- [:enum
:member.email-frequency/never
:member.email-frequency/daily
:member.email-frequency/periodic
:member.email-frequency/instant]}
;; TODO: better define this state.
;; - used for people who are no longer attending / need to be managed by organizers.
;; - different than deleted - user can still sign in to reactivate?
:member/inactive? {:doc "Marks a member inactive, hidden."
:admin true
:todo "If an inactive member signs in to a board, mark as active again?"
s- :boolean},
:member/as-map {s- [:map {:closed true}
:entity/id
:entity/kind
:member/entity
:member/account
(? :member/inactive?)
(? :member/email-frequency)
(? :member/ad-hoc-tags)
(? :member/newsletter-subscription?)
(? :member/tags)
(? :member/roles)
(? :member/last-visited)
;; TODO, backfill?
(? :entity/created-at)
(? :entity/updated-at)
(? :entity/field-entries)
(? :entity/deleted-at)
(? :entity/modified-by)]}})
(q/defquery show
{:prepare az/require-account!}
[params]
(dissoc (q/pull `[{:member/tags [:*]}
{:member/account [~@entity.data/fields
:account/display-name]}]
(:member-id params))
:member/password))
#?(:clj
(defn membership-id [account-id entity-id]
(dl/entid [:member/entity+account [(dl/resolve-id entity-id) (dl/resolve-id account-id)]])))
#?(:clj
(defn ensure-membership! [account-id entity-id]
(when-not (membership-id account-id entity-id)
(throw (ex-info "Not a member" {:status 403})))))
(q/defquery search
{:prepare [az/with-account-id!]}
[{:as params :keys [account-id entity-id search-term]}]
(if entity-id
;; scoped to entity
(do
(ensure-membership! account-id entity-id)
(dl/q '[:find [(pull ?account [:account/display-name
:entity/id
{:image/avatar [:entity/id]}]) ...]
:in $ ?entity ?search-term
:where
[?m :member/entity ?entity]
[?m :member/account ?account]
[(fulltext $ ?search-term {:top 20}) [[?account ?a ?v]]]]
entity-id
search-term))
;; all entities I'm also a member of
(dl/q '[:find [(pull ?your-account [:account/display-name
:entity/id
{:image/avatar [:entity/id]}]) ...]
:in $ ?my-account ?search-term
:where
[?me :member/account ?my-account]
[?me :member/entity ?entity]
[?you :member/entity ?entity]
[?you :member/account ?your-account]
[(fulltext $ ?search-term {:top 20}) [[?your-account ?a ?v]]]]
account-id
search-term)))
(comment
(search {:account-id [:entity/id #uuid "b08f39bf-4f31-3d0b-87a6-ef6a2f702d30"]
;:entity-id [:entity/id #uuid "a1630339-64b3-3604-8110-0f22355e12be"]
:search-term "matt"}))
#?(:clj
(defn member:log-visit! [entity-key]
(fn [req params]
(when-let [id (some-> (-> req :account :entity/id)
(membership-id (entity-key params)))]
(db/transact! [[:db/add id :member/last-visited
(-> (time/offset-date-time)
time/instant
Date/from)]]))
params)))
(defn new-entity-with-membership [entity account-id roles]
{:entity/id (random-uuid)
:entity/kind :member
:member/account (sch/wrap-id account-id)
:member/entity entity
:member/roles roles})