final cleanup steps
This commit is contained in:
parent
1dfacac66a
commit
3e88d9bc55
13
accounts.go
13
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
|
||||
|
|
1
apps.go
1
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"`
|
||||
|
|
2
go.mod
2
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
|
||||
|
|
2
go.sum
2
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=
|
4
lists.go
4
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
|
||||
}
|
||||
|
|
122
mastodon.go
122
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user