From 96a2448ad45a8f4c7346c91cc73b215213411ca0 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 29 Mar 2023 16:51:21 +0200 Subject: refactor trending-tags to view-trending, add trending-statuses --- lisp/mastodon-search.el | 52 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-) (limited to 'lisp') diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index e8ab093..bc51d22 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -40,10 +40,13 @@ (autoload 'mastodon-tl--render-text "mastodon-tl") (autoload 'mastodon-tl--set-buffer-spec "mastodon-tl") (autoload 'mastodon-tl--set-face "mastodon-tl") +(autoload 'mastodon-tl--timeline "mastodon-tl") +(autoload 'mastodon-tl--toot "mastodon-tl") (defvar mastodon-toot--completion-style-for-mentions) (defvar mastodon-instance-url) (defvar mastodon-tl--link-keymap) +(defvar mastodon-tl--horiz-bar) ;; functions for completion of mentions in mastodon-toot @@ -81,26 +84,53 @@ QUERY is the string to search." (defun mastodon-search--trending-tags () "Display a list of tags trending on your instance." (interactive) - (let* ((url (mastodon-http--api "trends")) + (mastodon-search--view-trending "tags" + #'mastodon-search--print-tags-list)) + +(defun mastodon-search--trending-statuses () + "Display a list of statuses trending on your instance." + (interactive) + (mastodon-search--view-trending "statuses" + #'mastodon-tl--timeline)) + +(defun mastodon-search--get-full-statuses-data (response) + "For statuses list in RESPONSE, fetch and return full status JSON." + (let ((status-ids-list + (mapcar #'mastodon-search--get-id-from-status response))) + (mapcar #'mastodon-search--fetch-full-status-from-id + status-ids-list))) + +(defun mastodon-search--view-trending (type print-fun) + "Display a list of tags trending on your instance. +TYPE is a string, either tags, statuses, or links. +PRINT-FUN is the function used to print the data from the response." + (let* ((url (mastodon-http--api + (format "trends/%s" type))) (response (mastodon-http--get-json url)) - (tags (mapcar #'mastodon-search--get-hashtag-info - response)) - (buffer (get-buffer-create "*mastodon-trending*"))) + (data (cond ((equal type "tags") + (mapcar #'mastodon-search--get-hashtag-info + response)) + ((equal type "statuses") + (mastodon-search--get-full-statuses-data response)) + ((equal type "links") + (message "todo")))) + (buffer (get-buffer-create + (format "*mastodon-trending-%s*" type)))) (with-current-buffer buffer (switch-to-buffer (current-buffer)) (mastodon-mode) (let ((inhibit-read-only t)) (erase-buffer) (mastodon-tl--set-buffer-spec (buffer-name buffer) - "api/v1/trends" + (format "api/v1/trends/%s" type) nil) ;; hashtag results: (insert (mastodon-tl--set-face (concat "\n " mastodon-tl--horiz-bar "\n" - " TRENDING HASHTAGS\n" + (upcase (format " TRENDING %s\n" type)) " " mastodon-tl--horiz-bar "\n\n") 'success)) - (mastodon-search--print-tags-list tags))))) + (funcall print-fun data))))) ;; functions for mastodon search @@ -118,12 +148,8 @@ QUERY is the string to search." ;; accts)) ; returns a list of three-item lists (tags-list (mapcar #'mastodon-search--get-hashtag-info tags)) - ;; (status-list (mapcar #'mastodon-search--get-status-info - ;; statuses)) - (status-ids-list (mapcar #'mastodon-search--get-id-from-status - statuses)) - (toots-list-json (mapcar #'mastodon-search--fetch-full-status-from-id - status-ids-list))) + (toots-list-json + (mastodon-search--get-full-statuses-data statuses))) (with-current-buffer (get-buffer-create buffer) (switch-to-buffer buffer) (mastodon-mode) -- cgit v1.2.3