Add support for Field, Source and Locked parameters to AccountUpdate()

pull/97/head
buckket 2019-05-14 02:51:33 +02:00 committed by mattn
parent bb2662b33c
commit 68ca31fccd
2 changed files with 40 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"time" "time"
) )
@ -38,6 +39,15 @@ type Field struct {
VerifiedAt time.Time `json:"verified_at"` VerifiedAt time.Time `json:"verified_at"`
} }
// Source is a Mastodon account profile field
type Source struct {
Privacy string `json:"privacy"`
Sensitive *bool `json:"sensitive"`
Language string `json:"language"`
Note string `json:"note"`
Fields *[]Field `json:"fields"`
}
// GetAccount return Account. // GetAccount return Account.
func (c *Client) GetAccount(ctx context.Context, id ID) (*Account, error) { func (c *Client) GetAccount(ctx context.Context, id ID) (*Account, error) {
var account Account var account Account
@ -64,6 +74,9 @@ type Profile struct {
// If it is empty, update it with empty. // If it is empty, update it with empty.
DisplayName *string DisplayName *string
Note *string Note *string
Locked *bool
Fields *[]Field
Source *Source
// Set the base64 encoded character string of the image. // Set the base64 encoded character string of the image.
Avatar string Avatar string
@ -79,6 +92,26 @@ func (c *Client) AccountUpdate(ctx context.Context, profile *Profile) (*Account,
if profile.Note != nil { if profile.Note != nil {
params.Set("note", *profile.Note) params.Set("note", *profile.Note)
} }
if profile.Locked != nil {
params.Set("locked", strconv.FormatBool(*profile.Locked))
}
if profile.Fields != nil {
for idx, field := range *profile.Fields {
params.Set(fmt.Sprintf("fields_attributes[%d][name]", idx), field.Name)
params.Set(fmt.Sprintf("fields_attributes[%d][value]", idx), field.Value)
}
}
if profile.Source != nil {
if len(profile.Source.Privacy) > 0 {
params.Set("source[privacy]", profile.Source.Privacy)
}
if profile.Source.Sensitive != nil {
params.Set("source[sensitive]", strconv.FormatBool(*profile.Source.Sensitive))
}
if len(profile.Source.Language) > 0 {
params.Set("source[language]", profile.Source.Language)
}
}
if profile.Avatar != "" { if profile.Avatar != "" {
params.Set("avatar", profile.Avatar) params.Set("avatar", profile.Avatar)
} }

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"time"
) )
func TestGetAccount(t *testing.T) { func TestGetAccount(t *testing.T) {
@ -93,9 +94,15 @@ func TestAccountUpdate(t *testing.T) {
if err == nil { if err == nil {
t.Fatalf("should be fail: %v", err) t.Fatalf("should be fail: %v", err)
} }
tbool := true
fields := []Field{{"foo", "bar", time.Time{}}, {"dum", "baz", time.Time{}}}
source := Source{Language: "de", Privacy: "public", Sensitive: &tbool}
a, err := client.AccountUpdate(context.Background(), &Profile{ a, err := client.AccountUpdate(context.Background(), &Profile{
DisplayName: String("display_name"), DisplayName: String("display_name"),
Note: String("note"), Note: String("note"),
Locked: &tbool,
Fields: &fields,
Source: &source,
Avatar: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUoAAADrCAYAAAA...", Avatar: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUoAAADrCAYAAAA...",
Header: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUoAAADrCAYAAAA...", Header: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUoAAADrCAYAAAA...",
}) })