]> Vexing Labs - dead-tooter.git/commitdiff
further lists
authorAdam Shamblin <adam@vexingworkshop.com>
Thu, 29 Dec 2022 00:13:07 +0000 (17:13 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Thu, 29 Dec 2022 00:13:07 +0000 (17:13 -0700)
cmd/account.go
cmd/list.go [new file with mode: 0644]
pkg/mastodon/account.go
pkg/mastodon/list.go

index 181d87f2457b22af478bc21265efe7dc2f9a87ca..325b2036ca4877899447d583ff7e7a41f380f9f5 100644 (file)
@@ -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 (file)
index 0000000..7160431
--- /dev/null
@@ -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)
+       },
+}
index 28d28a1d724a029f4a226270eff0ccb34a7be829..e75ba5e35dbbc5edf1da59b1256ddb30d106b94a 100644 (file)
@@ -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
+}
index f01057dd5765c367164493c4f3e1d14395dc972c..9c1e530e376a87f43a9051819b54bf7d3becadbb 100644 (file)
@@ -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)