aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarty hiatt <martianhiatus@riseup.net>2023-11-06 21:17:53 +0100
committermarty hiatt <martianhiatus@riseup.net>2023-11-06 21:33:04 +0100
commitf1af40e2c00e9a7db67e4e1c153a77cd61d8c562 (patch)
treefd4b64834d0409c5136ded62233222a6df0022de
parent406d87cc7ad075a2c1cc2044e151eba7d8110191 (diff)
edit toots adopt attachments. FIX #506
- fetch media from url (http--read-file-as-string) - on edit, set media-attachments/-ids with the data from the toot/url - nb: users still can't edit attachments, can only remove all or maintain them as they are. but that's much better than always nuking them!
-rw-r--r--lisp/mastodon-http.el10
-rw-r--r--lisp/mastodon-toot.el53
2 files changed, 43 insertions, 20 deletions
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el
index 1edc8b5..8764764 100644
--- a/lisp/mastodon-http.el
+++ b/lisp/mastodon-http.el
@@ -98,10 +98,14 @@ RESPONSE if unsuccessful."
(mastodon-http--process-json))))
(message "Error %s: %s" status (alist-get 'error json-response)))))))
-(defun mastodon-http--read-file-as-string (filename)
- "Read a file FILENAME as a string. Used to generate image preview."
+(defun mastodon-http--read-file-as-string (filename &optional url)
+ "Read a file FILENAME as a string.
+Used to generate image preview.
+URL means FILENAME is a URL."
(with-temp-buffer
- (insert-file-contents filename)
+ (if url
+ (url-insert-file-contents filename)
+ (insert-file-contents filename))
(string-to-unibyte (buffer-string))))
(defmacro mastodon-http--authorized-request (method body &optional unauthenticated-p)
diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el
index 0891c51..6f1aff9 100644
--- a/lisp/mastodon-toot.el
+++ b/lisp/mastodon-toot.el
@@ -634,19 +634,36 @@ REPLY-ID, TOOT-VISIBILITY, and TOOT-CW of deleted toot are preseved."
;; TODO set new lang/scheduled props here
nil))))
+(defun mastodon-toot--set-toot-media-attachments (media)
+ "Set the media attachments variables.
+MEDIA is the media_attachments data for a status from the server."
+ (mapcar (lambda (x)
+ (cl-pushnew (alist-get 'id x)
+ mastodon-toot--media-attachment-ids)
+ (cl-pushnew `((:contents . ,(mastodon-http--read-file-as-string
+ (alist-get 'url x) :url))
+ (:description . ,(alist-get 'description x)))
+ mastodon-toot--media-attachments))
+ media))
+
(defun mastodon-toot--set-toot-properties
- (reply-id visibility cw lang &optional scheduled scheduled-id)
+ (reply-id visibility cw lang &optional scheduled scheduled-id media)
"Set the toot properties for the current redrafted or edited toot.
-REPLY-ID, VISIBILITY, CW, SCHEDULED, and LANG are the properties to set."
- (when reply-id
- (setq mastodon-toot--reply-to-id reply-id))
- (setq mastodon-toot--visibility visibility)
- (setq mastodon-toot--scheduled-for scheduled)
- (setq mastodon-toot--scheduled-id scheduled-id)
- (when (not (string-empty-p lang))
- (setq mastodon-toot--language lang))
- (mastodon-toot--set-cw cw)
- (mastodon-toot--update-status-fields))
+REPLY-ID, VISIBILITY, CW, SCHEDULED, and LANG are the properties to set.
+MEDIA is the media_attachments data for a status from the server."
+ (with-current-buffer "*edit toot*"
+ (when reply-id
+ (setq mastodon-toot--reply-to-id reply-id))
+ (setq mastodon-toot--visibility visibility)
+ (setq mastodon-toot--scheduled-for scheduled)
+ (setq mastodon-toot--scheduled-id scheduled-id)
+ (when (not (string-empty-p lang))
+ (setq mastodon-toot--language lang))
+ (mastodon-toot--set-cw cw)
+ (when media
+ (mastodon-toot--set-toot-media-attachments media))
+ (mastodon-toot--refresh-attachments-display)
+ (mastodon-toot--update-status-fields)))
(defun mastodon-toot--kill (&optional cancel)
"Kill `mastodon-toot-mode' buffer and window.
@@ -888,14 +905,15 @@ instance to edit a toot."
(source-cw (alist-get 'spoiler_text source))
(toot-visibility (alist-get 'visibility toot))
(toot-language (alist-get 'language toot))
- (reply-id (alist-get 'in_reply_to_id toot)))
- (when (y-or-n-p "Edit this toot? (NB: attachments will be lost!) ")
+ (reply-id (alist-get 'in_reply_to_id toot))
+ (media (alist-get 'media_attachments toot)))
+ (when (y-or-n-p "Edit this toot? ")
(mastodon-toot--compose-buffer nil reply-id nil content :edit)
(goto-char (point-max))
- ;; adopt reply-to-id, visibility, CW, and language:
+ ;; adopt reply-to-id, visibility, CW, language, and media:
(mastodon-toot--set-toot-properties reply-id toot-visibility
- source-cw toot-language)
- (mastodon-toot--update-status-fields)
+ source-cw toot-language nil nil
+ media)
(setq mastodon-toot--edit-item-id id)))))))
(defun mastodon-toot--get-toot-source (id)
@@ -1178,7 +1196,8 @@ File is actually attached to the toot upon posting."
(:filename . ,file)))))
(mastodon-toot--refresh-attachments-display)
;; upload only most recent attachment:
- (mastodon-toot--upload-attached-media (car (last mastodon-toot--media-attachments)))))
+ (mastodon-toot--upload-attached-media
+ (car (last mastodon-toot--media-attachments)))))
(defun mastodon-toot--upload-attached-media (attachment)
"Upload a single ATTACHMENT using `mastodon-http--post-media-attachment'.