From 80c3d0250b9492b877ae93b0cb570b8e2774171e Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Tue, 2 May 2023 21:13:54 +0200 Subject: let-alist --poll-vote, refactor --read-poll-option --- lisp/mastodon-tl.el | 78 ++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 0cdbad8..0c24b86 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -1256,48 +1256,46 @@ To disable showing the stats, customize (plist-get parsed :hours) (plist-get parsed :minutes)))))) +(defun mastodon-tl--read-poll-option () + "Read a poll option to vote on a poll." + (list + (let-alist (mastodon-tl--property 'toot-json) + (let* ((poll (or .reblog.poll .poll)) + (options (mastodon-tl--field 'options poll)) + (options-titles (mastodon-tl--map-alist 'title options)) + (options-number-seq (number-sequence 1 (length options))) + (options-numbers (mapcar #'number-to-string options-number-seq)) + (options-alist (cl-mapcar #'cons options-numbers options-titles)) + ;; we display both option number and the option title + ;; but also store both as cons cell as cdr, as we need it below + (candidates (mapcar (lambda (cell) + (cons (format "%s | %s" (car cell) (cdr cell)) + cell)) + options-alist))) + (if (null .poll) ;(mastodon-tl--field 'poll (mastodon-tl--property 'toot-json))) + (message "No poll here.") + ;; var "option" = just the cdr, a cons of option number and desc + (cdr (assoc (completing-read "Poll option to vote for: " + candidates + nil ; (predicate) + t) ; require match + candidates))))))) + (defun mastodon-tl--poll-vote (option) "If there is a poll at point, prompt user for OPTION to vote on it." - (interactive - (list - (let* ((toot (mastodon-tl--property 'toot-json)) - (reblog (alist-get 'reblog toot)) - (poll (or (alist-get 'poll reblog) - (mastodon-tl--field 'poll toot))) - (options (mastodon-tl--field 'options poll)) - (options-titles (mastodon-tl--map-alist 'title options)) - (options-number-seq (number-sequence 1 (length options))) - (options-numbers (mapcar #'number-to-string options-number-seq)) - (options-alist (cl-mapcar #'cons options-numbers options-titles)) - ;; we display both option number and the option title - ;; but also store both as cons cell as cdr, as we need it below - (candidates (mapcar (lambda (cell) - (cons (format "%s | %s" (car cell) (cdr cell)) - cell)) - options-alist))) - (if (null (mastodon-tl--field 'poll (mastodon-tl--property 'toot-json))) - (message "No poll here.") - ;; var "option" = just the cdr, a cons of option number and desc - (cdr (assoc - (completing-read "Poll option to vote for: " - candidates - nil ; (predicate) - t) ; require match - candidates)))))) - (if (null (mastodon-tl--field 'poll (mastodon-tl--property 'toot-json))) - (message "No poll here.") - (let* ((toot (mastodon-tl--property 'toot-json)) - (poll (mastodon-tl--field 'poll toot)) - (poll-id (alist-get 'id poll)) - (url (mastodon-http--api (format "polls/%s/votes" poll-id))) - ;; need to zero-index our option: - (option-as-arg (number-to-string (1- (string-to-number (car option))))) - (arg `(("choices[]" . ,option-as-arg))) - (response (mastodon-http--post url arg))) - (mastodon-http--triage response - (lambda () - (message "You voted for option %s: %s!" - (car option) (cdr option))))))) + (interactive (mastodon-tl--read-poll-option)) + (let-alist (mastodon-tl--property 'toot-json) + (if (null .poll) ;(mastodon-tl--field 'poll (mastodon-tl--property 'toot-json))) + (message "No poll here.") + (let* ((url (mastodon-http--api (format "polls/%s/votes" .poll.id))) + ;; need to zero-index our option: + (option-as-arg (number-to-string (1- (string-to-number (car option))))) + (arg `(("choices[]" . ,option-as-arg))) + (response (mastodon-http--post url arg))) + (mastodon-http--triage response + (lambda () + (message "You voted for option %s: %s!" + (car option) (cdr option)))))))) ;; VIDEOS / MPV -- cgit v1.2.3