aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-transient.el
blob: 7de780ce4c1b07a7fe8f8666b0e357cbd7b904a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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