-
Notifications
You must be signed in to change notification settings - Fork 16
/
news.ur
345 lines (305 loc) · 16.4 KB
/
news.ur
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
open Bootstrap
datatype access =
Forbidden
| Read
| Post of {User : string, MayEdit : bool, MayDelete : bool}
| Admin of {User : string}
functor Make(M : sig
type title
type title_internal
type title_config
val title : Widget.t title title_internal title_config
val title_inj : sql_injectable title
type body
type body_internal
type body_config
val body : Widget.t body body_internal body_config
val body_inj : sql_injectable body
val access : transaction access
val onNewPost : {Title : title, Poster : string, Body : body}
-> transaction unit
end) = struct
open M
table post : { Title : title, Body : body, Poster : string, When : time }
PRIMARY KEY When
type post = { Title : title, Body : body, Poster : string, When : time }
type editing = {Title : title_internal, Body : body_internal, TitleId : id, BodyId : id}
datatype mode =
Hidden
| Expanded
| Editing of editing
type postr = {Post : source post,
Mode : source mode}
datatype posts =
Nil
| Cons of postr * source posts
datatype action =
Add of post
| Delete of time
| Modify of post
table listeners : {Channel : channel action}
type a = {TitleConfig : title_config,
BodyConfig : body_config,
Access : access,
Head : source posts,
Tail : source (source posts),
NewPost : source (option editing),
Channel : channel action}
val create =
acc <- access;
case acc of
Forbidden => error <xml>Access denied</xml>
| _ =>
tail <- source Nil;
posts <- queryL1 (SELECT *
FROM post
ORDER BY post.When DESC);
head <- List.foldlM (fn p ps =>
p <- source p;
mode <- source Hidden;
source (Cons ({Post = p, Mode = mode}, ps)))
tail posts;
tail <- source tail;
ch <- channel;
dml (INSERT INTO listeners(Channel)
VALUES ({[ch]}));
title <- @Widget.configure title;
body <- @Widget.configure body;
newPost <- source None;
return {TitleConfig = title,
BodyConfig = body,
Access = acc,
Head = head,
Tail = tail,
NewPost = newPost,
Channel = ch}
fun onload a =
let
fun loop () =
act <- recv a.Channel;
(case act of
Add p =>
tl <- get a.Tail;
tl' <- source Nil;
set a.Tail tl';
p <- source p;
mode <- source Hidden;
cell <- return (Cons ({Post = p, Mode = mode}, tl'));
set tl cell;
hd <- get a.Head;
(case hd of
Nil => set a.Head cell
| _ => return ())
| Delete tm =>
let
fun del ps =
v <- get ps;
case v of
Nil => return ()
| Cons (r, ps') =>
p <- get r.Post;
if p.When = tm then
v <- get ps';
set ps v
else
del ps'
in
del a.Head
end
| Modify p =>
let
fun mod ps =
v <- get ps;
case v of
Nil => return ()
| Cons (r, ps') =>
p' <- get r.Post;
if p'.When = p.When then
set r.Post p;
set r.Mode Hidden
else
mod ps'
in
mod a.Head
end);
loop ()
in
spawn (loop ())
end
fun mayAdd acc =
case acc of
Post r => Some r.User
| Admin r => Some r.User
| _ => None
fun mayModify acc u u' =
case acc of
Post r => r.User = u && r.User = u' && r.MayEdit
| Admin _ => True
| _ => False
fun mayDelete acc u =
case acc of
Post r => r.User = u && r.MayDelete
| Admin _ => True
| _ => False
fun add p =
tm <- now;
acc <- access;
case mayAdd acc of
None => error <xml>Access denied</xml>
| Some u =>
p <- return (p ++ {Poster = u, When = tm});
dml (INSERT INTO post(Poster, When, Title, Body)
VALUES ({[u]}, {[tm]}, {[p.Title]}, {[p.Body]}));
queryI1 (SELECT listeners.Channel FROM listeners)
(fn {Channel = ch} => send ch (Add p));
onNewPost {Title = p.Title, Body = p.Body, Poster = u}
fun delete tm =
currentUser <- oneRowE1 (SELECT (post.Poster)
FROM post
WHERE post.When = {[tm]});
acc <- access;
if not (mayDelete acc currentUser) then
error <xml>Access denied</xml>
else
dml (DELETE FROM post
WHERE When = {[tm]});
queryI1 (SELECT listeners.Channel FROM listeners)
(fn {Channel = ch} => send ch (Delete tm))
fun modify p =
currentUser <- oneRowE1 (SELECT (post.Poster)
FROM post
WHERE post.When = {[p.When]});
acc <- access;
if not (mayModify acc currentUser p.Poster) then
error <xml>Access denied</xml>
else
dml (UPDATE post
SET Title = {[p.Title]}, Body = {[p.Body]}
WHERE When = {[p.When]});
queryI1 (SELECT listeners.Channel FROM listeners)
(fn {Channel = ch} => send ch (Modify p))
fun render' ctx a ps = <xml>
<dyn signal={v <- signal ps;
return (case v of
Nil => <xml></xml>
| Cons (r, ps') => <xml>
{render' ctx a ps'}
<div class="card">
<div class="card-header">
<dyn signal={mode <- signal r.Mode;
return (case mode of
Hidden => <xml><button class="btn btn-secondary"
onclick={fn _ => set r.Mode Expanded}>
<span class="glyphicon glyphicon-caret-down"/>
</button></xml>
| _ => <xml><button class="btn btn-secondary"
onclick={fn _ => set r.Mode Hidden}>
<span class="glyphicon glyphicon-caret-up"/>
</button></xml>)}/>
<dyn signal={p <- signal r.Post;
return <xml>
{@Widget.asValue title p.Title} -- {[p.Poster]} at {[p.When]}
{if not (mayModify a.Access p.Poster p.Poster) then
<xml></xml>
else
<xml>
<button class="btn btn-secondary"
onclick={fn _ =>
title <- @Widget.initialize title a.TitleConfig p.Title;
body <- @Widget.initialize body a.BodyConfig p.Body;
tid <- fresh;
bid <- fresh;
set r.Mode (Editing {Title = title, Body = body, TitleId = tid, BodyId = bid})}>
<span class="glyphicon glyphicon-edit"/>
</button>
</xml>}
{if not (mayDelete a.Access p.Poster) then
<xml></xml>
else
Ui.modalButton ctx close
<xml>×</xml>
(return (Ui.modal
(rpc (delete p.When))
<xml>Are you sure you want to delete that post by {[p.Poster]}?</xml>
<xml/>
<xml>Yes!</xml>))}
</xml>}/>
</div>
<dyn signal={mode <- signal r.Mode;
return (case mode of
Hidden => <xml></xml>
| Expanded => <xml>
<div class="card-body">
<dyn signal={p <- signal r.Post;
return (@Widget.asValue body p.Body)}/>
</div>
</xml>
| Editing ed => <xml>
<div class="card-body">
<div class="form-group">
<label class="control-label" for={ed.TitleId}>Title</label>
{@Widget.asWidget title ed.Title (Some ed.TitleId)}
<label class="control-label" for={ed.BodyId}>Body</label>
{@Widget.asWidget body ed.Body (Some ed.BodyId)}
</div>
<dyn signal={p <- signal r.Post;
return <xml>
<button class="btn btn-primary"
value="Save"
onclick={fn _ =>
title <- current (@Widget.value title ed.Title);
body <- current (@Widget.value body ed.Body);
rpc (modify (p -- #Title -- #Body ++ {Title = title, Body = body}));
set r.Mode Expanded}/>
</xml>}/>
<button class="btn btn-secondary"
value="Cancel"
onclick={fn _ => set r.Mode Expanded}/>
</div>
</xml>)}/>
</div>
</xml>)}/>
</xml>
fun render ctx a = <xml>
{if (case a.Access of Forbidden => True | Read => True | _ => False) then
<xml></xml>
else <xml>
<dyn signal={np <- signal a.NewPost;
return (case np of
None => <xml><p><button class="btn btn-primary"
value="New Post"
onclick={fn _ =>
title <- @Widget.create title a.TitleConfig;
body <- @Widget.create body a.BodyConfig;
tid <- fresh;
bid <- fresh;
set a.NewPost (Some {Title = title, Body = body, TitleId = tid, BodyId = bid})}/></p></xml>
| Some np => <xml>
<div class="form-group">
<label class="control-label" for={np.TitleId}>Title</label>
{@Widget.asWidget title np.Title (Some np.TitleId)}
<label class="control-label" for={np.BodyId}>Body</label>
{@Widget.asWidget body np.Body (Some np.BodyId)}
<button class="btn btn-primary"
value="Add"
onclick={fn _ =>
title <- current (@Widget.value title np.Title);
body <- current (@Widget.value body np.Body);
rpc (add {Title = title, Body = body});
set a.NewPost None}/>
<button class="btn btn-secondary"
value="Cancel"
onclick={fn _ => set a.NewPost None}/>
</div>
</xml>)}/>
</xml>}
{render' ctx a a.Head}
</xml>
fun notification _ _ = <xml></xml>
fun buttons _ _ = <xml></xml>
val ui = {Create = create,
Onload = onload,
Render = render,
Notification = notification,
Buttons = buttons}
end