forked from Shirakumo/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.lisp
67 lines (58 loc) · 2.47 KB
/
tag.lisp
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
#|
This file is a part of Courier
(c) 2019 Shirakumo http://tymoon.eu ([email protected])
Author: Nicolas Hafner <[email protected]>
|#
(in-package #:courier)
(defun make-tag (campaign &key title description (save T))
(let ((campaign (ensure-campaign campaign)))
(check-title-exists 'tag title (db:query (:and (:= 'campaign (dm:id campaign))
(:= 'title title))))
(dm:with-model tag ('tag NIL)
(setf-dm-fields tag campaign title description)
(when save (dm:insert tag))
tag)))
(defun edit-tag (tag-ish &key title description (save T))
(let ((tag (ensure-tag tag-ish)))
(setf-dm-fields tag title description)
(when save (dm:save tag))
tag))
(defun ensure-tag (tag-ish)
(or
(etypecase tag-ish
(dm:data-model tag-ish)
(db:id (dm:get-one 'tag (db:query (:= '_id tag-ish))))
(string (ensure-tag (db:ensure-id tag-ish))))
(error 'request-not-found :message "No such tag.")))
(defun delete-tag (tag)
(db:with-transaction ()
(db:remove 'tag-table (db:query (:= 'tag (dm:id tag))))
(delete-triggers-for tag)
(dm:delete tag)))
(defun list-tags (thing &key amount (skip 0) query)
(with-query (query title description)
(ecase (dm:collection thing)
(campaign
(dm:get 'tag (query (:= 'campaign (dm:id thing)))
:sort '((title :asc)) :amount amount :skip skip))
(subscriber
(fixup-ids (dm:get (rdb:join (tag _id) (tag-table tag)) (query (:= 'subscriber (dm:id thing)))
:sort '((title :asc)) :amount amount :skip skip :hull 'tag)
"tag")))))
(defun tagged-p (tag subscriber)
(< 0 (db:count 'tag-table (db:query (:and (:= 'subscriber (ensure-id subscriber))
(:= 'tag (ensure-id tag)))))))
(defun tag (subscriber tag)
(db:with-transaction ()
(let ((tag (ensure-tag tag)))
(unless (tagged-p tag subscriber)
(db:insert 'tag-table `(("tag" . ,(ensure-id tag))
("subscriber" . ,(ensure-id subscriber))))
(process-triggers subscriber tag)))))
(defun untag (subscriber tag)
(db:with-transaction ()
(let ((tag (ensure-tag tag)))
(when (tagged-p tag subscriber)
(db:remove 'tag-table (db:query (:and (:= 'tag (ensure-id tag))
(:= 'subscriber (ensure-id subscriber)))))
(process-triggers subscriber (list-source-triggers tag :type 20))))))