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
|
;;; Copyright © 2019 - 2023 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 (jakob utils comments)
#:use-module (commonmark)
#:use-module (gcrypt base16)
#:use-module (gcrypt hash)
#:use-module (ice-9 iconv)
#:use-module (ice-9 match)
#:use-module (ice-9 receive)
#:use-module (jakob dynamic capabilities common)
#:use-module (jakob dynamic util)
#:use-module (jakob utils)
#:use-module (json)
#:use-module (oop goops)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-19)
#:use-module (srfi srfi-43)
#:use-module (srfi-197)
#:use-module (web client)
#:use-module (web response)
#:export (render-comment-view
fetch-comments
fetch-recent-comments
fetch-webmentions))
(define (gravatar-url email)
"Return the gravatar.com URL for user identified by EMAIL"
(chain email
(string-downcase _)
(string-trim-both _)
(string->bytevector _ "utf8")
(bytevector-hash _ (lookup-hash-algorithm 'md5))
(bytevector->base16-string _)
(format #f "https://www.gravatar.com/avatar/~a" _)))
(define (safe-markdown->sxml text)
"Convert TEXT to an sxml form filtering out any unsafe entities"
(define (sanitize sexp)
(cond ((and (list? sexp)
(positive? (length sexp))
(eqv? 'img (car sexp)))
#f)
((list? sexp)
(filter identity (map sanitize sexp)))
(else sexp)))
(sanitize (commonmark->sxml text)))
(define-record-type <webmention>
(make-webmention name photo comment url publish-time)
webmention?
(name webmention-name)
(photo webmention-photo)
(comment webmention-comment)
(url webmention-url)
(publish-time webmention-publish-time))
(define* (format-comment comment #:key (reply? #f))
"Format `comment', an alist, as SXML for a comment-type interaction"
(define (strip uri)
"Attempt to remove any sort of protocol specification from `uri'"
(let* ((needle "://")
(index (string-contains uri needle)))
(if index
(strip (substring uri (+ index (string-length needle))))
uri)))
(define (comment-photo comment)
(cond ((and (webmention? comment)
(webmention-photo comment))
(webmention-photo comment))
((and (internal-comment? comment)
(internal-comment-email comment))
(gravatar-url (internal-comment-email comment)))
(else "/static/image/bonus/icons-24/user.png")))
(define (comment-name comment)
((if (webmention? comment)
webmention-name
internal-comment-name)
comment))
(define (comment-content comment)
(if (webmention? comment)
`((p ,(webmention-comment comment)))
(safe-markdown->sxml
(internal-comment-comment comment))))
(define (comment-url comment)
(define url
((if (webmention? comment)
webmention-url
internal-comment-url)
comment))
(if (not (or (string-prefix? "http://" url)
(string-prefix? "https://" url)))
(format #f "http://~a" url)
url))
(define (comment-publish-time comment)
((if (webmention? comment)
webmention-publish-time
internal-comment-publish-time)
comment))
(define (comment-reactions comment)
(if (webmention? comment)
'()
(internal-comment-reactions comment)))
(define (comment-subject comment)
(if (webmention? comment)
#f
(internal-comment-subject comment)))
`(article (@ ,@(if (not (webmention? comment))
`((id ,(format #f "comment-~a" (internal-comment-id comment))))
'())
,(if reply?
'(class "comment-block reply")
'(class "comment-block")))
(header (@ (class "comment-meta"))
(img (@ (class "comment-avatar")
(src ,(comment-photo comment))
(alt "User Icon")))
(div (@ (class "comment-info"))
(strong ,(comment-name comment))
,@(if (comment-subject comment)
`((span (@ (class "subject")) ,(comment-subject comment)(span (@ (class "subject")))))
'())
(br)
,@(if (and (comment-url comment)
(not (string= "" (comment-url comment)))
(not (string= "http://" (comment-url comment)))
(not (string= "https://" (comment-url comment))))
`((span (a (@ (href ,(comment-url comment)))
,(comment-url comment))))
'())
(br)
(time (@ (class "date-stamp")
(datetime ,(date->string (comment-publish-time comment) "~Y-~m-~d")))
,(date->string (comment-publish-time comment) "~B ~e, ~Y at ~H:~M"))))
(div (@ (class "comment-body"))
,@(comment-content comment))
,@(if (not (webmention? comment))
`((footer (@ (class "comment-reactions"))
(button (@ (class "reply-button") (hidden #t)
(aria-label "Reply to comment")
(data-target ,(format #f "comment-~a" (internal-comment-id comment)))
(data-reply-to-id ,(internal-comment-id comment)))
"Reply")
,@(map (match-lambda
((emote . count)
`(span (@ (class "reaction")) ,(format #f "~a ~a" emote count))))
(comment-reactions comment))
(button (@ (class "add-reaction") (hidden #t)
(aria-label "Add reaction")
(data-reply-to-id ,(internal-comment-id comment)))
"+")))
'())
,@(if (and (not reply?)
(internal-comment? comment)
(positive? (length (internal-comment-replies comment))))
(map (lambda (comment)
(format-comment comment #:reply? #t))
(internal-comment-replies comment))
'())))
(define (wm-not-null? value)
(and value
(not (eqv? 'null value))
(not (string= "" value))))
(define (format-interaction webmention)
"Format `webmention', an alist, as SXML for a rich interaction without content"
(let* ((author (assoc-ref webmention "author"))
(author-name (assoc-ref author "name"))
(author-url (assoc-ref author "url"))
(author-url
(if (wm-not-null? author-url)
author-url
(assoc-ref webmention "wm-source")))
(author-photo (assoc-ref author "photo"))
(author-photo
(cond ((string-prefix? "https://lobste.rs/" author-url) "/static/image/lobsters.png")
((wm-not-null? author-photo) author-photo)
(else "/static/image/default-icon.png"))))
`(li (@ (class "p-comment h-cite interaction comment-source-webmention"))
(a (@ (href ,author-url))
(img (@ (class "u-photo") (src ,author-photo))))
(div (@ (class "e-content p-name comment-content"))
(em
,(match (assoc-ref webmention "wm-property")
("repost-of" "Reposted this!")
("like-of" "Favorited this!")
("bookmark-of" "Bookmarked this!")
("mention-of" "Mentioned this!")
(_ "[No Text Provided]"))))
(img (@ (class "comment-source-identifier")
(alt "Webmention logo")
(src "/static/image/webmention-logo.png"))))))
(define (alist->webmention alist)
(let* ((author (assoc-ref alist "author"))
(author-name (assoc-ref author "name"))
(author-url (assoc-ref author "url"))
(author-url
(if (wm-not-null? author-url)
author-url
(assoc-ref alist "wm-source")))
(author-photo (assoc-ref author "photo"))
(author-photo
(cond ((string-prefix? "https://lobste.rs/" author-url) "/static/image/lobsters.png")
((wm-not-null? author-photo) author-photo)
(else "/static/image/default-icon.png")))
(content (assoc-ref alist "content"))
(content (if content (assoc-ref content "text") #f))
(published-time (assoc-ref alist "published"))
(received-time (assoc-ref alist "wm-received"))
(url (assoc-ref alist "url"))
(time (if (eqv? 'null published-time) received-time published-time))
(time (string->date time "~Y~m~dT~H~M~S")))
(make-webmention
author-name
author-photo
content
author-url
time)))
(define (render-comment-view comments-response webmentions-response)
"Render `response', the output of `fetch-webmentions', as SXML"
(define (publish-time x)
((if (webmention? x)
webmention-publish-time
internal-comment-publish-time)
x))
(define (date>? a b) (time>? (date->time-utc a) (date->time-utc b)))
(let* ((webmentions
(map alist->webmention
(filter (lambda (x) (string= (assoc-ref x "wm-property") "in-reply-to"))
(vector->list (assoc-ref webmentions-response "children")))))
(all-comments (sort (append comments-response webmentions)
(lambda (a b) (date>? (publish-time a) (publish-time b))))))
`((h3 (@ (id "comments-heading") (class "module-title"))
,(format #f "Comments (~a)" (length all-comments)))
,(map format-comment all-comments))))
(define (fetch-comments slug)
"Blocking call to webmention.io to retrieve a vector of all Webmentions for `slug'"
(if (getenv "HAUNT_SKIP_COMMENTS")
'()
(let ((url (format #f "https://jakob.space/api/comments?p=~a" slug)))
(receive (response-status response-body)
(http-request url)
(chain response-body
(bytevector->string _ "UTF-8")
(json-string->scm _)
(vector->list _)
(map scm->json-string _)
(map (lambda (x) (call-with-input-string x json->internal-comment)) _))))))
(define (fetch-recent-comments)
"Blocking call to retrieve a vector of the three most recent comments"
(if (getenv "HAUNT_SKIP_COMMENTS")
'()
(let ((url "https://jakob.space/api/all-comments?limit=3"))
(receive (response-status response-body)
(http-request url)
(chain response-body
(bytevector->string _ "UTF-8")
(json-string->scm _)
(vector->list _)
(map scm->json-string _)
(map (lambda (x) (call-with-input-string x json->internal-comment)) _))))))
(define (fetch-webmentions slug)
"Blocking call to webmention.io to retrieve a vector of all Webmentions for `slug'"
(define prefixes '("http://jakob.space/" "https://jakob.space/"
"http://jakob.space/blog/" "https://jakob.space/blog/"))
(if (getenv "HAUNT_SKIP_COMMENTS")
`(("children" . #()))
(let* ((target-queries (map (lambda (pre)
(format #f "target[]=~a~a.html" pre slug))
prefixes))
(url (format #f "https://webmention.io/api/mentions.jf2?per-page=200&page=0&~a"
(string-join target-queries "&"))))
(receive (response-status response-body)
(http-request url)
(call-with-input-string (bytevector->string response-body "UTF-8") json->scm)))))
|