From 53bf30caab7e6076ecc8307098d6b331c8258ca5 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 23 Oct 2024 21:07:43 +0200 Subject: add mastodon-instance-data var and fun mainly used in mastodon-transient.el to avoid pinging the server for instance data non-stop --- lisp/mastodon-toot.el | 37 +++++++++++++++++++------------------ lisp/mastodon.el | 9 +++++++++ 2 files changed, 28 insertions(+), 18 deletions(-) (limited to 'lisp') diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 7440fe5..4177062 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -199,7 +199,7 @@ change the setting on the server, see "A list of any media attachment ids of the toot being composed.") (defvar-local mastodon-toot-poll nil - "A list of poll options for the toot being composed.") + "A plist of poll options for the toot being composed.") (defvar-local mastodon-toot--language nil "The language of the toot being composed, in ISO 639 (two-letter).") @@ -892,16 +892,18 @@ instance to edit a toot." (endpoint (mastodon-http--api (if edit-id ; we are sending an edit: (format "statuses/%s" edit-id) "statuses"))) - (args-no-media (append `(("status" . ,toot) - ("in_reply_to_id" . ,mastodon-toot--reply-to-id) - ("visibility" . ,mastodon-toot--visibility) - ("sensitive" . ,(when mastodon-toot--content-nsfw - (symbol-name t))) - ("spoiler_text" . ,mastodon-toot--content-warning) - ("language" . ,mastodon-toot--language)) - ;; Pleroma instances can't handle null-valued - ;; scheduled_at args, so only add if non-nil - (when scheduled `(("scheduled_at" . ,scheduled))))) + (args-no-media + (append + `(("status" . ,toot) + ("in_reply_to_id" . ,mastodon-toot--reply-to-id) + ("visibility" . ,mastodon-toot--visibility) + ("sensitive" . ,(when mastodon-toot--content-nsfw + (symbol-name t))) + ("spoiler_text" . ,mastodon-toot--content-warning) + ("language" . ,mastodon-toot--language)) + ;; Pleroma instances can't handle null-valued + ;; scheduled_at args, so only add if non-nil + (when scheduled `(("scheduled_at" . ,scheduled))))) (args-media (when mastodon-toot--media-attachment-ids (mastodon-http--build-array-params-alist "media_ids[]" @@ -1399,7 +1401,7 @@ MAX is the maximum number set by their instance." (defun mastodon-toot--create-poll () "Prompt for new poll options and return as a list." (interactive) - (let* ((instance (mastodon-http--get-json (mastodon-http--api "instance"))) + (let* ((instance (mastodon-instance-data)) (max-options (mastodon-toot--fetch-max-poll-options instance)) (count (mastodon-toot--read-poll-options-count max-options)) (length (mastodon-toot--fetch-max-poll-option-chars instance)) @@ -1424,12 +1426,11 @@ LENGTH is the maximum character length allowed for a poll option." (format "Poll option [%s/%s] [max %s chars]: " x count length)))) (longest (apply #'max (mapcar #'length choices)))) - (if (> longest length) - (progn - (user-error "Looks like you went over the max length. Try again") - (sleep-for 2) - (mastodon-toot--read-poll-options count length)) - choices))) + (if (not (> longest length)) + choices + (user-error "Looks like you went over the max length. Try again") + (sleep-for 2) + (mastodon-toot--read-poll-options count length)))) (defun mastodon-toot--read-poll-expiry () "Prompt for a poll expiry time. diff --git a/lisp/mastodon.el b/lisp/mastodon.el index 89e2a87..8560902 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -337,6 +337,15 @@ FORCE means to fetch from the server in any case and update ;; else just return the var: mastodon-profile-credential-account)) +(defvar mastodon-instance-data nil + "Instance data from the instance endpoint.") + +(defun mastodon-instance-data () + "Return `mastodon-instnace-data' or else fetch from instance endpoint." + (or mastodon-instance-data + (setq mastodon-instance-data + (mastodon-http--get-json (mastodon-http--api "instance"))))) + ;;;###autoload (defun mastodon-toot (&optional user reply-to-id reply-json) "Update instance with new toot. Content is captured in a new buffer. -- cgit v1.2.3 From a49d9e2c7df1e94be4e3a64f46daf3628f9a9c64 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 23 Oct 2024 21:09:10 +0200 Subject: transient: add poll transient transient poll setup name correct poll trans: fix prefix name in suffix --- lisp/mastodon-transient.el | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'lisp') diff --git a/lisp/mastodon-transient.el b/lisp/mastodon-transient.el index 526dfa4..f8905a8 100644 --- a/lisp/mastodon-transient.el +++ b/lisp/mastodon-transient.el @@ -190,6 +190,70 @@ the format fields.X.keyname." (user-error "User not set") (transient-setup 'mastodon-profile-fields))) +(defun mastodon-transient-max-poll-opts () + "Return max poll options of user's instance." + (let ((instance (mastodon-instance-data))) + (mastodon-toot--fetch-max-poll-options instance))) + +(defun mastodon-transient-max-poll-opt-chars () + "Return max poll option characters of user's instance." + (let ((instance (mastodon-instance-data))) + (mastodon-toot--fetch-max-poll-option-chars instance))) + +(transient-define-prefix mastodon-toot--create-poll () + "A transient for creating a poll." + ;; FIXME: handle existing polls when editing a toot + ;; FIXME: handle editing poll in same toot! + ;; :value (lambda () (mastodon-transient-init-poll)) + ["Create poll" + (:info (lambda () + (format "Max options: %s" + (mastodon-transient-max-poll-opts)))) + (:info (lambda () + (format "Max option length: %s" + (mastodon-transient-max-poll-opt-chars))))] + ["Options" + ("m" "Multiple choice?" "multi" :alist-key multi :class tp-bool) + ("h" "Hide vote count till expiry?" "hide" :alist-key hide :class tp-bool) + ("e" "Expiry" "expiry" :alist-key expiry :class mastodon-transient-expiry)] + ["Choices" + ("1" "" "1" :alist-key one :class tp-option-str) + ("2" "" "2" :alist-key two :class tp-option-str) + ("3" "" "3" :alist-key three :class tp-option-str) + ("4" "" "4" :alist-key four :class tp-option-str)] + ;; TODO: display the max number of options or add options cmd + ["Update" + ("C-c C-c" "Done" mastodon-create-poll-done) + ("C-c C-k" :info "Revert all")] + (interactive) + (if (not mastodon-active-user) + (user-error "User not set") + (transient-setup 'mastodon-toot--create-poll))) + +(transient-define-suffix mastodon-create-poll-done (args) + "Update current user profile fields." + :transient 'transient--do-exit + (interactive (list (transient-args 'mastodon-toot--create-poll))) + ;; (message "Done!\n%s" args) + ;; this is a mess, but we are just plugging our transient data into the + ;; existing variable, as we already have code to post that. we don't + ;; want to post the poll in our suffix, just set the variable and send + ;; the data when the toot is sent + + ;; FIXME: if + ;; - no options filled in + ;; - no expiry + ;; then offer to cancel or warn / return to transient + (let-alist args + (setq mastodon-toot-poll + `( :options ,(list .one .two .three .four) + ;; :length ,length + ;; :expiry-readable ,expiry-human + :multi ,.multi + :hide ,.hide + :expiry ,.expiry))) + (mastodon-toot--update-status-fields)) + ;;; CLASSES (defclass mastodon-transient-field (tp-option-str) @@ -226,5 +290,27 @@ CONS is a cons of the form \"(fields.1.name . val)\"." (not (equal (cdr cons) (alist-get server-key server-elt nil nil #'string=))))) +(defclass mastodon-transient-expiry (tp-option) + ((always-read :initarg :always-read :initform t))) + +(cl-defmethod transient-infix-read ((_obj mastodon-transient-expiry)) + "Reader function for OBJ, a poll expiry." + (cdr (mastodon-toot--read-poll-expiry))) + +(cl-defmethod transient-format-value ((obj mastodon-transient-expiry)) + "Format function for OBJ, a poll expiry." + (let* ((cons (transient-infix-value obj)) + (value (when cons (cdr cons)))) + (if (not value) + "" + (let ((readable + (car + (rassoc value + (mastodon-toot--poll-expiry-options-alist))))) + (propertize readable + 'face (if (tp-arg-changed-p obj cons) + 'transient-value + 'transient-inactive-value)))))) + (provide 'mastodon-transient) ;;; mastodon-transient.el ends here -- cgit v1.2.3 From 263d486c3bb83bc7cbfb9bb5196f25f1253c02a5 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 23 Oct 2024 21:09:25 +0200 Subject: autoloads, flycheck --- lisp/mastodon-transient.el | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'lisp') diff --git a/lisp/mastodon-transient.el b/lisp/mastodon-transient.el index f8905a8..1a9442e 100644 --- a/lisp/mastodon-transient.el +++ b/lisp/mastodon-transient.el @@ -26,6 +26,24 @@ (require 'tp) +(defvar mastodon-active-user) +(defvar mastodon-toot-visibility-settings-list) +(defvar mastodon-iso-639-regional) +(defvar mastodon-toot-poll) + +(autoload 'mastodon-toot-visibility-settings-list "mastodon-toot") +(autoload 'mastodon-http--get-json "mastodon-http") +(autoload 'mastodon-http--api "mastodon-http") +(autoload 'mastodon-http--triage "mastodon-http") +(autoload 'mastodon-http--patch "mastodon-http") +(autoload 'mastodon-profile--update-user-profile-note "mastodon-profile") +(autoload 'mastodon-toot--fetch-max-poll-options "mastodon-toot") +(autoload 'mastodon-toot--fetch-max-poll-option-chars "mastodon-toot") +(autoload 'mastodon-instance-data "mastodon") +(autoload 'mastodon-toot--update-status-fields "mastodon-toot") +(autoload 'mastodon-toot--read-poll-expiry "mastodon-toot") +(autoload 'mastodon-toot--poll-expiry-options-alist "mastodon-toot") + ;;; UTILS ;; some JSON fields that are returned under the "source" field need to be @@ -168,8 +186,7 @@ the format fields.X.keyname." (transient-define-prefix mastodon-profile-fields () "A transient for setting profile fields." - :value - (lambda () (mastodon-transient-fetch-fields)) + :value (lambda () (mastodon-transient-fetch-fields)) [:description "Fields" ["Name" @@ -263,8 +280,8 @@ We always read.") (cl-defmethod transient-init-value ((obj mastodon-transient-field)) "Initialize value of OBJ." - (let* ((prefix-val (oref transient--prefix value)) - (arg (oref obj alist-key))) + (let* ((prefix-val (oref transient--prefix value))) + ;; (arg (oref obj alist-key))) (oset obj value (tp-get-server-val obj prefix-val)))) @@ -283,7 +300,7 @@ only one level of nesting is supported." "T if value of OBJ is changed from the server value. CONS is a cons of the form \"(fields.1.name . val)\"." (let* ((key-split (split-string - (symbol-to-string (car cons)) "\\.")) + (symbol-name (car cons)) "\\.")) (num (1- (string-to-number (nth 1 key-split)))) (server-key (symbol-name (car cons))) (server-elt (nth num tp-server-settings))) -- cgit v1.2.3 From 7c9e5577f77da54d10bc0ed1426f0eff3570e3de Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 23 Oct 2024 21:29:41 +0200 Subject: boolean defcustom for transient poll --- lisp/mastodon-toot.el | 10 ++++++++++ lisp/mastodon-transient.el | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'lisp') diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 4177062..efcbc72 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -168,6 +168,10 @@ By default fixed width fonts are used." :type '(boolean :tag "Enable using proportional rather than fixed \ width fonts")) +(defcustom mastodon-toot-poll-use-transient t + "Whether to use the transient menu to create a poll." + :type '(boolean)) + (defvar-local mastodon-toot--content-warning nil "The content warning of the current toot.") @@ -1401,6 +1405,12 @@ MAX is the maximum number set by their instance." (defun mastodon-toot--create-poll () "Prompt for new poll options and return as a list." (interactive) + (if mastodon-toot-poll-use-transient + (mastodon-create-poll) + (mastodon-toot--read-poll))) + +(defun mastodon-toot--read-poll () + "Read poll options." (let* ((instance (mastodon-instance-data)) (max-options (mastodon-toot--fetch-max-poll-options instance)) (count (mastodon-toot--read-poll-options-count max-options)) diff --git a/lisp/mastodon-transient.el b/lisp/mastodon-transient.el index 1a9442e..fbdfcb0 100644 --- a/lisp/mastodon-transient.el +++ b/lisp/mastodon-transient.el @@ -217,7 +217,7 @@ the format fields.X.keyname." (let ((instance (mastodon-instance-data))) (mastodon-toot--fetch-max-poll-option-chars instance))) -(transient-define-prefix mastodon-toot--create-poll () +(transient-define-prefix mastodon-create-poll () "A transient for creating a poll." ;; FIXME: handle existing polls when editing a toot ;; FIXME: handle editing poll in same toot! @@ -245,12 +245,12 @@ the format fields.X.keyname." (interactive) (if (not mastodon-active-user) (user-error "User not set") - (transient-setup 'mastodon-toot--create-poll))) + (transient-setup 'mastodon-create-poll))) (transient-define-suffix mastodon-create-poll-done (args) "Update current user profile fields." :transient 'transient--do-exit - (interactive (list (transient-args 'mastodon-toot--create-poll))) + (interactive (list (transient-args 'mastodon-create-poll))) ;; (message "Done!\n%s" args) ;; this is a mess, but we are just plugging our transient data into the ;; existing variable, as we already have code to post that. we don't -- cgit v1.2.3 From 99da98a03519d08ab0779d5b21e04760848ae87d Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Thu, 24 Oct 2024 10:14:11 +0200 Subject: transient poll: use tp-transient-settings, handle repeated poll edits also integrate with toot code: send, update display, etc. --- lisp/mastodon-toot.el | 65 +++++++++++++++++++++++++++++++-------------- lisp/mastodon-transient.el | 66 ++++++++++++++++++++++++++++------------------ 2 files changed, 86 insertions(+), 45 deletions(-) (limited to 'lisp') diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index efcbc72..35124ea 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -53,6 +53,7 @@ (defvar mastodon-tl--enable-proportional-fonts) (defvar mastodon-profile-account-settings) (defvar mastodon-profile-acccount-preferences-data) +(defvar tp-transient-settings) (autoload 'iso8601-parse "iso8601") (autoload 'mastodon-auth--user-acct "mastodon-auth") @@ -100,6 +101,8 @@ (autoload 'mastodon-profile--get-preferences-pref "mastodon-profile") (autoload 'mastodon-views--get-own-instance "mastodon-views") (autoload 'mastodon-tl--image-trans-check "mastodon-tl") +(autoload 'mastodon-instance-data "mastodon") +(autoload 'mastodon-create-poll "mastodon-transient") ;; for mastodon-toot--translate-toot-text (autoload 'mastodon-tl--content "mastodon-tl") @@ -202,7 +205,7 @@ change the setting on the server, see (defvar-local mastodon-toot--media-attachment-ids nil "A list of any media attachment ids of the toot being composed.") -(defvar-local mastodon-toot-poll nil +(defvar mastodon-toot-poll nil "A plist of poll options for the toot being composed.") (defvar-local mastodon-toot--language nil @@ -760,9 +763,9 @@ If toot is not empty, prompt to save text as a draft." Pushes `mastodon-toot-current-toot-text' to `mastodon-toot-draft-toots-list'." (interactive) - (unless (eq mastodon-toot-current-toot-text nil) + (unless (string= mastodon-toot-current-toot-text nil) (cl-pushnew mastodon-toot-current-toot-text - mastodon-toot-draft-toots-list :test 'equal) + mastodon-toot-draft-toots-list :test 'string=) (message "Draft saved!"))) (defun mastodon-toot--empty-p (&optional text-only) @@ -771,7 +774,7 @@ TEXT-ONLY means don't check for attachments or polls." (and (if text-only t (and (not mastodon-toot--media-attachments) - (not mastodon-toot-poll))) + (not (mastodon-toot-poll-var)))) (string-empty-p (mastodon-tl--clean-tabs-and-nl (mastodon-toot--remove-docs))))) @@ -871,13 +874,22 @@ to `emojify-user-emojis', and the emoji data is updated." (defun mastodon-toot--build-poll-params () "Return an alist of parameters for POSTing a poll status." - (append - (mastodon-http--build-array-params-alist - "poll[options][]" - (plist-get mastodon-toot-poll :options)) - `(("poll[expires_in]" . ,(plist-get mastodon-toot-poll :expiry))) - `(("poll[multiple]" . ,(symbol-name (plist-get mastodon-toot-poll :multi)))) - `(("poll[hide_totals]" . ,(symbol-name (plist-get mastodon-toot-poll :hide)))))) + (if mastodon-toot-poll-use-transient + (let-alist tp-transient-settings + (append + (mastodon-http--build-array-params-alist + "poll[options][]" + (list .one .two .three .four)) + (list (cons "poll[expires_in]" .expiry) + (cons "poll[multiple]" .multi) + (cons "poll[hide_totals]" .hide)))) + (append + (mastodon-http--build-array-params-alist + "poll[options][]" + (plist-get mastodon-toot-poll :options)) + `(("poll[expires_in]" . ,(plist-get mastodon-toot-poll :expiry))) + `(("poll[multiple]" . ,(symbol-name (plist-get mastodon-toot-poll :multi)))) + `(("poll[hide_totals]" . ,(symbol-name (plist-get mastodon-toot-poll :hide))))))) ;;; SEND TOOT FUNCTION @@ -912,12 +924,13 @@ instance to edit a toot." (mastodon-http--build-array-params-alist "media_ids[]" mastodon-toot--media-attachment-ids))) - (args-poll (when mastodon-toot-poll + (poll-var (mastodon-toot-poll-var)) + (args-poll (when poll-var (mastodon-toot--build-poll-params))) ;; media || polls: (args (if mastodon-toot--media-attachment-ids (append args-media args-no-media) - (if mastodon-toot-poll + (if poll-var (append args-no-media args-poll) args-no-media))) (prev-window-config mastodon-toot-previous-window-config)) @@ -944,6 +957,8 @@ instance to edit a toot." (lambda (_) ;; kill buffer: (mastodon-toot--kill) + ;; nil our poll var: + (set poll-var nil) (message "Toot %s!" (if scheduled "scheduled" "toot")) ;; cancel scheduled toot if we were editing it: (when scheduled-id @@ -1374,6 +1389,12 @@ which is used to attach it to a toot when posting." ;;; POLL +(defun mastodon-toot-poll-var () + "Return the correct poll var." + (if mastodon-toot-poll-use-transient + 'tp-transient-settings + 'mastodon-toot-poll)) + (defun mastodon-toot--fetch-max-poll-options (instance) "Return the maximum number of poll options from JSON data INSTANCE." (mastodon-toot--fetch-poll-field 'max_options instance)) @@ -1469,10 +1490,11 @@ Return a cons of a human readable string, and a seconds-from-now string." "Remove poll from toot compose buffer. Sets `mastodon-toot-poll' to nil." (interactive) - (if (not mastodon-toot-poll) - (user-error "No poll?") - (setq mastodon-toot-poll nil) - (mastodon-toot--update-status-fields))) + (let ((var (mastodon-toot-poll-var))) + (if (not var) + (user-error "No poll?") + (set var nil) + (mastodon-toot--update-status-fields)))) (defun mastodon-toot--server-poll-to-local (json) "Convert server poll data JSON to a `mastodon-toot-poll' plist." @@ -1768,7 +1790,8 @@ REPLY-REGION is a string to be injected into the buffer." (poll-region (mastodon-tl--find-property-range 'toot-post-poll-flag (point-min))) (toot-string (buffer-substring-no-properties (cdr header-region) - (point-max)))) + (point-max))) + (poll-var (mastodon-toot-poll-var))) (mastodon-toot--apply-fields-props count-region (format "%s/%s chars" @@ -1802,9 +1825,11 @@ REPLY-REGION is a string to be injected into the buffer." 'mastodon-cw-face) (mastodon-toot--apply-fields-props poll-region - (if mastodon-toot-poll "POLL" "") + (if (symbol-value poll-var) + "POLL" + "") 'mastodon-cw-face - (prin1-to-string mastodon-toot-poll)) + (prin1-to-string (symbol-value poll-var))) (mastodon-toot--apply-fields-props cw-region (if (and mastodon-toot--content-warning diff --git a/lisp/mastodon-transient.el b/lisp/mastodon-transient.el index fbdfcb0..4776c4b 100644 --- a/lisp/mastodon-transient.el +++ b/lisp/mastodon-transient.el @@ -25,6 +25,7 @@ ;;; Code: (require 'tp) +(require 'transient) (defvar mastodon-active-user) (defvar mastodon-toot-visibility-settings-list) @@ -43,6 +44,7 @@ (autoload 'mastodon-toot--update-status-fields "mastodon-toot") (autoload 'mastodon-toot--read-poll-expiry "mastodon-toot") (autoload 'mastodon-toot--poll-expiry-options-alist "mastodon-toot") +(autoload 'mastodon-toot--clear-poll "mastodon-toot") ;;; UTILS @@ -181,8 +183,8 @@ the format fields.X.keyname." (defun mastodon-transient-fetch-fields () "Fetch profile fields (metadata)." (tp-return-data #'mastodon-transient-get-creds nil 'fields) - (setq tp-server-settings - (mastodon-transient--fields-alist tp-server-settings))) + (setq tp-transient-settings + (mastodon-transient--fields-alist tp-transient-settings))) (transient-define-prefix mastodon-profile-fields () "A transient for setting profile fields." @@ -220,8 +222,7 @@ the format fields.X.keyname." (transient-define-prefix mastodon-create-poll () "A transient for creating a poll." ;; FIXME: handle existing polls when editing a toot - ;; FIXME: handle editing poll in same toot! - ;; :value (lambda () (mastodon-transient-init-poll)) + :value (lambda () tp-transient-settings) ["Create poll" (:info (lambda () (format "Max options: %s" @@ -230,9 +231,12 @@ the format fields.X.keyname." (format "Max option length: %s" (mastodon-transient-max-poll-opt-chars))))] ["Options" - ("m" "Multiple choice?" "multi" :alist-key multi :class tp-bool) - ("h" "Hide vote count till expiry?" "hide" :alist-key hide :class tp-bool) - ("e" "Expiry" "expiry" :alist-key expiry :class mastodon-transient-expiry)] + ("m" "Multiple choice?" "multi" :alist-key multi + :class mastodon-transient-poll-bool) + ("h" "Hide vote count till expiry?" "hide" :alist-key hide + :class mastodon-transient-poll-bool) + ("e" "Expiry" "expiry" :alist-key expiry + :class mastodon-transient-expiry)] ["Choices" ("1" "" "1" :alist-key one :class tp-option-str) ("2" "" "2" :alist-key two :class tp-option-str) @@ -240,35 +244,31 @@ the format fields.X.keyname." ("4" "" "4" :alist-key four :class tp-option-str)] ;; TODO: display the max number of options or add options cmd ["Update" - ("C-c C-c" "Done" mastodon-create-poll-done) - ("C-c C-k" :info "Revert all")] + ("C-c C-c" "Save and done" mastodon-create-poll-done) + ("C-c C-k" "Delete all" mastodon-clear-poll) + ("C-x C-k" :info "Revert all")] (interactive) (if (not mastodon-active-user) (user-error "User not set") (transient-setup 'mastodon-create-poll))) +(transient-define-suffix mastodon-clear-poll () + "Clear current poll data." + :transient 'transient--do-stay + (interactive) + (mastodon-toot--clear-poll) + (transient-reset)) + (transient-define-suffix mastodon-create-poll-done (args) "Update current user profile fields." :transient 'transient--do-exit (interactive (list (transient-args 'mastodon-create-poll))) - ;; (message "Done!\n%s" args) - ;; this is a mess, but we are just plugging our transient data into the - ;; existing variable, as we already have code to post that. we don't - ;; want to post the poll in our suffix, just set the variable and send - ;; the data when the toot is sent - ;; FIXME: if ;; - no options filled in ;; - no expiry ;; then offer to cancel or warn / return to transient - (let-alist args - (setq mastodon-toot-poll - `( :options ,(list .one .two .three .four) - ;; :length ,length - ;; :expiry-readable ,expiry-human - :multi ,.multi - :hide ,.hide - :expiry ,.expiry))) + (setq tp-transient-settings + (tp-bools-to-strs args)) (mastodon-toot--update-status-fields)) ;;; CLASSES @@ -303,12 +303,28 @@ CONS is a cons of the form \"(fields.1.name . val)\"." (symbol-name (car cons)) "\\.")) (num (1- (string-to-number (nth 1 key-split)))) (server-key (symbol-name (car cons))) - (server-elt (nth num tp-server-settings))) + (server-elt (nth num tp-transient-settings))) (not (equal (cdr cons) (alist-get server-key server-elt nil nil #'string=))))) +(defclass mastodon-transient-poll-bool (tp-bool) + ()) + +(cl-defmethod transient-init-value ((obj mastodon-transient-poll-bool)) + "Initialize OBJ, an expiry option. +Pull value from `mastodon-tool-poll' if possible.'" + (let ((key (oref obj alist-key))) + (oset obj value + (alist-get key tp-transient-settings)))) + (defclass mastodon-transient-expiry (tp-option) - ((always-read :initarg :always-read :initform t))) + ()) + +(cl-defmethod transient-init-value ((obj mastodon-transient-expiry)) + "Initialize OBJ, an expiry option. +Pull value from `mastodon-tool-poll' if possible.'" + (oset obj value + (alist-get 'expiry tp-transient-settings))) (cl-defmethod transient-infix-read ((_obj mastodon-transient-expiry)) "Reader function for OBJ, a poll expiry." -- cgit v1.2.3 From 37a76491311fe1f6852be9ef6c683d3cbd1655a7 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Thu, 24 Oct 2024 10:40:29 +0200 Subject: poll: set tp-transient-settings on editing a toot --- lisp/mastodon-toot.el | 15 ++++++++++++--- lisp/mastodon-transient.el | 7 ++++--- 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'lisp') diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 35124ea..bd62728 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -1510,9 +1510,18 @@ Sets `mastodon-toot-poll' to nil." (mastodon-tl--human-duration expiry-seconds-rel))) (options (mastodon-tl--map-alist 'title .options)) (multiple (if (eq :json-false .multiple) nil t))) - (setq mastodon-toot-poll - `( :options ,options :expiry-readable ,expiry-human - :expiry ,expiry-str :multi ,multiple))))) + (if mastodon-toot-poll-use-transient + (setq tp-transient-settings + `((multi . ,multiple) + (expiry . ,expiry-str) + ;; (hide . ,hide) + (one . ,(nth 0 options)) + (two . ,(nth 1 options)) + (three . ,(nth 2 options)) + (four . ,(nth 3 options)))) + (setq mastodon-toot-poll + `( :options ,options :expiry-readable ,expiry-human + :expiry ,expiry-str :multi ,multiple)))))) ;;; SCHEDULE diff --git a/lisp/mastodon-transient.el b/lisp/mastodon-transient.el index 4776c4b..f418fcc 100644 --- a/lisp/mastodon-transient.el +++ b/lisp/mastodon-transient.el @@ -337,9 +337,10 @@ Pull value from `mastodon-tool-poll' if possible.'" (if (not value) "" (let ((readable - (car - (rassoc value - (mastodon-toot--poll-expiry-options-alist))))) + (or (car + (rassoc value + (mastodon-toot--poll-expiry-options-alist))) + (concat value " secs")))) ;; editing a poll wont match expiry list (propertize readable 'face (if (tp-arg-changed-p obj cons) 'transient-value -- cgit v1.2.3 From 5a2fd200faa067a6c8fec087d9c99a0d7fab0415 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Thu, 24 Oct 2024 10:45:20 +0200 Subject: transient poll: edit toot pulls in existing poll --- lisp/mastodon-transient.el | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'lisp') diff --git a/lisp/mastodon-transient.el b/lisp/mastodon-transient.el index f418fcc..38da285 100644 --- a/lisp/mastodon-transient.el +++ b/lisp/mastodon-transient.el @@ -238,10 +238,10 @@ the format fields.X.keyname." ("e" "Expiry" "expiry" :alist-key expiry :class mastodon-transient-expiry)] ["Choices" - ("1" "" "1" :alist-key one :class tp-option-str) - ("2" "" "2" :alist-key two :class tp-option-str) - ("3" "" "3" :alist-key three :class tp-option-str) - ("4" "" "4" :alist-key four :class tp-option-str)] + ("1" "" "1" :alist-key one :class mastodon-transient-poll-choice) + ("2" "" "2" :alist-key two :class mastodon-transient-poll-choice) + ("3" "" "3" :alist-key three :class mastodon-transient-poll-choice) + ("4" "" "4" :alist-key four :class mastodon-transient-poll-choice)] ;; TODO: display the max number of options or add options cmd ["Update" ("C-c C-c" "Save and done" mastodon-create-poll-done) @@ -311,8 +311,18 @@ CONS is a cons of the form \"(fields.1.name . val)\"." ()) (cl-defmethod transient-init-value ((obj mastodon-transient-poll-bool)) + "Initialize OBJ, a poll option. +Pull value from `tp-transient-settings' if possible.'" + (let ((key (oref obj alist-key))) + (oset obj value + (alist-get key tp-transient-settings)))) + +(defclass mastodon-transient-poll-choice (tp-option-str) + ()) + +(cl-defmethod transient-init-value ((obj mastodon-transient-poll-choice)) "Initialize OBJ, an expiry option. -Pull value from `mastodon-tool-poll' if possible.'" +Pull value from `tp-transient-settings' if possible.'" (let ((key (oref obj alist-key))) (oset obj value (alist-get key tp-transient-settings)))) @@ -322,7 +332,7 @@ Pull value from `mastodon-tool-poll' if possible.'" (cl-defmethod transient-init-value ((obj mastodon-transient-expiry)) "Initialize OBJ, an expiry option. -Pull value from `mastodon-tool-poll' if possible.'" +Pull value from `tp-transient-settings' if possible.'" (oset obj value (alist-get 'expiry tp-transient-settings))) -- cgit v1.2.3 From 8c77159f4f33d37e5c443d780a5cd88ccb545235 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Fri, 25 Oct 2024 19:29:58 +0200 Subject: factor out poll classes subbing off tp-option-var. FIX #601 --- lisp/mastodon-transient.el | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'lisp') diff --git a/lisp/mastodon-transient.el b/lisp/mastodon-transient.el index 38da285..67ea667 100644 --- a/lisp/mastodon-transient.el +++ b/lisp/mastodon-transient.el @@ -278,6 +278,18 @@ the format fields.X.keyname." "An infix option class for our options. We always read.") +(defclass mastodon-transient-opt (tp-option tp-option-var) + (())) + +(defclass mastodon-transient-poll-bool (tp-bool tp-option-var) + ()) + +(defclass mastodon-transient-poll-choice (tp-option-str tp-option-var) + ()) + +(defclass mastodon-transient-expiry (tp-option tp-option-var) + ()) + (cl-defmethod transient-init-value ((obj mastodon-transient-field)) "Initialize value of OBJ." (let* ((prefix-val (oref transient--prefix value))) @@ -307,35 +319,6 @@ CONS is a cons of the form \"(fields.1.name . val)\"." (not (equal (cdr cons) (alist-get server-key server-elt nil nil #'string=))))) -(defclass mastodon-transient-poll-bool (tp-bool) - ()) - -(cl-defmethod transient-init-value ((obj mastodon-transient-poll-bool)) - "Initialize OBJ, a poll option. -Pull value from `tp-transient-settings' if possible.'" - (let ((key (oref obj alist-key))) - (oset obj value - (alist-get key tp-transient-settings)))) - -(defclass mastodon-transient-poll-choice (tp-option-str) - ()) - -(cl-defmethod transient-init-value ((obj mastodon-transient-poll-choice)) - "Initialize OBJ, an expiry option. -Pull value from `tp-transient-settings' if possible.'" - (let ((key (oref obj alist-key))) - (oset obj value - (alist-get key tp-transient-settings)))) - -(defclass mastodon-transient-expiry (tp-option) - ()) - -(cl-defmethod transient-init-value ((obj mastodon-transient-expiry)) - "Initialize OBJ, an expiry option. -Pull value from `tp-transient-settings' if possible.'" - (oset obj value - (alist-get 'expiry tp-transient-settings))) - (cl-defmethod transient-infix-read ((_obj mastodon-transient-expiry)) "Reader function for OBJ, a poll expiry." (cdr (mastodon-toot--read-poll-expiry))) -- cgit v1.2.3