From 3e88d9bc55027016d83f511981c034d8d6347cba Mon Sep 17 00:00:00 2001 From: gutmet Date: Sun, 20 Nov 2022 21:53:02 +0100 Subject: [PATCH] final cleanup steps --- accounts.go | 13 +++--- apps.go | 1 - go.mod | 2 + go.sum | 2 + lists.go | 4 +- mastodon.go | 122 ++++++++++------------------------------------------ 6 files changed, 35 insertions(+), 109 deletions(-) diff --git a/accounts.go b/accounts.go index abb7aa2..67fd68e 100644 --- a/accounts.go +++ b/accounts.go @@ -32,9 +32,9 @@ type Account struct { Source *AccountSource `json:"source"` } -func (account *Account) GetID() string { - if account != nil { - return string(account.ID) +func (a *Account) GetID() string { + if a != nil { + return string(a.ID) } else { return "" } @@ -74,7 +74,6 @@ func GetCurrentAccount() (*Account, error) { return &account, nil } -// Profile is a struct for updating profiles. type Profile struct { // If it is nil it will not be updated. // If it is empty, update it with empty. @@ -246,10 +245,10 @@ func Unmute(account *Account) (*Relationship, error) { return &relationship, nil } -func GetRelationships(ids []string) ([]*Relationship, error) { +func GetRelationships(accounts []*Account) ([]*Relationship, error) { params := url.Values{} - for _, id := range ids { - params.Add("id[]", id) + for _, a := range accounts { + params.Add("id[]", a.GetID()) } var relationships []*Relationship diff --git a/apps.go b/apps.go index 921381e..0561e23 100644 --- a/apps.go +++ b/apps.go @@ -93,7 +93,6 @@ func RegisterApp(appConfig *AppConfig) (*Application, error) { return &app, nil } -// ApplicationVerification is mastodon application. type ApplicationVerification struct { Name string `json:"name"` Website string `json:"website"` diff --git a/go.mod b/go.mod index 5b9682a..b753ff1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.gutmet.org/go-mastodon.git go 1.16 + +require git.gutmet.org/linkheader.git v0.0.0-20221120205136-a51e89fd8486 diff --git a/go.sum b/go.sum index e69de29..a7a12d1 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +git.gutmet.org/linkheader.git v0.0.0-20221120205136-a51e89fd8486 h1:7F1dwJvIgvHNvglosyIE7SA49BwG6b8DFkvD8NtHMD8= +git.gutmet.org/linkheader.git v0.0.0-20221120205136-a51e89fd8486/go.mod h1:xArmd5A1lL5BEfB56+FUwqeG4XDfAvFGe35pqAgifCc= diff --git a/lists.go b/lists.go index a0265f9..3d5e954 100644 --- a/lists.go +++ b/lists.go @@ -37,9 +37,9 @@ func (a *Account) GetContainingLists() ([]*List, error) { return lists, nil } -func GetAccountsInList(list *List) ([]*Account, error) { +func (l *List) GetAccounts() ([]*Account, error) { var accounts []*Account - err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list.ID))), url.Values{"limit": {"0"}}, &accounts, nil) + err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(l.GetID())), url.Values{"limit": {"0"}}, &accounts, nil) if err != nil { return nil, err } diff --git a/mastodon.go b/mastodon.go index 4c7a640..92ecc20 100644 --- a/mastodon.go +++ b/mastodon.go @@ -12,6 +12,8 @@ import ( "path" "strings" "time" + + linkheader "git.gutmet.org/linkheader.git" ) type Config struct { @@ -146,84 +148,6 @@ func newClient(config *Config) *Client { } } -// Authenticate gets access-token to the API. -func (c *Client) Authenticate(ctx context.Context, username, password string) error { - params := url.Values{ - "client_id": {c.Config.ClientID}, - "client_secret": {c.Config.ClientSecret}, - "grant_type": {"password"}, - "username": {username}, - "password": {password}, - "scope": {"read write follow"}, - } - - return c.authenticate(ctx, params) -} - -// AuthenticateApp logs in using client credentials. -func (c *Client) AuthenticateApp(ctx context.Context) error { - params := url.Values{ - "client_id": {c.Config.ClientID}, - "client_secret": {c.Config.ClientSecret}, - "grant_type": {"client_credentials"}, - "redirect_uri": {"urn:ietf:wg:oauth:2.0:oob"}, - } - - return c.authenticate(ctx, params) -} - -// AuthenticateToken logs in using a grant token returned by Application.AuthURI. -// -// redirectURI should be the same as Application.RedirectURI. -func (c *Client) AuthenticateToken(ctx context.Context, authCode, redirectURI string) error { - params := url.Values{ - "client_id": {c.Config.ClientID}, - "client_secret": {c.Config.ClientSecret}, - "grant_type": {"authorization_code"}, - "code": {authCode}, - "redirect_uri": {redirectURI}, - } - - return c.authenticate(ctx, params) -} - -func (c *Client) authenticate(ctx context.Context, params url.Values) error { - u, err := url.Parse(c.Config.Server) - if err != nil { - return err - } - u.Path = path.Join(u.Path, "/oauth/token") - - req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(params.Encode())) - if err != nil { - return err - } - req = req.WithContext(ctx) - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - if c.UserAgent != "" { - req.Header.Set("User-Agent", c.UserAgent) - } - resp, err := c.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return parseAPIError("bad authorization", resp) - } - - var res struct { - AccessToken string `json:"access_token"` - } - err = json.NewDecoder(resp.Body).Decode(&res) - if err != nil { - return err - } - c.Config.AccessToken = res.AccessToken - return nil -} - const ( VisibilityPublic = "public" VisibilityUnlisted = "unlisted" @@ -319,28 +243,28 @@ func newPagination(rawlink string) (*Pagination, error) { } p := &Pagination{} - // for _, link := range linkheader.Parse(rawlink) { - // switch link.Rel { - // case "next": - // maxID, err := getPaginationID(link.URL, "max_id") - // if err != nil { - // return nil, err - // } - // p.MaxID = maxID - // case "prev": - // sinceID, err := getPaginationID(link.URL, "since_id") - // if err != nil { - // return nil, err - // } - // p.SinceID = sinceID + for _, link := range linkheader.Parse(rawlink) { + switch link.Rel { + case "next": + maxID, err := getPaginationID(link.URL, "max_id") + if err != nil { + return nil, err + } + p.MaxID = maxID + case "prev": + sinceID, err := getPaginationID(link.URL, "since_id") + if err != nil { + return nil, err + } + p.SinceID = sinceID - // minID, err := getPaginationID(link.URL, "min_id") - // if err != nil { - // return nil, err - // } - // p.MinID = minID - // } - // } + minID, err := getPaginationID(link.URL, "min_id") + if err != nil { + return nil, err + } + p.MinID = minID + } + } return p, nil }