]> Vexing Labs - dead-tooter.git/commitdiff
Begin work on List functions.
authorAdam Shamblin <adam@vexingworkshop.com>
Wed, 28 Dec 2022 22:37:17 +0000 (15:37 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Wed, 28 Dec 2022 22:37:17 +0000 (15:37 -0700)
cmd/account.go
pkg/mastodon/account.go
pkg/mastodon/api.go
pkg/mastodon/list.go [new file with mode: 0644]

index 538e1d69eb678192a613fac88d8b4c43cf879672..181d87f2457b22af478bc21265efe7dc2f9a87ca 100644 (file)
@@ -43,7 +43,7 @@ var getAccountCmd = &cobra.Command{
                        return
                }
 
-               acct, err := account.Get(args[0], host, token)
+               acct, err := mastodon.GetAccount(args[0], host, token)
                if err != nil {
                        panic(err.Error())
                }
index 503c761c715b59f9f80825fb18ed1e398895026e..213e62d42c7115fbeb03c48fb0dbda64c094eaaa 100644 (file)
@@ -153,8 +153,8 @@ func (a *Account) VerifyCredentials(host string, token Token) (err error) {
        return err
 }
 
-// Get returns the details of account specified by ID.
-func (a *Account) Get(
+// GetAccount returns the details of account specified by ID.
+func GetAccount(
        ID string, host string, token Token) (account Account, err error) {
 
        id := url.PathEscape(ID)
@@ -182,6 +182,11 @@ func (a *Account) Get(
        return account, err
 }
 
+// Get returns the currently logged in account.
+func (a *Account) Get(host string, token Token) (account Account, err error) {
+       return GetAccount(a.ID, host, token)
+}
+
 // GetFollowers returns a list of all accounts following the logged in user
 func (a *Account) GetFollowers(
        host string, token Token) (followers []Account, err error) {
@@ -215,8 +220,6 @@ func (a *Account) GetFollowers(
 func (a *Account) GetFollowing(
        host string, token Token) (following []Account, err error) {
 
-       client := &http.Client{}
-
        id := url.PathEscape(a.ID)
        path, err := url.JoinPath("api/v1/accounts", id, "following")
        if err != nil {
@@ -229,23 +232,7 @@ func (a *Account) GetFollowing(
        }
        u.Scheme = "https"
 
-       req, err := http.NewRequest("GET", u.String(), nil)
-       if err != nil {
-               return following, err
-       }
-       req.Header.Add("Authorization", "Bearer "+token.AccessToken)
-
-       resp, err := client.Do(req)
-       if err != nil {
-               return following, err
-       }
-       if resp.StatusCode != 200 {
-               err = errors.New(resp.Status)
-               return following, err
-       }
-       defer resp.Body.Close()
-
-       body, err := io.ReadAll(resp.Body)
+       body, err := Get(u, host, token)
        if err != nil {
                return following, err
        }
index feb6d8c9668a7419beb29391a4b0ebef45fe8257..c65df7bcae99e8eae6240792e5caac032568e738 100644 (file)
@@ -32,5 +32,5 @@ func Get(u url.URL, host string, token Token) (body []byte, err error) {
                return
        }
 
-       return
+       return body, err
 }
diff --git a/pkg/mastodon/list.go b/pkg/mastodon/list.go
new file mode 100644 (file)
index 0000000..05a1ef0
--- /dev/null
@@ -0,0 +1,67 @@
+package mastodon
+
+import (
+       "encoding/json"
+       "net/url"
+)
+
+// List represents a list of some users that the authenticated user follows.
+type List struct {
+       ID            string `json:"id"`
+       Title         string `json:"title"`
+       RepliesPolicy string `json:"replies_policy"`
+}
+
+// GetList returns a list specified by id.
+func GetList(ID string, host string, token Token) (list List, err error) {
+       id := url.PathEscape(ID)
+       path, err := url.JoinPath("api/v1/lists", id)
+       if err != nil {
+               return
+       }
+
+       u := url.URL{
+               Host: host,
+               Path: path,
+       }
+       u.Scheme = "https"
+
+       body, err := Get(u, host, token)
+       if err != nil {
+               return
+       }
+
+       err = json.Unmarshal(body, &list)
+       if err != nil {
+               return
+       }
+
+       return list, err
+}
+
+// GetAccounts returns the accounts associated with a list.
+func (l *List) GetAccounts(host string, token Token) (accounts []Account, err error) {
+       id := url.PathEscape(l.ID)
+       path, err := url.JoinPath("api/v1/lists", id, "accounts")
+       if err != nil {
+               return
+       }
+
+       u := url.URL{
+               Host: host,
+               Path: path,
+       }
+       u.Scheme = "https"
+
+       body, err := Get(u, host, token)
+       if err != nil {
+               return
+       }
+
+       err = json.Unmarshal(body, &accounts)
+       if err != nil {
+               return
+       }
+
+       return accounts, err
+}