aboutsummaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authormarty hiatt <martianhiatus@riseup.net>2024-10-02 10:15:45 +0200
committermarty hiatt <martianhiatus@riseup.net>2024-10-02 10:15:45 +0200
commit951ad5d60e4ff24ad108f60a9f45df8e241ef7c2 (patch)
tree1bd4a0b1c738daa9eb605878266e69d32d9c15cf /lisp
parent41f5bd5199c6d132dc6312746ac2ff150aba094c (diff)
add mastodon-transient.el
Diffstat (limited to 'lisp')
-rw-r--r--lisp/mastodon-transient.el111
-rw-r--r--lisp/mastodon.el8
2 files changed, 116 insertions, 3 deletions
diff --git a/lisp/mastodon-transient.el b/lisp/mastodon-transient.el
new file mode 100644
index 0000000..7de780c
--- /dev/null
+++ b/lisp/mastodon-transient.el
@@ -0,0 +1,111 @@
+;;; mastodon-transient.el --- transient menus for mastodon.el -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2024 martian hiatus
+
+;; Author: martian hiatus <martianhiatus@riseup.net>
+;; Keywords: convenience
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;
+
+;;; Code:
+
+(require 'tp)
+
+(defun mastodon-transient-parse-source-key (key)
+ "Parse mastodon source KEY.
+If KEY needs to be source[key], format like so, else just return
+the inner key part."
+ (let* ((split (split-string key "[][]"))
+ (array-key (cadr split)))
+ (if (or (= 1 (length split)) ;; no split
+ (member array-key '("privacy" "sensitive" "language")))
+ key
+ array-key)))
+
+(defun mastodon-transient-parse-source-keys (alist)
+ "Parse ALIST containing source[key] keys."
+ (cl-loop for a in alist
+ collect (cons (mastodon-transient-parse-source-key (car a))
+ (cdr a))))
+
+;; FIXME: PATCHing source vals as JSON request body doesn't work!
+;; existing `mastodon-profile--update-preference' doesn't use it! it just uses
+;; query params! strange thing is it works for non-source params
+(transient-define-suffix mastodon-user-settings-update (&optional args)
+ "Update current user settings on the server."
+ :transient 'transient--do-exit
+ ;; interactive receives args from the prefix:
+ (interactive (list (transient-args 'mastodon-user-settings)))
+ (let* ((alist (tp-transient-to-alist args))
+ (only-changed (tp-only-changed-args alist))
+ (arrays (tp-dots-to-arrays only-changed))
+ (parsed-source (mastodon-transient-parse-source-keys arrays))
+ (endpoint "accounts/update_credentials")
+ (url (mastodon-http--api endpoint))
+ (resp (mastodon-http--patch url parsed-source))) ; :json)))
+ (mastodon-http--triage
+ resp
+ (lambda (_)
+ (message "Settings updated!\n%s" parsed-source)))))
+
+(defun mastodon-transient-get-creds ()
+ "Fetch account data."
+ (mastodon-http--get-json
+ (mastodon-http--api "accounts/verify_credentials")
+ nil :silent))
+
+(transient-define-prefix mastodon-user-settings ()
+ "A transient for setting current user settings."
+ :value (lambda () (tp-return-data
+ #'mastodon-transient-get-creds))
+ [:description
+ ;; '()
+ (lambda ()
+ "Settings")
+ ;; (format "User settings for %s" mastodon-active-user))
+ (:info
+ "Note: use the empty string (\"\") to remove a value from an option.")
+ ]
+ ;; strings
+ ["Account info"
+ ("n" "display name" "display_name=" :class tp-option-str)]
+ ;; "choice" booleans (so we can PATCH :json-false explicitly):
+ ["Account options"
+ ("l" "locked" "locked=" :class tp-choice-bool)
+ ("b" "bot" "bot=" :class tp-choice-bool)
+ ("d" "discoverable" "discoverable=" :class tp-choice-bool)
+
+ ("c" "hide follower/following lists" "source.hide_collections=" :class tp-choice-bool)
+ ("i" "indexable" "source.indexable=" :class tp-choice-bool)]
+ ["Tooting options"
+ ("p" "default privacy" "source.privacy=" :class tp-option
+ :choices (lambda () mastodon-toot-visibility-settings-list))
+ ("s" "mark sensitive" "source.sensitive=" :class tp-choice-bool)
+ ("g" "default language" "source.language=" :class tp-option
+ :choices (lambda () mastodon-iso-639-regional))]
+ ["Update"
+ ("C-c C-c" "Save settings" mastodon-user-settings-update)
+ ;; ("C-c C-k" :info "to revert all changes")
+ ]
+ (interactive)
+ (if (not mastodon-active-user)
+ (user-error "User not set")
+ (transient-setup 'mastodon-user-settings)))
+
+(provide 'mastodon-transient)
+;;; mastodon-transient.el ends here
diff --git a/lisp/mastodon.el b/lisp/mastodon.el
index faeae61..6e9586b 100644
--- a/lisp/mastodon.el
+++ b/lisp/mastodon.el
@@ -38,13 +38,15 @@
;;; Code:
(require 'cl-lib) ; for `cl-some' call in mastodon
(eval-when-compile (require 'subr-x))
-(require 'mastodon-http)
-(require 'mastodon-toot)
-(require 'mastodon-search)
(require 'url)
(require 'thingatpt)
(require 'shr)
+(require 'mastodon-http)
+(require 'mastodon-toot)
+(require 'mastodon-search)
+(require 'mastodon-transient)
+
(declare-function discover-add-context-menu "discover")
(declare-function emojify-mode "emojify")
(declare-function request "request")