-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpox-model.scm
463 lines (415 loc) · 19.1 KB
/
pox-model.scm
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
(module pox-model
(user-map
user-name->id
user-id->name
string->user-name
string->user-id
select-users
select-tasks
persist-user-tasks
select-user-tasks
conflicts->string
task-list->string
task-list->json-serializable
read-json-tasks
with-db-connection
maybe-create-user)
(import chicken scheme ports srfi-1 srfi-13 data-structures extras)
(require-library regex)
(import irregex)
(use matchable postgresql sql-null spiffy srfi-18 medea)
(use downtime)
(use pox-db pox-db/helpers)
(use pox-notification)
(use pox-log)
(define-logger log model)
(define-log-category notification)
(define (alist-merge l1 l2)
(fold-right (lambda (a l)
(alist-update! (car a) (cdr a) l))
(alist-copy l2)
l1))
(define user-map (make-parameter '()))
(define (select-users)
(row-map (lambda (user)
(cons (car user) (cadr user)))
(db-query '(select (columns id name) (from users)))))
(define (user-name->id name)
(and name
(let ((user (find (lambda (user)
(string=? name (cdr user)))
(user-map))))
(if user
(car user)
(error (sprintf "user does not exist: ~A" name) name)))))
(define (user-id->name id)
(alist-ref id (user-map)))
(define (string->user-name user)
(if (irregex-match '(+ num) user) (user-id->name user) user))
(define (string->user-id user)
(if (irregex-match '(+ num) user)
(string->number user)
(user-name->id user)))
(define (maybe-create-user name)
(condition-case
(db-query '(insert (into users) (columns name) (values #($1))) (list name))
(exn (postgresql)
(unless (equal? "23505" (get-condition-property exn 'postgresql 'error-code))
(signal exn)))))
(define (remove-sql-nulls l)
(remove (compose sql-null? cdr) l))
(define tasks-query
`(select (columns creator creator_id
assigner assigner_id
assignee assignee_id
updater updater_id
id
created_at
revision
name
description
priority
done
category
tags)
(from tasks_with_tags)))
(define (select-tasks #!optional (additional '()) (vars '()))
(db-query (db-compose-query tasks-query additional) vars))
(define (select-task id)
(db-query (db-compose-query tasks-query `((where (= id ,id))))))
(define (partition* lst . preds)
(apply values
(let loop ((lst lst) (preds preds))
(if (null? preds)
(list lst)
(receive (m rest) (partition (car preds) lst)
(cons m (loop rest (cdr preds))))))))
(define (prepare-filters filters)
(partition* (or filters '()) keyword? number?))
(define (select-tasks groups filters include-done #!optional additional-conditions)
(receive (tag-filters id-filters filters)
(prepare-filters filters)
(let* ((conditions (if (pair? filters)
`((and . ,(map (lambda (i)
(let ((var (string->symbol (sprintf "$~A" i))))
`(or (like name ,var)
(like description ,var)
(like category ,var))))
(iota (length filters) 1))))
'()))
(tag-filters (map keyword->string tag-filters))
(conditions (if (pair? tag-filters)
`((and (@> tags (array . ,tag-filters))
. ,conditions))
conditions))
(conditions (if (pair? id-filters)
`((and (in id #(,@id-filters))
. ,conditions))
conditions))
(conditions (if include-done
conditions
`((and (= done #f) . ,conditions))))
(conditions (if additional-conditions
`((and ,additional-conditions . ,conditions))
conditions))
(result (db-query (db-compose-query tasks-query
`((order (desc priority) (asc created_at) (asc id))
. ,(if (null? conditions)
'()
`((where . ,conditions)))))
(map (cut sprintf "%~A%" <>) filters)))
(tasks (result->tasks result #t)))
(tasks-group-by groups tasks))))
(define (select-user-tasks user groups filters include-done)
(select-tasks groups filters include-done `(in ,user #(assignee assigner creator))))
(define (tasks-group-by groups tasks)
(if (and groups (pair? groups))
(let* ((group (car groups))
(group-str (symbol->string group))
(tag (and (eq? #\: (string-ref group-str 0))
(string-drop group-str 1)))
(group (if tag 'tags group)))
(cons* 'group
group
(map (lambda (pair)
(cons (car pair) (tasks-group-by (cdr groups) (cdr pair))))
(fold-right (lambda (task result)
(let* ((value (alist-ref group task))
(included? (or (not tag) (and value (member tag value equal?))))
(value (if (and tag included?)
tag
value)))
(if included?
(alist-update! value
(cons task (alist-ref value result equal? '()))
result equal?)
result)))
'()
tasks))))
tasks))
(define (->number s)
(and s (string->number (->string s))))
(define (select-user-notifications)
(fold (lambda (n result)
(let ((user-id (alist-ref 'user_id n)))
(alist-update! user-id
(cons (cons (notification-ref (string->symbol (alist-ref 'name n))) n)
(or (alist-ref user-id result) '()))
result
eq?)))
'()
(map (lambda (notification)
(let ((params (alist-ref 'params notification)))
(alist-update! 'params
(if (string? params)
(with-input-from-string params read-file)
'())
notification)))
(result->alists (db-query '(select (columns un.id un.user_id un.params nh.name)
(from (join inner
(as user_notifications un)
(as notification_handlers nh)
(on (= nh.id un.handler_id))))))))))
(define tasks-diff?
(let ((attrs '(description name done assignee_id assigner_id priority category)))
(lambda (task1 task2 ignore)
(log (debug tasks-diff?)
(cons 'task1 task1)
(cons 'task2 task2)
(cons 'ignore ignore))
(or (any (lambda (col)
(not (equal? (alist-ref col task2)
(alist-ref col task1))))
(lset-difference eq? attrs ignore))
(not (or (memq 'tags ignore)
(lset= equal?
(or (alist-ref 'tags task1) '())
(or (alist-ref 'tags task2) '()))))))))
;; FIXME: this is probably not required anymore as tasks should always contain user names now
;; ok, we still need for adding the names back in after saving a task
(define (task-with-user-names task)
(fold (lambda (field task)
(alist-update! field
(alist-ref (alist-ref (string->symbol (sprintf "~A_id" field)) task) (user-map))
task))
task
'(assigner assignee creator updater)))
(define (task-user-id-field? f)
(memq (car f) '(assigner_id assignee_id creator_id updater_id)))
(define (task-notifyees task)
(delete-duplicates (map cdr (filter task-user-id-field? task))))
(define (format-error e)
(format "Error: ~A~A"
(get-condition-property e 'exn 'message)
(let ((args (get-condition-property e 'exn 'arguments)))
(if (null? args)
""
(apply conc (cons ": " args))))))
(define (group-changes-by-notifyees user-id changes)
(fold (lambda (change notifyees)
(fold (lambda (notifyee notifyees)
(alist-update! notifyee
(cons change
(or (alist-ref notifyee notifyees) '()))
notifyees))
notifyees
(delete user-id (task-notifyees (cadr change)))))
'()
changes))
;; Notifies all affected users about changes (a list of the structure
;; (<event> <new-task> <old-task>)) with event being either `insert'
;; or `update'.
;;
;; TODO: Should perhaps also notify users that *were* affected in the
;; old-task but aren't anymore in new-task. Currently only users
;; affected in new-task are notified.
(define (notify-users user-id changes)
(unless (null? changes)
(thread-start!
(lambda ()
(with-db-connection
(lambda ()
(let ((notifyees (group-changes-by-notifyees user-id changes))
(user-notifications (select-user-notifications)))
(for-each (lambda (notifyee/changes)
(and-let* ((notifyee (car notifyee/changes))
(changes (cdr notifyee/changes))
(notifications (alist-ref notifyee user-notifications))
(notifyee (user-id->name notifyee))
(user (user-id->name user-id)))
(log (info notification)
(cons 'user user)
(cons 'notifyee notifyee))
(for-each (lambda (n)
(log (debug notification)
(cons 'notification (cdr n))
(cons 'changes changes))
(thread-start!
(lambda ()
(condition-case
((car n)
user
notifyee
(alist-ref 'params (cdr n))
changes)
(exn () (log-to (error-log)
"Error with notification ~A for ~A: ~S"
(alist-ref 'id (cdr n))
user
(format-error exn)))))))
notifications)))
notifyees))))))))
(define (row-task row #!optional remove-sql-nulls? (num 0))
(let* ((task (row-alist row num))
(task (if remove-sql-nulls? (remove-sql-nulls task) task))
(tags (alist-ref 'tags task)))
(alist-update! 'tags (vector->list tags) task)))
(define (result->tasks result #!optional remove-sql-nulls?)
(map (lambda (i)
(row-task result remove-sql-nulls? i))
(iota (row-count result))))
(define (select-task-tags task-id)
(result->alists
(db-query `(select (columns tt.id t.name)
(from (join inner
(as task_tags tt)
(as tags t)
(on (= t.id tt.tag_id))))
(where (= tt.task_id ,task-id))))))
(define (select-tag-id tag)
(or (db-select-one 'tags 'name tag 'id)
(value-at (db-query `(insert (into tags) (columns name) (values #($1)) (returning id))
(list tag)))))
(define (persist-tags task)
(let* ((tags (alist-ref 'tags task))
(tags (and tags (delete-duplicates tags equal?)))
(id (alist-ref 'id task)))
(if (or (not tags) (null? tags))
(db-query `(delete (from task_tags) (where (= task_id ,id))))
(let* ((old-tags (fold (lambda (tag old-tags)
(let ((old-tags* (remove (lambda (old-tag)
(equal? tag (alist-ref 'name old-tag)))
old-tags)))
(when (= (length old-tags) (length old-tags*))
(db-query `(insert (into task_tags)
(columns task_id tag_id)
(values #($1 $2)))
(list id (select-tag-id tag))))
old-tags*))
(select-task-tags id)
tags))
(old-tags (map (lambda (tag)
(alist-ref 'id tag))
old-tags)))
(unless (null? old-tags)
(db-query `(delete (from task_tags)
(where (in id ,(list->vector old-tags))))))))))
(define persist-user-tasks
(let* ((prepare (lambda (user-id task old-task)
(let* ((task (alist-merge `((description . ,(or (alist-ref 'description task) (sql-null)))
(category . ,(or (alist-ref 'category task) (sql-null)))
(done . ,(alist-ref 'done task))
(creator_id . ,(if old-task (alist-ref 'creator_id old-task) user-id))
(updater_id . ,user-id)
(assignee_id . ,(or (user-name->id (alist-ref 'assignee task)) user-id))
(assigner_id . ,(or (user-name->id (alist-ref 'assigner task)) user-id))
(priority . ,(or (->number (alist-ref 'priority task)) 0))
(revision . ,(add1 (or (->number (alist-ref 'revision task)) 1))))
task))
(task (alist-delete 'assignee task))
(task (alist-delete 'assigner task)))
(remove-sql-nulls task))))
(insert (lambda (task user-id)
(let* ((task (alist-update! 'creator_id user-id task))
(task (alist-delete 'revision task))
(task (alist-delete 'tags task)))
(alist->ssql-insert 'tasks task))))
(update (lambda (task user-id)
(alist->ssql-update 'tasks (alist-delete 'tags task)
conditions: `(= revision ,(sub1 (alist-ref 'revision task))))))
(save (lambda (user-id task preamble)
(log (debug save)
(cons 'preamble preamble))
(let* ((old-task (alist-ref 'id task))
(old-task (and old-task (select-task old-task)))
(old-task (and old-task
(< 0 (row-count old-task))
(row-task old-task #t)))
(task (prepare user-id task old-task))
(action (if old-task
(and (tasks-diff? old-task task (or (alist-ref 'ignore preamble) '())) 'update)
'insert))
(statement (case action
((update) (update task user-id))
((insert) (insert task user-id))
(else #f)))
(result (and statement
(db-query (db-compose-query (car statement) '((returning id revision)))
(cdr statement))))
(task (if (and result (not (zero? (row-count result))))
(alist-merge (row-alist result) task)
task))
(task (task-with-user-names task))
(affected (and result (affected-rows result))))
(or (not affected)
(and (= 1 affected)
(begin
(persist-tags task)
(list action task old-task)))))))
(save-all (lambda (user-id tasks)
(log (debug save-all)
(cons 'tasks tasks))
(let ((preamble (car tasks))
(tasks (cdr tasks)))
(fold-right (lambda (task result)
(let ((change (save user-id task preamble)))
(if change
(if (pair? change)
(cons (cons change (car result))
(cdr result))
result)
(let* ((new-task (select-task (alist-ref 'id task)))
(new-task (and (< 0 (row-count new-task)) (row-task new-task #t)))
(task (if new-task
(alist-update! 'revision (alist-ref 'revision new-task) task)
task)))
(cons (car result)
(cons (list task new-task)
(cdr result)))))))
'(())
tasks)))))
(lambda (user-id tasks)
(with-transaction (db-connection)
(lambda ()
(let ((results (save-all user-id tasks)))
(notify-users user-id (car results))
(cdr results)))))))
(define (task-list->string tasks user #!optional origin (ignore '()) (skip '(user)))
(with-output-to-string
(lambda ()
(downtime-write `(downtime
((user . ,user)
(origin . ,origin)
(ignore . ,ignore))
. ,tasks)
skip))))
(define (conflicts->string conflicts user)
(string-intersperse (map (lambda (conflict)
(string-intersperse (map (lambda (task)
(task->string task user)) conflict)
"\n# ---\n\n"))
conflicts)
"\n\n# =====\n\n"))
(define (task-list->json-serializable tasks)
(list->vector (map (lambda (task)
(let* ((tags (or (alist-ref 'tags task) '()))
(task (alist-delete 'creator task))
(task (alist-delete 'creator_id task)))
(alist-update! 'tags (list->vector tags) task)))
tasks)))
(define task-json-parsers
(alist-cons 'array identity (json-parsers)))
(define (read-json-tasks str)
(parameterize ((json-parsers task-json-parsers))
(read-json str)))
)