1 (uiop:define-package
#:lw2-viewer
2 (:use
#:cl
#:sb-thread
#:flexi-streams
#:djula
#:iterate
3 #:lw2-viewer.config
#:lw2.utils
#:lw2.lmdb
#:lw2.backend
#:lw2.links
#:lw2.clean-html
#:lw2.login
#:lw2.context
#:lw2.sites
#:lw2.components
#:lw2.html-reader
#:lw2.fonts
12 #:lw2.data-viewers.post
13 #:lw2.data-viewers.comment
16 (:import-from
#:alexandria
#:with-gensyms
#:once-only
#:ensure-list
#:when-let
#:when-let
* #:if-let
#:alist-hash-table
)
17 (:import-from
#:collectors
#:with-collector
)
18 (:import-from
#:ppcre
#:regex-replace-all
)
20 #:define-regex-handler
#:*fonts-stylesheet-uri
* #:generate-fonts-link
21 #:user-nav-bar
#:*primary-nav
* #:*secondary-nav
* #:*nav-bars
*
22 #:begin-html
#:end-html
23 #:*fonts-stylesheet-uris
* #:*fonts-redirect-data
* #:*fonts-redirect-lock
* #:*fonts-redirect-thread
*
24 #:postprocess-conversation-title
27 #:*extra-external-scripts
* #:*extra-inline-scripts
*
28 #:site-stylesheets
#:site-inline-scripts
#:site-scripts
#:site-external-scripts
#:site-head-elements
33 (:recycle
#:lw2-viewer
#:lw2.backend
))
35 (in-package #:lw2-viewer
)
37 (named-readtables:in-readtable html-reader
)
39 (add-template-directory (asdf:system-relative-pathname
"lw2-viewer" "templates/"))
41 (define-cache-database 'backend-lw2-legacy
42 "auth-token-to-userid" "auth-token-to-username" "user-ignore-list")
44 (defvar *read-only-mode
* nil
)
45 (defvar *read-only-default-message
* "Due to a system outage, you cannot log in or post at this time.")
47 (defparameter *default-prefs
* (alist :items-per-page
20 :default-sort
"new"))
48 (defvar *current-prefs
* nil
)
50 (defun get-post-sequences (post-id)
51 (when-let (sequence-ids (get-post-sequence-ids post-id
))
53 (dolist (sequence-id sequence-ids
)
54 (let* ((sequence (get-sequence sequence-id
))
55 (posts (sequence-post-ids sequence
)))
56 (multiple-value-bind (prev next
)
57 (loop for prev
= nil then
(car current
)
59 when
(string= (car current
) post-id
)
60 return
(values prev
(second current
)))
63 (and prev
(get-sequence-post sequence prev
))
64 (and next
(get-sequence-post sequence next
))))))))
67 (defun rectify-post-relations (post-relations)
68 (remove-if-not (lambda (pr) (cdr (assoc :--id
(cdr (first pr
))))) post-relations
))
70 (defun post-nav-links (post post-sequences
)
71 <nav class
="post-nav-links">
72 (schema-bind (:post post
(target-post-relations source-post-relations
) :context
:body
)
73 (let ((target-post-relations (rectify-post-relations target-post-relations
))
74 (source-post-relations (rectify-post-relations source-post-relations
)))
75 (when (or target-post-relations source-post-relations
)
76 <div class
="related-posts">
77 (dolist (relations `((,source-post-relations
"Parent question")
78 (,target-post-relations
"Sub-question")))
79 (destructuring-bind (related-posts relation-type
) relations
81 <div class
="related-post-group">
82 <div class
="related-post-type">("~A~A" relation-type
(if (second related-posts
) "s" ""))</div
>
83 (dolist (post-relation related-posts
)
84 (post-headline-to-html (or (cdr (assoc :source-post post-relation
))
85 (cdr (assoc :target-post post-relation
)))))
88 (loop for
(sequence prev next
) in post-sequences do
90 <div class
="post-nav-item sequence">
91 <a class
="post-nav sequence-title" href
=("/s/~A" (cdr (assoc :--id sequence
)))>
92 <span class
="post-nav-label">Part of the sequence
:</span
>
93 <span class
="post-nav-title">(safe (clean-text-to-html (cdr (assoc :title sequence
))))</span
>
95 (labels ((post-nav-link (direction post
)
96 <a class
=("post-nav ~A" (string-downcase direction
)) href
=(generate-item-link :post post
)>
97 <span class
="post-nav-label">(case direction
(:prev
"Previous: ") (:next
"Next: "))</span
>
98 <span class
="post-nav-title">(safe (clean-text-to-html (cdr (assoc :title post
))))</span
>
100 (when prev
(post-nav-link :prev prev
))
101 (when next
(post-nav-link :next next
)))
105 (defun rectify-conversation (conversation)
106 (alist-bind ((title (or null string
)))
108 (if (or (null title
) (string= title
""))
109 (acons :title
"[Untitled conversation]" conversation
)
112 (defun conversation-message-to-html (out-stream message
)
113 (alist-bind ((user-id string
)
115 (highlight-new boolean
)
119 (html-body (or string null
)))
121 (let ((conversation (rectify-conversation conversation
)))
122 (multiple-value-bind (pretty-time js-time
) (pretty-time created-at
)
123 (format out-stream
"<div class=\"comment private-message~A\"><div class=\"comment-meta\"><a class=\"author\" href=\"/users/~A\">~A</a> <span class=\"date\" data-js-date=\"~A\">~A~A</span><div class=\"comment-post-title\">Private message in: <a href=\"/conversation?id=~A\">~A</a></div></div><div class=\"body-text comment-body\">"
124 (if highlight-new
" comment-item-highlight" "")
125 (encode-entities (get-user-slug user-id
))
126 (encode-entities (get-username user-id
))
130 (encode-entities (cdr (assoc :--id conversation
)))
131 (encode-entities (cdr (assoc :title conversation
)))))
132 (labels ((ws (html-body) (let ((*memoized-output-stream
* out-stream
)) (clean-html* html-body
))))
134 (contents (ws (cdr (assoc :html contents
))))
135 (html-body (ws html-body
))
136 (t (format out-stream
"~{<p>~A</p>~}" (loop for block in
(cdr (assoc :blocks content
)) collect
(encode-entities (cdr (assoc :text block
))))))))
137 (format out-stream
"</div></div>"))))
139 (defun conversation-index-to-html (out-stream conversation
)
140 (alist-bind ((conversation-id string
:--id
)
141 (title (or null string
))
142 (created-at (or null string
))
144 (messages-total fixnum
))
145 (rectify-conversation conversation
)
146 (multiple-value-bind (pretty-time js-time
) (if created-at
(pretty-time created-at
) (values "[Error]" 0))
147 (format out-stream
"<h1 class=\"listing\"><a href=\"/conversation?id=~A\">~A</a></h1><div class=\"post-meta\"><div class=\"conversation-participants\"><ul>~:{<li><a href=\"/users/~A\">~A</a></li>~}</ul></div><div class=\"messages-count\">~A</div><div class=\"date\" data-js-date=\"~A\">~A~A</div></div>"
148 (encode-entities conversation-id
)
149 (encode-entities title
)
150 (loop for p in participants
151 collect
(list (encode-entities (cdr (assoc :slug p
))) (encode-entities (cdr (assoc :display-name p
)))))
152 (pretty-number messages-total
"message")
157 (defun sequence-to-html (sequence)
158 (labels ((contents-to-html (contents &key title subtitle number
)
159 (let ((html-body (cdr (assoc :html contents
))))
160 (when (or html-body title subtitle
)
161 <div class
="body-text sequence-text">
163 <h1 class
="sequence-chapter">(safe (format nil
"~@[~A. ~]~A" number
(clean-text-to-html title
:hyphenation nil
)))</h1
>)
165 <div class
="sequence-subtitle">(clean-text-to-html subtitle
)</div
>)
166 (with-html-stream-output (:stream stream
)
168 (let ((*memoized-output-stream
* stream
)) (clean-html* html-body
))))
170 (chapter-to-html (chapter)
171 (alist-bind ((title (or string null
))
172 (subtitle (or string null
))
173 (number (or fixnum null
))
178 (with-html-stream-output
179 (contents-to-html contents
:title title
:subtitle subtitle
:number number
)
181 (with-html-stream-output
183 (post-headline-to-html post
)))
186 (alist-bind ((sequence-id string
:--id
)
193 (multiple-value-bind (pretty-time js-time
) (pretty-time created-at
)
196 <h1 class
="post-title">(safe (clean-text-to-html title
:hyphenation nil
))</h1
>
197 <h1 class
="listing"><a href
=("/s/~A" sequence-id
)>(safe (clean-text-to-html title
:hyphenation nil
))</a
></h1
>)
198 <div class
="post-meta">
199 <a class
=("author~{ ~A~}" (list-cond ((logged-in-userid user-id
) "own-user-author")))
200 href
=("/users/~A" (get-user-slug user-id
))
202 (get-username user-id
)
204 <div class
="date" data-js-date
=js-time
>
206 (safe (pretty-time-js))
209 (with-html-stream-output
211 (contents-to-html contents
)
212 (dolist (chapter chapters
)
213 (chapter-to-html chapter
))))
216 (defun abort-response ()
217 (throw 'abort-response nil
))
219 (defun abort-response-if-unrecoverable (condition)
220 (when (html-output-stream-error-p condition
)
223 (defmacro with-error-html-block
(() &body body
)
224 "If an error occurs within BODY, write an HTML representation of the
225 signaled condition to *HTML-OUTPUT*."
226 `(block with-error-html-block
227 (handler-bind ((serious-condition (lambda (c)
228 (abort-response-if-unrecoverable c
)
230 (return-from with-error-html-block nil
))))
231 (log-conditions (progn ,@body
)))))
233 (defun make-comment-parent-hash (comments)
234 (let ((existing-comment-hash (make-hash-table :test
'equal
))
235 (hash (make-hash-table :test
'equal
)))
237 (if-let (id (cdr (assoc :--id c
)))
238 (setf (gethash id existing-comment-hash
) t
)))
240 (let* ((parent-id (cdr (assoc :parent-comment-id c
)))
241 (old (gethash parent-id hash
)))
242 (setf (gethash parent-id hash
) (cons c old
))
243 (when (and parent-id
(not (gethash parent-id existing-comment-hash
)))
244 (let ((placeholder (alist :--id parent-id
:parent-comment-id nil
:deleted t
)))
245 (setf (gethash parent-id existing-comment-hash
) t
246 (gethash nil hash
) (cons placeholder
(gethash nil hash
)))))))
247 (maphash (lambda (k old
)
248 (setf (gethash k hash
) (nreverse old
)))
251 ((count-children (parent)
252 (let ((children (gethash (cdr (assoc :--id parent
)) hash
)))
253 (+ (length children
) (apply #'+ (map 'list
#'count-children children
)))))
254 (add-child-counts (comment-list)
255 (loop for c in comment-list
256 as id
= (cdr (assoc :--id c
))
257 do
(setf (gethash id hash
) (add-child-counts (gethash id hash
)))
258 collecting
(cons (cons :child-count
(count-children c
)) c
))))
259 (setf (gethash nil hash
) (add-child-counts (gethash nil hash
))))
262 (defun comment-thread-to-html (out-stream emit-comment-item-fn
)
263 (format out-stream
"<ul class=\"comment-thread\">")
264 (funcall emit-comment-item-fn
)
265 (format out-stream
"</ul>"))
267 (defun comment-item-to-html (out-stream comment
&key extra-html-fn with-post-title level level-invert
)
268 (with-error-html-block ()
269 (let ((c-id (cdr (assoc :--id comment
)))
270 (user-id (cdr (assoc :user-id comment
))))
271 (format out-stream
"<li id=\"comment-~A\" class=\"comment-item~{ ~A~}\">"
274 (t (if (let ((is-odd (or (not level
) (evenp level
)))) ;inverted because level counts from 0
275 (if level-invert
(not is-odd
) is-odd
))
276 "depth-odd" "depth-even"))
277 ((and *current-ignore-hash
* (gethash user-id
*current-ignore-hash
*)) "ignored")))
279 (comment-to-html out-stream comment
:with-post-title with-post-title
)
280 (if extra-html-fn
(funcall extra-html-fn c-id
))
281 (format out-stream
"</li>")))))
283 (defun comment-tree-to-html (out-stream comment-hash
&key
(target nil
) (level (if target
1 0)) level-invert
)
284 (let ((comments (gethash target comment-hash
)))
286 (comment-thread-to-html out-stream
288 (loop for c in comments do
289 (comment-item-to-html out-stream c
291 :level-invert level-invert
292 :extra-html-fn
(lambda (c-id)
293 (if (and (= level
10) (gethash c-id comment-hash
))
294 (format out-stream
"<input type=\"checkbox\" id=\"expand-~A\"><label for=\"expand-~:*~A\" data-child-count=\"~A comment~:P\">Expand this thread</label>"
296 (cdr (assoc :child-count c
))))
297 (comment-tree-to-html out-stream comment-hash
:target c-id
:level
(1+ level
) :level-invert level-invert
)))))))))
299 (defun sort-items (items sort-by
)
300 (multiple-value-bind (sort-fn key-fn
)
302 ((:old
:new
) (values (if (eq sort-by
:old
)
303 (lambda (a b
) (ignore-errors (local-time:timestamp
< a b
)))
304 (lambda (a b
) (ignore-errors (local-time:timestamp
> a b
))))
305 (lambda (c) (ignore-errors (local-time:parse-timestring
(or (cdr (assoc :posted-at c
))
306 (cdr (assoc :created-at c
)))))))))
307 (sort items sort-fn
:key key-fn
)))
309 (defun comment-chrono-to-html (out-stream comments
)
310 (let ((comment-hash (make-comment-parent-hash comments
))
311 (comments (sort-items comments
:old
)))
312 (comment-thread-to-html out-stream
314 (loop for c in comments do
315 (let* ((c-id (cdr (assoc :--id c
)))
316 (new-c (acons :children
(gethash c-id comment-hash
) c
)))
317 (comment-item-to-html out-stream new-c
)))))))
319 (defun comment-post-interleave (list &key limit offset
(sort-by :date
))
320 (multiple-value-bind (sort-fn sort-key
)
322 (:date
(values #'local-time
:timestamp
> (lambda (x) (local-time:parse-timestring
(cdr (assoc :posted-at x
))))))
323 (:date-reverse
(values #'local-time
:timestamp
< (lambda (x) (local-time:parse-timestring
(cdr (assoc :posted-at x
))))))
324 (:score
(values #'> (lambda (x) (cdr (assoc :base-score x
))))))
325 (let ((sorted (sort list sort-fn
:key sort-key
)))
326 (loop for end
= (if (or limit offset
) (+ (or limit
0) (or offset
0)))
329 until
(and end
(>= count end
))
330 when
(or (not offset
) (>= count offset
))
333 (defun identify-item (x)
336 (if-let (typename (cdr (assoc :----typename x
)))
337 (find-symbol (string-upcase typename
) (find-package :keyword
))
341 ((assoc :comment-count x
)
345 (condition :condition
)))
347 (defun write-index-items-to-html (out-stream items
&key need-auth
(empty-message "No entries.") skip-section
)
350 (with-error-html-block ()
351 (ecase (identify-item x
)
355 (format out-stream
"<p>~A</p>" (cdr (assoc :message x
))))
357 (format out-stream
"<ul class=\"comment-thread\"><li class=\"comment-item depth-odd\">")
359 (conversation-message-to-html out-stream x
)
360 (format out-stream
"</li></ul>")))
362 (conversation-index-to-html out-stream x
))
364 (post-headline-to-html x
:need-auth need-auth
:skip-section skip-section
))
366 (comment-thread-to-html out-stream
367 (lambda () (comment-item-to-html out-stream x
:with-post-title t
))))
369 (sequence-to-html x
)))))
370 (format out-stream
"<div class=\"listing-message\">~A</div>" empty-message
)))
372 (defun write-index-items-to-rss (out-stream items
&key title need-auth
)
373 (let ((full-title (format nil
"~@[~A - ~]~A" title
(site-title *current-site
*)))
374 (items (firstn (sort-items items
:new
) 20)))
375 (xml-emitter:with-rss2
(out-stream :encoding
"UTF-8")
376 (xml-emitter:rss-channel-header full-title
(site-uri *current-site
*) :description full-title
)
377 (labels ((emit-item (item &key title link
(guid (cdr (assoc :--id item
))) (author (get-username (cdr (assoc :user-id item
))))
378 (date (pretty-time (cdr (assoc :posted-at item
)) :format local-time
:+rfc-1123-format
+)) body
)
379 (xml-emitter:rss-item
387 (ecase (identify-item item
)
389 (let ((author (get-username (cdr (assoc :user-id item
)))))
391 :title
(clean-text (format nil
"~A by ~A" (cdr (assoc :title item
)) author
))
393 :link
(generate-post-auth-link item nil t need-auth
)
394 :body
(clean-html (or (cdr (assoc :html-body
(get-post-body (cdr (assoc :--id item
)) :revalidate nil
))) "") :post-id
(cdr (assoc :--id item
))))))
396 (schema-bind (:comment item
(comment-id post-id user-id html-body
))
397 (when post-id
; XXX fixme
399 :title
(format nil
"Comment by ~A on ~A" (get-username user-id
) (get-post-title post-id
))
400 :link
(generate-item-link :post post-id
:comment-id comment-id
:absolute t
)
401 :body
(clean-html html-body
)))))))))))
403 (defun search-bar-to-html (out-stream)
404 (declare (special *current-search-query
*))
405 (let ((query (and (boundp '*current-search-query
*) (hunchentoot:escape-for-html
*current-search-query
*))))
406 (format out-stream
"<form action=\"/search\" class=\"nav-inner\"><input name=\"q\" type=\"search\" ~@[value=\"~A\"~] autocomplete=\"off\" accesskey=\"s\" title=\"Search [s]~@[ Tip: Paste a ~A URL here to jump to that page.~]\"><button>Search</button></form>" query
(main-site-title *current-site
*))))
408 (defun inbox-to-html (out-stream user-slug
&optional new-messages
)
409 (let* ((target-uri (format nil
"/users/~A?show=inbox" user-slug
))
410 (as-link (string= (hunchentoot:request-uri
*) target-uri
)))
411 (multiple-value-bind (nm-class nm-text
)
412 (if new-messages
(values "new-messages" "New messages") (values "no-messages" "Inbox"))
413 (format out-stream
"<~:[a href=\"~A\"~;span~*~] id=\"inbox-indicator\" class=\"~A\" accesskey=\"o\" title=\"~A~:[ [o]~;~]\">~A</a>"
414 as-link target-uri nm-class nm-text as-link nm-text
))))
416 (defmethod site-nav-bars ((site site
))
417 '((:secondary-bar
(("archive" "/archive" "Archive" :accesskey
"r")
418 ("about" "/about" "About" :accesskey
"t")
419 ("search" "/search" "Search" :html search-bar-to-html
)
421 (:primary-bar
(("home" "/" "Home" :description
"Latest frontpage posts" :accesskey
"h")
422 ("recent-comments" "/recentcomments" "<span>Recent </span>Comments" :description
"Latest comments" :accesskey
"c")))))
424 (defmethod site-nav-bars ((site lesswrong-viewer-site
))
425 '((:secondary-bar
(("archive" "/archive" "Archive" :accesskey
"r")
426 ("sequences" "/library" "Sequences" :description
"Sequences" :accesskey
"q")
427 ("about" "/about" "About" :accesskey
"t")
428 ("search" "/search" "Search" :html search-bar-to-html
)
430 (:tertiary-bar
(("questions" "/index?view=questions" "Questions")
431 ("events" "/index?view=events" "Events")
432 ("shortform" "/shortform" "Shortform" :description
"Latest Shortform posts")
433 ("alignment-forum" "/index?view=alignment-forum" "Alignment Forum")
434 ("alignment-forum-comments" "/recentcomments?view=alignment-forum" "AF Comments")))
435 (:primary-bar
(("home" "/" "Home" :description
"Latest frontpage posts" :accesskey
"h")
436 ("featured" "/index?view=featured" "Featured" :description
"Latest featured posts" :accesskey
"f")
437 ("all" "/index?view=all" "All" :description
"Latest posts from all sections" :accesskey
"a")
438 ("tags" "/tags" "Tags" :description
"All tags" :accesskey
"v")
439 ("recent-comments" "/recentcomments" "<span>Recent </span>Comments" :description
"Latest comments" :accesskey
"c")))))
441 (defmethod site-nav-bars ((site ea-forum-viewer-site
))
442 '((:secondary-bar
(("archive" "/archive" "Archive" :accesskey
"r")
443 ("about" "/about" "About" :accesskey
"t")
444 ("search" "/search" "Search" :html search-bar-to-html
)
446 (:primary-bar
(("home" "/" "Home" :description
"Latest frontpage posts" :accesskey
"h")
447 ("all" "/index?view=all" "All" :description
"Latest posts from all sections" :accesskey
"a")
448 ("meta" "/index?view=community" "Community" :description
"Latest community posts" :accesskey
"m")
449 ("tags" "/tags" "Tags" :description
"All tags" :accesskey
"v")
450 ("shortform" "/shortform" "Shortform" :description
"Latest Shortform posts")
451 ("recent-comments" "/recentcomments" "<span>Recent </span>Comments" :description
"Latest comments" :accesskey
"c")))))
453 (defun prepare-nav-bar (nav-bar current-uri
)
454 (list (first nav-bar
)
455 (map 'list
(lambda (item) (if (listp item
) item
(funcall item current-uri
)))
458 (defun nav-item-active (item current-uri
)
460 (destructuring-bind (id uri name
&key description html accesskey nofollow trailing-html override-uri
) item
461 (declare (ignore id name description html accesskey nofollow trailing-html
))
462 (string= (or override-uri uri
) current-uri
))))
464 (defun nav-bar-active (nav-bar current-uri
)
465 (some (lambda (x) (nav-item-active x current-uri
)) (second nav-bar
)))
467 (defun nav-bar-inner (out-stream items
&optional current-uri
)
468 (maplist (lambda (items)
469 (let ((item (first items
)))
470 (destructuring-bind (id uri name
&key description html accesskey nofollow trailing-html override-uri
) item
471 (declare (ignore override-uri
))
472 (let* ((item-active (nav-item-active item current-uri
))
473 (nav-class (format nil
"nav-item ~:[nav-inactive~;nav-current~]~:[~; nav-item-last-before-current~]"
474 item-active
(and (not item-active
) (nav-item-active (cadr items
) current-uri
)))))
475 (format out-stream
"<span id=\"nav-item-~A\" class=\"~A\" ~@[title=\"~A\"~]>"
476 id nav-class description
)
478 (funcall html out-stream
)
479 (link-if-not out-stream item-active uri
"nav-inner" name
:accesskey accesskey
:nofollow nofollow
))
481 (funcall trailing-html out-stream
))
482 (format out-stream
"</span>")))))
485 (defun nav-bar-outer (out-stream class nav-bar
&optional current-uri
)
486 (format out-stream
"<nav id=\"~A\" class=\"nav-bar~@[ ~A~]\">" (string-downcase (first nav-bar
)) class
)
487 (nav-bar-inner out-stream
(second nav-bar
) current-uri
)
488 (format out-stream
"</nav>"))
490 (defun nav-bar-to-html (out-stream class current-uri
)
491 (let* ((nav-bars (map 'list
(lambda (x) (prepare-nav-bar x current-uri
)) (site-nav-bars *current-site
*)))
492 (active-bar (or (find-if (lambda (x) (nav-bar-active x current-uri
)) nav-bars
) (car (last nav-bars
))))
493 (inactive-bars (remove active-bar nav-bars
)))
494 (dolist (bar inactive-bars
)
495 (nav-bar-outer out-stream
(format nil
"~@[~A ~]inactive-bar" class
) bar current-uri
))
496 (nav-bar-outer out-stream
(format nil
"~@[~A ~]active-bar" class
) active-bar current-uri
)))
498 (defun user-nav-item (&optional current-uri
)
500 `("login" "/login" "Read Only Mode" :html
,(lambda (out-stream)
501 (format out-stream
"<span class=\"nav-inner\" title=\"~A\">[Read Only Mode]</span>"
502 (typecase *read-only-mode
*
503 (string *read-only-mode
*)
504 (t *read-only-default-message
*)))))
505 (if-let (username (logged-in-username))
506 (let ((user-slug (encode-entities (logged-in-user-slug))))
507 `("login" ,(format nil
"/users/~A" user-slug
) ,(plump:encode-entities username
) :description
"User page" :accesskey
"u"
508 :trailing-html
,(lambda (out-stream) (inbox-to-html out-stream user-slug
))))
509 `("login" ,(format nil
"/login?return=~A" (url-rewrite:url-encode current-uri
)) "Log In" :accesskey
"u" :nofollow t
:override-uri
"/login"))))
511 (defun sublevel-nav-to-html (options current
&key default
(base-uri (hunchentoot:request-uri
*)) (param-name "show") (remove-params '("offset")) extra-class
)
512 (declare (type (or null string
) extra-class
))
513 (let ((out-stream *html-output
*))
514 (format out-stream
"<nav class=\"sublevel-nav~@[ ~A~]\">" extra-class
)
515 (loop for item in options
516 do
(destructuring-bind (param-value &key
(text (string-capitalize param-value
)) description
) (if (atom item
) (list item
) item
)
517 (let* ((param-value (string-downcase param-value
))
518 (selected (string-equal current param-value
))
519 (class (if selected
"sublevel-item selected" "sublevel-item")))
520 (link-if-not out-stream selected
(apply #'replace-query-params base-uri param-name
(unless (string-equal param-value default
) param-value
)
521 (loop for x in remove-params nconc
(list x nil
)))
522 class text
:title description
))))
523 (format out-stream
"</nav>")))
525 (defmacro set-script-variables
(&rest clauses
)
526 (with-gensyms (out-stream name value
)
527 `(with-html-stream-output (:stream
,out-stream
)
528 ,.
(loop for clause in clauses
529 collect
(destructuring-bind (name-form value-form
) clause
530 `(let ((,name
,name-form
)
531 (,value
,value-form
))
532 (declare (dynamic-extent ,name
,value
))
533 (write-string ,name
,out-stream
)
534 (write-string "=" ,out-stream
)
535 (json:encode-json
,value
,out-stream
)
536 (write-string #.
(format nil
";~%") ,out-stream
)))))))
538 (defun html-body (out-stream fn
&key title description social-description current-uri content-class robots extra-head
)
539 (macrolet ((for-resource-type ((resource-type &rest args
) &body body
)
540 (with-gensyms (resource)
541 `(dolist (,resource page-resources
)
542 (when (eq (first ,resource
) ,resource-type
)
543 (destructuring-bind ,args
(rest ,resource
)
545 (with-html-stream-output
546 (let* ((session-token (hunchentoot:cookie-in
"session-token"))
547 (csrf-token (and session-token
(make-csrf-token session-token
)))
548 (hide-nav-bars (truthy-string-p (hunchentoot:get-parameter
"hide-nav-bars")))
550 (page-resources (nreverse *page-resources
*))
551 (site-domain (site-domain *current-site
*)))
552 (setf *page-resources
* nil
)
553 (write-string "<!DOCTYPE html><html lang=\"en-US\"><head>
554 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
555 <meta name=\"HandheldFriendly\" content=\"True\" />"
557 (with-delimited-writer (out-stream delimit
:begin
"<script>" :end
"</script>")
560 (set-script-variables ("document.domain" site-domain
)))
563 (set-script-variables
564 ("applicationServerKey" (get-vapid-public-key))
565 ("loggedInUserId" (or (logged-in-userid) ""))
566 ("loggedInUserDisplayName" (or (logged-in-username) ""))
567 ("loggedInUserSlug" (or (logged-in-user-slug) ""))
568 ("GW" (alist "useFancyFeatures" (not (typep *current-site
* 'arbital-site
))
569 "secureCookies" (to-boolean (site-secure *current-site
*))
570 "csrfToken" csrf-token
571 "assets" (alist "popup.svg" (generate-versioned-link "/assets/popup.svg"))
572 "sites" (if site-domain
573 (loop for site in
*sites
*
574 when
(let ((sd (site-domain site
))) (and sd
(string-equal sd site-domain
)))
575 collect
(cons (site-host site
) t
))
576 (alist (site-host *current-site
*) t
)))))
577 (for-resource-type (:inline-script script-text
)
578 (write-string ";" out-stream
)
579 (write-string script-text out-stream
))))
581 (funcall lw2.resources
::*script-tags
* out-stream
)
582 (for-resource-type (:script uri
)
583 (format out-stream
"<script src=\"~A\"></script>" uri
))
584 (funcall lw2.resources
::*async-script-tags
* out-stream
)
585 (for-resource-type (:async-script uri
)
586 (format out-stream
"<script src=\"~A\" async></script>" uri
)))
587 (funcall lw2.resources
::*style-tags
* out-stream
)
588 (for-resource-type (:stylesheet uri
&key media class
)
589 (format out-stream
"<link rel=\"stylesheet\" href=\"~A\"~@[ media=\"~A\"~]~@[ class=\"~A\"~]>" uri media class
))
590 (generate-fonts-html-headers (site-fonts-source *current-site
*))
591 (format out-stream
"<link rel=\"shortcut icon\" href=\"~A\">"
592 (generate-versioned-link "/assets/favicon.ico"))
593 (format out-stream
"<title>~@[~A - ~]~A</title>~@[<meta name=\"description\" content=\"~A\">~]~@[<meta name=\"robots\" content=\"~A\">~]"
594 (if title
(encode-entities title
))
595 (site-title *current-site
*)
600 <meta property
="og:title" content
=title
>)
601 (when social-description
602 <meta property
="og:description" content
=social-description
>
603 <meta property
="og:type" content
="article">))
604 (with-delimited-writer (out-stream delimit
:begin
"<style>" :between
#.
(format nil
"~%") :end
"</style>")
605 (unless (logged-in-userid)
607 (write-string "button.vote { display: none }" out-stream
))
608 (when *memoized-output-without-hyphens
*
609 ;; The browser has been detected as having bugs related to soft-hyphen characters.
610 ;; But there is some hope that it could still do hyphenation by itself.
612 (write-string ".body-text { hyphens: auto; -ms-hyphens: auto; -webkit-hyphens: auto; }" out-stream
)))
614 (format out-stream
"<base target='_top'>"))
615 (when extra-head
(funcall extra-head
))
616 (format out-stream
"</head>")
619 (format out-stream
"<body class=\"theme-~A\"><div id=\"content\"~@[ class=\"~{~A~^ ~}\"~]>"
620 (let ((theme (hunchentoot:cookie-in
"theme")))
621 (if (and theme
(> (length theme
) 0))
624 (list-cond (content-class content-class
)
625 (hide-nav-bars "no-nav-bars")
626 (preview "preview")))
627 (unless (or hide-nav-bars preview
)
628 (nav-bar-to-html out-stream
"nav-bar-top" (or current-uri
(replace-query-params (hunchentoot:request-uri
*) "offset" nil
"sort" nil
))))
630 (format out-stream
"</div></body></html>"))))))
632 (defun replace-query-params (uri &rest params
)
633 (let* ((quri (quri:uri uri
))
634 (old-params (quri:uri-query-params quri
))
635 (new-params (loop with out
= old-params
636 for
(param value
) on params by
#'cddr
638 (if-let (old-cons (assoc param out
:test
#'equal
))
639 (setf (cdr old-cons
) value
)
640 (setf out
(nconc out
(list (cons param value
)))))
641 (setf out
(remove-if (lambda (x) (equal (car x
) param
)) out
)))
642 finally
(return out
))))
644 (setf (quri:uri-query-params quri
) new-params
)
645 (setf (quri:uri-query quri
) nil
))
646 (quri:render-uri quri
)))
648 (defun pagination-nav-bars (&key offset total with-next
(items-per-page (user-pref :items-per-page
)))
650 (lambda (out-stream fn
)
651 (declare (ignore out-stream
))
653 (lambda (out-stream fn
)
654 (labels ((pages-to-end (n) (< (+ offset
(* items-per-page n
)) total
)))
655 (let* ((with-next (if total
(pages-to-end 1) with-next
))
656 (next (if (and offset with-next
) (+ offset items-per-page
)))
657 (prev (if (and offset
(>= offset items-per-page
)) (- offset items-per-page
)))
658 (request-uri (hunchentoot:request-uri
*))
659 (first-uri (if (and prev
(> prev
0)) (replace-query-params request-uri
"offset" nil
)))
660 (prev-uri (if prev
(replace-query-params request-uri
"offset" (if (= prev
0) nil prev
))))
661 (next-uri (if next
(replace-query-params request-uri
"offset" next
)))
662 (last-uri (if (and total offset
(pages-to-end 2))
663 (replace-query-params request-uri
"offset" (- total
(mod (- total
1) items-per-page
) 1)))))
664 (if (or next prev last-uri
)
665 (labels ((write-item (uri class title accesskey
)
666 (format out-stream
"<a href=\"~A\" class=\"button nav-item-~A~:[ disabled~;~]\" title=\"~A [~A]\" accesskey=\"~A\"></a>"
667 (or uri
"#") class uri title accesskey accesskey
)))
668 (format out-stream
"<nav id='top-nav-bar'>")
669 (write-item first-uri
"first" "First page" "\\")
670 (write-item prev-uri
"prev" "Previous page" "[")
671 (format out-stream
"<span class='page-number'><span class='page-number-label'>Page</span> ~A</span>" (+ 1 (/ (or offset
0) items-per-page
)))
672 (write-item next-uri
"next" "Next page" "]")
673 (write-item last-uri
"last" "Last page" "/")
674 (format out-stream
"</nav>")))
676 (nav-bar-outer out-stream nil
(list :bottom-bar
678 (first-uri `("first" ,first-uri
"Back to first"))
679 (prev-uri `("prev" ,prev-uri
"Previous" :nofollow t
))
680 (t `("top" "#top" "Back to top"))
681 (next-uri `("next" ,next-uri
"Next" :nofollow t
))
682 (last-uri `("last" ,last-uri
"Last" :nofollow t
)))))
683 (format out-stream
"<script>document.querySelectorAll('#bottom-bar').forEach(bb => { bb.classList.add('decorative'); });</script>"))))))
685 (defun decode-json-as-hash-table (json-source)
686 (let (current-hash-table current-key
)
687 (declare (special current-hash-table current-key
))
688 (json:bind-custom-vars
689 (:beginning-of-object
(lambda () (setf current-hash-table
(make-hash-table :test
'equal
)))
690 :object-key
(lambda (x) (setf current-key x
))
691 :object-value
(lambda (x) (setf (gethash current-key current-hash-table
) x
))
692 :end-of-object
(lambda () current-hash-table
)
693 :aggregate-scope
'(current-hash-table current-key
))
694 (json:decode-json-from-source json-source
))))
696 (defun get-ignore-hash (&optional
(user-id (logged-in-userid)))
697 (if-let (ignore-json (and user-id
(cache-get "user-ignore-list" user-id
)))
698 (decode-json-as-hash-table ignore-json
)
699 (make-hash-table :test
'equal
)))
701 (defmacro with-outputs
((out-stream) &body body
)
702 (with-gensyms (stream-sym)
703 (let ((out-body (map 'list
(lambda (x) `(princ ,x
,stream-sym
)) body
)))
704 `(let ((,stream-sym
,out-stream
))
707 (defun call-with-emit-page (out-stream fn
&key title description social-description current-uri content-class
(return-code 200) robots
(pagination (pagination-nav-bars)) top-nav extra-head
)
708 (declare (ignore return-code
))
711 (html-body out-stream
713 (when top-nav
(funcall top-nav
))
714 (funcall pagination out-stream fn
))
715 :title title
:description description
:social-description social-description
:current-uri current-uri
:content-class content-class
:robots robots
:extra-head extra-head
))))
717 (defun set-cookie (key value
&key
(max-age (- (expt 2 31) 1)) (path "/"))
718 (hunchentoot:set-cookie key
:value value
:path path
:max-age max-age
:secure
(site-secure *current-site
*)))
720 (defun set-default-headers (return-code)
721 (setf (hunchentoot:content-type
*) "text/html; charset=utf-8"
722 (hunchentoot:return-code
*) return-code
723 (hunchentoot:header-out
:link
) (let ((output
724 (with-output-to-string (stream)
725 (with-delimited-writer (stream delimit
:between
",")
726 (funcall lw2.resources
::*link-header
* stream delimit
)))))
727 (when (> (length output
) 0)
729 (unless lw2.resources
::*push-option
* (set-cookie "push" "t" :max-age
(* 4 60 60))))
731 (defun user-pref (key)
732 (or (cdr (assoc key
*current-prefs
*))
733 (cdr (assoc key
*default-prefs
*))))
735 (defun set-user-pref (key value
)
736 (assert (boundp 'hunchentoot
:*reply
*))
738 (setf *current-prefs
* (remove-duplicates (acons key value
*current-prefs
*) :key
#'car
:from-end t
))
739 (setf *current-prefs
* (remove key
*current-prefs
* :key
#'car
)))
740 (set-cookie "prefs" (quri:url-encode
(json:encode-json-to-string
*current-prefs
*))))
742 (defmacro with-response-stream
((out-stream) &body body
) `(dynamic-flet ((fn (,out-stream
) ,@body
)) (call-with-response-stream #'fn
)))
744 (defun call-with-response-stream (fn)
745 (unless (eq (hunchentoot:request-method
*) :head
)
746 (let ((*html-output
* (make-flexi-stream (hunchentoot:send-headers
) :external-format
:utf-8
)))
748 (funcall fn
*html-output
*)
749 (serious-condition () (close *html-output
*))
750 (:no-error
(&rest x
) (declare (ignore x
)) (finish-output *html-output
*))))))
752 (defmacro emit-page
((out-stream &rest args
&key
(return-code 200) &allow-other-keys
) &body body
)
753 (once-only (return-code)
755 (set-default-headers ,return-code
)
756 (with-response-stream (,out-stream
)
757 (dynamic-flet ((fn () ,@body
))
758 (call-with-emit-page ,out-stream
762 (defun call-with-error-page (fn)
763 (let* ((*current-auth-status
* (safe-decode-json (hunchentoot:cookie-in
"lw2-status")))
764 (*current-prefs
* (safe-decode-json (hunchentoot:cookie-in
"prefs")))
765 (*preview
* (string-equal (hunchentoot:get-parameter
"format") "preview")))
766 (multiple-value-bind (*revalidate-default
* *force-revalidate-default
*)
767 (cond ((ppcre:scan
"(?:^|,?)\\s*(?:no-cache|max-age=0)(?:$|,)" (hunchentoot:header-in
* :cache-control
))
773 (when (not *revalidate-default
*)
774 (setf (hunchentoot:header-out
:cache-control
) (format nil
"public, max-age=~A" (* 5 60))))
775 (with-site-context ((let ((host (or (hunchentoot:header-in
* :x-forwarded-host
) (hunchentoot:header-in
* :host
))))
777 (error "Unknown site: ~A" host
))))
778 (multiple-value-bind (*current-auth-token
* *current-userid
* *current-username
*)
779 (let* ((auth-token (hunchentoot:cookie-in
"lw2-auth-token"))
780 (expires (cdr (assoc :expires
*current-auth-status
*))))
781 (when (and (nonempty-string auth-token
)
782 (not *read-only-mode
*)
784 (and (integerp expires
) (<= (get-unix-time) (- expires
(* 60 60 24))))))
785 (with-cache-readonly-transaction
788 (cache-get "auth-token-to-userid" auth-token
)
789 (cache-get "auth-token-to-username" auth-token
)))))
790 (let ((*current-user-slug
* (and *current-userid
* (get-user-slug *current-userid
*)))
791 (*current-ignore-hash
* (get-ignore-hash))
792 (*memoized-output-without-hyphens
*
793 ;; Soft hyphen characters mess up middle-click paste and screen readers, so try to identify whether they are necessary.
794 ;; See https://caniuse.com/?search=hyphens
795 (if-let ((ua (hunchentoot:header-in
* :user-agent
)))
798 (declare (regex-groups-min 1))
799 (or (> (parse-integer (reg 0)) 87)
800 (ppcre:scan
"Macintosh|Android" ua
)))
802 (declare (regex-groups-min 1))
803 (> 19 (parse-integer (reg 0))))
807 (catch 'abort-response
809 ((fatal-error (lambda (condition)
810 (abort-response-if-unrecoverable condition
)
811 (let ((error-html (with-output-to-string (*html-output
*) (error-to-html condition
))))
812 (emit-page (out-stream :title
"Error" :return-code
(condition-http-return-code condition
) :content-class
"error-page")
813 (write-string error-html out-stream
)
814 (when (eq (hunchentoot:request-method
*) :post
)
815 <form method
="post" class
="error-retry-form">
816 (loop for
(key . value
) in
(hunchentoot:post-parameters
*)
817 do
<input type
="hidden" name
=key value
=value
>)
818 <input type
="submit" value
="Retry">
820 (return-from call-with-error-page
)))))
822 (if (or (eq (hunchentoot:request-method
*) :post
)
823 (not (and (boundp '*test-acceptor
*) (boundp '*hunchentoot-taskmaster
*)))) ; TODO fix this hack
825 (sb-sys:with-deadline
(:seconds
(expt 1.3
826 (min (round (log 30 1.3))
827 (- (hunchentoot:taskmaster-max-thread-count
(symbol-value '*hunchentoot-taskmaster
*))
828 (hunchentoot:acceptor-requests-in-progress
(symbol-value '*test-acceptor
*))))))
829 (funcall fn
)))))))))))))
831 (defmacro with-error-page
(&body body
)
832 `(dynamic-flet ((fn () ,@body
)) (call-with-error-page #'fn
)))
834 (defun output-form (out-stream method action id heading csrf-token fields button-label
&key textarea end-html
)
835 (format out-stream
"<form method=\"~A\" action=\"~A\" id=\"~A\"><h1>~A</h1>" method action id heading
)
836 (loop for
(id label type . params
) in fields
837 do
(format out-stream
"<label for=\"~A\">~A:</label>" id label
)
839 ((string= type
"select")
840 (destructuring-bind (option-list &optional default
) params
841 (format out-stream
"<select name=\"~A\">" id
)
842 (loop for
(value label
) in option-list
843 do
(format out-stream
"<option value=\"~A\"~:[~; selected~]>~A</option>" value
(string= default value
) label
))
844 (format out-stream
"</select>")))
846 (destructuring-bind (&optional
(autocomplete "off") default
) params
847 (format out-stream
"<input type=\"~A\" name=\"~A\" autocomplete=\"~A\"~@[ value=\"~A\"~]>" type id autocomplete
(and default
(encode-entities default
))))))
848 do
(format out-stream
""))
850 (destructuring-bind (ta-name ta-contents
) textarea
851 (format out-stream
"<div class=\"textarea-container\"><textarea name=\"~A\">~A</textarea><span class='markdown-reference-link'>You can use <a href='http://commonmark.org/help/' target='_blank'>Markdown</a> here.</span></div>" ta-name
(encode-entities ta-contents
))))
852 (format out-stream
"<input type=\"hidden\" name=\"csrf-token\" value=\"~A\"><input type=\"submit\" value=\"~A\">~@[~A~]</form>"
853 csrf-token button-label end-html
))
855 (defun page-toolbar-to-html (&key title new-post new-conversation logout
(rss t
) ignore enable-push-notifications hide-cov
)
857 (let ((out-stream *html-output
*)
858 (liu (logged-in-userid)))
859 (format out-stream
"<div class=\"page-toolbar~@[ hide-until-init~]\">" enable-push-notifications
)
861 (format out-stream
"<form method=\"post\" action=\"/logout\"><input type=\"hidden\" name=\"csrf-token\" value=\"~A\"><button class=\"logout-button button\" name=\"logout\">Log out</button></form>"
865 (when enable-push-notifications
866 (format out-stream
"<script>document.currentScript.outerHTML='<button id=\"enable-push-notifications\" class=\"button\" style=\"display: none\" data-enabled=\"~:[~;true~]\">~:*~:[En~;Dis~]able push notifications</button>'</script>"
867 (find-subscription *current-auth-token
*)))
868 (when (and new-conversation liu
)
869 (multiple-value-bind (text to
)
870 (typecase new-conversation
(string (values "Send private message" new-conversation
)) (t "New conversation"))
871 (format out-stream
"<a class=\"new-private-message button\" href=\"/conversation~@[?to=~A~]\">~A</a>"
874 (let ((cov-pref (user-pref :hide-cov
)))
876 <button name
="set-cov-pref" value
=(if cov-pref
0 1)>("~:[Hide~;Show~] coronavirus posts" cov-pref
)</button
>
878 (when (and new-post liu
)
879 (format out-stream
"<a class=\"new-post button\" href=\"/edit-post~@[?section=~A~]\" accesskey=\"n\" title=\"Create new post [n]\">New post</a>"
880 (typecase new-post
(string new-post
) (t nil
))))
881 (when (and title rss
)
882 (format out-stream
"<a class=\"rss\" rel=\"alternate\" type=\"application/rss+xml\" title=\"~A RSS feed\" href=\"~A\">RSS</a>"
883 title
(replace-query-params (hunchentoot:request-uri
*) "offset" nil
"format" "rss")))
884 (format out-stream
"</div>"))))
886 (defun view-items-index (items &key section title current-uri hide-title need-auth
(pagination (pagination-nav-bars)) (top-nav (lambda () (page-toolbar-to-html :title title
))) (content-class "index-page") alternate-html
)
887 (alexandria:switch
((hunchentoot:get-parameter
"format") :test
#'string
=)
889 (setf (hunchentoot:content-type
*) "application/rss+xml; charset=utf-8")
890 (with-response-stream (out-stream)
891 (write-index-items-to-rss out-stream items
:title title
)))
893 (emit-page (out-stream :title
(if hide-title nil title
) :description
(site-description *current-site
*) :content-class content-class
894 :current-uri current-uri
:robots
(if (hunchentoot:get-parameter
:offset
) "noindex, nofollow")
895 :pagination pagination
:top-nav top-nav
)
897 (funcall alternate-html
)
898 (write-index-items-to-html out-stream items
900 :skip-section section
))))))
902 (defun link-if-not (stream linkp url class text
&key accesskey nofollow title
)
903 (declare (dynamic-extent linkp url text
))
905 (format stream
"<a href=\"~A\" class=\"~A\"~@[ accesskey=\"~A\"~]~:[~; rel=\"nofollow\"~]~@[ title=\"~A\"~]>~A</a>" url class accesskey nofollow title text
)
906 (format stream
"<span class=\"~A\"~@[ title=\"~A\"~]>~A</span>" class title text
)))
908 (defun postprocess-markdown (markdown)
909 (if (typep *current-site
* 'alternate-frontend-site
)
911 ((concatenate 'string
(regex-replace-all "\\." (site-uri *current-site
*) "\\.") "posts/([^/ ]{17})/([^/# ]*)(?:(#comment-|/comment/|/answer/)([^/ ]{17}))?")
913 (let* ((post-id (reg 0))
916 (direct-comment-link (and comment-id
(reg 2) (string= "/" (reg 2) :end2
1))))
919 (format nil
"/posts/~A/~A~[~;#~A~;?commentId=~A~]" post-id slug
(if comment-id
(if direct-comment-link
2 1) 0) comment-id
)
920 (main-site-uri *current-site
*)))))
923 (defun redirect (uri &key
(type :see-other
) preserve-query
)
924 (setf (hunchentoot:return-code
*) (ecase type
(:see-other
303) (:permanent
301))
925 (hunchentoot:header-out
"Location") (if-let ((query (and preserve-query
(hunchentoot:query-string
*))))
926 (quri:render-uri
(quri:make-uri
:defaults uri
:query query
))
929 (defun main-site-redirect (uri &key
(type :see-other
))
930 (redirect (quri:render-uri
(quri:merge-uris uri
(main-site-uri *current-site
*))) :type type
))
932 (defmacro request-method
(&body method-clauses
)
933 (with-gensyms (request-method)
934 `(let ((,request-method
(hunchentoot:request-method
*)))
936 ,.
(loop for method-body in method-clauses
937 collect
(destructuring-bind (method args
&body inner-body
) method-body
938 `(,(if (eq method
:get
) `(member ,request-method
'(:get
:head
)) `(eq ,request-method
,method
))
939 ,(make-binding-form (mapcar (lambda (x) (append (ensure-list x
) `(:request-type
,method
))) args
)
942 (defmacro define-page
(name path-specifier additional-vars
&body body
)
943 (labels ((make-lambda (args)
945 collect
(if (atom a
) a
(first a
)))))
946 (multiple-value-bind (path-specifier-form path-bindings-wrapper specifier-vars
)
947 (if (stringp path-specifier
)
948 (values path-specifier
#'identity
)
949 (destructuring-bind (specifier-type specifier-body
&rest specifier-args
) path-specifier
950 (ecase specifier-type
952 (values `(lambda (r) (funcall ,specifier-body
(hunchentoot:request-uri r
)))
954 (lambda (body) `(ignorable-multiple-value-bind ,(make-lambda specifier-args
) (funcall ,specifier-body
(hunchentoot:request-uri
*)) ,body
))
958 (let ((fn `(lambda (r) (ppcre:scan-to-strings
,specifier-body
(hunchentoot:request-uri r
)))))
961 (with-gensyms (result-vector)
962 `(let ((,result-vector
(nth-value 1 (funcall ,fn hunchentoot
:*request
*))))
963 (declare (type simple-vector
,result-vector
))
965 ,(loop for v in
(make-lambda specifier-args
) as x from
0 collecting
`(,v
(if (> (length ,result-vector
) ,x
) (aref ,result-vector
,x
))))
968 `(hunchentoot:define-easy-handler
(,name
:uri
,path-specifier-form
) ()
971 ,(funcall path-bindings-wrapper
972 (make-binding-form (append (mapcar (lambda (x) (append (ensure-list x
) '(:passthrough t
))) specifier-vars
) additional-vars
)
975 (define-component sort-widget
(&key
(sort-options '((:new
:description
"Sort by date posted")
976 (:hot
:description
"Sort by time-weighted score")
977 (:active
:description
"Sort by date posted or last comment")
978 (:old
:description
"Sort by date posted, oldest first")))
979 (pref :default-sort
) (param-name "sort") (html-class "sort"))
980 (:http-args
((sort :real-name param-name
:member
(mapcar (lambda (x) (if (listp x
) (first x
) x
)) sort-options
))
981 (sortedby :real-name
"sortedBy" :type string
)
982 &without-csrf-check
))
987 (let ((sort-string (if sort
(string-downcase sort
))))
989 (set-user-pref :default-sort sort-string
))
991 (sublevel-nav-to-html sort-options
993 :param-name param-name
994 :extra-class html-class
))
995 (or sort-string
(user-pref pref
)))))
997 (define-component view-index
()
998 (:http-args
((view :member
'(:all
:new
:frontpage
:featured
:meta
:community
:alignment-forum
:questions
:nominations
:reviews
:events
) :default
:frontpage
)
1000 (offset :type fixnum
)
1001 (limit :type fixnum
:default
(user-pref :items-per-page
))
1002 (set-cov-pref :request-type
:post
)
1003 &without-csrf-check
))
1005 (set-user-pref :hide-cov
(string= set-cov-pref
"1")))
1006 (when (eq view
:new
) (redirect (replace-query-params (hunchentoot:request-uri
*) "view" "all" "all" nil
) :type
:permanent
) (return))
1007 (component-value-bind ((sort-string sort-widget
))
1008 (multiple-value-bind (posts total
)
1009 (get-posts-index :view
(string-downcase view
) :before before
:after after
:offset offset
:limit
(1+ limit
) :sort sort-string
1010 :hide-tags
(if (user-pref :hide-cov
) (list (get-slug-tagid "coronavirus"))))
1011 (let ((page-title (format nil
"~@(~A posts~)" view
)))
1013 (view-items-index (firstn posts limit
)
1014 :section view
:title page-title
:hide-title
(eq view
:frontpage
)
1015 :pagination
(pagination-nav-bars :offset
(or offset
0) :total total
:with-next
(and (not total
) (> (length posts
) limit
)))
1016 :content-class
(format nil
"index-page ~(~A~)-index-page" view
)
1018 (page-toolbar-to-html :title page-title
1019 :new-post
(if (eq view
:meta
) "meta" t
)
1020 :hide-cov
(typep *current-site
* 'lesswrong-viewer-site
))
1021 (if (member view
'(:frontpage
:all
))
1022 (funcall sort-widget
)))))))))
1024 (defmacro route-component
(name lambda-list
&rest args
)
1025 `(lambda ,lambda-list
1027 (component-value-bind ((() (,name
,@args
)))
1029 (funcall ,name
))))))
1031 (defmacro define-component-routes
(site-class &rest clauses
)
1034 (for clause in clauses
)
1035 (destructuring-bind (name (route-class &rest route-args
) route-bindings
(component-name &rest component-args
)) clause
1036 (collect `(define-route ',site-class
',route-class
1037 :name
',name
,@route-args
1038 :handler
(route-component ,component-name
,route-bindings
,@component-args
)))))))
1040 (define-component-routes forum-site
1041 (view-root (standard-route :uri
"/") () (view-index))
1042 (view-index (standard-route :uri
"/index") () (view-index)))
1044 (hunchentoot:define-easy-handler
1047 (declare (ignore req
))
1048 (with-site-context ((let ((host (or (hunchentoot:header-in
* :x-forwarded-host
) (hunchentoot:header-in
* :host
))))
1049 (or (find-site host
)
1050 (error "Unknown site: ~A" host
))))
1051 (call-route-handler *current-site
* (hunchentoot:script-name
*)))))
1054 (define-page view-post
"/post" ((id :required t
))
1055 (redirect (generate-item-link :post id
) :type
:permanent
))
1057 (define-page view-post-lw1-link
(:function
#'match-lw1-link
) ()
1058 (redirect (convert-lw1-link (hunchentoot:script-name
*)) :preserve-query t
:type
:permanent
))
1060 (define-page view-post-ea1-link
(:function
#'match-ea1-link
) ()
1061 (redirect (convert-ea1-link (hunchentoot:script-name
*)) :preserve-query t
:type
:permanent
))
1063 (define-page view-post-lw2-slug-link
(:function
#'match-lw2-slug-link
) ()
1064 (redirect (convert-lw2-slug-link (hunchentoot:request-uri
*)) :type
:see-other
))
1066 (define-page view-post-lw2-sequence-link
(:function
#'match-lw2-sequence-link
) ()
1067 (redirect (convert-lw2-sequence-link (hunchentoot:request-uri
*)) :type
:see-other
))
1069 (define-page view-feed
"/feed" ()
1070 (redirect "/?format=rss" :type
:permanent
))
1072 (define-page view-allposts
"/allPosts" ()
1073 (redirect (format nil
"/index~@[?~A~]" (hunchentoot:query-string
*)) :type
:see-other
))
1075 (define-page view-nominations
"/nominations" ()
1076 (redirect "/index?view=nominations" :type
:see-other
))
1078 (define-page view-reviews
"/reviews" ()
1079 (redirect "/index?view=reviews" :type
:see-other
))
1081 (define-page view-review-voting
"/reviewVoting" ()
1082 (redirect "https://www.lesswrong.com/reviewVoting" :type
:see-other
))
1084 (define-page view-coronavirus-link-database
"/coronavirus-link-database" ()
1085 (redirect "https://www.lesswrong.com/coronavirus-link-database" :type
:see-other
))
1087 (defun post-comment (post-id-real &key shortform
)
1089 (:post
(text answer nomination nomination-review af post-id parent-answer-id parent-comment-id edit-comment-id retract-comment-id unretract-comment-id delete-comment-id
)
1090 (let ((lw2-auth-token *current-auth-token
*))
1091 (assert lw2-auth-token
)
1092 (let* ((post-id (or post-id-real post-id
))
1093 (question (when post-id
(cdr (assoc :question
(get-post-body post-id
:auth-token lw2-auth-token
)))))
1099 (t :body
(postprocess-markdown text
))
1100 ((not (or edit-comment-id
(and shortform
(not parent-comment-id
)))) :post-id post-id
)
1101 (parent-comment-id :parent-comment-id parent-comment-id
)
1103 (nomination :nominated-for-review
"2019")
1104 (nomination-review :reviewing-for-review
"2019")
1105 (parent-answer-id :parent-answer-id parent-answer-id
)
1107 ((and shortform
(not parent-comment-id
)) :shortform t
))))
1109 (do-lw2-comment-edit lw2-auth-token edit-comment-id comment-data
)
1110 (do-lw2-comment lw2-auth-token comment-data
))))
1112 (do-lw2-comment-edit lw2-auth-token retract-comment-id
'((:retracted . t
))))
1113 (unretract-comment-id
1114 (do-lw2-comment-edit lw2-auth-token unretract-comment-id
'((:retracted . nil
))))
1116 (do-lw2-comment-remove lw2-auth-token delete-comment-id
:reason
"Comment deleted by its author.")
1120 (get-post-comments post-id
:force-revalidate t
)
1122 (get-post-answers post-id
:force-revalidate t
))))
1124 (alist-bind ((new-comment-id simple-string
:--id
)
1125 (new-comment-html simple-string
:html-body
))
1127 (setf (markdown-source :comment new-comment-id new-comment-html
) text
)
1128 (redirect (quri:render-uri
1129 (quri:merge-uris
(quri:make-uri
:fragment
(format nil
"comment-~A" new-comment-id
))
1130 (hunchentoot:request-uri
*)))))))))))
1132 (defun output-comments (out-stream id comments target
&key overcomingbias-sort preview chrono replies-open
)
1133 (labels ((output-comments-inner ()
1134 (with-error-html-block ()
1136 (comment-thread-to-html out-stream
1138 (comment-item-to-html
1141 :level-invert preview
1142 :extra-html-fn
(lambda (c-id)
1143 (let ((*comment-individual-link
* nil
))
1144 (comment-tree-to-html out-stream
(make-comment-parent-hash comments
)
1146 :level-invert preview
))))))
1148 (progn #|
<div class
="comments-empty-message">(safe (pretty-number (length comments
) id
))</div
>|
#
1150 (comment-chrono-to-html out-stream comments
)
1151 (let ((parent-hash (make-comment-parent-hash comments
)))
1152 (when overcomingbias-sort
1153 (setf (gethash nil parent-hash
)
1154 (sort-items (gethash nil parent-hash
) :old
)))
1155 (comment-tree-to-html out-stream parent-hash
))))
1156 <div class
="comments-empty-message">("No ~As." id
)</div
>)))))
1158 (output-comments-inner)
1159 (progn (format out-stream
"<div id=\"~As\" class=\"comments~:[~; replies-open~]\">" id replies-open
)
1161 <script
>initializeCommentControls\
(\
)</script
>)
1162 (output-comments-inner)
1163 (format out-stream
"</div>")))))
1165 (define-page view-post-lw2-link
(:function
#'match-lw2-link post-id comment-id
* comment-link-type
)
1168 (show-comments :real-name
"comments" :type boolean
:default t
)
1169 (format :type string
))
1172 (when (hunchentoot:get-parameter
"commentId")
1173 (redirect (format nil
"~A/comment/~A" (generate-item-link :post post-id
) comment-id
))
1175 (let* ((lw2-auth-token *current-auth-token
*)
1176 (preview (string-equal format
"preview"))
1177 (show-comments (and (not preview
) show-comments
)))
1178 (multiple-value-bind (post title condition
)
1179 (handler-case (nth-value 0 (get-post-body post-id
:auth-token
(and need-auth lw2-auth-token
)))
1180 (serious-condition (c) (values nil
"Error" c
))
1181 (:no-error
(post) (values post
(cdr (assoc :title post
)) nil
)))
1182 (labels ((extra-head ()
1183 (when-let (canonical-source (and (not comment-id
)
1184 (cdr (assoc :canonical-source post
))))
1185 <link rel
="canonical" href
=canonical-source
>)
1186 <script
>postId
=(with-html-stream-output (:stream stream
) (json:encode-json post-id stream
))</script
>
1187 <script
>alignmentForumPost
=(if (cdr (assoc :af post
)) "true" "false")</script
>)
1188 (retrieve-individual-comment (comment-thread-type)
1189 (let* ((comments (case comment-thread-type
1190 (:comment
(get-post-comments post-id
))
1191 (:answer
(get-post-answers post-id
))))
1192 (target-comment (find comment-id comments
:key
(lambda (c) (cdr (assoc :--id c
))) :test
#'string
=)))
1193 (values comments target-comment
))))
1195 (let ((comment-thread-type (if (string= comment-link-type
"answer") :answer
:comment
)))
1196 (multiple-value-bind (comments target-comment
) (retrieve-individual-comment comment-thread-type
)
1197 (unless target-comment
1198 ;; If the comment was not found, try as an answer, or vice versa.
1199 (setf comment-thread-type
(if (eq comment-thread-type
:comment
) :answer
:comment
)
1200 (values comments target-comment
) (retrieve-individual-comment comment-thread-type
))
1201 (unless target-comment
1202 (error (make-condition 'lw2-not-found-error
))))
1203 (let* ((*comment-individual-link
* t
)
1204 (display-name (get-username (cdr (assoc :user-id target-comment
))))
1206 ((and (eq comment-thread-type
:answer
)
1207 (not (cdr (assoc :parent-comment-id target-comment
))))
1209 (t "comments on"))))
1210 (emit-page (out-stream :title
(format nil
"~A ~A ~A" display-name verb-phrase title
)
1211 :content-class
"individual-thread-page comment-thread-page"
1212 :social-description
(when-let (x (cdr (assoc :html-body target-comment
))) (extract-excerpt x
))
1213 :extra-head
#'extra-head
)
1215 (format out-stream
"<h1 class=\"post-title\">~A ~A <a href=\"~A\">~A</a></h1>"
1216 (encode-entities display-name
)
1218 (generate-item-link :post post-id
)
1219 (clean-text-to-html title
:hyphenation nil
)))
1220 (output-comments out-stream
"comment" comments target-comment
:chrono chrono
:preview preview
)))))
1221 (let ((post-sequences (get-post-sequences post-id
)))
1222 (emit-page (out-stream :title title
1223 :content-class
(format nil
"post-page comment-thread-page~{ ~A~}"
1224 (list-cond ((cdr (assoc :question post
)) "question-post-page")
1225 (post-sequences "in-sequence")
1226 ((not show-comments
) "no-comments")))
1227 :social-description
(when-let (x (cdr (assoc :html-body post
))) (extract-excerpt x
))
1228 :extra-head
#'extra-head
)
1231 (error-explanation-case (error-to-html condition
)
1232 (lw2-not-allowed-error
1233 <p
>This probably means the post has been deleted or moved back to the author
's drafts.
</p
>)))
1235 (post-body-to-html post
)))
1236 (when (and lw2-auth-token
(equal (logged-in-userid) (cdr (assoc :user-id post
))))
1237 (format out-stream
"<div class=\"post-controls\"><a class=\"edit-post-link button\" href=\"/edit-post?post-id=~A\" accesskey=\"e\" title=\"Edit post [e]\">Edit post</a></div>"
1238 (cdr (assoc :--id post
))))
1239 (post-nav-links post post-sequences
)
1241 (write-string "<script> </script>" out-stream
)
1242 (force-output out-stream
)
1243 (with-error-html-block ()
1244 ;; Temporary hack to support nominations
1245 (let* ((real-comments (get-post-comments post-id
))
1246 (answers (when (cdr (assoc :question post
))
1247 (get-post-answers post-id
)))
1248 (posted-at (and (typep *current-backend
* 'backend-lw2
)
1249 (cdr (assoc :posted-at post
))))
1250 (posted-timestamp (and posted-at
(local-time:parse-timestring posted-at
)))
1251 (nominations-eligible (timerange "2018-01-01" posted-timestamp
"2020-01-01"))
1252 (nominations-current (and nominations-eligible
(timerange "2019-01-01" posted-timestamp
)))
1253 (now (local-time:now
))
1254 (nominations-open (and nominations-current
(timerange "2020-12-01" now
"2020-12-14")))
1255 (reviews-open (and nominations-current
(timerange "2020-12-14" now
"2021-01-11"))))
1256 (labels ((top-level-property (comment property
)
1257 (or (cdr (assoc property comment
))
1258 (cdr (assoc property
(cdr (assoc :top-level-comment comment
)))))))
1259 (multiple-value-bind (normal-comments nominations reviews
)
1260 (loop for comment in real-comments
1261 if
(top-level-property comment
:nominated-for-review
)
1262 collect comment into nominations
1263 else if
(top-level-property comment
:reviewing-for-review
)
1264 collect comment into reviews
1266 collect comment into normal-comments
1267 finally
(return (values normal-comments nominations reviews
)))
1268 (loop for
(name comments open
) in
(list-cond (nominations-eligible
1269 (list "nomination" nominations nominations-open
))
1270 (nominations-eligible
1271 (list "review" reviews reviews-open
))
1272 ((cdr (assoc :question post
))
1273 (list "answer" answers t
))
1275 (list "comment" normal-comments t
)))
1276 do
(output-comments out-stream name comments nil
1278 :overcomingbias-sort
(cdr (assoc :comment-sort-order post
)) :chrono chrono
:preview preview
))))))))))))))
1280 (post-comment post-id
))))
1282 (defparameter *edit-post-template
* (compile-template* "edit-post.html"))
1284 (define-page view-edit-post
"/edit-post" (title url section tags post-id link-post
)
1287 (let* ((csrf-token (make-csrf-token))
1288 (post-body (if post-id
(get-post-body post-id
:auth-token
(hunchentoot:cookie-in
"lw2-auth-token"))))
1289 (section (or section
(loop for
(sym . sec
) in
'((:draft .
"drafts") (:meta .
"meta") (:frontpage-date .
"frontpage"))
1290 if
(cdr (assoc sym post-body
)) return sec
1291 finally
(return "all")))))
1292 (emit-page (out-stream :title
(if post-id
"Edit Post" "New Post") :content-class
"edit-post-page")
1293 (render-template* *edit-post-template
* out-stream
1294 :csrf-token csrf-token
1295 :title
(cdr (assoc :title post-body
))
1296 :url
(cdr (assoc :url post-body
))
1297 :question
(cdr (assoc :question post-body
))
1298 :tags-supported
(typep *current-backend
* 'backend-accordius
)
1299 :tags
(when (and post-id
(typep *current-backend
* 'backend-accordius
)) (do-wl-rest-query (format nil
"posts/~a/update_tagset/" post-id
) '()))
1301 :section-list
(loop for
(name desc
) in
'(("all" "All") ("meta" "Meta") ("drafts" "Drafts"))
1302 collect
(alist :name name
:desc desc
:selected
(string= name section
)))
1303 :lesswrong-misc
(typep *current-backend
* 'backend-lw2-misc-features
)
1304 :submit-to-frontpage
(if post-id
(cdr (assoc :submit-to-frontpage post-body
)) t
)
1305 :markdown-source
(or (and post-id
(markdown-source :post post-id
(cdr (assoc :html-body post-body
)))) "")))))
1306 (:post
(text question submit-to-frontpage
)
1307 (let ((lw2-auth-token *current-auth-token
*)
1308 (url (if (string= url
"") nil url
)))
1309 (assert lw2-auth-token
)
1312 (t :body
(postprocess-markdown text
))
1314 (link-post :url url
)
1315 (t :meta
(or (string= section
"meta") :false
))
1316 ((not post-id
) :is-event nil
)
1317 (t :draft
(or (string= section
"drafts") :false
))
1318 ((typep *current-backend
* 'backend-lw2-misc-features
) :submit-to-frontpage
(and submit-to-frontpage t
))
1319 ((not post-id
) :question
(if question t
:false
))))
1322 ((not link-post
) :url t
)))
1325 (do-lw2-post-edit lw2-auth-token post-id post-data post-unset
)
1326 (do-lw2-post lw2-auth-token post-data
)))
1327 (new-post-id (cdr (assoc :--id new-post-data
))))
1328 (assert new-post-id
)
1329 (when (typep *current-backend
* 'backend-accordius
)
1330 (do-wl-rest-mutate :post
1331 (format nil
"posts/~a/update_tagset/" post-id
)
1334 (setf (markdown-source :post new-post-id
(cdr (assoc :html-body new-post-data
))) text
)
1335 (ignore-errors (get-post-body post-id
:force-revalidate t
))
1336 (redirect (if (js-true (cdr (assoc :draft post-data
)))
1337 (concatenate 'string
(generate-item-link :post new-post-data
) "?need-auth=y")
1338 (generate-item-link :post new-post-data
))))))))
1340 (hunchentoot:define-easy-handler
(view-karma-vote :uri
"/karma-vote") ()
1342 (setf (hunchentoot:content-type
*) "application/json")
1343 (let ((auth-token *current-auth-token
*))
1344 (with-response-stream (out-stream)
1346 (:get
(post-id shortform
(offset :type fixnum
:default
0))
1347 (json:encode-json-alist
1349 (post-id "postVote" (get-post-vote post-id auth-token
))
1350 (post-id "commentVotes" (alist-hash-table (delete-if (lambda (x) (null (cdr x
))) (get-post-comments-votes post-id auth-token
))))
1351 (shortform "commentVotes" (alist-hash-table (delete-if (lambda (x) (null (cdr x
))) (get-shortform-votes auth-token
:offset offset
))))
1352 (t "alignmentForumAllowed"
1353 (member "alignmentForum" (cdr (assoc :groups
(get-user :user-id
(logged-in-userid)))) :test
#'string
=)))
1355 (:post
(target target-type vote-type
)
1356 (multiple-value-bind (points vote-type vote-data
) (do-lw2-vote auth-token target target-type vote-type
)
1358 (alist-bind (vote-count af af-base-score
) vote-data
1359 (list (vote-buttons points
:as-text t
:af-score
(and af af-base-score
)) vote-type
(votes-to-tooltip vote-count
)))
1362 (hunchentoot:define-easy-handler
(view-check-notifications :uri
"/check-notifications") (format)
1364 (setf (hunchentoot:content-type
*) "application/json")
1365 (if *current-auth-token
*
1366 (let ((notifications-status (check-notifications (logged-in-userid) *current-auth-token
* :full
(string= format
"push"))))
1367 (json:encode-json-to-string notifications-status
)))))
1369 (hunchentoot:define-easy-handler
(view-ignore-user :uri
"/ignore-user") ((csrf-token :request-type
:post
) (target-id :request-type
:post
) (state :request-type
:post
) return
)
1371 (check-csrf-token csrf-token
)
1372 (let ((user-id (logged-in-userid)))
1373 (unless user-id
(error "Not logged in."))
1374 (with-cache-transaction
1375 (let ((ignore-hash (get-ignore-hash user-id
)))
1376 (if (string= state
"ignore")
1377 (setf (gethash target-id ignore-hash
) t
)
1378 (remhash target-id ignore-hash
))
1379 (cache-put "user-ignore-list" user-id ignore-hash
:value-type
:json
))))
1381 (redirect return
))))
1383 (client-defun comment-controls (&key standalone parent-comment-id parent-answer-id edit-comment-id
)
1385 <form method
="post" id
="conversation-form" class
="aligned-form">
1386 <div class
="textarea-container">
1387 <textarea name
="text" oninput
="enableBeforeUnload();"></textarea
>
1388 <span class
="markdown-reference-link">You can use
<a href
="http://commonmark.org/help/" target
="_blank">Markdown
</a
> here.
</span
>
1389 <button type
="button" class
="guiedit-mobile-auxiliary-button guiedit-mobile-help-button">Help
</button
>
1390 <button type
="button" class
="guiedit-mobile-auxiliary-button guiedit-mobile-exit-button">Exit
</button
>
1393 (macrolet ((hidden-var (name)
1394 `(when ,name
<input type
="hidden" name
=,(string-downcase name
) value
=,name
>)))
1395 (hidden-var parent-comment-id
)
1396 (hidden-var parent-answer-id
)
1397 (hidden-var edit-comment-id
))
1398 <input name
="csrf-token" value
=(make-csrf-token) type
="hidden">
1399 <input value
="Submit" type
="submit">
1403 <div class
="posting-controls standalone with-markdown-editor" onsubmit
="disableBeforeUnload();">(with-html-stream-output (inner))</div
>
1406 (define-component view-comments-index
(index-type)
1407 (:http-args
((offset :type fixnum
)
1408 (limit :type fixnum
)
1409 (view :member
'(nil :alignment-forum
))))
1412 (let ((want-total (not (or (typep *current-backend
* 'backend-lw2
) (typep *current-backend
* 'backend-ea-forum
)))) ; LW2/EAF can't handle total queries. TODO: handle this in backend.
1413 (shortform (eq index-type
:shortform
)))
1414 (multiple-value-bind (title query-view top-nav
)
1416 (shortform (values "Shortform" "shortform" (if (logged-in-userid) (lambda () (comment-controls :standalone t
)))))
1417 (t (values (case view
(:alignment-forum
"Alignment Forum recent comments") (t "Recent comments")) "allRecentComments" nil
)))
1418 (multiple-value-bind (recent-comments total
)
1419 (if (or (not (eq index-type
:recent-comments
)) offset limit view
(/= (user-pref :items-per-page
) 20))
1420 (let ((*use-alignment-forum
* (eq view
:alignment-forum
)))
1421 (lw2-graphql-query (lw2-query-string :comment
:list
1422 (remove nil
(alist :view query-view
1423 :limit
(or limit
(user-pref :items-per-page
)) :offset offset
)
1425 :context
(if shortform
:shortform
:index
)
1426 :with-total want-total
)))
1427 (get-recent-comments :with-total want-total
))
1429 (view-items-index recent-comments
1431 :content-class
(if shortform
"index-page shortform-index-page comment-thread-page" "index-page comment-index-page")
1432 :pagination
(pagination-nav-bars :offset
(or offset
0) :with-next
(not want-total
) :total
(if want-total total
))
1433 :top-nav
(lambda () (page-toolbar-to-html :title title
) (when top-nav
(funcall top-nav
)))
1434 :alternate-html
(if (eq index-type
:shortform
)
1436 <div class
="comments">
1437 (comment-tree-to-html *html-output
*
1438 (make-comment-parent-hash
1439 (flatten-shortform-comments recent-comments
)))
1442 (post-comment nil
:shortform t
))))
1444 (define-component-routes forum-site
(view-recent-comments (standard-route :uri
"/recentcomments") () (view-comments-index :recent-comments
)))
1445 (define-component-routes shortform-site
(view-shortform (standard-route :uri
"/shortform") () (view-comments-index :shortform
)))
1447 (delete-easy-handler 'view-recent-comments
)
1449 (defun tag-to-html (tag &key skip-headline
)
1450 (schema-bind (:tag tag
:auto
:context
:body
)
1451 (alist-bind (edited-at html user-id
) description
1452 (unless skip-headline
1453 <h1 class
="post-title">(safe (clean-text-to-html name
))</h1
>
1454 <div class
="post-meta">
1455 <span
>(if core
"Core ")(if wiki-only
"Wiki" "Tag")</span
>
1456 <span
>Last edit
: (pretty-time-html edited-at
:inline t
)
1457 \ by
<a class
="author" href
=("/users/~A" (get-user-slug user-id
))>(get-username user-id
)</a
>
1461 <div class
="tag-description body-text">(with-html-stream-output (:stream stream
) (let ((*memoized-output-stream
* stream
)) (clean-html* html
)))</div
>))))
1463 (defun tag-list-to-html (tags)
1464 <ul class
="tag-list">
1465 (iter (for tag in tags
)
1466 (schema-bind (:tag tag
:auto
:context
:index
)
1467 <li
><a class
="post-title-link" href
=("/tag/~A" slug
)>(safe (clean-text-to-html name
))(if wiki-only
1469 (if post-count
(format nil
" (~A)" post-count
)))</a
></li
>))
1472 (define-component view-tag
(slug)
1473 (:http-args
((sort :default
:relevant
:member
'(:relevant
:new
:old
))))
1474 (let* ((tag (first (lw2-graphql-query (lw2-query-string :tag
:list
(alist :view
"tagBySlug" :slug slug
) :context
:body
))))
1475 (posts (get-tag-posts slug
))
1476 (posts (if (eq sort
:relevant
) posts
(sort-items posts sort
))))
1477 (schema-bind (:tag tag
:auto
:context
:body
)
1479 (view-items-index posts
1480 :title
(format nil
"~A tag" name
)
1482 (page-toolbar-to-html :title name
:rss
(not wiki-only
))
1484 (when (and posts
(not *preview
*))
1485 (sublevel-nav-to-html '(:relevant
:new
:old
)
1489 :extra-class
"sort")))
1490 :content-class
"index-page tag-index-page"
1491 :alternate-html
(lambda ()
1493 (write-index-items-to-html *html-output
* posts
))
1494 (when (typep *current-backend
* 'backend-lw2-tags-comments
)
1495 (finish-output *html-output
*)
1496 (let ((comments (lw2-graphql-query (lw2-query-string :comment
:list
(alist :view
"commentsOnTag" :tag-id tag-id
)))))
1497 (output-comments *html-output
* "comment" comments nil
)))))))))
1499 (define-component-routes forum-site
(view-tag (regex-route :regex
"/tag/([^/?]+)") (slug) (view-tag slug
)))
1501 (define-component view-tags-index
()
1503 (multiple-value-bind (all-tags portal
)
1504 (lw2-graphql-query-multi
1505 (list (lw2-query-string* :tag
:list
(alist :view
"allTagsAlphabetical"))
1506 (lw2-query-string* :tag
:list
(alist :view
"tagBySlug" :slug
"portal") :context
:body
)))
1508 (iter (for tag in all-tags
)
1509 (schema-bind (:tag tag
(core))
1510 (when core
(collect tag
)))))
1511 (title (if (typep *current-site
* 'lesswrong-viewer-site
) "Concepts Portal" "Tags Portal")))
1513 (emit-page (out-stream :title title
)
1515 <h1 class
="post-title">(safe title
)</h1
>
1516 (tag-to-html (first portal
) :skip-headline t
)
1518 (iter (for (title . tags
) in
(alist "Core Tags" core-tags
"All Tags" all-tags
))
1520 <h1
>(safe title
)</h1
>
1521 (tag-list-to-html tags
))))))))
1523 (define-component-routes forum-site
(view-tags-index (standard-route :uri
"/tags") () (view-tags-index)))
1525 (define-route 'forum-site
'standard-route
:name
'view-tags-index-redirect
:uri
"/tags/all" :handler
(lambda () (redirect "/tags")))
1527 (define-route 'alternate-frontend-site
'standard-route
:name
'view-tags-voting-redirect
:uri
"/tagVoting" :handler
(lambda () (main-site-redirect "/tagVoting")))
1528 (define-route 'alternate-frontend-site
'standard-route
:name
'view-tags-dashboard-redirect
:uri
"/tags/dashboard" :handler
(lambda () (main-site-redirect "/tags/dashboard")))
1530 (hunchentoot:define-easy-handler
(view-push-register :uri
"/push/register") ()
1532 (let* ((data (call-with-safe-json (lambda ()
1533 (json:decode-json-from-string
1534 (hunchentoot:raw-post-data
:force-text t
:external-format
:utf-8
)))))
1535 (subscription (cdr (assoc :subscription data
)))
1536 (cancel (cdr (assoc :cancel data
))))
1539 (make-subscription *current-auth-token
*
1540 (cdr (assoc :endpoint subscription
))
1541 (cdr (assoc :expires
*current-auth-status
*)))
1542 (send-notification (cdr (assoc :endpoint subscription
)) :ttl
120))
1544 (delete-subscription *current-auth-token
*)))
1547 (hunchentoot:define-easy-handler
(view-inbox-redirect :uri
"/push/go-inbox") ()
1549 (redirect (format nil
"/users/~A?show=inbox" *current-user-slug
*))))
1551 (define-page view-user
(:regex
"^/users/(.*?)(?:$|\\?)|^/user" user-slug
) (id
1552 (offset :type fixnum
:default
0)
1553 (show :member
'(:all
:posts
:comments
:drafts
:conversations
:inbox
) :default
:all
)
1554 (sort :member
'(:top
:new
:old
) :default
:new
))
1555 (let* ((auth-token (if (eq show
:inbox
) *current-auth-token
*))
1557 (let ((ui (get-user (cond (user-slug :user-slug
) (id :user-id
)) (or user-slug id
) :auth-token auth-token
)))
1558 (if (and (not (cdr (assoc :deleted ui
))) (cdr (assoc :--id ui
)))
1560 (error (make-condition 'lw2-user-not-found-error
)))))
1561 (user-id (cdr (assoc :--id user-info
)))
1562 (own-user-page (logged-in-userid user-id
))
1563 (display-name (if user-slug
(cdr (assoc :display-name user-info
)) user-id
))
1564 (show-text (if (not (eq show
:all
)) (string-capitalize show
)))
1565 (title (format nil
"~A~@['s ~A~]" display-name show-text
))
1566 (sort-type (case sort
(:top
:score
) (:new
:date
) (:old
:date-reverse
))))
1567 (multiple-value-bind (items total
)
1570 (get-user-page-items user-id
:posts
:offset offset
:limit
(+ 1 (user-pref :items-per-page
)) :sort-type sort-type
))
1572 (get-user-page-items user-id
:comments
:offset offset
:limit
(+ 1 (user-pref :items-per-page
)) :sort-type sort-type
))
1574 (get-user-page-items user-id
:posts
:drafts t
:offset offset
:limit
(+ 1 (user-pref :items-per-page
)) :auth-token
(hunchentoot:cookie-in
"lw2-auth-token")))
1576 (let ((conversations
1577 (lw2-graphql-query (lw2-query-string :conversation
:list
1578 (alist :view
"userConversations" :limit
(+ 1 (user-pref :items-per-page
)) :offset offset
:user-id user-id
)
1579 :fields
'(:--id
:created-at
:title
(:participants
:display-name
:slug
) :----typename
))
1580 :auth-token
(hunchentoot:cookie-in
"lw2-auth-token"))))
1581 (lw2-graphql-query-map
1583 (lw2-query-string* :message
:total
(alist :view
"messagesConversation" :conversation-id
(cdr (assoc :--id c
)))))
1585 :postprocess
(lambda (c result
)
1586 (acons :messages-total result c
))
1587 :auth-token
(hunchentoot:cookie-in
"lw2-auth-token"))))
1589 (error-explanation-case
1591 (let ((notifications (get-notifications :user-id user-id
:offset offset
:auth-token
(hunchentoot:cookie-in
"lw2-auth-token")))
1592 (last-check (ignore-errors (local-time:parse-timestring
(cdr (assoc :last-notifications-check user-info
))))))
1593 (labels ((check-new (key obj
)
1594 (if (ignore-errors (local-time:timestamp
< last-check
(local-time:parse-timestring
(cdr (assoc key obj
)))))
1595 (acons :highlight-new t obj
)
1597 (check-replied (comment)
1598 (let* ((post-id (cdr (assoc :post-id comment
)))
1599 (comment-id (cdr (assoc :--id comment
)))
1600 (comments (funcall (if (or (cdr (assoc :answer comment
))
1601 (cdr (assoc :parent-answer-id comment
)))
1606 (reply-comment (find-if (lambda (c)
1607 (and (string= (cdr (assoc :parent-comment-id c
)) comment-id
)
1608 (string= (cdr (assoc :user-id c
)) user-id
)))
1611 (acons :replied
(list :post post-id
:comment-id
(cdr (assoc :--id reply-comment
))) comment
)
1613 (lw2-graphql-query-map
1615 (alexandria:switch
((cdr (assoc :document-type n
)) :test
#'string
=)
1617 (lw2-query-string* :comment
:single
1618 (alist :document-id
(cdr (assoc :document-id n
)))
1621 (lw2-query-string* :post
:single
(alist :document-id
(cdr (assoc :document-id n
)))))
1623 (lw2-query-string* :message
:single
(alist :document-id
(cdr (assoc :document-id n
)))
1624 :fields
*messages-index-fields
*))
1628 :postprocess
(lambda (n result
)
1630 (funcall (if (string= (cdr (assoc :document-type n
)) "comment") #'check-replied
#'identity
)
1632 (alexandria:switch
((cdr (assoc :document-type n
)) :test
#'string
=)
1633 ("comment" :posted-at
)
1635 ("message" :created-at
))
1638 :auth-token auth-token
)))
1640 (hunchentoot:cookie-in
"lw2-auth-token")
1642 (alist :last-notifications-check
(timestamp-to-graphql (local-time:now
)))))
1643 (lw2-not-allowed-error
1644 <p
>This may mean your login token has expired or become invalid. You can try
<a href
="/login">logging in again
</a
>.
</p
>)))
1646 (get-user-page-items user-id
:both
:offset offset
:limit
(+ 1 (user-pref :items-per-page
)) :sort-type sort-type
)))
1647 (let ((with-next (> (length items
) (+ (if (eq show
:all
) offset
0) (user-pref :items-per-page
))))
1648 (interleave (if (eq show
:all
) (comment-post-interleave items
:limit
(user-pref :items-per-page
) :offset
(if (eq show
:all
) offset nil
) :sort-by sort-type
) (firstn items
(user-pref :items-per-page
))))) ; this destructively sorts items
1649 (view-items-index interleave
:title title
1650 :content-class
(format nil
"user-page~@[ ~A-user-page~]~:[~; own-user-page~]" (string-downcase show
) own-user-page
)
1651 :current-uri
(format nil
"/users/~A" user-slug
)
1653 :pagination
(pagination-nav-bars :offset offset
:total total
:with-next
(if (not total
) with-next
))
1654 :need-auth
(eq show
:drafts
) :section
(if (eq show
:drafts
) "drafts" nil
)
1656 (page-toolbar-to-html :title title
1657 :enable-push-notifications
(eq show
:inbox
)
1658 :rss
(not (member show
'(:drafts
:conversations
:inbox
)))
1659 :new-post
(if (eq show
:drafts
) "drafts" t
)
1660 :new-conversation
(if own-user-page t user-slug
)
1661 :ignore
(if (and (logged-in-userid) (not own-user-page
))
1663 (let ((ignored (gethash user-id
*current-ignore-hash
*)))
1664 <form method
="post" action
="/ignore-user">
1665 <button class
=("button ~A" (if ignored
"unignore-button" "ignore-button"))>(if ignored
"Unignore user" "Ignore user")</button
>
1666 <input type
="hidden" name
="csrf-token" value
=(make-csrf-token)>
1667 <input type
="hidden" name
="target-id" value
=user-id
>
1668 <input type
="hidden" name
="state" value
=(if ignored
"unignore" "ignore")>
1669 <input type
="hidden" name
="return" value
=(hunchentoot:request-uri
*)>
1671 :logout own-user-page
)
1672 (alist-bind ((actual-id string
:--id
)
1673 (actual-slug string
:slug
)
1674 (karma (or null fixnum
))
1675 (af-karma (or null fixnum
)))
1677 <h1 class
="page-main-heading"
1678 (when (not own-user-page
)
1679 (format nil
"data-~:[~;anti-~]~:*kibitzer-redirect=~:[/users/~*~A~;/user?id=~A~]"
1680 user-slug actual-id actual-slug
))>
1681 (progn display-name
)
1682 (let ((full-name (get-user-full-name user-id
)))
1683 (when (and user-slug
(stringp full-name
) (> (length full-name
) 0))
1684 <span class
="user-full-name">\
((progn full-name
)\
)</span
>))
1686 <div class
="user-stats">
1688 <span class
="karma-type">
1689 <span class
="karma-total">(if user-slug
(pretty-number (or karma
0)) "##")</span
>(if af-karma
" (LW),")
1692 <span class
="karma-type">
1693 <span class
="karma-total af-karma-total">(if user-slug
(pretty-number (or af-karma
0)) "##")</span
> \
(AF\
)
1696 (when-let (html-bio (cdr (assoc :html-bio user-info
)))
1697 <div class
="user-bio body-text">
1698 (with-html-stream-output (:stream stream
)
1699 (let ((*memoized-output-stream
* stream
))
1700 (clean-html* html-bio
)))
1702 (sublevel-nav-to-html `(:all
:posts
:comments
1704 '(:drafts
:conversations
:inbox
)))
1707 (when (member show
'(:all
:posts
:comments
))
1708 (sublevel-nav-to-html '(:new
:top
:old
)
1712 :extra-class
"sort"))))))))
1714 (defparameter *conversation-template
* (compile-template* "conversation.html"))
1716 (define-page view-conversation
"/conversation" (id to subject
)
1720 ((and id to
) (error "This is an invalid URL."))
1722 (multiple-value-bind (conversation messages
)
1723 (get-conversation-messages id
(hunchentoot:cookie-in
"lw2-auth-token"))
1724 (let ((conversation (rectify-conversation conversation
)))
1725 (view-items-index (nreverse messages
) :content-class
"conversation-page" :need-auth t
:title
(encode-entities (cdr (assoc :title conversation
)))
1726 :top-nav
(lambda () (render-template* *conversation-template
* *html-output
*
1727 :conversation conversation
:csrf-token
(make-csrf-token)))))))
1729 (emit-page (out-stream :title
"New conversation" :content-class
"conversation-page")
1730 (render-template* *conversation-template
* out-stream
1732 :csrf-token
(make-csrf-token))))))
1733 (:post
((text :required t
))
1735 (let ((participant-ids (list (logged-in-userid) (cdar (lw2-graphql-query (lw2-query-string :user
:single
(alist :slug to
) :fields
'(:--id
)))))))
1736 (do-create-conversation (hunchentoot:cookie-in
"lw2-auth-token") (alist :participant-ids participant-ids
:title subject
))))))
1737 (do-create-message (hunchentoot:cookie-in
"lw2-auth-token") id text
)
1738 (redirect (format nil
"/conversation?id=~A" id
))))))
1740 (defun search-result-markdown-to-html (item)
1741 (cons (cons :html-body
1742 (handler-case (nth-value 1 (cl-markdown:markdown
(cdr (assoc :body item
)) :stream nil
))
1743 (serious-condition () "[Error while processing search result]")))
1746 (define-page view-search
"/search" ((q :required t
))
1747 (let ((*current-search-query
* q
)
1748 (link (convert-any-link* q
))
1749 (title (format nil
"~@[~A - ~]Search" q
)))
1750 (declare (special *current-search-query
*))
1753 (multiple-value-bind (tags posts comments
) (lw2-search-query q
)
1754 (let ((tags (map 'list
(lambda (tag)
1755 (alist* :----typename
"Tag" tag
))
1757 (view-items-index (nconc
1758 (map 'list
(lambda (post) (if (cdr (assoc :comment-count post
)) post
(alist* :comment-count
0 post
))) posts
)
1759 (map 'list
#'search-result-markdown-to-html comments
))
1761 (page-toolbar-to-html :title title
:rss t
)
1764 (tag-list-to-html tags
)))
1765 :content-class
"search-results-page" :current-uri
"/search"
1768 (define-page view-login
"/login" (return cookie-check
1769 (login-username :request-type
:post
) (login-password :request-type
:post
)
1770 (signup-username :request-type
:post
) (signup-email :request-type
:post
) (signup-password :request-type
:post
) (signup-password2 :request-type
:post
))
1772 ((emit-login-page (&key error-message
)
1773 (let ((csrf-token (make-csrf-token)))
1774 (emit-page (out-stream :title
"Log in" :current-uri
"/login" :content-class
"login-page" :robots
"noindex, nofollow")
1776 (format out-stream
"<div class=\"error-box\">~A</div>" error-message
))
1777 (with-outputs (out-stream) "<div class=\"login-container\">")
1778 (output-form out-stream
"post" (format nil
"/login~@[?return=~A~]" (if return
(url-rewrite:url-encode return
))) "login-form" "Log in" csrf-token
1779 '(("login-username" "Username" "text" "username")
1780 ("login-password" "Password" "password" "current-password"))
1782 :end-html
(when (typep *current-backend
* 'backend-websocket-login
) ;other backends not supported yet
1783 "<a href=\"/reset-password\">Forgot password</a>"))
1784 (output-form out-stream
"post" (format nil
"/login~@[?return=~A~]" (if return
(url-rewrite:url-encode return
))) "signup-form" "Create account" csrf-token
1785 '(("signup-username" "Username" "text" "username")
1786 ("signup-email" "Email" "text" "email")
1787 ("signup-password" "Password" "password" "new-password")
1788 ("signup-password2" "Confirm password" "password" "new-password"))
1790 (if-let (main-site-title (main-site-title *current-site
*))
1791 (format out-stream
"<div class=\"login-tip\"><span>Tip:</span> You can log in with the same username and password that you use on ~A~:*. Creating an account here also creates one on ~A.</div>"
1793 (format out-stream
"</div>"))))
1794 (finish-login (username user-id auth-token error-message
&optional expires
)
1797 (set-cookie "lw2-auth-token" auth-token
:max-age
(if expires
(+ (- expires
(get-unix-time)) (* 24 60 60)) (1- (expt 2 31))))
1798 (if expires
(set-cookie "lw2-status" (json:encode-json-to-string
(alist :expires expires
))))
1799 (cache-put "auth-token-to-userid" auth-token user-id
)
1800 (cache-put "auth-token-to-username" auth-token username
)
1801 (redirect (if (and return
(ppcre:scan
"^/[^/]" return
)) return
"/")))
1803 (emit-login-page :error-message error-message
)))))
1805 ((not (or cookie-check
(hunchentoot:cookie-in
"session-token")))
1806 (set-cookie "session-token" (base64:usb8-array-to-base64-string
(ironclad:make-random-salt
)))
1807 (redirect (format nil
"/login?~@[return=~A&~]cookie-check=y" (if return
(url-rewrite:url-encode return
)))))
1809 (if (hunchentoot:cookie-in
"session-token")
1810 (redirect (format nil
"/login~@[?return=~A~]" (if return
(url-rewrite:url-encode return
))))
1811 (emit-page (out-stream :title
"Log in" :current-uri
"/login")
1812 (format out-stream
"<h1>Enable cookies</h1><p>Please enable cookies in your browser and <a href=\"/login~@[?return=~A~]\">try again</a>.</p>" (if return
(url-rewrite:url-encode return
))))))
1815 ((or (string= login-username
"") (string= login-password
"")) (emit-login-page :error-message
"Please enter a username and password"))
1816 ((lw2.dnsbl
:dnsbl-check
(hunchentoot:real-remote-addr
)) (emit-login-page :error-message
"Your IP address is blacklisted."))
1817 (t (multiple-value-call #'finish-login login-username
(do-login login-username login-password
)))))
1820 ((not (every (lambda (x) (not (string= x
""))) (list signup-username signup-email signup-password signup-password2
)))
1821 (emit-login-page :error-message
"Please fill in all fields"))
1822 ((not (string= signup-password signup-password2
))
1823 (emit-login-page :error-message
"Passwords do not match"))
1824 ((lw2.dnsbl
:dnsbl-check
(hunchentoot:real-remote-addr
)) (emit-login-page :error-message
"Your IP address is blacklisted."))
1825 (t (multiple-value-call #'finish-login signup-username
(do-lw2-create-user signup-username signup-email signup-password
)))))
1827 (emit-login-page)))))
1829 (define-page view-logout
"/logout" ()
1832 (set-cookie "lw2-auth-token" "" :max-age
0)
1833 (do-logout *current-auth-token
*)
1836 (defparameter *reset-password-template
* (compile-template* "reset-password.html"))
1838 (define-page view-reset-password
"/reset-password" ((email :request-type
:post
) (reset-link :request-type
:post
) (password :request-type
:post
) (password2 :request-type
:post
))
1839 (labels ((emit-rpw-page (&key message message-type step
)
1840 (let ((csrf-token (make-csrf-token)))
1841 (emit-page (out-stream :title
"Reset password" :content-class
"reset-password" :robots
"noindex, nofollow")
1842 (render-template* *reset-password-template
* out-stream
1843 :csrf-token csrf-token
1844 :reset-link reset-link
1846 :message-type message-type
1850 (multiple-value-bind (ret error
)
1851 (do-lw2-forgot-password email
)
1852 (declare (ignore ret
))
1854 (emit-rpw-page :step
1 :message error
:message-type
"error")
1855 (emit-rpw-page :step
1 :message
"Password reset email sent." :message-type
"success"))))
1857 (ppcre:register-groups-bind
(reset-token) ("(?:reset-password/|^)([^/#]+)$" reset-link
)
1860 (emit-rpw-page :step
2 :message
"Invalid password reset link." :message-type
"error"))
1861 ((not (string= password password2
))
1862 (emit-rpw-page :step
2 :message
"Passwords do not match." :message-type
"error"))
1864 (multiple-value-bind (user-id auth-token error-message
) (do-lw2-reset-password reset-token password
)
1865 (declare (ignore user-id auth-token
))
1867 (error-message (emit-rpw-page :step
2 :message error-message
:message-type
"error"))
1869 (with-error-page (emit-page (out-stream :title
"Reset password" :content-class
"reset-password")
1870 (format out-stream
"<h1>Password reset complete</h1><p>You can now <a href=\"/login\">log in</a> with your new password.</p>"))))))))))
1874 (define-page view-sequences
"/library"
1875 ((view :member
'(:featured
:community
) :default
:featured
))
1878 (lw2-query-string :sequence
:list
1879 (alist :view
(case view
1880 (:featured
"curatedSequences")
1881 (:community
"communitySequences")))
1882 :fields
'(:--id
:created-at
:user-id
:title
:----typename
)))))
1885 :title
"Sequences Library"
1886 :content-class
"sequences-page"
1887 :current-uri
"/library"
1889 (sublevel-nav-to-html '(:featured
:community
)
1893 :extra-class
"sequences-view")))))
1895 (define-page view-sequence
(:regex
"^/s(?:equences)?/([^/?#]+)(?:/?$|\\?)" sequence-id
) ()
1896 (let ((sequence (get-sequence sequence-id
)))
1897 (alist-bind ((title string
))
1899 (emit-page (out-stream
1901 :content-class
"sequence-page")
1902 (sequence-to-html sequence
)))))
1904 (define-page view-archive
(:regex
"^/archive(?:/(\\d{4})|/?(?:$|\\?.*$))(?:/(\\d{1,2})|/?(?:$|\\?.*$))(?:/(\\d{1,2})|/?(?:$|\\?.*$))"
1905 (year :type
(mod 10000))
1906 (month :type
(integer 1 12))
1907 (day :type
(integer 1 31)))
1908 ((offset :type fixnum
:default
0))
1909 (local-time:with-decoded-timestamp
(:day current-day
:month current-month
:year current-year
:timezone local-time
:+utc-zone
+) (local-time:now
)
1910 (local-time:with-decoded-timestamp
(:day earliest-day
:month earliest-month
:year earliest-year
:timezone local-time
:+utc-zone
+) (earliest-post-time)
1911 (labels ((url-elements (&rest url-elements
)
1912 (declare (dynamic-extent url-elements
))
1913 (format nil
"/~{~A~^/~}" url-elements
))
1915 (let ((out-stream *html-output
*))
1916 (with-outputs (out-stream) "<div class=\"archive-nav\"><div class=\"archive-nav-years\">")
1917 (link-if-not out-stream
(not (or year month day
)) (url-elements "archive") "archive-nav-item-year" "All")
1918 (loop for y from earliest-year to current-year
1919 do
(link-if-not out-stream
(eq y year
) (url-elements "archive" y
) "archive-nav-item-year" y
))
1920 (format out-stream
"</div>")
1922 (format out-stream
"<div class=\"archive-nav-months\">")
1923 (link-if-not out-stream
(not month
) (url-elements "archive" year
) "archive-nav-item-month" "All")
1924 (loop for m from
(if (= (or year current-year
) earliest-year
) earliest-month
1) to
(if (= (or year current-year
) current-year
) current-month
12)
1925 do
(link-if-not out-stream
(eq m month
) (url-elements "archive" (or year current-year
) m
) "archive-nav-item-month" (elt local-time
:+short-month-names
+ m
)))
1926 (format out-stream
"</div>"))
1928 (format out-stream
"<div class=\"archive-nav-days\">")
1929 (link-if-not out-stream
(not day
) (url-elements "archive" year month
) "archive-nav-item-day" "All")
1930 (loop for d from
(if (and (= (or year current-year
) earliest-year
) (= (or month current-month
) earliest-month
)) earliest-day
1)
1931 to
(if (and (= (or year current-year
) current-year
) (= (or month current-month
) current-month
)) current-day
(local-time:days-in-month
(or month current-month
) (or year current-year
)))
1932 do
(link-if-not out-stream
(eq d day
) (url-elements "archive" (or year current-year
) (or month current-month
) d
) "archive-nav-item-day" d
))
1933 (format out-stream
"</div>"))
1934 (format out-stream
"</div>"))))
1935 (multiple-value-bind (posts total
)
1936 (lw2-graphql-query (lw2-query-string :post
:list
1937 (alist :view
(if day
"new" "top") :limit
51 :offset offset
1938 :after
(if (and year
(not day
)) (format nil
"~A-~A-~A" (or year earliest-year
) (or month
1) (or day
1)))
1939 :before
(if year
(format nil
"~A-~A-~A" (or year current-year
) (or month
12)
1940 (or day
(local-time:days-in-month
(or month
12) (or year current-year
))))))))
1941 (emit-page (out-stream :title
"Archive" :current-uri
"/archive" :content-class
"archive-page"
1942 :top-nav
#'archive-nav
1943 :pagination
(pagination-nav-bars :items-per-page
50 :offset offset
:total total
:with-next
(if total nil
(> (length posts
) 50))))
1944 (write-index-items-to-html out-stream
(firstn posts
50) :empty-message
"No posts for the selected period.")))))))
1946 (define-page view-about
"/about" ()
1947 (emit-page (out-stream :title
"About" :current-uri
"/about" :content-class
"about-page")
1948 (alexandria:with-input-from-file
(in-stream "www/about.html" :element-type
'(unsigned-byte 8))
1949 (alexandria:copy-stream in-stream out-stream
))))
1951 (define-page view-generated-script
(:regex
"^/generated/([^/]+)\\.js" (name :type string
)) ()
1952 (when-let ((package (find-package (string-upcase name
))))
1953 (setf (hunchentoot:header-out
"Content-Type") "text/javascript")
1954 (let ((stream (make-flexi-stream (hunchentoot:send-headers
) :external-format
:utf-8
)))
1955 (write-package-client-scripts package stream
))))
1957 (hunchentoot:define-easy-handler
1961 (multiple-value-bind (match? strings
) (ppcre:scan-to-strings
"^/proxy-assets/([0-9A-Za-z]+)(-inverted)?$" (hunchentoot:script-name r
) :sharedp t
)
1962 (when-let* ((base-filename (and match?
(svref strings
0)))
1963 (image-data (cache-get "cached-images" base-filename
:value-type
:json
)))
1964 (let ((inverted (svref strings
1)))
1965 (alist-bind ((mime-type simple-string
)) image-data
1966 (setf (hunchentoot:header-out
"X-Content-Type-Options") "nosniff"
1967 (hunchentoot:header-out
"Cache-Control") #.
(format nil
"public, max-age=~A, immutable" 600))
1968 (hunchentoot:handle-static-file
(concatenate 'string
"www/proxy-assets/" base-filename
(if inverted
"-inverted" "")) mime-type
)