From d91d881a634f84e50c70e8be882bcfb278c64823 Mon Sep 17 00:00:00 2001 From: mousebot Date: Mon, 9 Aug 2021 23:14:44 +0200 Subject: functions to vote on polls in timelines, bound to "v" - masto view favorites binding moved to "V", in line with other separate views being in capitals --- README.org | 5 +++-- lisp/mastodon-tl.el | 46 ++++++++++++++++++++++++++++++++++++++++++---- lisp/mastodon.el | 4 +++- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/README.org b/README.org index 5969950..6cf318e 100644 --- a/README.org +++ b/README.org @@ -63,12 +63,13 @@ I might add a few more features if the ones I added turn out to work ok. Possibl - [X] update profile note. - [X] fix loading more notifications re-loads the same ones - [X] view/accept/reject follow requests in notifications view. -- voting on polls +- [X] fix sometimes usernames don't appear in timelines +- [X] voting on polls - better display of polls - display number of boosts/faves in toot byline - mention all thread participants in replies - handle newlines in toots better, for poetry, etc. -- improve async. +- improve (or even partially disable) async. It looks like 2-factor auth was never completed in the original repo. It's not a priority for me, auth ain't my thing. If you want to hack on it, its on the develop branch in the original repo. diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 8f368a3..107f7eb 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -52,7 +52,7 @@ (autoload 'mastodon-http--triage "mastodon-http") (autoload 'mastodon-http--get-json-async "mastodon-http") (autoload 'mastodon-profile--lookup-account-in-status "mastodon-profile") - +(autoload 'mastodon-profile-mode "mastodon-profile") (defvar mastodon-instance-url) (defvar mastodon-toot-timestamp-format) (defvar shr-use-fonts) ;; declare it since Emacs24 didn't have this @@ -691,15 +691,53 @@ it is `mastodon-tl--byline-boosted'" (defun mastodon-tl--get-poll (toot) "If post TOOT is a poll, return a formatted string of poll." (let* ((poll (mastodon-tl--field 'poll toot)) - (options (mastodon-tl--field 'options poll))) + (options (mastodon-tl--field 'options poll)) + (option-counter 0)) (concat "Poll: \n\n" (mapconcat (lambda (option) - (format "Option: %s, %s votes.\n" + (progn + (format "Option %s: %s, %s votes.\n" + (setq option-counter (1+ option-counter)) (cdr (assoc 'title option)) - (cdr (assoc 'votes_count option)))) + (cdr (assoc 'votes_count option))))) options "\n") "\n"))) +(defun mastodon-tl--poll-vote () + "If toot at point is poll, call `mastodon-tl--poll-vote-yes'." + (interactive) + ;; hack coz i don't know how to put this if test before my interactive + (if (null (mastodon-tl--field 'poll (mastodon-tl--property 'toot-json))) + (message "No poll here.") + (call-interactively 'mastodon-tl--poll-vote-yes))) + +(defun mastodon-tl--poll-vote-yes (option) + "Prompt user for OPTION to vote on poll at point." + (interactive + (list + (let* ((toot (mastodon-tl--property 'toot-json)) + (poll (mastodon-tl--field 'poll toot)) + (options (mastodon-tl--field 'options poll)) + (options-number-seq (number-sequence 1 (length options))) + (options-numbers (mapcar (lambda(x) + (number-to-string x)) + options-number-seq))) + (completing-read "Poll option to vote for: " + options-numbers + nil ;predicate + t)))) ;require match + (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 (cdr (assoc 'id poll))) + (url (mastodon-http--api (format "polls/%s/votes" poll-id))) + (arg `(("choices[]" . ,option))) + (response (mastodon-http--post url arg nil))) + (mastodon-http--triage response + (lambda () + (message "You voted for option %s!" option)))))) + (defun mastodon-tl--toot (toot) "Formats TOOT and insertes it into the buffer." (mastodon-tl--insert-status diff --git a/lisp/mastodon.el b/lisp/mastodon.el index a918b44..fd00ee9 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -79,6 +79,7 @@ (autoload 'mastodon-async--stream-notifications "mastodon-async") (autoload 'mastodon-profile--update-user-profile-note "mastodon-profile") (autoload 'mastodon-auth--user-acct "mastodon-auth") +(autoload 'mastodon-tl--poll-vote "mastodon-http") (defgroup mastodon nil "Interface with Mastodon." @@ -144,7 +145,7 @@ Use. e.g. \"%c\" for your locale's date and time format." (define-key map (kbd "d") #'mastodon-toot--delete-toot) (define-key map (kbd "C") #'mastodon-toot--copy-toot-url) (define-key map (kbd "i") #'mastodon-toot--pin-toot-toggle) - (define-key map (kbd "v") #'mastodon-profile--view-favourites) + (define-key map (kbd "V") #'mastodon-profile--view-favourites) (define-key map (kbd "R") #'mastodon-profile--view-follow-requests) (define-key map (kbd "C-c h") #'mastodon-async--stream-home) (define-key map (kbd "C-c f") #'mastodon-async--stream-federated) @@ -153,6 +154,7 @@ Use. e.g. \"%c\" for your locale's date and time format." (define-key map (kbd "U") #'mastodon-profile--update-user-profile-note) (define-key map (kbd "a") #'mastodon-notifications--follow-request-accept-notifs) (define-key map (kbd "j") #'mastodon-notifications--follow-request-reject-notifs) + (define-key map (kbd "v") #'mastodon-tl--poll-vote) map) "Keymap for `mastodon-mode'.") -- cgit v1.2.3