-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
164 lines (138 loc) · 4.19 KB
/
app.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cmd
import (
"fmt"
"os"
"time"
"github.com/quells/mastobot/internal/oauth2"
"github.com/quells/mastobot/internal/toot"
"github.com/spf13/cobra"
)
var (
appName string
userEmail string
password string
tootVisibilityS string
tootVisibility toot.Visibility
tootSensitive bool
tootSpoilerText string
maxAge time.Duration
)
func init() {
appCmd.PersistentFlags().StringVar(&instance, "instance", "", "Mastodon (or compatible) instance to interact with")
must(appCmd.MarkPersistentFlagRequired("instance"))
appCmd.PersistentFlags().StringVar(&appName, "name", "", "Name of the application")
must(appCmd.MarkPersistentFlagRequired("name"))
appTokenRenewCmd.Flags().StringVarP(&userEmail, "email", "U", "", "Account email")
appTokenRenewCmd.Flags().StringVarP(&password, "password", "P", "", "Account password")
must(appTokenRenewCmd.MarkFlagRequired("email"))
must(appTokenRenewCmd.MarkFlagRequired("password"))
appTokenCmd.AddCommand(appTokenRenewCmd)
appTokenCmd.AddCommand(appTokenRevokeCmd)
appCmd.AddCommand(appRegisterCmd)
appCmd.AddCommand(appTokenCmd)
appTootCmd.Flags().StringVar(&tootVisibilityS, "visibility", "private", "[private, unlisted, public, direct]")
appTootCmd.Flags().BoolVar(&tootSensitive, "sensitive", false, "Mark Toot as containing sensitive material")
appTootCmd.Flags().StringVar(&tootSpoilerText, "spoiler", "", "Spoiler text")
appCmd.AddCommand(appTootCmd)
appExpireCmd.Flags().DurationVar(&maxAge, "max-age", 30*24*time.Hour, "Maximum age")
appCmd.AddCommand(appExpireCmd)
rootCmd.AddCommand(appCmd)
}
var appCmd = &cobra.Command{
Use: "app",
Short: "Application Helpers",
Long: `Application Helpers
Register an application with an instance.
Generate OAuth2 access tokens.`,
}
var appRegisterCmd = &cobra.Command{
Use: "register",
Short: "Register Application",
Long: "Register an application with an instance.",
RunE: func(cmd *cobra.Command, args []string) error {
return oauth2.RegisterApp(cmd.Context(), instance, appName)
},
}
var appTokenCmd = &cobra.Command{
Use: "token",
Short: "Access Token Helpers",
Long: `Access Token Helpers to connect to an instance as a user.
Renew an access token.
Revoke an access token.`,
}
var appTokenRenewCmd = &cobra.Command{
Use: "renew",
Short: "Renew an access token",
RunE: func(cmd *cobra.Command, args []string) error {
return oauth2.GetAccessToken(cmd.Context(), instance, appName, userEmail, password)
},
}
var appTokenRevokeCmd = &cobra.Command{} // TODO
var appTootCmd = &cobra.Command{
Use: "toot",
Short: "Toot!",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("must provide toot message")
}
tootVisibility = toot.VisibilityFrom(tootVisibilityS)
if tootVisibility == toot.VisibilityInvalid {
return fmt.Errorf("invalid visibility value")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
_, err := toot.VerifyCredentials(cmd.Context(), instance, appName)
if err != nil {
return err
}
status := toot.Status{
Text: args[0],
Visibility: tootVisibility,
Sensitive: tootSensitive,
Spoiler: tootSpoilerText,
}
id, err := status.Submit(cmd.Context(), instance, appName)
if err != nil {
return err
}
_, _ = fmt.Fprintln(os.Stdout, id)
return nil
},
}
var appExpireCmd = &cobra.Command{
Use: "expire",
Short: "Delete old toots",
Long: "Delete all toots older than a certain age.",
RunE: func(cmd *cobra.Command, args []string) error {
accountID, err := toot.VerifyCredentials(cmd.Context(), instance, appName)
if err != nil {
return err
}
list := toot.ListStatuses{
Limit: 40,
}
for {
statuses, err := list.ForAccount(cmd.Context(), instance, appName, accountID)
if err != nil {
return err
}
if len(statuses) == 0 {
break
}
for _, status := range statuses {
if time.Since(status.CreatedAt) < maxAge {
continue
}
// TODO: check mentions; add option to prevent deletion if mentioned
err = toot.Delete(cmd.Context(), instance, appName, status.ID)
if err != nil {
return err
}
_, _ = fmt.Fprintln(os.Stdout, status.ID)
}
list.MaxID = statuses[len(statuses)-1].ID
}
return nil
},
}