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:
parent
dac665ceb1
commit
245d2b702e
1 changed files with 49 additions and 51 deletions
100
discoursel.el
100
discoursel.el
|
@ -55,9 +55,14 @@ posts manually and incrementally."
|
|||
(add-text-properties p (point)
|
||||
'(pre-tag t face discoursel-code-face))))
|
||||
|
||||
(defun discoursel--get (endpoint)
|
||||
(defun discoursel--get (endpoint &optional args)
|
||||
(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)
|
||||
(plz-error
|
||||
(error "Could not query endpoint '%s': '%s'"
|
||||
|
@ -192,41 +197,15 @@ characters."
|
|||
'post-id post-id)
|
||||
(buffer-string))))))
|
||||
|
||||
(defun discoursel--open-topic (&optional id)
|
||||
(interactive)
|
||||
(when (and (not (eq major-mode 'discoursel-mode))
|
||||
(not id))
|
||||
(user-error "You must be in a discoursel buffer"))
|
||||
(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)
|
||||
(defun discoursel-render-posts-from-topic (topic &optional start-idx)
|
||||
(let* ((posts (alist-get 'posts (alist-get 'post_stream topic)))
|
||||
(topic-slug (alist-get 'slug topic))
|
||||
(buf (discoursel-get-create-buffer topic-slug))
|
||||
(startidx (or start-idx 0)))
|
||||
(with-current-buffer buf
|
||||
(discoursel-topic-mode)
|
||||
(save-excursion
|
||||
(let ((inhibit-read-only t))
|
||||
(erase-buffer)
|
||||
(discoursel--insert-post first-post topic-title)
|
||||
(cl-loop for idx from 1 to (1- (length posts)) do
|
||||
(cl-loop for idx from startidx to (1- (length posts)) do
|
||||
(goto-char (point-max))
|
||||
(insert
|
||||
"\n"
|
||||
|
@ -235,23 +214,42 @@ characters."
|
|||
(shr-render-region (point-min) (point-max))
|
||||
(buffer-string)))
|
||||
(discoursel--insert-post (aref posts idx))))))))
|
||||
;; (save-excursion
|
||||
;; (insert
|
||||
;; (with-temp-buffer
|
||||
;; (insert (format (propertize "Title: %s\n" 'face 'message-header-name)
|
||||
;; (propertize topic-title 'face 'message-header-subject)))
|
||||
;; (insert (format (propertize "Author: %s\n" 'face 'message-header-name)
|
||||
;; (propertize topic-author 'face 'message-header-to)))
|
||||
;; (insert (format (propertize "Date: %s\n" 'face 'message-header-name)
|
||||
;; (propertize topic-date-string 'face 'message-header-other)))
|
||||
;; (insert "\n")
|
||||
;; (save-excursion
|
||||
;; (insert (alist-get 'cooked first-post)))
|
||||
;; (let ((shr-use-fonts nil)
|
||||
;; (shr-external-rendering-functions
|
||||
;; '((pre . discoursel--parse-pre))))
|
||||
;; (shr-render-region (point) (point-max)))
|
||||
;; (buffer-string))))))))
|
||||
|
||||
(defun discoursel--fetch-all-posts-from-topic (topic-id number-of-pages)
|
||||
(cl-loop for page from 2 to number-of-pages do
|
||||
(with-delayed-message (2 (format "Rendering page %d out of %d..." page number-of-pages))
|
||||
(let* ((topic (discoursel--get (concat "t/" topic-id)
|
||||
`(("page" . ,(number-to-string page)))))
|
||||
(posts (alist-get 'posts (alist-get 'post_stream topic))))
|
||||
(discoursel-render-posts-from-topic topic)))))
|
||||
|
||||
(defun discoursel--open-topic (&optional id)
|
||||
(interactive)
|
||||
(when (and (not (eq major-mode 'discoursel-mode))
|
||||
(not id))
|
||||
(user-error "You must be in a discoursel buffer"))
|
||||
(let* ((topic-id (number-to-string
|
||||
(or id (get-text-property (point) 'discoursel-topic-id))))
|
||||
(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)
|
||||
(interactive "nTopic ID: ")
|
||||
|
|
Loading…
Reference in a new issue