diff options
| -rw-r--r-- | lisp/mastodon-http.el | 31 | ||||
| -rw-r--r-- | lisp/mastodon-search.el | 2 | ||||
| -rw-r--r-- | lisp/mastodon-toot.el | 83 | 
3 files changed, 60 insertions, 56 deletions
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index 4f4cc3f..d6158eb 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -256,7 +256,9 @@ Authorization header is included by default unless UNAUTHENTICED-P is non-nil."  ;; TODO: test for curl first?  (defun mastodon-http--post-media-attachment (url filename caption)    "Make POST request to upload FILENAME with CAPTION to the server's media URL. -The upload is asynchronous. On succeeding, `mastodon-toot--media-attachment-ids' is set to the id(s) of the item uploaded, and `mastodon-toot--update-status-fields' is run." +The upload is asynchronous. On succeeding, +`mastodon-toot--media-attachment-ids' is set to the id(s) of the +item uploaded, and `mastodon-toot--update-status-fields' is run."    (let* ((file (file-name-nondirectory filename))           (request-backend 'curl))           ;; (response @@ -269,14 +271,13 @@ The upload is asynchronous. On succeeding, `mastodon-toot--media-attachment-ids'              :parser 'json-read              :headers `(("Authorization" . ,(concat "Bearer "                                                     (mastodon-auth--access-token)))) -            :sync nil +            :sync t              :success (cl-function                        (lambda (&key data &allow-other-keys)                          (when data                            (progn                              (push (cdr (assoc 'id data))                                    mastodon-toot--media-attachment-ids) ; add ID to list -                            (push file mastodon-toot--media-attachment-filenames)                              (message "%s file %s with id %S and caption '%s' uploaded!"                                       (capitalize (cdr (assoc 'type data)))                                       file @@ -285,15 +286,21 @@ The upload is asynchronous. On succeeding, `mastodon-toot--media-attachment-ids'                              (mastodon-toot--update-status-fields)))))              :error (cl-function                      (lambda (&key error-thrown &allow-other-keys) -                      (message "%s" (car (last error-thrown))) -                      (message "%s" (type-of (car (last error-thrown)))) -                      (cond ((= (car (last error-thrown)) 401) -                             (message "Got error: %s Unauthorized: The access token is invalid" error-thrown)) -                            ((= (car (last error-thrown)) 422) -                             (message "Got error: %s Unprocessable entity: file or file type is unsupported or invalid" error-thrown)) -                            (t -                             (message "Got error: %s Shit went south" -                                      error-thrown)))))))) +                      (cond +                       ;; handle curl errors first (eg 26, can't read file/path) +                       ;; because the '=' test below fails for them +                       ;; they have the form (error . error message 24) +                       ((not (proper-list-p error-thrown)) ; not dotted list +			            (message "Got error: %s. Shit went south." (cdr error-thrown))) +                       ;; handle mastodon api errors +                       ;; they have the form (error http 401) +			           ((= (car (last error-thrown)) 401) +                        (message "Got error: %s Unauthorized: The access token is invalid" error-thrown)) +                       ((= (car (last error-thrown)) 422) +                        (message "Got error: %s Unprocessable entity: file or file type is unsupported or invalid" error-thrown)) +                       (t +                        (message "Got error: %s Shit went south" +                                 error-thrown))))))))  (provide 'mastodon-http)  ;;; mastodon-http.el ends here diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index ccac5e6..687b50c 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -52,7 +52,7 @@          (concat "@" (cdr (assoc 'acct account)))))  (defun mastodon-search--search-accounts-query (query) -  "Prompt for a search QUERY and return accounts. +  "Prompt for a search QUERY and return accounts synchronously.  Returns a nested list containing user handle, display name, and URL."    (interactive "sSearch mastodon for: ")    (let* ((url (format "%s/api/v1/accounts/search" mastodon-instance-url)) diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 70b95d3..dd13251 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -116,25 +116,17 @@ Valid values are \"direct\", \"private\" (followers-only), \"unlisted\", and \"p  (make-variable-buffer-local 'mastodon-toot--visibility)  (defvar mastodon-toot--media-attachments nil -  "A list of the media attachments of the toot being composed .") +  "A list of the media attachments of the toot being composed.")  (make-variable-buffer-local 'mastodon-toot--media-attachments)  (defvar mastodon-toot--media-attachment-ids nil    "A list of any media attachment ids of the toot being composed.")  (make-variable-buffer-local 'mastodon-toot--media-attachment-ids) -(defvar mastodon-toot--media-attachment-filenames nil -  "A list of any media attachment filenames of the toot being composed.") -(make-variable-buffer-local 'mastodon-toot--media-attachment-filenames) -  (defvar mastodon-toot--reply-to-id nil    "Buffer-local variable to hold the id of the toot being replied to.")  (make-variable-buffer-local 'mastodon-toot--reply-to-id) -(defvar mastodon-toot--media-attachments nil -  "Buffer-local variable to hold the list of media attachments.") -(make-variable-buffer-local 'mastodon-toot--media-attachments) -  (defvar mastodon-toot--max-toot-chars nil    "The maximum allowed characters count for a single toot.") @@ -147,19 +139,23 @@ Valid values are \"direct\", \"private\" (followers-only), \"unlisted\", and \"p      (define-key map (kbd "C-c C-v") #'mastodon-toot--change-visibility)      (when (require 'emojify nil :noerror)        (define-key map (kbd "C-c C-e") #'mastodon-toot--insert-emoji)) -    (define-key map (kbd "C-c C-u") #'mastodon-toot--upload-attached-media)      (define-key map (kbd "C-c C-a") #'mastodon-toot--attach-media)      (define-key map (kbd "C-c !") #'mastodon-toot--clear-all-attachments)      map)    "Keymap for `mastodon-toot'.")  (defun mastodon-toot--get-max-toot-chars () -  "Fetch max_toot_chars from `mastodon-instance-url'." -  (let ((instance-json (mastodon-http--get-json -                        (mastodon-http--api "instance")))) -    (setq mastodon-toot--max-toot-chars -          (number-to-string -          (cdr (assoc 'max_toot_chars instance-json)))))) +  "Fetch max_toot_chars from `mastodon-instance-url' asynchronously." +  (mastodon-http--get-json-async +   (mastodon-http--api "instance") 'mastodon-toot--get-max-toot-chars-callback)) + +(defun mastodon-toot--get-max-toot-chars-callback (json-response) +  "Set max_toot_chars returned in JSON-RESPONSE." +  (setq mastodon-toot--max-toot-chars +        (number-to-string +         (cdr (assoc 'max_toot_chars json-response)))) +  (with-current-buffer "*new toot*" +    (mastodon-toot--update-status-fields)))  (defun mastodon-toot--action-success (marker byline-region remove)    "Insert/remove the text MARKER with 'success face in byline. @@ -378,9 +374,11 @@ Remove MARKER if REMOVE is non-nil, otherwise add it."    (message "Visibility set to %s" visibility))  (defun mastodon-toot--send () -  "Kill new-toot buffer/window and POST contents to the Mastodon instance. - -If media items have been uploaded with `mastodon-toot--add-media-attachment', attach them to the toot." +  "POST contents of new-toot buffer to Mastodon instance and kill buffer. +If media items have been attached with +`mastodon-toot--attach-media', upload them with +`mastodon-toot-upload-attached-media' and attach them to the +toot."    (interactive)    (let* ((toot (mastodon-toot--remove-docs))           (empty-toot-p (and (not mastodon-toot--media-attachments) @@ -395,25 +393,21 @@ If media items have been uploaded with `mastodon-toot--add-media-attachment', at                            ("sensitive" . ,(when mastodon-toot--content-nsfw                                              (symbol-name t)))                            ("spoiler_text" . ,spoiler))) -         (args-media -          (when mastodon-toot--media-attachment-ids -              (mapcar -               (lambda (id) -                 (cons "media_ids[]" id)) -               mastodon-toot--media-attachment-ids))) -         (args (append args-no-media args-media))) -    (if (and mastodon-toot--media-attachments -             (equal mastodon-toot--media-attachment-ids nil)) -        (message "Looks like your uploads are not up: C-c C-u to upload...") -      (if (> (length toot) (string-to-number mastodon-toot--max-toot-chars)) -          (message "Looks like your toot is longer than that maximum allowed length.") -        (if empty-toot-p -            (message "Empty toot. Cowardly refusing to post this.") -          (let ((response (mastodon-http--post endpoint args nil))) -            (mastodon-http--triage response -                                   (lambda () -                                     (mastodon-toot--kill) -                                     (message "Toot toot!"))))))))) +         (args-media (when mastodon-toot--media-attachments +                       (mastodon-toot--upload-attached-media) ; sync upload so we wait (and pray) till done +                       (mapcar (lambda (id) +                                 (cons "media_ids[]" id)) +                               mastodon-toot--media-attachment-ids))) +         (args (append args-media args-no-media))) +    (if (> (length toot) (string-to-number mastodon-toot--max-toot-chars)) +        (message "Looks like your toot is longer than that maximum allowed length.") +      (if empty-toot-p +          (message "Empty toot. Cowardly refusing to post this.") +        (let ((response (mastodon-http--post endpoint args nil))) +          (mastodon-http--triage response +                                 (lambda () +                                   (mastodon-toot--kill) +                                   (message "Toot toot!"))))))))  (defun mastodon-toot--process-local (acct)    "Add domain to local ACCT and replace the curent user name with \"\". @@ -453,7 +447,7 @@ eg. \"feduser@fed.social\" -> \"feduser@fed.social\"."  (defun mastodon-toot--mentions-company-candidates (prefix)    "Given a company PREFIX, build a list of candidates. -The prefix string is tested against both user handles and display names." +The prefix string can match against both user handles and display names."    (let (res)      (dolist (item (mastodon-search--search-accounts-query prefix))        (when (or (string-prefix-p prefix (cadr item)) @@ -541,6 +535,7 @@ The prefix string is tested against both user handles and display names."    "Remove all attachments from a toot draft."    (interactive)    (setq mastodon-toot--media-attachments nil) +  (setq mastodon-toot--media-attachment-ids nil)    (mastodon-toot--refresh-attachments-display)    (mastodon-toot--update-status-fields)) @@ -562,15 +557,17 @@ will be uploaded and attached to the toot upon sending."  (defun mastodon-toot--upload-attached-media ()    "Actually upload attachments using `mastodon-http--post-media-attachment'. -It adds the items' ids to `mastodon-toot--media-attachment-ids', which is used to actually attach them to a toot after uploading." -  (interactive) +The files to be uploaded are in `mastodon-toot--media-attachments'. +The items' ids are added to `mastodon-toot--media-attachment-ids', +which are used to attach them to a toot after uploading."    (mapcar (lambda (attachment) -            (let* ((filename (cdr (assoc :filename attachment))) +            (let* ((filename (expand-file-name +                              (cdr (assoc :filename attachment))))                     (caption (cdr (assoc :description attachment)))                     (url (concat mastodon-instance-url "/api/v2/media")))                (message "Uploading %s..." (file-name-nondirectory filename))                (mastodon-http--post-media-attachment url filename caption))) -            mastodon-toot--media-attachments)) +          mastodon-toot--media-attachments))  (defun mastodon-toot--refresh-attachments-display ()    "Update the display attachment previews in toot draft buffer."  | 
