accountCmd.AddCommand(getAccountCmd)
accountCmd.AddCommand(getFollowersCmd)
accountCmd.AddCommand(getFollowingCmd)
+ accountCmd.AddCommand(getListsCmd)
rootCmd.AddCommand(accountCmd)
}
Short: "Get account info",
Long: "Return information the account specified by [id]. If no id is supplied, return information on the current logged in user.",
- Run: func(cmd *cobra.Command, args []string) {
+ PreRun: func(cmd *cobra.Command, args []string) {
err := account.VerifyCredentials(host, token)
if err != nil {
fmt.Printf("Error verifying credentials: %s", err.Error())
- return
}
+ },
+
+ Run: func(cmd *cobra.Command, args []string) {
+ var id string
if len(args) < 1 {
- fmt.Printf("%+v", account)
- return
+ id = account.ID
+ } else {
+ id = args[0]
}
- acct, err := mastodon.GetAccount(args[0], host, token)
+ acct, err := mastodon.GetAccount(id, host, token)
if err != nil {
panic(err.Error())
}
w.Flush()
},
}
+
+var getListsCmd = &cobra.Command{
+ Use: "lists",
+ Short: "Get account's lists",
+ Long: "Fetch all lists the account owns",
+
+ PreRun: func(cmd *cobra.Command, args []string) {
+ err := account.VerifyCredentials(host, token)
+ if err != nil {
+ panic(err.Error())
+ }
+ },
+
+ Run: func(cmd *cobra.Command, args []string) {
+ lists, err := account.GetLists(host, token)
+ if err != nil {
+ panic(err.Error())
+ }
+
+ w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
+ for _, value := range lists {
+ fmt.Fprintln(w, strings.Join([]string{
+ value.ID, value.Title, value.RepliesPolicy}, "\t"))
+ }
+ w.Flush()
+ },
+}
--- /dev/null
+package tooter
+
+import (
+ "fmt"
+
+ "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon"
+ "github.com/spf13/cobra"
+)
+
+func init() {
+ listCmd.AddCommand(listsContainingCmd)
+
+ rootCmd.AddCommand(listCmd)
+}
+
+var listCmd = &cobra.Command{
+ Use: "lists",
+ Short: "List commands",
+ Long: "Commands related to account lists.",
+}
+
+var listsContainingCmd = &cobra.Command{
+ Use: "containing",
+ Short: "Get lists containing [accountid]",
+ Long: "Get lists containing a specified account",
+
+ PreRun: func(cmd *cobra.Command, args []string) {
+ err := account.VerifyCredentials(host, token)
+ if err != nil {
+ panic(err.Error())
+ }
+ },
+
+ Run: func(cmd *cobra.Command, args []string) {
+ var id string
+
+ if len(args) < 1 {
+ id = account.ID
+ } else {
+ id = args[0]
+ }
+
+ lists, err := mastodon.ListsContainingAccount(id, host, token)
+ if err != nil {
+ panic(err.Error())
+ }
+
+ fmt.Printf("%+v", lists)
+ },
+}
id := url.PathEscape(a.ID)
path, err := url.JoinPath("api/v1/accounts", id, "following")
if err != nil {
- return following, err
+ return
}
u := url.URL{
body, err := api.Get(u, host, token)
if err != nil {
- return following, err
+ return
}
err = json.Unmarshal(body, &following)
if err != nil {
- return following, err
+ return
}
return following, err
}
+
+// GetLists fetches all lists the user owns
+func (a *Account) GetLists(host string, token api.Token) (lists []List, err error) {
+ u := url.URL{
+ Host: host,
+ Path: "api/v1/lists",
+ }
+ u.Scheme = "https"
+
+ body, err := api.Get(u, host, token)
+ if err != nil {
+ return
+ }
+
+ err = json.Unmarshal(body, &lists)
+ if err != nil {
+ return
+ }
+
+ return lists, err
+}
return list, err
}
+// ListsContainingAccount returns lists containing this account.
+func ListsContainingAccount(
+ accountID string, host string, token api.Token) (lists []List, err error) {
+
+ id := url.PathEscape(accountID)
+ path, err := url.JoinPath("api/v1/accounts", id, "lists")
+ if err != nil {
+ return
+ }
+
+ u := url.URL{
+ Host: host,
+ Path: path,
+ }
+ u.Scheme = "https"
+
+ body, err := api.Get(u, host, token)
+ if err != nil {
+ return
+ }
+
+ err = json.Unmarshal(body, &lists)
+ if err != nil {
+ return
+ }
+
+ return lists, err
+}
+
// GetAccounts returns the accounts associated with a list.
func (l *List) GetAccounts(host string, token api.Token) (accounts []Account, err error) {
id := url.PathEscape(l.ID)