-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.cljc
200 lines (182 loc) · 10.4 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(ns sb.app.board.data
(:require [re-db.api :as db]
[sb.app.field.data :as field.data]
[sb.app.entity.data :as entity.data]
[sb.app.membership.data :as member.data]
[sb.authorize :as az]
[sb.query :as q]
[sb.schema :as sch :refer [? s-]]
[sb.server.datalevin :as dl]
[sb.validate :as validate]
[sb.util :as u]
[sb.app.field.data :as field.data]))
(sch/register!
{:board/project-numbers? {s- :boolean
:hint "Assign numbers to this board's projects."}
:board/max-members-per-project {s- :int}
:board/project-sharing-buttons {:hint "Social sharing buttons to be displayed on project detail pages"
s- [:map-of :social/sharing-button :boolean]}
:board/is-template? {:doc "Board is only used as a template for creating other boards"
s- :boolean},
:board/labels {:unsure "How can this be handled w.r.t. locale?"
s- [:map-of [:enum
:label/member.one
:label/member.many
:label/project.one
:label/project.many] :string]},
:board/home-page-message {:hint "Additional instructions for a board, displayed when a member has signed in."
s- :prose/as-map},
:board/max-projects-per-member {s- :int}
:board/sticky-color {:doc "Deprecate - sticky notes can pick their own colors"
s- :html/color}
:board/invite-email-text {:hint "Text of email sent when inviting a user to a board."
s- :string},
:board/registration-newsletter-field? {:hint "During registration, request permission to send the user an email newsletter"
s- :boolean},
:board/registration-open? {:hint "Allows new registrations via the registration page. Does not affect invitations."
s- :boolean},
:board/registration-page-message {:hint "Content displayed on registration screen (before user chooses provider / enters email)"
s- :prose/as-map},
:board/registration-url-override {:hint "URL to redirect user for registration (replaces the Sparkboard registration page, admins are expected to invite users)",
s- :http/url},
:board/registration-codes {s- [:map-of :string [:map {:closed true} [:registration-code/active? :boolean]]]}
:board/new-projects-require-approval? {s- :boolean}
:board/custom-css {s- :string}
:board/custom-js {s- :string}
:board/as-map {s- [:map {:closed true}
:entity/id
:entity/title
:entity/created-at
:entity/public?
:entity/kind
:entity/parent
:board/registration-open?
(? :image/avatar)
(? :image/logo-large)
(? :image/footer)
(? :image/background)
(? :image/sub-header)
(? :entity/website)
(? :entity/meta-description)
(? :entity/description)
(? :entity/domain-name)
(? :entity/locale-default)
(? :entity/locale-dicts)
(? :entity/locale-suggestions)
(? :entity/social-feed)
(? :entity/deleted-at)
(? :entity/created-by)
(? :board/custom-css)
(? :board/custom-js)
(? :board/home-page-message)
(? :board/is-template?)
(? :board/labels)
(? :board/max-members-per-project)
(? :board/max-projects-per-member)
(? :entity/member-fields)
(? :entity/member-tags)
(? :board/new-projects-require-approval?)
(? :entity/project-fields)
(? :board/project-sharing-buttons)
(? :board/registration-codes)
(? :board/invite-email-text)
(? :board/registration-page-message)
(? :board/registration-newsletter-field?)
(? :board/registration-url-override)
(? :board/project-numbers?)
(? :board/slack.team)
(? :board/sticky-color)
(? :member-vote/open?)
(? :webhook/subscriptions)]}})
(q/defx board:register!
[req {:as params registration-data :body}]
;; create membership
)
(q/defquery show
{:prepare [(az/with-roles :board-id)
(member.data/assert-can-view :board-id)]}
[{:keys [board-id membership/roles]}]
(u/timed `show
(if-let [board (db/pull `[~@entity.data/entity-keys
:entity/member-tags
:entity/member-fields
:entity/project-fields
:board/registration-open?
{:entity/parent [~@entity.data/entity-keys :org/show-org-tab?]}]
board-id)]
(merge board {:membership/roles roles})
(throw (ex-info "Board not found!" {:status 400})))))
(def project-fields `[~@entity.data/entity-keys])
(def member-fields [{:membership/member [:entity/id
:entity/kind
{:image/avatar [:entity/id]}
:account/display-name]}
{:entity/tags [:entity/id
:tag/label
:tag/color]}
:entity/field-entries
{:membership/entity [:entity/id]}
{:entity/custom-tags [:tag/label]}
:membership/roles])
(q/defquery members
{:prepare [(az/with-roles :board-id)
(member.data/assert-can-view :board-id)]}
[{:keys [board-id membership/roles]}]
(u/timed `members (->> (db/entity board-id)
:membership/_entity
(remove (some-fn :entity/deleted-at :entity/archived?))
(mapv (db/pull `[~@entity.data/entity-keys
~@member-fields])))))
(q/defquery projects
{:prepare [(az/with-roles :board-id)
(member.data/assert-can-view :board-id)]}
[{:keys [board-id membership/roles]}]
(u/timed `projects
(->> (db/where [[:entity/parent board-id]])
(remove (some-fn :entity/draft? :entity/deleted-at :entity/archived?))
(mapv (db/pull project-fields)))))
(q/defquery drafts
{:prepare az/with-account-id}
[{:keys [account-id board-id]}]
(->> (member.data/membership account-id board-id)
:membership/_member
(filter :entity/draft?)
(map #(db/pull project-fields (:membership/entity %)))))
(defn authorize-edit! [board account-id]
(when-not (or (validate/can-edit? account-id board)
(validate/can-edit? account-id (:entity/parent board)))
(validate/permission-denied!)))
(defn authorize-create! [board account-id]
(when-not (validate/can-edit? account-id (:entity/parent board))
(validate/permission-denied!)))
(q/defx new!
{:prepare [az/with-account-id!]}
[{:keys [board account-id]}]
(let [board (-> (dl/new-entity board :board :by account-id)
(validate/conform :board/as-map))
_ (authorize-create! board account-id)
member (-> {:membership/entity board
:membership/member account-id
:membership/roles #{:role/admin}}
(dl/new-entity :membership))]
(db/transact! [member])
board))
(q/defquery settings
{:prepare [az/with-account-id!
(az/with-roles :board-id)
(fn [_ {:as params :keys [board-id account-id]}]
(validate/assert-can-edit! account-id (dl/entity board-id))
params)]}
[{:keys [board-id membership/roles]}]
(u/timed `settings
(some->
(q/pull `[~@entity.data/entity-keys
:entity/member-tags
{:entity/member-fields ~field.data/field-keys}
{:entity/project-fields ~field.data/field-keys}] board-id)
(merge {:membership/roles roles}))))
(comment
[:ul ;; i18n stuff
[:li "suggested locales:" (str (:entity/locale-suggestions board))]
[:li "default locale:" (str (:i18n/default-locale board))]
[:li "extra-translations:" (str (:i18n/locale-dicts board))]])