From 951ad5d60e4ff24ad108f60a9f45df8e241ef7c2 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Wed, 2 Oct 2024 10:15:45 +0200 Subject: add mastodon-transient.el --- lisp/mastodon-transient.el | 111 +++++++++++++++++++++++++++++++++++++++++++++ lisp/mastodon.el | 8 ++-- 2 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 lisp/mastodon-transient.el (limited to 'lisp') 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 +;; 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 . + +;;; 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") -- cgit v1.2.3