From: Adam Shamblin Date: Wed, 28 Dec 2022 22:37:17 +0000 (-0700) Subject: Begin work on List functions. X-Git-Url: https://git.vexinglabs.com/?a=commitdiff_plain;h=07b10301753e3335cae00b5fadd2fc71fc311470;p=dead-tooter.git Begin work on List functions. --- diff --git a/cmd/account.go b/cmd/account.go index 538e1d6..181d87f 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -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()) } diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index 503c761..213e62d 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -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 } diff --git a/pkg/mastodon/api.go b/pkg/mastodon/api.go index feb6d8c..c65df7b 100644 --- a/pkg/mastodon/api.go +++ b/pkg/mastodon/api.go @@ -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 index 0000000..05a1ef0 --- /dev/null +++ b/pkg/mastodon/list.go @@ -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 +}