1 (uiop:define-package
#:lw2.backend
2 (:use
#:cl
#:sb-thread
#:flexi-streams
#:alexandria
#:iterate
#:lw2-viewer.config
#:lw2.sites
#:lw2.context
#:lw2.graphql
#:lw2.lmdb
3 #:lw2.utils
#:lw2.hash-utils
#:lw2.backend-modules
#:lw2.schema-type
#:lw2.conditions
#:lw2.web-push
)
4 (:import-from
#:collectors
#:with-collector
)
5 (:import-from
#:lw2.user-context
#:*current-auth-token
*)
6 (:reexport
#:lw2.backend-modules
)
7 (:export
#:*use-alignment-forum
*
8 #:*graphql-debug-output
*
9 #:*revalidate-default
* #:*force-revalidate-default
*
10 #:*messages-index-fields
*
11 #:*notifications-base-terms
*
12 #:start-background-loader
#:stop-background-loader
#:background-loader-running-p
13 #:call-with-http-response
14 #:forwarded-header
#:backend-request-headers
15 #:lw2-graphql-query
#:lw2-query-string
* #:lw2-query-string
16 #:lw2-graphql-query-map
#:lw2-graphql-query-multi
19 #:flatten-shortform-comments
#:get-shortform-votes
21 #:get-post-tag-votes
#:get-tag-post-votes
23 #:get-posts-index
#:get-posts-json
#:get-post-body
#:get-post-vote
#:get-post-comments
#:get-post-answers
24 #:get-post-comments-votes
25 #:get-tag-comments-votes
26 #:get-recent-comments
#:get-recent-comments-json
28 #:sequence-post-ids
#:get-sequence
#:get-post-sequence-ids
#:get-sequence-post
29 #:get-conversation-messages
32 #:get-notifications
#:check-notifications
33 #:mark-comment-replied
#:check-comment-replied
34 #:lw2-search-query
#:get-post-title
#:get-post-slug
#:get-slug-postid
#:get-username
#:get-user-full-name
#:get-user-slug
35 #:do-wl-rest-mutate
#:do-wl-rest-query
#:do-wl-create-tag
)
36 (:recycle
#:lw2-viewer
)
37 (:unintern
#:get-posts
#:make-posts-list-query
#:define-backend-fields
38 #:*posts-index-fields
* #:posts-index-fields
#:post-body-fields
39 #:*comments-index-fields
* #:comments-index-fields
40 #:*post-comments-fields
* #:post-comments-fields
41 #:define-index-fields
#:decode-graphql-json
42 #:lw2-graphql-query-noparse
#:lw2-graphql-query-streamparse
44 #:with-connection-pool
#:call-with-connection-pool
))
46 (in-package #:lw2.backend
)
48 ;; Dexador settings required for the system to work properly.
49 (setf dex
:*default-connect-timeout
* nil
50 dex
:*default-read-timeout
* nil
51 dex
:*use-connection-pool
* nil
)
53 (defvar *use-alignment-forum
* nil
)
55 (defvar *graphql-debug-output
* nil
)
57 (defvar *revalidate-default
* t
)
58 (defvar *force-revalidate-default
* nil
)
60 (defparameter *messages-index-fields
* '(:--id
:user-id
:created-at
(:contents
:html
) (:conversation
:--id
:title
) :----typename
))
61 (defparameter *user-fields
* '(:--id
:slug
:display-name
:karma
))
63 (defparameter *notifications-base-terms
* (alist :view
"userNotifications" :created-at
:null
:viewed
:null
))
65 (defun request-fields (query-type return-type context
)
66 "Returns the desired fields for a given type of request."
71 (let ((backend *current-backend
*)
72 (schema-type (find-schema-type query-type
))
73 (added (make-hash-table :test
'eq
)))
74 (dolist (field (cdr (assoc :fields schema-type
)) (col))
75 (destructuring-bind (field-name field-type
&key alias backend-type graphql-ignore subfields
((:context field-context
)) context-not
&allow-other-keys
) field
76 (declare (ignore field-type
))
77 (when (and (not (gethash field-name added
))
79 (or (not backend-type
) (typep backend backend-type
))
80 (or (not field-context
) (eq context field-context
))
81 (or (not context-not
) (not (eq context context-not
))))
82 (setf (gethash field-name added
) t
)
84 (let ((result-name (or alias field-name
)))
86 (list* result-name subfields
)
87 result-name
)))))))))))
89 (define-backend-function user-fields
()
90 (backend-lw2-legacy (load-time-value *user-fields
*))
91 (backend-lw2-modernized (append (call-next-method) '(:groups
:deleted
:html-bio
)))
92 (backend-alignment-forum (append (call-next-method) '(:af-karma
:full-name
))))
94 (define-cache-database 'backend-lw2-legacy
96 "post-comments-json" "post-comments-json-meta" "post-answers-json" "post-answers-json-meta"
97 "post-body-json" "post-body-json-meta"
98 "sequence-json" "sequence-json-meta" "post-sequence"
99 "user-json" "user-json-meta"
100 "user-page-items" "user-page-items-meta")
102 (define-cache-database 'backend-lw2-modernized
105 (define-backend-function comments-list-to-graphql-json
(comments-list)
107 (json:encode-json-to-string
108 (plist-hash-table (list :data
(plist-hash-table (list :*comments-list comments-list
))))))
109 (backend-lw2-modernized
110 (json:encode-json-to-string
111 (plist-hash-table (list :data
(plist-hash-table (list :*comments-list
(plist-hash-table (list :results comments-list
)))))))))
113 (defun do-graphql-debug (query)
114 (when *graphql-debug-output
*
115 (format *graphql-debug-output
* "~&GraphQL query: ~A~%" query
)))
117 (defmacro with-retrying
((maybe-retry-fn-name &key retries before-maybe-retry before-retry
) &body body
)
118 (with-gensyms (remaining-retries retry
)
119 `(let ((,remaining-retries
,retries
))
121 (labels ((,maybe-retry-fn-name
()
123 (when (> ,remaining-retries
0)
124 (decf ,remaining-retries
)
129 (defun force-close (stream)
130 (ignore-errors (close stream
:abort t
)))
132 (sb-ext:defglobal
*connection-pool
* (make-hash-table :test
'equal
))
133 (sb-ext:defglobal
*connection-pool-lock
* (sb-thread:make-mutex
:name
"*connection-pool-lock*"))
135 (defun connection-push (dest connection
)
136 (let ((connection-pool *connection-pool
*)
138 (sb-thread:with-mutex
(*connection-pool-lock
*)
139 (let ((vector (or (gethash dest connection-pool
)
140 (setf (gethash dest connection-pool
)
141 (make-array 4 :fill-pointer
0)))))
142 (unless (vector-push connection vector
)
143 (setf old-connection
(vector-pop vector
))
144 (vector-push connection vector
))))
146 (force-close old-connection
))))
148 (defun connection-pop (dest)
149 (let ((connection-pool *connection-pool
*))
150 (sb-thread:with-mutex
(*connection-pool-lock
*)
151 (when-let (vector (gethash dest connection-pool
))
152 (when (> (fill-pointer vector
) 0)
153 (vector-pop vector
))))))
155 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
156 (defstruct (token-bucket (:constructor %make-token-bucket
))
157 (tokens internal-time-units-per-second
:type fixnum
)
158 (base-cost internal-time-units-per-second
:type fixnum
)
159 (last-update (get-internal-real-time) :type fixnum
)
160 (tokens-per-unit 1 :type fixnum
)
161 (token-limit internal-time-units-per-second
:type fixnum
))
163 (defun make-token-bucket (&key rate burst
(fill-ratio 1.0))
164 (let* ((scaled-rate (rationalize (/ internal-time-units-per-second rate
)))
165 (base-cost (numerator scaled-rate
))
166 (tokens-per-unit (denominator scaled-rate
))
167 (token-limit (* base-cost burst
)))
170 :tokens-per-unit tokens-per-unit
171 :token-limit token-limit
172 :tokens
(round (* token-limit fill-ratio
))))))
174 (defun token-bucket-decrement (token-bucket n
&optional with-punishment
)
175 (let* ((time (get-internal-real-time))
176 (tpu (token-bucket-tokens-per-unit token-bucket
))
177 (limit (token-bucket-token-limit token-bucket
))
178 (cost (round (* n
(token-bucket-base-cost token-bucket
))))
179 (max-increment (+ limit cost
))
180 (max-timediff (ceiling max-increment tpu
))
183 (declare (type (and fixnum
(integer 0)) time
)
184 (type (unsigned-byte 32) limit cost max-increment increment
)
185 (type (integer 1 10000000) tpu
))
186 (sb-ext:atomic-update
(token-bucket-last-update token-bucket
)
188 (declare (type (and fixnum
(integer 0)) previous
))
189 (if (> (- time max-timediff
) previous
)
190 (setf increment max-increment
)
191 (let ((timediff (- time
(min previous time
))))
192 (declare (type (unsigned-byte 32) timediff
))
193 (setf increment
(* timediff tpu
))))
194 (max previous time
)))
195 (sb-ext:atomic-update
(token-bucket-tokens token-bucket
)
197 (declare (type (signed-byte 32) previous
))
198 (let* ((new-refill (+ previous increment
))
199 (new (- new-refill cost
)))
200 (setf result
(> new
0))
201 (if (or result with-punishment
)
203 (max (- limit
) (min new-refill limit
))))))
206 (defun parse-ipv4 (string)
207 (let ((l (map 'list
#'parse-integer
208 (split-sequence:split-sequence
#\. string
))))
211 do
(setf res
(+ n
(ash res
8)))
212 finally
(return res
))))
214 (defparameter *rate-limit-cost-factor
* 1)
216 (sb-ext:defglobal
*global-token-bucket
* (make-token-bucket :rate
3 :burst
180))
218 (defun check-rate-limit ()
219 (or (token-bucket-decrement *global-token-bucket
* *rate-limit-cost-factor
*)
220 (error "Rate limit exceeded. Try again later.")))
222 (defun call-with-http-response (fn uri-string
&rest args
&key
&allow-other-keys
)
224 (let* ((uri (quri:uri uri-string
))
225 (uri-dest (concatenate 'string
(quri:uri-host uri
) ":" (format nil
"~d" (quri:uri-port uri
))))
226 (stream (connection-pop uri-dest
)))
227 (let (response status-code headers response-uri new-stream success
)
228 (with-retrying (maybe-retry :retries
3
229 :before-retry
(sleep 0.2))
231 (handler-bind (((or dex
:http-request-failed usocket
:ns-condition usocket
:socket-condition
)
233 (if-let ((r (find-restart 'dex
:ignore-and-continue condition
))
234 (ct (gethash "content-type" (dex:response-headers condition
))))
235 (if (ppcre:scan
"^application/json" ct
)
238 (setf (values response status-code headers response-uri new-stream
)
239 (apply 'dex
:request uri
:use-connection-pool nil
:keep-alive t
:stream stream args
))
240 (unless (eq stream new-stream
)
241 (when stream
(force-close stream
))
242 (setf stream new-stream
244 (when (<= 500 status-code
599)
246 (error 'lw2-connection-error
:message
(format nil
"HTTP status ~A" status-code
)))
249 (when (streamp response
)
250 (force-close response
))
252 (force-close stream
))
255 (funcall fn response
)
256 (when (streamp response
)
258 (when stream
; the connection is reusable
259 (connection-push uri-dest stream
))))))
261 (defun forwarded-header ()
262 (let ((addr (and (boundp 'hunchentoot
:*request
*) (hunchentoot:real-remote-addr
))))
263 (list-cond (addr "X-Forwarded-For" addr
))))
265 (defun signal-lw2-errors (errors)
266 (loop for error in errors
267 do
(let ((message (cdr (assoc :message error
)))
268 (path (cdr (assoc :path error
))))
269 (unless (and path
(> (length path
) 1))
271 ((search "document_not_found" message
) (error 'lw2-not-found-error
))
272 ((search "app.missing_document" message
) (error 'lw2-not-found-error
))
273 ((search "only visible to logged-in users" message
) (error 'lw2-login-required-error
))
274 ((search "not_allowed" message
) (error 'lw2-not-allowed-error
))
275 (t (error 'lw2-unknown-error
:message message
)))))))
277 (define-backend-function earliest-post-time
()
278 (backend-lw2 (load-time-value (local-time:parse-timestring
"2005-01-01")))
279 (backend-ea-forum (load-time-value (local-time:parse-timestring
"2011-11-24"))))
281 (define-backend-function fixup-lw2-return-value
(value)
284 (backend-lw2-modernized
288 (if (member (car x
) '(:result
:results
:total-count
))
295 (define-backend-function deserialize-query-result
(result-source)
297 (let ((string-source (typecase result-source
301 (flexi-streams:make-in-memory-input-stream result-source
))
303 (ensure-character-stream result-source
)))))
304 (json:decode-json-from-source string-source
))))
306 (define-backend-function postprocess-query-result
(result)
310 (signal-lw2-errors (cdr (assoc :errors result
)))
311 (fixup-lw2-return-value (cdadr (assoc :data result
)))))
313 (define-backend-function decode-query-result
(result-source)
315 (postprocess-query-result
316 (deserialize-query-result result-source
))))
318 (defmethod graphql-uri ((backend backend-alignment-forum
))
319 (if *use-alignment-forum
*
320 "https://www.alignmentforum.org/graphql"
323 (define-backend-function backend-request-headers
(auth-token forwarded
)
324 (backend-websocket-login
325 (list-cond* (auth-token :authorization auth-token
)
327 (backend-passport-js-login
328 (list-cond* (auth-token :cookie
(concatenate 'string
"loginToken=" auth-token
))
331 (alist* :content-type
"application/json"
332 (if forwarded
(forwarded-header)))))
334 (define-backend-function call-with-backend-response
(fn query
&key return-type auth-token
)
336 (call-with-http-response
338 (graphql-uri *current-backend
*)
340 :headers
(backend-request-headers auth-token nil
)
341 :content
(dynamic-let ((q (alist :query query
))) (json:encode-json-to-string q
))
342 :want-stream
(not return-type
))))
344 (define-backend-function lw2-graphql-query
(query &key auth-token return-type
(decoder 'decode-query-result
))
346 (do-graphql-debug query
)
347 (call-with-backend-response
351 (:both
(lambda (string) (values (funcall decoder string
) string
))))
353 :return-type return-type
354 :auth-token auth-token
)))
356 (defun lw2-graphql-query-map (fn data
&key auth-token postprocess
)
357 (multiple-value-bind (map-values queries
)
359 as out-values
= (multiple-value-list (funcall fn d
))
360 as
(out passthrough-p
) = out-values
361 collect out-values into map-values
362 when
(not passthrough-p
) collect out into queries
363 finally
(return (values map-values queries
)))
365 (with-output-to-string (stream)
369 do
(format stream
"g~6,'0D:~A " n q
))
370 (format stream
"}")))
371 (query-result-data (when queries
(lw2-graphql-query query-string
:decoder
'deserialize-query-result
:auth-token auth-token
)))
372 (errors (cdr (assoc :errors query-result-data
))))
373 (signal-lw2-errors errors
)
375 (loop as results
= (sort (cdr (assoc :data query-result-data
)) #'string
< :key
#'car
) then
(if passthrough-p results
(rest results
))
376 for
(out passthrough-p
) in map-values
377 as result-data-cell
= (first results
)
378 as result-data
= (if passthrough-p out
(fixup-lw2-return-value (cdr result-data-cell
)))
379 for input-data in data
380 collect
(if postprocess
(funcall postprocess input-data result-data
) result-data
))
383 (defun lw2-graphql-query-multi (query-list &key auth-token
)
384 (values-list (lw2-graphql-query-map #'identity query-list
:auth-token auth-token
)))
386 (defvar *background-cache-update-threads
* (make-hash-table :test
'equal
390 (defun cache-update (cache-db key data
)
391 (let ((meta-db (format nil
"~A-meta" cache-db
))
392 (new-hash (hash-string data
))
393 (current-time (get-unix-time)))
394 (with-cache-transaction
395 (let* ((metadata (cache-get meta-db key
:value-type
:lisp
))
396 (same-data (equalp new-hash
(cdr (assoc :city-128-hash metadata
))))
397 (last-mod (if same-data
398 (or (cdr (assoc :last-modified metadata
)) current-time
)
400 (cache-put meta-db key
(alist :last-checked current-time
:last-modified last-mod
:city-128-hash new-hash
) :value-type
:lisp
)
402 (cache-put cache-db key data
))))))
404 (defun cache-mark-stale (cache-db key
)
405 (let ((meta-db (format nil
"~A-meta" cache-db
))
406 (current-time (get-unix-time)))
407 (with-cache-transaction
408 (let* ((metadata (cache-get meta-db key
:value-type
:lisp
))
409 (metadata (alist* :last-modified current-time
(delete :last-modified metadata
:key
#'car
))))
410 (cache-put meta-db key metadata
:value-type
:lisp
)))))
412 (declaim (type (and fixnum
(integer 1)) *cache-stale-factor
* *cache-skip-factor
*))
413 (defparameter *cache-stale-factor
* 100)
414 (defparameter *cache-skip-factor
* 5000)
416 (defun cache-is-fresh (cache-db key
)
417 (let ((metadata (cache-get (format nil
"~A-meta" cache-db
) key
:value-type
:lisp
))
418 (current-time (get-unix-time)))
419 (if-let ((last-mod (cdr (assoc :last-modified metadata
)))
420 (last-checked (cdr (assoc :last-checked metadata
))))
421 (let ((unmodified-time (- last-checked last-mod
))
422 (last-checked-time (- current-time last-checked
)))
423 (if (> unmodified-time
(* *cache-skip-factor
* last-checked-time
))
425 (> unmodified-time
(* *cache-stale-factor
* last-checked-time
)))))))
427 (defgeneric run-query
(query)
428 (:method
((query string
))
429 (lw2-graphql-query query
:return-type
:string
))
430 (:method
((query function
))
433 (declaim (inline make-thread-with-current-backend
))
435 (defun make-thread-with-current-backend (fn &rest args
)
436 (let ((current-backend *current-backend
*))
437 (apply #'sb-thread
:make-thread
439 (let ((*current-backend
* current-backend
))
443 (defun ensure-cache-update-thread (query cache-db cache-key
)
444 (let ((key (format nil
"~A-~A" cache-db cache-key
)))
445 (labels ((background-fn ()
447 (multiple-value-bind (value error
)
448 (log-and-ignore-errors
449 (sb-sys:with-deadline
(:seconds
60)
451 (cache-update cache-db cache-key
(run-query query
)))))
453 (remhash key
*background-cache-update-threads
*))))
454 (sb-ext:with-locked-hash-table
(*background-cache-update-threads
*)
455 (let ((thread (gethash key
*background-cache-update-threads
*)))
457 (setf (gethash key
*background-cache-update-threads
*)
458 (make-thread-with-current-backend #'background-fn
:name
"background cache update"))))))))
460 (define-backend-function lw2-graphql-query-timeout-cached
(query cache-db cache-key
&key
(decoder 'decode-query-result
)
461 (revalidate *revalidate-default
*) (force-revalidate *force-revalidate-default
*))
463 (multiple-value-bind (cached-result is-fresh
) (with-cache-readonly-transaction
464 (values (cache-exists cache-db cache-key
)
465 (cache-is-fresh cache-db cache-key
)))
466 (labels ((get-cached-result ()
467 (with-cache-readonly-transaction (funcall decoder
(cache-get cache-db cache-key
:return-type
'binary-stream
)))))
468 (if (and cached-result
(or (not revalidate
)
469 (and (not force-revalidate
) (eq is-fresh
:skip
))))
471 (let ((timeout (if cached-result
472 (if force-revalidate nil
3)
474 (thread (ensure-cache-update-thread query cache-db cache-key
)))
475 (block retrieve-result
476 (if (and cached-result
(if force-revalidate
(not revalidate
) (or is-fresh
(not revalidate
))))
479 ((fatal-error (lambda (c)
482 (return-from retrieve-result
(get-cached-result))))))
483 (let ((new-result (sb-thread:join-thread thread
:timeout timeout
)))
485 (condition (error new-result
))
486 (t (funcall decoder new-result
)))))))))))))
488 (define-backend-function lw2-query-string
* (query-type return-type args
&key context fields with-total
))
490 (define-backend-operation lw2-query-string
* backend-lw2-legacy
(query-type return-type args
&key context
(fields (request-fields query-type return-type context
)) with-total
)
491 (declare (ignore with-total
))
492 (labels ((lisp-to-lw2-case (x) (json:lisp-to-camel-case
(format nil
"*~A" x
))))
493 (graphql-query-string*
495 (lisp-to-lw2-case query-type
)
497 (lisp-to-lw2-case return-type
))
498 (if (eq return-type
:single
)
503 (define-backend-operation lw2-query-string
* backend-lw2-modernized
(query-type return-type args
&key context
(fields (request-fields query-type return-type context
)) with-total
)
504 (graphql-query-string*
505 (if (eq return-type
:single
)
506 (json:lisp-to-camel-case
(string query-type
))
507 (concatenate 'string
(json:lisp-to-camel-case
(string query-type
)) "s"))
508 (alist :input
(case return-type
509 (:single
(alist :selector args
))
510 (:list
(alist :enable-total with-total
:terms args
))
511 (:total
(alist :enable-total t
:terms args
))))
513 (:total
'(:total-count
))
514 (:list
(list-cond (t :results fields
)
515 (with-total :total-count
)))
516 (:single
(alist :result fields
)))))
518 (define-backend-function lw2-query-string
(query-type return-type args
&key context fields with-total
))
520 (define-backend-operation lw2-query-string backend-lw2-legacy
(query-type return-type args
&rest rest
&key context fields with-total
)
521 (declare (ignore context fields with-total
))
522 (format nil
"{~A}" (apply 'lw2-query-string
* query-type return-type args rest
)))
524 (define-backend-function lw2-query-list-limit-workaround
(query-type terms
&rest rest
&key fields context auth-token
)
526 (declare (ignore fields context
))
528 (loop for offset from
0 by
500
529 as items-next
= (lw2-graphql-query (apply 'lw2-query-string query-type
:list
(alist* :limit
500 :offset offset terms
) (filter-plist rest
:fields
:context
))
530 :auth-token auth-token
)
531 as length
= (length items-next
)
532 do
(setf items-list
(nconc items-list items-next
))
533 while
(>= length
500))
536 (declare (ignore fields context
))
537 (lw2-graphql-query (apply 'lw2-query-string query-type
:list terms
(filter-plist rest
:fields
:context
)) :auth-token auth-token
)))
539 (defun get-cached-index-query (cache-id query
)
540 (labels ((query-and-put ()
541 (let* ((result (lw2-graphql-query query
:return-type
:string
))
542 (decoded-result (multiple-value-list (decode-query-result result
))))
543 (cache-put "index-json" cache-id result
)
544 (values-list decoded-result
)))
545 (get-cached-result ()
546 (with-cache-readonly-transaction (decode-query-result (cache-get "index-json" cache-id
:return-type
'binary-stream
)))))
547 (let ((cached-result (cache-get "index-json" cache-id
:return-type
'existence
)))
548 (if (and cached-result
(background-loader-ready-p))
553 (t () (get-cached-result)))
556 (define-backend-function get-posts-index-query-terms
(&key view
(sort "new") (limit 21) offset before after karma-threshold
&allow-other-keys
)
558 (let ((sort-key (alexandria:switch
(sort :test
#'string
=)
561 ("active" "recentComments")
564 (multiple-value-bind (view-terms cache-key
)
565 (alexandria:switch
(view :test
#'string
=)
566 ("featured" (alist :sorted-by sort-key
:filter
"curated"))
567 ("all" (alist :sorted-by sort-key
:filter
"all"))
568 ("alignment-forum" (alist :sorted-by sort-key
:af t
))
569 ("questions" (alist :sorted-by sort-key
:filter
"questions"))
570 ("events" (alist :sorted-by sort-key
:filter
"events"))
571 ("nominations" (alist :view
"nominations2019"))
572 ("reviews" (alist :view
"reviews2019"))
574 (alist :sorted-by sort-key
:filter
"frontpage")
575 (if (not (or (string/= sort
"new") (/= limit
21) offset before after karma-threshold
)) "new-not-meta"))))
577 (alist-without-null* :before before
581 :karma-threshold karma-threshold
583 (values terms cache-key
))))))
585 (define-backend-operation get-posts-index-query-terms backend-lw2-tags
:around
(&key hide-tags
&allow-other-keys
)
586 (multiple-value-bind (query-terms cache-key
) (call-next-method)
588 (values (acons :filter-settings
(alist :tags
(list* :list
(map 'list
(lambda (tagid) (alist :tag-id tagid
:filter-mode
"Hidden"))
592 (values query-terms cache-key
))))
594 (define-backend-function get-posts-index-query-string
(&rest args
&key
&allow-other-keys
)
596 (declare (dynamic-extent args
))
597 (multiple-value-bind (query-terms cache-key
)
598 (apply #'%get-posts-index-query-terms backend args
)
599 (values (lw2-query-string :post
:list query-terms
)
602 (define-backend-function get-posts-index
(&rest args
&key
&allow-other-keys
)
604 (declare (dynamic-extent args
))
605 (multiple-value-bind (query-string cache-key
)
606 (apply #'%get-posts-index-query-string backend args
)
608 (get-cached-index-query cache-key query-string
)
609 (lw2-graphql-query query-string
)))))
611 (define-backend-operation get-posts-index backend-lw2-tags
:around
(&rest args
&key hide-tags offset
(limit 21) &allow-other-keys
)
612 ;; Workaround for https://github.com/LessWrong2/Lesswrong2/issues/3099
613 (declare (dynamic-extent args
))
615 (let ((offset (or offset
0)))
617 (apply #'call-next-method backend
:offset
0 :limit
(+ limit offset
) args
)
621 (defun get-posts-json ()
622 (lw2-graphql-query (get-posts-index-query-string) :return-type
:string
))
624 (defun get-recent-comments (&key with-total
)
625 (get-cached-index-query "recent-comments" (lw2-query-string :comment
:list
'((:view .
"allRecentComments") (:limit .
20)) :context
:index
:with-total with-total
)))
627 (defun get-recent-comments-json ()
628 (lw2-graphql-query (lw2-query-string :comment
:list
'((:view .
"allRecentComments") (:limit .
20)) :context
:index
) :return-type
:string
))
630 (defun process-vote-result (res)
631 (alist-bind ((id (or null simple-string
) :--id
)
634 current-user-extended-vote
)
636 (let ((karma-vote (or (nonempty-string current-user-vote
)
637 (cdr (assoc :vote-type
(first current-user-votes
))))))
638 (values (list-cond* (karma-vote :karma karma-vote
)
639 current-user-extended-vote
)
642 (defun process-votes-result (res)
643 (let ((hash (make-hash-table)))
645 (multiple-value-bind (vote id
) (process-vote-result v
)
647 (setf (gethash id hash
) vote
))))))
649 (defun flatten-shortform-comments (comments)
650 (let ((output comments
))
651 (loop for comment in comments do
652 (setf output
(append (cdr (assoc :latest-children comment
)) output
)))
655 (defun get-shortform-votes (auth-token &key
(offset 0) (limit 20))
656 (process-votes-result
657 (flatten-shortform-comments
658 (lw2-graphql-query (lw2-query-string :comment
:list
(alist :view
"shortform" :offset offset
:limit limit
)
659 :fields
'(:--id
:current-user-vote
:current-user-extended-vote
(:latest-children
:--id
:current-user-vote
:current-user-extended-vote
)))
660 :auth-token auth-token
))))
662 (defun get-post-vote (post-id auth-token
)
663 (process-vote-result (lw2-graphql-query (lw2-query-string :post
:single
(alist :document-id post-id
) :fields
'(:--id
:current-user-vote
:current-user-extended-vote
)) :auth-token auth-token
)))
665 (define-cache-database 'backend-lw2-tags
"tag-posts" "tag-posts-meta" "post-tags" "post-tags-meta")
667 (define-backend-function get-tag-posts
(slug &key
(revalidate *revalidate-default
*) (force-revalidate *force-revalidate-default
*))
668 (backend-base (declare (ignore revalidate force-revalidate
)) nil
)
670 (let* ((tagid (get-slug-tagid slug
))
672 (comments-list-to-graphql-json
673 (lw2-query-list-limit-workaround
675 (alist :view
"postsWithTag" :tag-id tagid
)
676 :fields
(list (list* :post
(request-fields :post
:list
:index
))))))))
678 (for x in
(lw2-graphql-query-timeout-cached query-fn
"tag-posts" tagid
:revalidate revalidate
:force-revalidate force-revalidate
))
679 (when-let (post (cdr (assoc :post x
)))
682 (define-backend-function get-tag-post-votes
(tag-id auth-token
)
683 (backend-base (progn tag-id auth-token nil
))
685 (process-votes-result
687 (lw2-query-list-limit-workaround
689 (alist :view
"postsWithTag" :tag-id tag-id
)
690 :fields
'(:--id
:current-user-vote
:current-user-extended-vote
)
691 :auth-token auth-token
)))))
693 (define-backend-function get-post-tags
(post-id &key
(revalidate *revalidate-default
*) (force-revalidate *force-revalidate-default
*))
694 (backend-base (declare (ignore revalidate force-revalidate
)) nil
)
696 (let ((query-string (lw2-query-string :tag-rel
:list
(alist :view
"tagsOnPost" :post-id post-id
) :fields
'((:tag
:name
:slug
)))))
697 (lw2-graphql-query-timeout-cached query-string
"post-tags" post-id
:revalidate revalidate
:force-revalidate force-revalidate
))))
699 (define-backend-function get-post-tag-votes
(post-id auth-token
)
700 (backend-base (progn post-id auth-token nil
))
702 (process-votes-result
703 (lw2-graphql-query (lw2-query-string :tag-rel
:list
(alist :view
"tagsOnPost" :post-id post-id
) :fields
'(:--id
:current-user-vote
:current-user-extended-vote
))
704 :auth-token auth-token
))))
706 (define-backend-function get-post-body
(post-id &key
(revalidate *revalidate-default
*) (force-revalidate *force-revalidate-default
*) auth-token
)
708 (let ((query-string (lw2-query-string :post
:single
(alist :document-id post-id
) :context
:body
)))
711 (handler-bind ((lw2-login-required-error (lambda (&rest args
)
712 (declare (ignore args
))
713 (let ((current-auth-token *current-auth-token
*))
714 (when (and (not auth-token
) current-auth-token
)
715 (setf auth-token current-auth-token
)
719 (lw2-graphql-query query-string
:auth-token auth-token
)
720 (lw2-graphql-query-timeout-cached query-string
"post-body-json" post-id
:revalidate revalidate
:force-revalidate force-revalidate
))))))))
722 (declare (ignore auth-token
))
723 (acons :tags
(get-post-tags post-id
:revalidate revalidate
:force-revalidate force-revalidate
) (call-next-method)))
724 (backend-magnum-crossposts
725 (declare (ignore auth-token
))
726 (let ((post (call-next-method)))
727 (alist-bind (is-crosspost hosted-here foreign-post-id
) (cdr (assoc :fm-crosspost post
))
728 (cond ((not (and (backend-magnum-crosspost-site backend
)
729 (not (boundp '*retrieve-crosspost
*))
730 is-crosspost foreign-post-id
))
733 (let* ((*current-site
* (find-site (backend-magnum-crosspost-site backend
)))
734 (*current-backend
* (site-backend *current-site
*))
735 (*retrieve-crosspost
* nil
)
736 (foreign-post (get-post-body foreign-post-id
:revalidate revalidate
:force-revalidate force-revalidate
))
737 (foreign-post-body (cdr (assoc :html-body foreign-post
))))
738 (declare (special *retrieve-crosspost
*))
739 (if foreign-post-body
741 ((not hosted-here
) :html-body foreign-post-body
)
742 (t :foreign-post foreign-post
)
746 (defun get-post-comments-list (post-id view
&rest rest
&key auth-token parent-answer-id fields context
)
747 (declare (ignore fields context auth-token
))
748 (let ((terms (alist :view view
:post-id post-id
)))
749 (when parent-answer-id
750 (setf terms
(acons :parent-answer-id parent-answer-id terms
)))
751 (apply 'lw2-query-list-limit-workaround
:comment terms
(filter-plist rest
:fields
:context
:auth-token
))))
753 (defun get-post-answer-replies (post-id answers
&rest rest
&key auth-token fields context
)
754 ;; todo: support more than 500 answers per question
755 (declare (ignore fields context
))
756 (let* ((terms (alist :view
"repliesToAnswer" :post-id post-id
:limit
500))
757 (result (lw2-graphql-query-map
759 (mapcar (lambda (answer) (apply 'lw2-query-string
* :comment
:list
760 (acons :parent-answer-id
(cdr (assoc :--id answer
)) terms
)
761 (filter-plist rest
:fields
:context
)))
763 :auth-token auth-token
)))
764 (apply #'nconc result
)))
766 (define-backend-function get-post-comments-votes
(post-id auth-token
)
768 (let ((fields '(:--id
:current-user-vote
:current-user-extended-vote
)))
769 (process-votes-result
770 (get-post-comments-list post-id
"postCommentsTop" :auth-token auth-token
:fields fields
))))
772 (let* ((fields '(:--id
:current-user-vote
:current-user-extended-vote
))
773 (answers (get-post-comments-list post-id
"questionAnswers" :auth-token auth-token
:fields fields
)))
774 (process-votes-result
776 (get-post-comments-list post-id
"postCommentsTop" :auth-token auth-token
:fields fields
)
777 (get-post-answer-replies post-id answers
:auth-token auth-token
:fields fields
)
780 (define-backend-function get-tag-comments-votes
(tag-id auth-token
)
781 (backend-lw2-tags-comments
782 (process-votes-result (lw2-graphql-query (lw2-query-string :comment
:list
(alist :tag-id tag-id
:view
"tagDiscussionComments") :fields
'(:--id
:current-user-vote
:current-user-extended-vote
))
783 :auth-token auth-token
))))
785 (define-backend-function get-post-comments
(post-id &key
(revalidate *revalidate-default
*) (force-revalidate *force-revalidate-default
*))
788 (comments-list-to-graphql-json
789 (get-post-comments-list post-id
"postCommentsTop")))))
790 (lw2-graphql-query-timeout-cached fn
"post-comments-json" post-id
:revalidate revalidate
:force-revalidate force-revalidate
)))
792 ;; Work around bizarre parent comment bug in EA forum
793 (declare (ignore revalidate force-revalidate
))
794 (let ((comments (call-next-method)))
796 (if-let (parent-id-cons (assoc :parent-comment-id c
))
797 (if (and (string= (cdr parent-id-cons
) "rjgZaK8uzHG3jAu2p")
798 (not (string= post-id
"h26Kx7uGfQfNewi7d")))
799 (setf (cdr parent-id-cons
) nil
))))
802 (defun get-post-answers (post-id &key
(revalidate *revalidate-default
*) (force-revalidate *force-revalidate-default
*))
804 (let ((answers (get-post-comments-list post-id
"questionAnswers")))
805 (comments-list-to-graphql-json
808 (get-post-answer-replies post-id answers
)))))))
809 (lw2-graphql-query-timeout-cached fn
"post-answers-json" post-id
:revalidate revalidate
:force-revalidate force-revalidate
)))
811 (define-backend-function get-collection
(collection-id)
814 (lw2-query-string :collection
:single
815 (alist :document-id collection-id
)
816 :fields
`(:--id
:title
(:contents
:html
) :grid-image-id
:----typename
817 (:books
:title
:subtitle
(:contents
:html
) :----typename
818 (:sequences
:title
(:contents
:html
) :grid-image-id
:----typename
819 (:chapters
:title
:subtitle
:number
(:contents
:html
) (:posts
,@(request-fields :post
:list nil
))))))))))
821 (defun sequence-iterate (sequence fn
)
822 (dolist (chapter (cdr (assoc :chapters sequence
)))
823 (dolist (post (cdr (assoc :posts chapter
)))
826 (defun sequence-post-ids (sequence)
827 (with-collector (col)
828 (sequence-iterate sequence
830 (col (cdr (assoc :--id post
)))))
833 (defun get-sequence-post (sequence post-id
)
834 (sequence-iterate sequence
836 (when (string= (cdr (assoc :--id post
)) post-id
)
837 (return-from get-sequence-post post
))))
840 (define-backend-function get-sequence
(sequence-id)
843 (multiple-value-bind (sequence sequence-json
)
845 (lw2-query-string :sequence
:single
846 (alist :document-id sequence-id
)
847 :fields
`(:--id
:title
:created-at
:user-id
849 (:chapters
:title
:subtitle
:number
(:contents
:html
) (:posts
,@(request-fields :post
:list nil
)))
850 :grid-image-id
:----typename
))
852 (let ((posts (sequence-post-ids sequence
)))
853 (with-cache-transaction
854 (dolist (post-id posts
)
855 (let ((old-seqs (cache-get "post-sequence" post-id
:value-type
:json
)))
856 (unless (member sequence-id old-seqs
:test
#'string
=)
857 (cache-put "post-sequence" post-id
(cons sequence-id old-seqs
) :value-type
:json
)))))
859 (lw2-graphql-query-timeout-cached fn
"sequence-json" sequence-id
))))
861 (define-backend-function get-post-sequence-ids
(post-id)
863 (cache-get "post-sequence" post-id
:value-type
:json
)))
865 (defun preload-sequences-cache ()
866 (declare (optimize space
(compilation-speed 2) (speed 0)))
867 (let ((sequences (apply #'append
868 (loop for view in
'("curatedSequences" "communitySequences")
869 collect
(lw2-graphql-query (lw2-query-string :sequence
:list
(alist :view view
) :fields
'(:--id
)))))))
870 (dolist (sequence sequences
)
871 (get-sequence (cdr (assoc :--id sequence
))))
872 (format t
"Retrieved ~A sequences." (length sequences
)))
875 (define-backend-function user-deleted
(user-id &optional
(status nil set
))
877 (declare (ignore user-id status set
))
879 (backend-lw2-modernized
882 (cache-put "user-deleted" user-id
"1")
883 (cache-del "user-deleted" user-id
))
884 (cache-get "user-deleted" user-id
:return-type
'existence
))))
886 (define-backend-function get-user
(user-identifier-type user-identifier
&key
(revalidate *revalidate-default
*) (force-revalidate *force-revalidate-default
*) auth-token
)
888 (let* ((user-id (ccase user-identifier-type
889 (:user-id user-identifier
)
890 (:user-slug
(get-slug-userid user-identifier
))))
891 (query-string (lw2-query-string :user
:single
(alist :document-id user-id
) :fields
(list-cond* (auth-token :last-notifications-check
) (user-fields))))
892 (result (if auth-token
893 (lw2-graphql-query query-string
:auth-token auth-token
)
894 (lw2-graphql-query-timeout-cached query-string
"user-json" user-id
:revalidate revalidate
:force-revalidate force-revalidate
))))
895 (alist-bind ((user-id (or simple-string null
) :--id
)
896 (display-name (or simple-string null
))
897 (full-name (or simple-string null
))
898 (slug (or simple-string null
))
902 (with-cache-transaction
904 (cache-username user-id display-name
))
906 (cache-user-full-name user-id full-name
))
908 (cache-user-slug user-id slug
)
909 (cache-slug-userid slug user-id
))
910 (user-deleted user-id deleted
))))
913 (define-backend-function get-notifications
(&key user-id
(offset 0) (limit 21) auth-token
)
915 (lw2-graphql-query (lw2-query-string :notification
:list
916 (alist* :user-id user-id
:limit limit
:offset offset
*notifications-base-terms
*)
917 :fields
'(:--id
:document-type
:document-id
:link
:title
:message
:type
:viewed
))
918 :auth-token auth-token
))
919 (backend-lw2-modernized
920 (declare (ignore user-id offset limit auth-token
))
921 (let ((*notifications-base-terms
* (remove :null
*notifications-base-terms
* :key
#'cdr
)))
922 (call-next-method))))
924 (define-backend-function check-notifications
(user-id auth-token
&key full since
)
926 (multiple-value-bind (notifications user-info
)
927 (lw2-graphql-query-multi (list
928 (lw2-query-string* :notification
:list
(alist* :user-id user-id
:limit
(if full
3 1) *notifications-base-terms
*)
929 :fields
(if full
'(:--id
:message
:created-at
) '(:created-at
)))
930 (lw2-query-string* :user
:single
(alist :document-id user-id
) :fields
'(:last-notifications-check
)))
931 :auth-token auth-token
)
932 (let ((last-check (or since
933 (let ((last-check-string (cdr (assoc :last-notifications-check user-info
))))
934 (when (and (stringp last-check-string
) (not (equal last-check-string
"")))
935 (local-time:parse-timestring last-check-string
))))))
937 (labels ((unread-p (notification)
939 (local-time:timestamp
>
940 (local-time:parse-timestring
(cdr (assoc :created-at notification
)))
942 ;; User has never checked notifications before -- all are unread
945 (remove-if-not #'unread-p notifications
)
946 (unread-p (first notifications
))))))))
947 (backend-lw2-modernized
948 (declare (ignore user-id auth-token full since
))
949 (let ((*notifications-base-terms
* (remove :null
*notifications-base-terms
* :key
#'cdr
)))
950 (call-next-method))))
952 (define-cache-database 'backend-lw2-legacy
"comment-reply-by-user")
954 (define-backend-function mark-comment-replied
(reply)
956 (alexandria:when-let
* ((parent-comment-id (cdr (assoc :parent-comment-id reply
)))
957 (reply-id (cdr (assoc :--id reply
)))
958 (user-id (cdr (assoc :user-id reply
))))
959 (cache-put "comment-reply-by-user" (concatenate 'string parent-comment-id
" " user-id
) reply-id
))))
961 (define-backend-function check-comment-replied
(comment-id user-id
)
963 (cache-get "comment-reply-by-user" (concatenate 'string comment-id
" " user-id
))))
965 (define-backend-function get-user-page-items
(user-id request-type
&key
(offset 0) (limit 21) (sort-type :date
) drafts
966 (revalidate *revalidate-default
*) (force-revalidate *force-revalidate-default
*) auth-token
)
968 (multiple-value-bind (real-offset real-limit
) (if (eq request-type
:both
)
969 (values 0 (+ offset limit
))
970 (values offset limit
))
971 (let* ((cache-database (when (and (eq request-type
:both
)
972 (or (not offset
) (= offset
0))
978 (return-type (if cache-database
:string nil
))
980 (labels ((posts-query-string ()
983 (drafts (alist :view
"drafts"))
984 ((eq sort-type
:score
) (alist :view
"top"))
985 ((eq sort-type
:date-reverse
) (alist :view
"old"))
986 (t (alist :view
"userPosts"))))
987 (terms (alist* :offset real-offset
:limit real-limit
:user-id user-id base-terms
)))
988 (declare (dynamic-extent base-terms terms
))
989 (lw2-query-string* :post
:list terms
)))
990 (comments-query-string ()
991 (let* ((view (ecase sort-type
992 (:score
"postCommentsTop")
993 (:date
"allRecentComments")
994 (:date-reverse
"postCommentsOld")))
995 (terms (alist :offset real-offset
999 (declare (dynamic-extent view terms
))
1000 (lw2-query-string* :comment
:list terms
1002 (declare (dynamic-extent #'posts-query-string
#'comments-query-string
))
1004 (:both
(let ((result (multiple-value-call #'concatenate
'list
1005 (lw2-graphql-query-multi (list (posts-query-string) (comments-query-string))))))
1007 (:string
(json:encode-json-to-string result
))
1009 (:posts
(lw2-graphql-query (format nil
"{~A}" (posts-query-string)) :auth-token auth-token
:return-type return-type
))
1010 (:comments
(lw2-graphql-query (format nil
"{~A}" (comments-query-string)) :auth-token auth-token
:return-type return-type
)))))))
1012 (lw2-graphql-query-timeout-cached fn cache-database user-id
:decoder
'deserialize-query-result
:revalidate revalidate
:force-revalidate force-revalidate
)
1015 (define-backend-function get-conversation-messages
(conversation-id auth-token
)
1017 (lw2-graphql-query-multi
1019 (lw2-query-string* :conversation
:single
(alist :document-id conversation-id
) :fields
'(:title
(:participants
:display-name
:slug
)))
1020 (lw2-query-string* :message
:list
(alist :view
"messagesConversation" :conversation-id conversation-id
) :fields
*messages-index-fields
*))
1021 :auth-token auth-token
)))
1023 (define-backend-function lw2-search-query
(query &key
(indexes '("test_tags" "test_posts" "test_comments")))
1024 (backend-algolia-search
1025 (call-with-http-response
1026 (lambda (req-stream)
1027 (values-list (loop for r in
(cdr (assoc :results
(json:decode-json req-stream
)))
1028 collect
(cdr (assoc :hits r
)))))
1029 (algolia-search-uri *current-backend
*)
1031 :headers
'(("Origin" .
"https://www.greaterwrong.com")
1032 ("Referer" .
"https://www.greaterwrong.com/")
1033 ("Content-Type" .
"application/json"))
1034 :content
(json:encode-json-alist-to-string
1035 (alist "requests" (loop for index in indexes
1036 collect
(alist "indexName" index
1037 "params" (format nil
"query=~A&hitsPerPage=20&page=0"
1038 (url-rewrite:url-encode query
))))))
1041 (define-backend-function get-username-wrapper
(user-id fn
)
1043 (funcall fn user-id
))
1044 (backend-lw2-modernized
1045 (if (user-deleted user-id
)
1047 (funcall fn user-id
))))
1049 (define-cache-database 'backend-lw2-legacy
"comment-markdown-source" "post-markdown-source")
1051 (defun markdown-source-db-name (target-type)
1052 (ecase target-type
(:comment
"comment-markdown-source") (:post
"post-markdown-source")))
1054 (define-backend-function markdown-source
(target-type id version
)
1055 (backend-lw2-modernized
1056 (let ((db-name (markdown-source-db-name target-type
))
1057 (version (base64:usb8-array-to-base64-string
(hash-string version
))))
1059 (if-let ((cache-data (cache-get db-name id
:value-type
:lisp
)))
1060 (alist-bind ((cached-version simple-string
:version
)
1061 (markdown simple-string
))
1063 (when (string= version cached-version
)
1065 (trivia:ematch
(lw2-graphql-query (lw2-query-string target-type
:single
1066 (alist :document-id id
)
1067 :fields
'(:html-body
(:contents
:markdown
)))
1068 :auth-token
*current-auth-token
*)
1069 ((trivia:alist
(:html-body . html-body
)
1070 (:contents .
(assoc :markdown markdown
)))
1071 (cache-put db-name id
(alist :version
(base64:usb8-array-to-base64-string
(hash-string html-body
)) :markdown markdown
) :value-type
:lisp
)
1074 (define-backend-function (setf markdown-source
) (markdown target-type id version
)
1075 (backend-lw2-modernized
1076 (let ((version (base64:usb8-array-to-base64-string
(hash-string version
))))
1077 (cache-put (markdown-source-db-name target-type
)
1079 (alist :version version
:markdown markdown
)
1080 :value-type
:lisp
))))
1082 (defun get-elicit-question-title (question-id)
1084 (lw2-graphql-query (graphql-query-string "ElicitBlockData" (alist :question-id question-id
) '(:title
)))))
1086 (defun make-rate-limiter (delay)
1087 (let ((rl-hash (make-hash-table :test
'equal
:synchronized t
)))
1089 (let ((unix-time (get-unix-time)))
1090 (if (sb-ext:with-locked-hash-table
(rl-hash)
1091 (maphash (lambda (k v
)
1092 (if (> (- unix-time v
) delay
)
1093 (remhash k rl-hash
)))
1095 (not (gethash datum rl-hash
)))
1097 (setf (gethash datum rl-hash
) unix-time
)
1099 (error "Request aborted due to rate limit."))))))
1101 (defmacro with-rate-limit
(&body outer-body
)
1102 `(let ((rate-limiter (make-rate-limiter 30)))
1103 (macrolet ((rate-limit ((key) &body inner-body
)
1104 `(funcall rate-limiter
,key
(lambda () ,@inner-body
))))
1108 (simple-cacheable ("post-title" 'backend-lw2-legacy
"postid-to-title" post-id
)
1109 (rate-limit (post-id) (cdr (first (lw2-graphql-query (lw2-query-string :post
:single
(alist :document-id post-id
) :fields
'(:title
))))))))
1112 (simple-cacheable ("post-slug" 'backend-lw2-legacy
"postid-to-slug" post-id
)
1113 (rate-limit (post-id) (cdr (first (lw2-graphql-query (lw2-query-string :post
:single
(alist :document-id post-id
) :fields
'(:slug
))))))))
1116 (simple-cacheable ("slug-postid" 'backend-lw2-legacy
"slug-to-postid" slug
)
1117 (rate-limit (slug) (cdr (first (lw2-graphql-query (lw2-query-string :post
:single
(alist :slug slug
) :fields
'(:--id
))))))))
1120 (simple-cacheable ("username" 'backend-lw2-legacy
"userid-to-displayname" user-id
:get-wrapper
#'get-username-wrapper
)
1121 (rate-limit (user-id) (cdr (first (lw2-graphql-query (lw2-query-string :user
:single
(alist :document-id user-id
) :fields
'(:display-name
))))))))
1124 (simple-cacheable ("user-slug" 'backend-lw2-legacy
"userid-to-slug" user-id
)
1125 (rate-limit (user-id) (cdr (first (lw2-graphql-query (lw2-query-string :user
:single
(alist :document-id user-id
) :fields
'(:slug
))))))))
1128 (simple-cacheable ("user-full-name" 'backend-lw2-legacy
"userid-to-full-name" user-id
)
1129 (rate-limit (user-id) (or (cdr (first (lw2-graphql-query (lw2-query-string :user
:single
(alist :document-id user-id
) :fields
'(:full-name
)))))
1133 (simple-cacheable ("slug-userid" 'backend-lw2-legacy
"slug-to-userid" slug
)
1134 (rate-limit (slug) (cdr (first (lw2-graphql-query (lw2-query-string :user
:single
(alist :slug slug
) :fields
'(:--id
))))))))
1137 (simple-cacheable ("slug-tagid" 'backend-lw2-tags
"slug-to-tagid" slug
:catch-errors nil
)
1138 (rate-limit (slug) (cdr (first (first (lw2-graphql-query (lw2-query-string :tag
:list
(alist :view
"tagBySlug" :slug slug
) :fields
'(:--id
)))))))))
1140 (defun preload-username-cache ()
1141 (declare (optimize space
(compilation-speed 2) (speed 0)))
1142 (let ((user-list (lw2-graphql-query (lw2-query-string :user
:list
'() :fields
'(:--id
:slug
:display-name
)))))
1143 (with-cache-transaction
1144 (loop for user in user-list
1145 do
(alist-bind ((user-id (or simple-string null
) :--id
)
1146 (slug (or simple-string null
))
1147 (display-name (or simple-string null
)))
1151 (cache-username user-id display-name
))
1153 (cache-user-slug user-id slug
)
1154 (cache-slug-userid slug user-id
))))))))