diff options
author | mousebot <mousebot@riseup.net> | 2021-06-05 12:43:19 +0200 |
---|---|---|
committer | mousebot <mousebot@riseup.net> | 2021-06-05 12:43:19 +0200 |
commit | a32a239d7e64d46df8e76c712a2335cecf8776e9 (patch) | |
tree | 8835e5713b81b50b96ef99f71636e67b06ab4f69 /lisp | |
parent | 21bbab0885097ce7e47cbb88f9214c1f507adf87 (diff) |
implement updating user profile note
uses a tiny minor mode 'profile-update', with bindings.
U is a general binding for it.
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/mastodon-http.el | 25 | ||||
-rw-r--r-- | lisp/mastodon-profile.el | 69 | ||||
-rw-r--r-- | lisp/mastodon.el | 4 |
3 files changed, 89 insertions, 9 deletions
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index e85429f..678e628 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -137,7 +137,7 @@ Pass response buffer to CALLBACK function." (with-temp-buffer (url-retrieve-synchronously url)))) -;; http functions for search: +;; search functions: (defun mastodon-http--process-json-search () "Process JSON returned by a search query to the server." (goto-char (point-min)) @@ -168,6 +168,29 @@ Pass response buffer to CALLBACK function." (url-retrieve-synchronously url) (url-retrieve-synchronously url nil nil mastodon-http--timeout)))) +;; 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) + (mastodon-http--process-json))) + +;; hard coded just for bio note for now: +(defun mastodon-http--patch (base-url &optional note) + "Make synchronous PATCH request to URL. + +Pass response buffer to CALLBACK function." + (let ((url-request-method "PATCH") + (url (if note + (concat base-url "?note=" (url-hexify-string note)) + base-url)) + (url-request-extra-headers + `(("Authorization" . ,(concat "Bearer " + (mastodon-auth--access-token)))))) + (if (< (cdr (func-arity 'url-retrieve-synchronously)) 4) + (url-retrieve-synchronously url) + (url-retrieve-synchronously url nil nil mastodon-http--timeout)))) + ;; Asynchronous functions (defun mastodon-http--get-async (url &optional callback &rest cbargs) diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 84664c0..0b7ecc4 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -53,15 +53,28 @@ (autoload 'mastodon-tl--toot-id "mastodon-tl") (autoload 'mastodon-tl--toot "mastodon-tl") (autoload 'mastodon-tl--init "mastodon-tl.el") +(autoload 'mastodon-http--patch "mastodon-http") +(autoload 'mastodon-http--patch-json "mastodon-http") (defvar mastodon-instance-url) (defvar mastodon-tl--buffer-spec) (defvar mastodon-tl--update-point) + (defvar mastodon-profile--account nil "The data for the account being described in the current profile buffer.") (make-variable-buffer-local 'mastodon-profile--account) +;; this way you can update it with C-M-x: +(defvar mastodon-profile-mode-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "O") #'mastodon-profile--open-followers) + (define-key map (kbd "o") #'mastodon-profile--open-following) + (define-key map (kbd "a") #'mastodon-profile--follow-request-accept) + (define-key map (kbd "r") #'mastodon-profile--follow-request-reject) + map) + "Keymap for `mastodon-profile-mode'.") + (define-minor-mode mastodon-profile-mode "Toggle mastodon profile minor mode. @@ -70,12 +83,26 @@ extra keybindings." :init-value nil ;; The mode line indicator. :lighter " Profile" - ;; The key bindings - :keymap '(((kbd "O") . mastodon-profile--open-followers) - ((kbd "o") . mastodon-profile--open-following) - ((kbd "a") . mastodon-profile--follow-request-accept) - ((kbd "r") . mastodon-profile--follow-request-reject)) - :group 'mastodon) + :keymap mastodon-profile-mode-map + ;; :keymap '(((kbd "O") . mastodon-profile--open-followers) + ;; ((kbd "o") . mastodon-profile--open-following) + ;; ((kbd "a") . mastodon-profile--follow-request-accept) + ;; ((kbd "r") . mastodon-profile--follow-request-reject) + :group 'mastodon + :global nil) + +(defvar mastodon-profile-update-mode-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "C-c C-c") #'mastodon-profile--user-profile-send-updated) + (define-key map (kbd "C-c C-k") #'kill-buffer-and-window) + map) + "Keymap for `mastodon-profile-update-mode'.") + +(define-minor-mode mastodon-profile-update-mode + "Minor mode to update Mastodon user profile." + :group 'mastodon-profile + :keymap mastodon-profile-update-mode-map + :global nil) (defun mastodon-profile--toot-json () "Get the next toot-json." @@ -112,6 +139,7 @@ following the current profile." (defun mastodon-profile--view-favourites () "Open a new buffer displaying the user's favourites." (interactive) + (message "Loading your favourited toots...") (mastodon-tl--init "favourites" "favourites" 'mastodon-tl--timeline)) @@ -164,6 +192,33 @@ following the current profile." name handle)))) (message "No account result at point?")))) +(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")) + ;; (buffer (mastodon-http--patch url)) + (json (mastodon-http--patch-json url)) + (source (cdr (assoc 'source json))) + (note (cdr (assoc 'note source))) + (buffer (get-buffer-create "*mastodon-update-profile*")) + (inhibit-read-only t)) + (switch-to-buffer-other-window buffer) + (mastodon-profile-update-mode t) + (insert note) + (goto-char (point-min)) + (message "Edit your profile note. C-c C-c to send, C-c C-k to cancel."))) + +(defun mastodon-profile--user-profile-send-updated () + "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"))) + (kill-buffer-and-window) + (let ((response (mastodon-http--patch url note))) + (mastodon-http--triage response + (lambda () (message "Profile note updated!")))))) (defun mastodon-profile--relationships-get (id) "Fetch info about logged-in user's relationship to user with id ID." @@ -205,7 +260,7 @@ Returns a list of lists." (format " :: %s" (cadr field))) ;; (make-string (- (+ 1 right-width) (length (cdr field))) ?_) ;; " |") - nil)) + field)) ; nil)) ; hack to make links tabstops fields ""))) (defun mastodon-profile--get-statuses-pinned (account) diff --git a/lisp/mastodon.el b/lisp/mastodon.el index 4d0b940..c1c4360 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -73,6 +73,7 @@ (autoload 'mastodon-async--stream-local "mastodon-async") (autoload 'mastodon-async--stream-home "mastodon-async") (autoload 'mastodon-async--stream-notifications "mastodon-async") +(autoload 'mastodon-profile--update-user-profile-note "mastodon-profile") (defgroup mastodon nil "Interface with Mastodon." @@ -143,7 +144,8 @@ Use. e.g. \"%c\" for your locale's date and time format." (define-key map (kbd "C-c h") #'mastodon-async--stream-home) (define-key map (kbd "C-c f") #'mastodon-async--stream-federated) (define-key map (kbd "C-c l") #'mastodon-async--stream-local) - (define-key map (kbd "C-c n") #'mastodon-async--stream-notifications) + (define-key map (kbd "C-c n") #'mastodon-async--stream-notifications) + (define-key map (kbd "U") #'mastodon-profile--update-user-profile-note) map) "Keymap for `mastodon-mode'.") |