Implement support for fetching all pages

Still have to implement support for incrementally fetching pages by
hand, though.

Signed-off-by: Sergio Durigan Junior <sergiodj@sergiodj.net>
This commit is contained in:
Sergio Durigan Junior 2024-04-12 22:17:16 -04:00
parent dac665ceb1
commit 245d2b702e
Signed by: sergiodj
GPG key ID: D0EB762865FC5E36

View file

@ -55,9 +55,14 @@ posts manually and incrementally."
(add-text-properties p (point) (add-text-properties p (point)
'(pre-tag t face discoursel-code-face)))) '(pre-tag t face discoursel-code-face))))
(defun discoursel--get (endpoint) (defun discoursel--get (endpoint &optional args)
(condition-case plzerror (condition-case plzerror
(plz 'get (concat discoursel-instance "/" endpoint ".json") (plz 'get (concat discoursel-instance "/" endpoint ".json"
(when args
(concat "?"
(mapconcat (lambda (elt)
(concat (car elt) "=" (cdr elt)))
args "&"))))
:as #'json-read) :as #'json-read)
(plz-error (plz-error
(error "Could not query endpoint '%s': '%s'" (error "Could not query endpoint '%s': '%s'"
@ -192,41 +197,15 @@ characters."
'post-id post-id) 'post-id post-id)
(buffer-string)))))) (buffer-string))))))
(defun discoursel--open-topic (&optional id) (defun discoursel-render-posts-from-topic (topic &optional start-idx)
(interactive) (let* ((posts (alist-get 'posts (alist-get 'post_stream topic)))
(when (and (not (eq major-mode 'discoursel-mode)) (topic-slug (alist-get 'slug topic))
(not id)) (buf (discoursel-get-create-buffer topic-slug))
(user-error "You must be in a discoursel buffer")) (startidx (or start-idx 0)))
(let* ((topic-id (number-to-string
(or id (get-text-property (point) 'discoursel-topic-id))))
(r (discoursel--get (concat "t/" topic-id)))
(posts (alist-get 'posts (alist-get 'post_stream r)))
(first-post (aref posts 0))
(topic-slug (alist-get 'slug r))
(topic-title (alist-get 'title r))
(topic-date (alist-get 'created_at r))
(topic-date-string
(format-time-string "%c"
(encode-time
(parse-time-string topic-date))))
(userid (alist-get 'user_id r))
(participants (alist-get 'participants (alist-get 'details r)))
(topic-username (alist-get 'username first-post))
(topic-author-alist (seq-find (lambda (p)
(string= (alist-get 'username p) topic-username))
participants))
; (topic-author (alist-get 'username topic-author-alist))
(topic-flairname (alist-get 'name topic-author-alist))
(topic-author (concat (alist-get 'display_username first-post) " ("(alist-get 'username first-post) ")"))
(buf (discoursel-get-create-buffer topic-slug)))
(switch-to-buffer buf)
(with-current-buffer buf (with-current-buffer buf
(discoursel-topic-mode)
(save-excursion (save-excursion
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
(erase-buffer) (cl-loop for idx from startidx to (1- (length posts)) do
(discoursel--insert-post first-post topic-title)
(cl-loop for idx from 1 to (1- (length posts)) do
(goto-char (point-max)) (goto-char (point-max))
(insert (insert
"\n" "\n"
@ -235,23 +214,42 @@ characters."
(shr-render-region (point-min) (point-max)) (shr-render-region (point-min) (point-max))
(buffer-string))) (buffer-string)))
(discoursel--insert-post (aref posts idx)))))))) (discoursel--insert-post (aref posts idx))))))))
;; (save-excursion
;; (insert (defun discoursel--fetch-all-posts-from-topic (topic-id number-of-pages)
;; (with-temp-buffer (cl-loop for page from 2 to number-of-pages do
;; (insert (format (propertize "Title: %s\n" 'face 'message-header-name) (with-delayed-message (2 (format "Rendering page %d out of %d..." page number-of-pages))
;; (propertize topic-title 'face 'message-header-subject))) (let* ((topic (discoursel--get (concat "t/" topic-id)
;; (insert (format (propertize "Author: %s\n" 'face 'message-header-name) `(("page" . ,(number-to-string page)))))
;; (propertize topic-author 'face 'message-header-to))) (posts (alist-get 'posts (alist-get 'post_stream topic))))
;; (insert (format (propertize "Date: %s\n" 'face 'message-header-name) (discoursel-render-posts-from-topic topic)))))
;; (propertize topic-date-string 'face 'message-header-other)))
;; (insert "\n") (defun discoursel--open-topic (&optional id)
;; (save-excursion (interactive)
;; (insert (alist-get 'cooked first-post))) (when (and (not (eq major-mode 'discoursel-mode))
;; (let ((shr-use-fonts nil) (not id))
;; (shr-external-rendering-functions (user-error "You must be in a discoursel buffer"))
;; '((pre . discoursel--parse-pre)))) (let* ((topic-id (number-to-string
;; (shr-render-region (point) (point-max))) (or id (get-text-property (point) 'discoursel-topic-id))))
;; (buffer-string)))))))) (topic (discoursel--get (concat "t/" topic-id)))
(posts (alist-get 'posts (alist-get 'post_stream topic)))
(first-post (aref posts 0))
(topic-slug (alist-get 'slug topic))
(topic-title (alist-get 'title topic))
(buf (discoursel-get-create-buffer topic-slug))
(number-of-pages (ceiling (/ (float (alist-get 'posts_count topic))
(float (alist-get 'chunk_size topic))))))
(switch-to-buffer buf)
(with-current-buffer buf
(discoursel-topic-mode)
(save-excursion
(let ((inhibit-read-only t))
(erase-buffer)
(discoursel--insert-post first-post topic-title)
(discoursel-render-posts-from-topic topic 1)
(when (> number-of-pages 1)
(if discoursel-fetch-all-posts-from-topic
(discoursel--fetch-all-posts-from-topic topic-id number-of-pages)
(message "TODO"))))))))
(defun discoursel-open-topic (id) (defun discoursel-open-topic (id)
(interactive "nTopic ID: ") (interactive "nTopic ID: ")