aboutsummaryrefslogtreecommitdiffstats
path: root/scrumli.lisp
blob: 7b324fbd40531aff8d379b377891b49a1c42fccc (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
(in-package #:scrumli)

(defvar *scrumli-host* "http://localhost:8080"
  "The host currently running Scrumli. Used by Mozilla Persona.")

(defvar *scrumli-bootstrap-css-location*
  "http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css"
  "The location of the twitter bootstrap CSS file.")

(defvar *scrumli-bootstrap-js-location*
  "http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"
  "The location of the twitter bootstrap JS file.")

(defvar *scrumli-font-awesome-css-location*
  "http://netdna.bootstrapcdn.com/font-awesome/3.1.1/css/font-awesome.min.css"
  "The location of the font awesome CSS file.")

(defvar *scrumli-jquery-js-location*
  "http://code.jquery.com/jquery-2.0.0.min.js"
  "The location of the jQuery JS file.")

(defvar *scrumli-react-js-location*
  "http://cdnjs.cloudflare.com/ajax/libs/react/0.3.2/react.min.js"
  "The location of the React JS file.")

(defvar *scrumli-jsxtransformer-js-location*
  "http://cdnjs.cloudflare.com/ajax/libs/react/0.3.2/JSXTransformer.js"
  "The location of the JSX Transformer JS file.")

(defun logged-in-p ()
  (hunchentoot:session-value :username))

(defun page-title (title)
  (concatenate 'string title " | scrumli"))

(defun css (&rest sheets)
  (apply 'concatenate 'string
         (mapcar (lambda (s)
                   (<:link :href s :rel "stylesheet" :type "text/css"))
                 sheets)))

(defun js (&rest scripts)
  (apply 'concatenate 'string
         (mapcar (lambda (s)
                   (<:script :type "text/javascript" :src s))
                 scripts)))

(defmacro navbar (&body body)
  `(<:div :class "navbar navbar-static-top navbar-inverse"
          (<:div :class "navbar-inner"
                 (<:div :class "container"
                        (<:a :class "brand" "scrumli")
                        ,@body))))

(define-route main ("")
  (if (logged-in-p)
      (scrumli-templates:main
       `(:title ,(page-title "Backlog")
         :csss ,(list *scrumli-bootstrap-css-location*
                      *scrumli-font-awesome-css-location*
                      (genurl 'scrumli-css))
         :jss ,(list *scrumli-bootstrap-js-location*
                     *scrumli-jquery-js-location*
                     *scrumli-react-js-location*
                     *scrumli-jsxtransformer-js-location*)
         :username ,(hunchentoot:session-value :username)
         :ulogout ,(genurl 'logout-page)
         :umainjs ,(genurl 'main-js)))
      (redirect 'login-page)))

(defmacro serve-static (name relpath)
  `(define-route ,name (,relpath :content-type "application/ecmascript")
     (merge-pathnames ,relpath *static-directory*)))

(serve-static main-js "js/main.js")
(serve-static login-js "js/login.js")
(serve-static scrumli-css "css/scrumli.css")

(define-route stories-json ("stories" :content-type "text/json")
  (if (logged-in-p)
      (encode-json-to-string (get-all-stories))
      403))

(defmacro with-post-parameters (parameters &body body)
  `(let ,(mapcar (lambda (p)
                   (list (intern (string-upcase p))
                         `(hunchentoot:post-parameter ,p)))
                 parameters)
     ,@body))

(define-route stories-new ("stories/new" :method :post
                                         :content-type "text/json")
  (if (logged-in-p)
      (with-post-parameters ("role" "necessity" "headline" "content")
        (post-story role necessity headline content
                    (hunchentoot:session-value :username))
        (encode-json-to-string '((status . "ok"))))
      403))

(define-route tasks-new ("stories/tasks/new" :method :post
                                             :content-type "text/json")
  (if (logged-in-p)
      (with-post-parameters ("storyId" "description")
        (post-task storyid description
                   (hunchentoot:session-value :username))
        (encode-json-to-string '((status . "ok"))))
      403))

(define-route stories-state ("stories/state" :method :post
                                             :content-type "text/json")
  (if (logged-in-p)
      (let* ((id (hunchentoot:post-parameter "id"))
             (current-state (story-get-state 'story id))
             (next (ecase (intern current-state :scrumli)
                     (todo "DOING")
                     (doing "DONE")
                     (done "TODO"))))
        (story-set-state 'story id next)
        (encode-json-to-string `((status . "ok") (state . ,next))))
      403))

(define-route task-state ("tasks/state" :method :post
                                        :content-type "text/json")
  (if (logged-in-p)
      (let* ((id (hunchentoot:post-parameter "id"))
             (current-state (story-get-state 'task id))
             (next (ecase (intern current-state :scrumli)
                     (todo "DOING")
                     (doing "DONE")
                     (done "TODO"))))
        (story-set-state 'task id next)
        (encode-json-to-string `((status . "ok") (state . ,next))))
      403))

(define-route stories-priority ("stories/:dir" :method :post
                                               :content-type "text/json")
  (if (logged-in-p)
      (let* ((id (hunchentoot:post-parameter "id")))
        (story-change-priority
         'story id (intern (string-upcase dir) :keyword))
        (encode-json-to-string '((status . "ok"))))
      403))

(define-route task-priority ("tasks/:dir" :method :post
                                          :content-type "text/json")
  (if (logged-in-p)
      (let* ((id (hunchentoot:post-parameter "id")))
        (story-change-priority
         'task id (intern (string-upcase dir) :keyword))
        (encode-json-to-string '((status . "ok"))))
      403))

(define-route login-page ("login")
  (if (not (logged-in-p))
      (scrumli-templates:login
       `(:title ,(page-title "Login")
         :csss ,(list *scrumli-bootstrap-css-location*)
         :jss ,(list *scrumli-bootstrap-js-location*
                     "https://login.persona.org/include.js"
                     (genurl 'login-js))))
      (redirect 'main)))

(define-route logout-page ("logout")
  (if (logged-in-p)
      (setf (hunchentoot:session-value :username) nil))
  (redirect 'login-page))

(defun verify-credentials (audience assertion)
  (let ((response
         (http-request "https://verifier.login.persona.org/verify"
                       :method :post :content-type "application/json"
                       :content (encode-json-to-string
                                 `(("assertion" . ,assertion)
                                   ("audience" . ,audience)))
                       :want-stream t)))
    (decode-json response)))

(define-route login-page/post ("login" :method :post)
  (let ((result (verify-credentials
                 *scrumli-host*
                 (hunchentoot:post-parameter "assertion"))))
    (if (equal (cdr (assoc :status result)) "okay")
        (progn
          (hunchentoot:start-session)
          (setf (hunchentoot:session-value :username)
                (cdr (assoc :email result)))
          (redirect 'main))
        403)))

(define-route scrumli-story ("stories/:id" :content-type "json")
  (if (logged-in-p)
      (encode-json-to-string (get-story id))
      403))

(define-route scrumli-story-tasks ("stories/:id/tasks"
                                   :content-type "json")
  (if (logged-in-p)
      (encode-json-to-string (get-tasks-for-story id))
      403))