From 7e7b6c5c67af47c37d2a856dd72ccc040c967482 Mon Sep 17 00:00:00 2001 From: mousebot Date: Fri, 29 Oct 2021 17:55:27 +0200 Subject: merge upload-media-attachments functionality into toot-send. this obviates the need for the user to upload files before posting their toot. - this commit makes http--post-media-attachment synchronous, so that toot-send has to wait for it. - in toot-send: if mastodon-toot--media-attachements is non-nil, the files it contains are uploaded synchronously, and their returned ids are added to toot-media-attachment-ids, which are parsed as args for the POST request to be attached to the toot. - then we send toot as usual. - clear-all-attachments also clears mastodon-toot--media-attachment-ids just in case. - we have no more need of media-attachments-filenames, as media-attachments is now a list and not a boolean value. --- lisp/mastodon-toot.el | 70 +++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 38 deletions(-) (limited to 'lisp/mastodon-toot.el') diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 309b64a..063b346 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.") @@ -378,9 +370,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 the new-toot buffer/window to the Mastodon instance and kill the 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) @@ -389,31 +383,28 @@ If media items have been uploaded with `mastodon-toot--add-media-attachment', at (spoiler (when (and (not empty-toot-p) mastodon-toot--content-warning) (read-string "Warning: " mastodon-toot--content-warning-from-reply-or-redraft))) - (args-no-media `(("status" . ,toot) - ("in_reply_to_id" . ,mastodon-toot--reply-to-id) - ("visibility" . ,mastodon-toot--visibility) - ("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 `(("status" . ,toot) + ("in_reply_to_id" . ,mastodon-toot--reply-to-id) + ("visibility" . ,mastodon-toot--visibility) + ("sensitive" . ,(when mastodon-toot--content-nsfw + (symbol-name t))) + ("spoiler_text" . ,spoiler)))) + (when mastodon-toot--media-attachments + (mastodon-toot--upload-attached-media) ; sync upload so we wait (and pray) till done + (let* ((args-media (mapcar + (lambda (id) + (cons "media_ids[]" id)) + mastodon-toot--media-attachment-ids)) + (args (append args args-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 \"\". @@ -541,6 +532,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,7 +554,9 @@ 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." +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." (interactive) (mapcar (lambda (attachment) (let* ((filename (expand-file-name -- cgit v1.2.3 From d74f462624b66040a78a3e4a13ccb0d3c681f509 Mon Sep 17 00:00:00 2001 From: mousebot Date: Fri, 29 Oct 2021 18:12:31 +0200 Subject: docstrings --- lisp/mastodon-search.el | 2 +- lisp/mastodon-toot.el | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lisp/mastodon-toot.el') 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 063b346..952ff58 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -444,7 +444,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)) -- cgit v1.2.3 From cf13db002b47f8e17267f48a0906be57d01eaf03 Mon Sep 17 00:00:00 2001 From: mousebot Date: Fri, 29 Oct 2021 19:00:22 +0200 Subject: make get-max-toot-chars async --- lisp/mastodon-toot.el | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'lisp/mastodon-toot.el') diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 952ff58..57e279f 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -146,12 +146,17 @@ Valid values are \"direct\", \"private\" (followers-only), \"unlisted\", and \"p "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. -- cgit v1.2.3 From 5d226e03737240a763419d1753769b983b46a1a9 Mon Sep 17 00:00:00 2001 From: mousebot Date: Fri, 29 Oct 2021 19:02:03 +0200 Subject: fix toot--send setting args/args-media --- lisp/mastodon-toot.el | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'lisp/mastodon-toot.el') diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 57e279f..dd13251 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -139,7 +139,6 @@ 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) @@ -375,7 +374,7 @@ Remove MARKER if REMOVE is non-nil, otherwise add it." (message "Visibility set to %s" visibility)) (defun mastodon-toot--send () - "POST contents of the new-toot buffer/window to the Mastodon instance and kill the buffer. + "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 @@ -388,19 +387,18 @@ toot." (spoiler (when (and (not empty-toot-p) mastodon-toot--content-warning) (read-string "Warning: " mastodon-toot--content-warning-from-reply-or-redraft))) - (args `(("status" . ,toot) - ("in_reply_to_id" . ,mastodon-toot--reply-to-id) - ("visibility" . ,mastodon-toot--visibility) - ("sensitive" . ,(when mastodon-toot--content-nsfw - (symbol-name t))) - ("spoiler_text" . ,spoiler)))) - (when mastodon-toot--media-attachments - (mastodon-toot--upload-attached-media) ; sync upload so we wait (and pray) till done - (let* ((args-media (mapcar - (lambda (id) - (cons "media_ids[]" id)) - mastodon-toot--media-attachment-ids)) - (args (append args args-media))))) + (args-no-media `(("status" . ,toot) + ("in_reply_to_id" . ,mastodon-toot--reply-to-id) + ("visibility" . ,mastodon-toot--visibility) + ("sensitive" . ,(when mastodon-toot--content-nsfw + (symbol-name t))) + ("spoiler_text" . ,spoiler))) + (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 @@ -562,7 +560,6 @@ will be uploaded and attached to the toot upon sending." 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." - (interactive) (mapcar (lambda (attachment) (let* ((filename (expand-file-name (cdr (assoc :filename attachment)))) @@ -570,7 +567,7 @@ which are used to attach them to a toot after uploading." (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." -- cgit v1.2.3