From 7672a0240b45675c1fde0ca960fea56c02ecc4ab Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 31 Aug 2022 08:49:25 +0200 Subject: http--api in -profile.el --- lisp/mastodon-profile.el | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 516059e..9c28703 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -212,8 +212,7 @@ JSON is the data returned by the server." (defun mastodon-profile--update-user-profile-note () "Fetch user's profile note and display for editing." (interactive) - (let* ((url (concat mastodon-instance-url - "/api/v1/accounts/update_credentials")) + (let* ((url (mastodon-http--api "accounts/update_credentials")) ;; (buffer (mastodon-http--patch url)) (json (mastodon-http--patch-json url)) (source (alist-get 'source json)) @@ -236,8 +235,7 @@ JSON is the data returned by the server." "Send PATCH request with the updated profile note." (interactive) (let* ((note (buffer-substring-no-properties (point-min) (point-max))) - (url (concat mastodon-instance-url - "/api/v1/accounts/update_credentials"))) + (url (mastodon-http--api "accounts/update_credentials"))) (kill-buffer-and-window) (let ((response (mastodon-http--patch url note))) (mastodon-http--triage response -- cgit v1.2.3 From e670268cc5dbaf1f4b0605a79ec63422bda03c3a Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 31 Aug 2022 08:49:41 +0200 Subject: view brief prefs fun --- lisp/mastodon-profile.el | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 9c28703..0496d53 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -241,6 +241,26 @@ JSON is the data returned by the server." (mastodon-http--triage response (lambda () (message "Profile note updated!")))))) +(defun mastodon-profile-view-preferences () + "View user preferences in another window." + (interactive) + (let* ((url (mastodon-http--api "preferences")) + (response (mastodon-http--get-json url)) + (buf (get-buffer-create "*mastodon-preferences*"))) + (with-current-buffer buf + (switch-to-buffer-other-window buf) + (erase-buffer) + (special-mode) + (let ((inhibit-read-only t)) + (while response + (let ((el (pop response))) + (insert + (format "%-30s %s" + (prin1-to-string (car el)) + (prin1-to-string (cdr el))) + "\n\n")))) + (goto-char (point-min))))) + (defun mastodon-profile--relationships-get (id) "Fetch info about logged-in user's relationship to user with id ID." (let* ((their-id id) -- cgit v1.2.3 From 59a1fbda1f6c3d8cc20714eb000e05bee7c5310e Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 31 Aug 2022 12:44:52 +0200 Subject: http--patch: make general function, not just for note - this way we can build funs to change basic account options. --- lisp/mastodon-http.el | 19 ++++++++----------- lisp/mastodon-profile.el | 2 +- 2 files changed, 9 insertions(+), 12 deletions(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index 49b2375..9904232 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -168,7 +168,7 @@ Pass response buffer to CALLBACK function." (defun mastodon-http--append-query-string (url params) "Append PARAMS to URL as query strings and return it. -PARAMS should be an alist as required `url-build-query-string'." +PARAMS should be an alist as required by `url-build-query-string'." (let ((query-string (url-build-query-string params))) (concat url "?" query-string))) @@ -204,21 +204,18 @@ PARAM is a formatted request parameter, eg 'following=true'." ;; profile update functions -(defun mastodon-http--patch-json (url) - "Make synchronous PATCH request to URL. Return JSON response." - (with-current-buffer (mastodon-http--patch url) +(defun mastodon-http--patch-json (url &optional params) + "Make synchronous PATCH request to URL. Return JSON response. +Optionally specify the PARAMS to send." + (with-current-buffer (mastodon-http--patch url params) (mastodon-http--process-json))) -;; hard coded just for bio note for now: -(defun mastodon-http--patch (base-url &optional note) +(defun mastodon-http--patch (base-url &optional params) "Make synchronous PATCH request to BASE-URL. -Optionally specify the NOTE to edit. -Pass response buffer to CALLBACK function." +Optionally specify the PARAMS to send." (mastodon-http--authorized-request "PATCH" - (let ((url (if note - (concat base-url "?note=" (url-hexify-string note)) - base-url))) + (let ((url (mastodon-http--append-query-string base-url params))) (mastodon-http--url-retrieve-synchronously url)))) ;; Asynchronous functions diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 0496d53..a27afd2 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -237,7 +237,7 @@ JSON is the data returned by the server." (let* ((note (buffer-substring-no-properties (point-min) (point-max))) (url (mastodon-http--api "accounts/update_credentials"))) (kill-buffer-and-window) - (let ((response (mastodon-http--patch url note))) + (let ((response (mastodon-http--patch url `((note ,note))))) (mastodon-http--triage response (lambda () (message "Profile note updated!")))))) -- cgit v1.2.3 From 7b48b973bfccab713af5938cbf6124984d4a7b25 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 31 Aug 2022 14:20:13 +0200 Subject: get and set account preference functions --- lisp/mastodon-profile.el | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index a27afd2..6fe3e14 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -207,13 +207,25 @@ JSON is the data returned by the server." 'byline t 'toot-id "0")) (mastodon-search--insert-users-propertized json :note))) - ;; (mastodon-profile--add-author-bylines json))) +;; (mastodon-profile--add-author-bylines json))) + +;;; account preferences + +(defun mastodon-profile--get-source-prefs () + "Return the \"source\" preferences from the server." + (let* ((url (mastodon-http--api "accounts/verify_credentials")) + (response (mastodon-http--get-json url))) + (alist-get 'source response))) + +(defun mastodon-profile--get-source-pref (pref) + "Return account PREF erence from the \"source\" section on the server." + (let ((source (mastodon-profile--get-source-prefs))) + (alist-get pref source))) (defun mastodon-profile--update-user-profile-note () "Fetch user's profile note and display for editing." (interactive) (let* ((url (mastodon-http--api "accounts/update_credentials")) - ;; (buffer (mastodon-http--patch url)) (json (mastodon-http--patch-json url)) (source (alist-get 'source json)) (note (alist-get 'note source)) @@ -241,6 +253,16 @@ JSON is the data returned by the server." (mastodon-http--triage response (lambda () (message "Profile note updated!")))))) +(defun mastodon-profile--update-preference (pref val &optional source) + "Update a single acount PREF erence to setting VAL. +SOURCE means that the preference is in the 'source' part of the account json." + (let* ((url (mastodon-http--api "accounts/update_credentials")) + (pref (if source (concat "source[" pref "]") pref)) + (response (mastodon-http--patch url `((,pref ,val))))) + (mastodon-http--triage response + (lambda () + (message "Account setting %s updated!" pref))))) + (defun mastodon-profile-view-preferences () "View user preferences in another window." (interactive) -- cgit v1.2.3 From 81bc0978fd36a17d2e0fba4123754ccc2af9035d Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 31 Aug 2022 15:10:52 +0200 Subject: set default visibility fun --- lisp/mastodon-profile.el | 9 +++++++++ lisp/mastodon-toot.el | 4 ++++ 2 files changed, 13 insertions(+) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 6fe3e14..0c91556 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -70,6 +70,7 @@ (defvar mastodon-tl--buffer-spec) (defvar mastodon-tl--update-point) (defvar mastodon-mode-map) +(defvar mastodon-toot-visibility-list) (defvar-local mastodon-profile--account nil "The data for the account being described in the current profile buffer.") @@ -263,6 +264,14 @@ SOURCE means that the preference is in the 'source' part of the account json." (lambda () (message "Account setting %s updated!" pref))))) +(defun mastodon-profile-set-default-toot-visibility () + "Set the default visibility for toots." + (interactive) + (let ((vis (completing-read "Set default visibility to:" + mastodon-toot-visibility-list + nil t))) + (mastodon-profile--update-preference "privacy" vis :source))) + (defun mastodon-profile-view-preferences () "View user preferences in another window." (interactive) diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 253fb5d..8d8b9f3 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -126,6 +126,10 @@ This is only used if company mode is installed." (defvar-local mastodon-toot--content-nsfw nil "A flag indicating whether the toot should be marked as NSFW.") +(defvar mastodon-toot-visibility-list + '(direct private unlisted public) + "A list of the available toot visibility settings.") + (defvar-local mastodon-toot--visibility "public" "A string indicating the visibility of the toot being composed. -- cgit v1.2.3 From 4d9073299485d3dc6095f824ae017b7d35bd0c1d Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 31 Aug 2022 16:32:26 +0200 Subject: profile: refactor get-source-prefs with get-json-value --- lisp/mastodon-profile.el | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 0c91556..1fc4d09 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -212,11 +212,15 @@ JSON is the data returned by the server." ;;; account preferences -(defun mastodon-profile--get-source-prefs () - "Return the \"source\" preferences from the server." +(defun mastodon-profile--get-json-value (val) + "Fetch current VAL ue from account." (let* ((url (mastodon-http--api "accounts/verify_credentials")) (response (mastodon-http--get-json url))) - (alist-get 'source response))) + (alist-get val response))) + +(defun mastodon-profile--get-source-prefs () + "Return the \"source\" preferences from the server." + (mastodon-profile--get-json-value 'source)) (defun mastodon-profile--get-source-pref (pref) "Return account PREF erence from the \"source\" section on the server." -- cgit v1.2.3 From 6fe9d764d568c2ba001a932305f5d926c2f63c25 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 31 Aug 2022 16:32:50 +0200 Subject: toggle account locked status --- lisp/mastodon-profile.el | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 1fc4d09..284bae6 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -260,6 +260,7 @@ JSON is the data returned by the server." (defun mastodon-profile--update-preference (pref val &optional source) "Update a single acount PREF erence to setting VAL. +Both args are strings. SOURCE means that the preference is in the 'source' part of the account json." (let* ((url (mastodon-http--api "accounts/update_credentials")) (pref (if source (concat "source[" pref "]") pref)) @@ -268,6 +269,17 @@ SOURCE means that the preference is in the 'source' part of the account json." (lambda () (message "Account setting %s updated!" pref))))) +(defun mastodon-profile-account-locked-toggle () + "Toggle the locked status of the user's account. +Locked accounts mean follow requests have to be manually approved." + (interactive) + (let ((locked-p (mastodon-profile--get-json-value 'locked))) + (if (not (equal locked-p :json-false)) + (when (y-or-n-p "Account is locked to new followers. Unlock?") + (mastodon-profile--update-preference "locked" "false")) + (when (y-or-n-p "Account is not locked to new followers. Lock it?") + (mastodon-profile--update-preference "locked" "true"))))) + (defun mastodon-profile-set-default-toot-visibility () "Set the default visibility for toots." (interactive) -- cgit v1.2.3 From a30b8353d122a753be6fcf38fe84f7da99ad2ebd Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 31 Aug 2022 16:46:12 +0200 Subject: refactor locked toggle, add discoverable toggle --- lisp/mastodon-profile.el | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 284bae6..5c4adaa 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -273,12 +273,23 @@ SOURCE means that the preference is in the 'source' part of the account json." "Toggle the locked status of the user's account. Locked accounts mean follow requests have to be manually approved." (interactive) - (let ((locked-p (mastodon-profile--get-json-value 'locked))) - (if (not (equal locked-p :json-false)) - (when (y-or-n-p "Account is locked to new followers. Unlock?") - (mastodon-profile--update-preference "locked" "false")) - (when (y-or-n-p "Account is not locked to new followers. Lock it?") - (mastodon-profile--update-preference "locked" "true"))))) + (mastodon-profile--toggle-account-key 'locked)) + +(defun mastodon-profile-account-discoverable-toggle () + "Toggle the discoverable status of the user's account. +Discoverable means the account is listed in the server directory." + (interactive) + (mastodon-profile--toggle-account-key 'discoverable)) + +(defun mastodon-profile--toggle-account-key (key) + "Toggle the boolean account setting KEY." + (let* ((val (mastodon-profile--get-json-value key)) + (prompt (format "Account setting %s is %s. Toggle?" key val))) + (if (not (equal val :json-false)) + (when (y-or-n-p prompt) + (mastodon-profile--update-preference (symbol-name key) "false")) + (when (y-or-n-p prompt) + (mastodon-profile--update-preference (symbol-name key) "true"))))) (defun mastodon-profile-set-default-toot-visibility () "Set the default visibility for toots." -- cgit v1.2.3 From 7e98dae4756b62b34a2526ef6c776258db4c5202 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 31 Aug 2022 21:44:26 +0200 Subject: improve set default-toot-visibility formatting --- lisp/mastodon-profile.el | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 5c4adaa..c43906f 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -263,11 +263,11 @@ JSON is the data returned by the server." Both args are strings. SOURCE means that the preference is in the 'source' part of the account json." (let* ((url (mastodon-http--api "accounts/update_credentials")) - (pref (if source (concat "source[" pref "]") pref)) - (response (mastodon-http--patch url `((,pref ,val))))) + (pref-formatted (if source (concat "source[" pref "]") pref)) + (response (mastodon-http--patch url `((,pref-formatted ,val))))) (mastodon-http--triage response (lambda () - (message "Account setting %s updated!" pref))))) + (message "Account setting %s updated to %s!" pref val))))) (defun mastodon-profile-account-locked-toggle () "Toggle the locked status of the user's account. -- cgit v1.2.3 From 5142c83a75dd5728d11beba1711ae1f1d53cb423 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Thu, 1 Sep 2022 09:27:50 +0200 Subject: edit display_name fun --- lisp/mastodon-profile.el | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index c43906f..116af5d 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -270,13 +270,13 @@ SOURCE means that the preference is in the 'source' part of the account json." (message "Account setting %s updated to %s!" pref val))))) (defun mastodon-profile-account-locked-toggle () - "Toggle the locked status of the user's account. + "Toggle the locked status of your account. Locked accounts mean follow requests have to be manually approved." (interactive) (mastodon-profile--toggle-account-key 'locked)) (defun mastodon-profile-account-discoverable-toggle () - "Toggle the discoverable status of the user's account. + "Toggle the discoverable status of your account. Discoverable means the account is listed in the server directory." (interactive) (mastodon-profile--toggle-account-key 'discoverable)) @@ -291,6 +291,19 @@ Discoverable means the account is listed in the server directory." (when (y-or-n-p prompt) (mastodon-profile--update-preference (symbol-name key) "true"))))) +(defun mastodon-profile--edit-account-string (key) + "Edit the string for account setting KEY." + (let* ((val (mastodon-profile--get-json-value key)) + (new-val + (read-string (format "Edit account setting %s: " key) + val))) + (mastodon-profile--update-preference (symbol-name key) new-val))) + +(defun mastodon-profile-update-display-name () + "Update display name for your account." + (interactive) + (mastodon-profile--edit-account-string 'display_name)) + (defun mastodon-profile-set-default-toot-visibility () "Set the default visibility for toots." (interactive) -- cgit v1.2.3 From f6a2a14066cae107deab1805b8e5ff99d28c5125 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Thu, 1 Sep 2022 14:13:34 +0200 Subject: move set-default-toot-visibility to -toot.el --- lisp/mastodon-profile.el | 9 --------- lisp/mastodon-toot.el | 9 +++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 116af5d..25edfba 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -70,7 +70,6 @@ (defvar mastodon-tl--buffer-spec) (defvar mastodon-tl--update-point) (defvar mastodon-mode-map) -(defvar mastodon-toot-visibility-list) (defvar-local mastodon-profile--account nil "The data for the account being described in the current profile buffer.") @@ -304,14 +303,6 @@ Discoverable means the account is listed in the server directory." (interactive) (mastodon-profile--edit-account-string 'display_name)) -(defun mastodon-profile-set-default-toot-visibility () - "Set the default visibility for toots." - (interactive) - (let ((vis (completing-read "Set default visibility to:" - mastodon-toot-visibility-list - nil t))) - (mastodon-profile--update-preference "privacy" vis :source))) - (defun mastodon-profile-view-preferences () "View user preferences in another window." (interactive) diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 8d8b9f3..d7efd44 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -72,6 +72,7 @@ (autoload 'mastodon-tl--toot-id "mastodon-tl") (autoload 'mastodon-toot "mastodon") (autoload 'mastodon-profile--get-source-pref "mastodon-profile") +(autoload 'mastodon-profile--update-preference "mastodon-profile") ;; for mastodon-toot--translate-toot-text (autoload 'mastodon-tl--content "mastodon-tl") @@ -164,6 +165,14 @@ This may be set by the account setting on the server.") map) "Keymap for `mastodon-toot'.") +(defun mastodon-toot-set-default-visibility () + "Set the default visibility for toots on the server." + (interactive) + (let ((vis (completing-read "Set default visibility to:" + mastodon-toot-visibility-list + nil t))) + (mastodon-profile--update-preference "privacy" vis :source))) + (defun mastodon-toot--get-max-toot-chars () "Fetch max_toot_chars from `mastodon-instance-url' asynchronously." (mastodon-http--get-json-async -- cgit v1.2.3 From 6d3e9cf18a74045e17f8e5c1fc6e20a8c40f497f Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Thu, 1 Sep 2022 17:03:47 +0200 Subject: cl-reduce for metadata fields format --- lisp/mastodon-profile.el | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 25edfba..aa5ec2e 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -34,6 +34,7 @@ ;;; Code: (require 'seq) +(require 'cl-lib) (autoload 'mastodon-http--api "mastodon-http.el") (autoload 'mastodon-http--get-json "mastodon-http.el") @@ -344,7 +345,8 @@ Returns a list of lists." (defun mastodon-profile--fields-insert (fields) "Format and insert field pairs (a.k.a profile metadata) in FIELDS." (let* ((car-fields (mapcar 'car fields)) - (left-width (car (sort (mapcar 'length car-fields) '>)))) + (left-width (cl-reduce + #'max (mapcar 'length car-fields)))) (mapconcat (lambda (field) (mastodon-tl--render-text (concat -- cgit v1.2.3 From 82aac0a4b33eb54d510bcf19c162213682e1dea8 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Fri, 2 Sep 2022 09:42:36 +0200 Subject: use # for quoted functions --- lisp/mastodon-profile.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lisp/mastodon-profile.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index aa5ec2e..00ffedd 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -344,9 +344,9 @@ Returns a list of lists." (defun mastodon-profile--fields-insert (fields) "Format and insert field pairs (a.k.a profile metadata) in FIELDS." - (let* ((car-fields (mapcar 'car fields)) + (let* ((car-fields (mapcar #'car fields)) (left-width (cl-reduce - #'max (mapcar 'length car-fields)))) + #'max (mapcar #'length car-fields)))) (mapconcat (lambda (field) (mastodon-tl--render-text (concat -- cgit v1.2.3