From: Adam Shamblin Date: Thu, 29 Dec 2022 00:13:07 +0000 (-0700) Subject: further lists X-Git-Url: https://git.vexinglabs.com/?a=commitdiff_plain;h=93b12b085469448adf5db7fac8f9f5bad767a042;p=dead-tooter.git further lists --- diff --git a/cmd/account.go b/cmd/account.go index 181d87f..325b203 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -16,6 +16,7 @@ func init() { accountCmd.AddCommand(getAccountCmd) accountCmd.AddCommand(getFollowersCmd) accountCmd.AddCommand(getFollowingCmd) + accountCmd.AddCommand(getListsCmd) rootCmd.AddCommand(accountCmd) } @@ -31,19 +32,23 @@ var getAccountCmd = &cobra.Command{ 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()) } @@ -103,3 +108,30 @@ var getFollowingCmd = &cobra.Command{ 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() + }, +} diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 0000000..7160431 --- /dev/null +++ b/cmd/list.go @@ -0,0 +1,50 @@ +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) + }, +} diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index 28d28a1..e75ba5e 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -225,7 +225,7 @@ func (a *Account) GetFollowing( id := url.PathEscape(a.ID) path, err := url.JoinPath("api/v1/accounts", id, "following") if err != nil { - return following, err + return } u := url.URL{ @@ -236,13 +236,34 @@ func (a *Account) GetFollowing( 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 +} diff --git a/pkg/mastodon/list.go b/pkg/mastodon/list.go index f01057d..9c1e530 100644 --- a/pkg/mastodon/list.go +++ b/pkg/mastodon/list.go @@ -41,6 +41,35 @@ func GetList(ID string, host string, token api.Token) (list List, err error) { 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)