summaryrefslogtreecommitdiff
path: root/journal-content.lisp
blob: 65a8a5943a3fd6af269a0b6431e93ff0c4b2061d (plain)
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
;;;; -*- coding: utf-8; mode: lisp -*-
;;;; Copyright 2007, Matthias Andreas Benkard.

;;;------------------------------------------------------------------------
;;; This file is part of The Mulkblog Project.
;;;
;;; The Mulkblog Project is free software.  You can redistribute it and/or
;;; modify it under the terms of the Affero General Public License as
;;; published by Affero, Inc.; either version 1 of the License, or
;;; (at your option) any later version.
;;;
;;; The Mulkblog Project is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty
;;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the Affero General Public
;;; License in the COPYING file that comes with The Mulkblog Project; if
;;; not, write to Affero, Inc., 510 Third Street, Suite 225, San
;;; Francisco, CA 94107 USA.
;;;------------------------------------------------------------------------

(in-package #:mulk.journal)


;;; (@* "Class definitions")
(clsql:def-view-class journal-entry ()
  ((id :db-kind :key
       :type integer
       :db-constraints :not-null
       :accessor id-of
       :initarg :id)
   (uuid :type (string 36)
         :db-constraints :not-null
         :accessor uuid-of
         :initarg :uuid)
   (title :type string
          :db-constraints :not-null
          :accessor title-of
          :initarg :title
          :initform "")
   (date :type universal-time
         :db-constraints :not-null
         :accessor date-of
         :initarg :date)
   (last-modification :type integer
                      :accessor last-modification-of
                      :initarg :last-modification
                      :initform nil)
   (body :type string
         :db-constraints :not-null
         :accessor body-of
         :initarg :body
         :initform "")
   (categories :db-kind :join
               :db-constraints :not-null
               :accessor categories-of
               :initarg :categories
               :initform '()
               :db-info (:join-class journal-category
                         :home-key id
                         :foreign-key entry-id
                         :set t))
   (comments :db-kind :join
             :db-constraints :not-null
             :accessor %comments-about
             :initarg :comments
             :db-info (:join-class journal-comment
                       :home-key id
                       :foreign-key entry-id
                       :set t))))


(clsql:def-view-class journal-comment ()
  ((id :db-kind :key
       :type integer
       :db-constraints :not-null
       :accessor id-of
       :initarg :id)
   (entry-id :db-kind :key
             :type integer
             :db-constraints :not-null
             :accessor id-of
             :initarg :entry-id)
   (entry :db-kind :join
          :db-constraints :not-null
          :accessor entry-of
          :initarg :entries
          :initform '()
          :db-info (:join-class journal-entry
                    :home-key entry-id
                    :foreign-key id
                    :set nil))
   (uuid :type (string 36)
         :db-constraints :not-null
         :accessor uuid-of
         :initarg :uuid)
   (date :type universal-time
         :db-constraints :not-null
         :accessor date-of
         :initarg :date)
   (body :type string
         :db-constraints :not-null
         :accessor body-of
         :initarg :body
         :initform "")
   (author :type string
           :accessor author-of
           :initarg :author
           :initform nil)
   (email :type string
          :accessor email-of
          :initarg :email
          :initform nil)
   (website :type string
            :accessor website-of
            :initarg :website
            :initform nil)))


(clsql:def-view-class journal-category ()
  ((id :db-kind :key
       :type integer
       :db-constraints :not-null
       :accessor id-of
       :initarg :id)
   (uuid :type (string 36)
         :db-constraints :not-null
         :accessor uuid-of
         :initarg :uuid)
   (entries :db-kind :join
            :db-constraints :not-null
            :accessor entries-in
            :initarg :entries
            :initform '()
            :db-info (:join-class journal-entry
                      :home-key id
                      :foreign-key catogory-ids
                      :set t))))


;; (@* "Journal entry operations")
(defgeneric comments-about (thing &key ordered-p))
(defgeneric (setf comments-about) (new-value thing &key ordered-p))

(defmethod comments-about ((journal-entry journal-entry) &key ordered-p)
  #.(locally-enable-sql-reader-syntax)
  (prog1 (if ordered-p
             (mapcar #'car
                     (select 'journal-comment 'journal-entry
                             :where [= [slot-value 'journal-comment 'entry-id]
                                       [slot-value 'journal-entry 'id]]
                             :order-by '([journal-comment.date])
                             :flatp t))
             (%comments-about journal-entry))
    #.(restore-sql-reader-syntax-state)))


(defmethod (setf comments-about) (new-value
                                  (journal-entry journal-entry)
                                  &key ordered-p)
  (declare (ignore ordered-p))
  (setf (%comments-about journal-entry) new-value))


(defun make-journal-entry-id ()
  #.(locally-enable-sql-reader-syntax)
  (prog1
      (1+ (or (single-object (select [max [slot-value 'journal-entry 'id]]
                                     :from [journal-entry]
                                     :flatp t))
              -1))
    #.(restore-sql-reader-syntax-state)))


(defun make-journal-comment-id ()
  #.(locally-enable-sql-reader-syntax)
  (prog1
      (1+ (or (single-object (select [max [slot-value 'journal-comment 'id]]
                                     :from [journal-comment]
                                     :flatp t))
              -1))
    #.(restore-sql-reader-syntax-state)))


(defun find-entry (number)
  #.(locally-enable-sql-reader-syntax)
  (prog1
    (single-object (select 'journal-entry
                           :where [= [slot-value 'journal-entry 'id] number]
                           :flatp t)
                   nil)
    #.(restore-sql-reader-syntax-state)))


(defun journal-markup->html (markup)
  (if (string= "" markup)
      markup
      (handler-bind
          ((error   ;; method-call-type-error or not
            ;; Work around a weird bug in cl-markdown or CLISP.  (I
            ;; don't know which.)
            #'(lambda (c)
                (declare (ignore c))
                #+nil (<:as-html
                       (with-output-to-string (s)
                         (system::pretty-print-condition c s)))
                (invoke-restart 'return nil))))
        (fixup-markdown-output
         (with-output-to-string (s)
           ;; Normally, we shouldn't need to create our own stream to
           ;; write into, but this is, of course, yet another
           ;; CLISP/Markdown hack, because Markdown's default
           ;; *OUTPUT-STREAM* seems to spontaneously close itself, making
           ;; everything break when Markdown tries to render more stuff.
           (markdown markup :stream s))))))


(defun compute-journal-last-modified-date ()
  #.(locally-enable-sql-reader-syntax)
  #-clisp (get-universal-time)
  #+clisp
  (max (compute-script-last-modified-date)
       (select [max [slot-value 'journal-entry 'last-modification]])
       (select [max [slot-value 'journal-entry 'date]])
       (select [max [slot-value 'journal-comment 'date]]))
  #.(restore-sql-reader-syntax-state))