summaryrefslogtreecommitdiff
path: root/dynamic/capabilities/comments.scm
blob: 6786474d6f3694f124dfa328acd0e362fdca6c10 (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
;;; Copyright © 2019 - 2022 Jakob L. Kreuze <zerodaysfordays@sdf.org>
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program 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 GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (dynamic capabilities comments)
  #:use-module (dynamic util)
  #:use-module (ice-9 match)
  #:use-module (json)
  #:use-module (squee)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (web uri)
  #:export (get-comments put-comment put-reaction))

(define conn (connect-to-postgres-paramstring "dbname=jakob_comments"))

(define (get-comments-slug slug)
  (define (format-comment comment)
    (match comment
      ((id name subject email comment reactions)
       `((id . ,id)
         (name . ,name)
         (subject . ,subject)
         (email . ,email)
         (comment . ,comment)
         (reactions . ,reactions)))))
  (let* ((query "SELECT id, name, subject, email, comment, reactions
                 FROM comments WHERE slug = $1")
         (result (exec-query conn query (list (car slug)))))
    (values '((content-type . (application/json)))
            (scm->json-string
             (list->vector
              (map format-comment result))))))

(define (get-comments request body)
  (let* ((query-string (uri-query (request-uri request)))
         (params (if query-string
                     (decode-form query-string)
                     '()))
         (slug (assoc-ref params "p")))
    (if slug
        (get-comments-slug slug)
        (values (build-response #:code 400)
                (scm->json-string
                 `((success . #f)
                   (error . "missing `slug' query parameter")))))))



(define (assoc-value alist key)
  (let ((result (assoc-ref alist key)))
    (if result (car result) result)))

(define (insert-comment form-data)
  (exec-query conn
     "INSERT INTO comments (submitted, slug, name, subject, email, url, comment)
      VALUES (now(), $1, $2, $3, $4, $5, $6);"
     (list (assoc-value form-data "slug")
           (assoc-value form-data "name")
           (assoc-value form-data "subject")
           (assoc-value form-data "email")
           (assoc-value form-data "url")
           (assoc-value form-data "comment")))
  (values '((content-type . (text/plain))) "Hello hacker!"))

(define (valid-comment? form-data)
  (and (assoc "slug" form-data)
       (assoc "name" form-data)
       (assoc "comment" form-data)))

(define (put-comment request body)
  (let ((form-data (decode-form body)))
    (if (valid-comment? form-data)
        (insert-comment form-data)
        (values (build-response #:code 400)
                (scm->json-string
                 `((success . #f)
                   (error . "missing `slug', `name', or `comment'")))))))



(define (comment-reactions id)
  (let* ((query "SELECT reactions FROM comments WHERE id = $1")
         (result (exec-query conn query (list id))))
    ;; It could be NULL, in which case we want the empty list instead.
    (if (positive? (length result)) (or (caar result) "()") #f)))

(define (acons-normalize key value alist)
  (cons (cons key value)
        (filter (lambda (pair) (not (equal? (car pair) key))) alist)))

(define (add-reaction reactions reaction)
  (with-output-to-string
    (lambda ()
      (let ((parsed (call-with-input-string reactions read)))
        (write (acons-normalize reaction
                                (if (assoc reaction parsed) (+ 1 (assoc-value parsed reaction)) 1)
                                parsed))))))

(define (set-reactions id reactions)
  (exec-query conn
     "UPDATE comments SET reactions = $1 WHERE id = $2"
     (list reactions id))
  (values '((content-type . (text/plain))) "Reacted hacker!"))

;; TODO: Need to validate `form-data' first.
(define (put-reaction request body)
  (let* ((form-data (decode-form body))
         (id (assoc-value form-data "id"))
         (reaction (assoc-value form-data "reaction"))
         (reactions (comment-reactions id)))
    (if reactions
        (set-reactions id (add-reaction reactions reaction))
        (values (build-response #:code 400)
                (scm->json-string
                 `((success . #f)
                   (error . "no such comment")))))))

;; (define (make-challenge request body)
;;   (let-values (((uuid value image) (new-captcha)))
;;     (hash-set! challenges uuid value)
;;     (hash-for-each (lambda (x y) (display x) (newline)) challenges)
;;     (values `((content-type . (application/base64))
;;               (access-control-allow-origin . "*")
;;               (x-captcha-id . ,uuid))
;;             (base64-encode image))))