aboutsummaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
Diffstat (limited to 'lisp')
-rw-r--r--lisp/mastodon-http.el1
-rw-r--r--lisp/mastodon-toot.el13
-rw-r--r--lisp/mastodon-transient.el91
3 files changed, 60 insertions, 45 deletions
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el
index c1b0599..c0402f0 100644
--- a/lisp/mastodon-http.el
+++ b/lisp/mastodon-http.el
@@ -319,7 +319,6 @@ Optionally specify the PARAMS to send."
("Accept" . "application/json"))))
(url-request-extra-headers
(append url-request-extra-headers headers)))
- (message "Data: %s" url-request-data)
(mastodon-http--url-retrieve-synchronously url))))
;; Asynchronous functions
diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el
index 8dfee74..9aeb790 100644
--- a/lisp/mastodon-toot.el
+++ b/lisp/mastodon-toot.el
@@ -1673,7 +1673,7 @@ REPLY-TEXT is the text of the toot being replied to."
'read-only "Edit your message below."
'toot-post-header t))
;; allow us to enter text after read-only header:
- (propertize "\n"
+ (propertize "\n\n"
'rear-nonsticky t))))
(defun mastodon-toot--most-restrictive-visibility (reply-visibility)
@@ -1686,22 +1686,17 @@ The default is given by `mastodon-toot--default-reply-visibility'."
mastodon-toot--default-reply-visibility
reply-visibility))))
-(defun mastodon-toot--fill-buffer ()
- "Mark buffer, call `fill-region'."
- (mark-whole-buffer) ; lisp code should not set mark
- ;; (fill-region (point-min) (point-max)) ; but this doesn't work
- (fill-region (region-beginning) (region-end)))
-
(defun mastodon-toot--render-reply-region-str (str)
"Refill STR and prefix all lines with >, as reply-quote text."
(with-temp-buffer
(insert str)
;; unfill first:
(let ((fill-column (point-max)))
- (mastodon-toot--fill-buffer))
+ (fill-region (point-min) (point-max)))
;; then fill:
- (mastodon-toot--fill-buffer)
+ (fill-region (point-min) (point-max))
;; add our own prefix, pauschal:
+ (goto-char (point-min))
(save-match-data
(while (re-search-forward "^" nil t)
(replace-match " > ")))
diff --git a/lisp/mastodon-transient.el b/lisp/mastodon-transient.el
index abb845a..fe70eac 100644
--- a/lisp/mastodon-transient.el
+++ b/lisp/mastodon-transient.el
@@ -28,6 +28,9 @@
;;; UTILS
+;; some JSON fields that are returned under the "source" field need to be
+;; sent back in the format source[key], while some others are sent kust as
+;; key:
(defun mastodon-transient-parse-source-key (key)
"Parse mastodon source KEY.
If KEY needs to be source[key], format like so, else just return
@@ -54,22 +57,23 @@ the inner key part."
;; fields utils:
;; to PATCH fields, we just need fields[x][name] and fields[x][value]
-(defun mastodon-transient-fields-to-transient (fields)
- "Convert fields in FIELDS to transient key=val args."
- (flatten-tree
- (cl-loop
- for f in fields
- for count from 1 to 5
- collect
- (cl-loop for x in f
- collect
- (concat "fields." (number-to-string count)
- "." (symbol-name (car x))
- "=" (cdr x))))))
+(defun mastodon-transient--fields-alist (fields)
+ "Convert fields in FIELDS to numbered conses.
+The keys in the data are not numbered, so we convert the key into
+the format fields.X.keyname."
+ (cl-loop
+ for f in fields
+ for count from 1 to 5
+ collect
+ (cl-loop for x in f
+ collect
+ (cons (concat "fields." (number-to-string count)
+ "." (symbol-name (car x)))
+ (cdr x)))))
(defun mastodon-transient-field-dot-to-array (key)
"Convert KEY from tp dot annotation to array[key] annotation."
- (tp-dot-to-array key nil "_attributes"))
+ (tp-dot-to-array (symbol-name key) nil "_attributes"))
(defun mastodon-transient-dot-fields-to-arrays (alist)
"Parse fields ALIST in dot notation to array notation."
@@ -86,11 +90,9 @@ the inner key part."
(transient-define-suffix mastodon-user-settings-update (&optional args)
"Update current user settings on the server."
:transient 'transient--do-exit
- ;; interactive receives args from the prefix:
(interactive (list (transient-args 'mastodon-user-settings)))
- (let* ((only-changed (tp-only-changed-args args))
- (arrays (tp-dots-to-arrays only-changed))
- (strs (tp-bools-to-strs arrays)) ;; we can't PATCH json
+ (let* ((parsed (tp-parse-args-for-send args :strings))
+ (strs (mastodon-transient-parse-source-keys parsed))
(url (mastodon-http--api "accounts/update_credentials"))
(resp (mastodon-http--patch url strs))) ;; :json fails
(mastodon-http--triage
@@ -148,25 +150,26 @@ the inner key part."
"Update current user profile fields."
:transient 'transient--do-return
(interactive (list (transient-args 'mastodon-profile-fields)))
- (let* ((alist (tp-transient-to-alist args))
- ;; FIXME: maybe only changed also won't work with fields, as
+ (let* (;; FIXME: maybe only changed also won't work with fields, as
;; perhaps what is PATCHed overwrites whatever is on the server?
;; (only-changed (tp-only-changed-args alist))
- (arrays (mastodon-transient-dot-fields-to-arrays alist))
+ (arrays (mastodon-transient-dot-fields-to-arrays args))
(endpoint "accounts/update_credentials")
(url (mastodon-http--api endpoint))
(resp (mastodon-http--patch url arrays))) ; :json)))
(mastodon-http--triage
resp (lambda (_) (message "Fields updated!")))))
+(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)))
+
(transient-define-prefix mastodon-profile-fields ()
"A transient for setting profile fields."
:value
- (lambda ()
- (tp-return-data #'mastodon-transient-get-creds nil 'fields)
- (setq tp-server-settings
- (mastodon-transient-fields-to-transient
- tp-server-settings)))
+ (lambda () (mastodon-transient-fetch-fields))
[:description
"Fields"
["Name"
@@ -194,16 +197,34 @@ the inner key part."
"An infix option class for our options.
We always read.")
-(cl-defmethod tp-arg-changed-p ((_obj mastodon-transient-field) pair)
- "T if value of OBJ is changed.
-PAIR is a transient arg of the form \"fields.1.name=val\"."
- (let* ((pair-split (split-string pair "="))
- (keys-split (split-string (car pair-split) "\\."))
- (num (1- (string-to-number (nth 1 keys-split))))
- (server-key (intern (nth 2 keys-split)))
- (server-elt (nth num tp-server-settings))
- (value (when pair (cadr pair-split))))
- (not (equal value (alist-get server-key server-elt)))))
+(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)))
+ (oset obj value
+ (tp-get-server-val obj prefix-val))))
+
+(cl-defmethod tp-get-server-val ((obj mastodon-transient-field) data)
+ "Return the server value for OBJ from DATA.
+If OBJ's key has dotted notation, drill down into the alist. Currently
+only one level of nesting is supported."
+ ;; TODO: handle nested alist keys
+ (let* ((key (oref obj alist-key))
+ (split (split-string (symbol-name key) "\\."))
+ (num (string-to-number (cadr split))))
+ (alist-get key
+ (nth (1- num) data) nil nil #'string=)))
+
+(cl-defmethod tp-arg-changed-p ((_obj mastodon-transient-field) cons)
+ "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)) "\\."))
+ (num (1- (string-to-number (nth 1 key-split))))
+ (server-key (symbol-name (car cons)))
+ (server-elt (nth num tp-server-settings)))
+ (not (equal (cdr cons)
+ (alist-get server-key server-elt nil nil #'string=)))))
(provide 'mastodon-transient)
;;; mastodon-transient.el ends here