diff options
author | Johnson Denen <johnson.denen@gmail.com> | 2017-04-20 11:41:41 -0400 |
---|---|---|
committer | Johnson Denen <johnson.denen@gmail.com> | 2017-04-20 12:30:02 -0400 |
commit | df6320f5174e610f1eb281ff619fd88a5fb1ba6a (patch) | |
tree | 2735f2289ae25436ea6172c38cfa590fce8cd5de | |
parent | 9a705f3884168bf9ac0708b64f333b3893f0d0d8 (diff) |
Close #11 with new callbacks
-rw-r--r-- | lisp/mastodon-http.el | 7 | ||||
-rw-r--r-- | lisp/mastodon-toot.el | 37 |
2 files changed, 24 insertions, 20 deletions
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index 50b560f..92c876f 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -108,6 +108,13 @@ If response code is not 2XX, switches to the response buffer created by `url-ret (funcall success) (switch-to-buffer (current-buffer)))) +(defun mastodon-http--triage (response success) + (let ((status (with-current-buffer response + (mastodon--response-code)))) + (if (string-prefix-p "2" status) + (funcall success) + (switch-to-buffer response)))) + (defun mastodon-http--post (url args headers) "POST synchronously to URL with ARGS and HEADERS. diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 93980f0..13e23b7 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -44,24 +44,7 @@ STATUS is passed by `url-retrieve'." (mastodon--http-response-triage status (lambda () (switch-to-buffer (current-buffer))))) ;; FIXME -(defun mastodon-toot-send () - "Kill new-toot buffer/window and POST contents to the Mastodon instance." - (interactive) - (let* ((toot (buffer-string)) - (endpoint (mastodon--api-for "statuses")) - (args `(("status" . ,toot) - ("in_reply_to_id" . ,mastodon-toot--reply-to-id)))) - (progn - (kill-buffer-and-window) - (setq mastodon-toot--reply-to-id nil) - (mastodon--http-post endpoint - 'mastodon-toot--send-triage - args - `(("Authorization" . ,(concat - "Bearer " - (mastodon--access-token)))))))) - -(defun mastodon-toot-cancel () +(defun mastodon-toot--cancel () "Kill new-toot buffer/window. Does not POST content to Mastodon." (interactive) (setq mastodon-toot--reply-to-id nil) @@ -94,6 +77,20 @@ Execute CALLBACK function if response was OK." (let ((response (mastodon-http--post url nil nil))) (mastodon-toot--action-triage response callback)))) +(defun mastodon-toot--send () + "Kill new-toot buffer/window and POST contents to the Mastodon instance." + (interactive) + (let* ((toot (buffer-string)) + (endpoint (mastodon--api-for "statuses")) + (args `(("status" . ,toot) + ("in_reply_to_id" . ,mastodon-toot--reply-to-id)))) + (progn + (kill-buffer-and-window) + (setq mastodon-toot--reply-to-id nil) + (let ((response (mastodon-http--post endpoint args nil))) + (mastodon-http--triage response + (lambda () (message "Toot toot!"))))))) + (defun mastodon-toot--boost () "Boost toot at `point'." (interactive) @@ -121,8 +118,8 @@ Execute CALLBACK function if response was OK." (defvar mastodon-toot-mode-map (let ((map (make-sparse-keymap))) - (define-key map (kbd "C-c C-c") #'mastodon-toot-send) - (define-key map (kbd "C-c C-k") #'mastodon-toot-cancel) + (define-key map (kbd "C-c C-c") #'mastodon-toot--send) + (define-key map (kbd "C-c C-k") #'mastodon-toot--cancel) map) "Keymap for `mastodon-toot-mode'.") |