UpdateAccount: Add support for FieldsAttributes and Source parameters
authorMikael Berthe <mikael@lilotux.net>
Wed, 05 Sep 2018 02:33:23 +0200
changeset 232 de13b1d39099
parent 231 741291bb4772
child 233 b5fb9a1b68c4
UpdateAccount: Add support for FieldsAttributes and Source parameters The source parameters don't seem to work, though (tested against Mastodon 2.5.0)...
account.go
types.go
--- a/account.go	Wed Sep 05 02:33:23 2018 +0200
+++ b/account.go	Wed Sep 05 02:33:23 2018 +0200
@@ -35,14 +35,14 @@
 
 // UpdateAccountParams contains option fields for the UpdateAccount command
 type UpdateAccountParams struct {
-	DisplayName     *string
-	Note            *string
-	AvatarImagePath *string
-	HeaderImagePath *string
-	Locked          *bool
-	Bot             *bool
-	//FieldsAttributes *[]Field
-	//Source           *SourceParams
+	DisplayName      *string
+	Note             *string
+	AvatarImagePath  *string
+	HeaderImagePath  *string
+	Locked           *bool
+	Bot              *bool
+	FieldsAttributes *[]Field
+	Source           *SourceParams
 }
 
 // updateRelationship returns a Relationship entity
@@ -447,6 +447,24 @@
 			params["bot"] = "false"
 		}
 	}
+	if cmdParams.FieldsAttributes != nil {
+		if len(*cmdParams.FieldsAttributes) > 4 {
+			return nil, errors.New("too many fields (max=4)")
+		}
+		for i, attr := range *cmdParams.FieldsAttributes {
+			qName := fmt.Sprintf("fields_attributes[%d][name]", i)
+			qValue := fmt.Sprintf("fields_attributes[%d][value]", i)
+			params[qName] = attr.Name
+			params[qValue] = attr.Value
+		}
+	}
+	if cmdParams.Source != nil {
+		sourceJSON, err := json.Marshal(*cmdParams.Source)
+		if err != nil {
+			return nil, errors.Wrap(err, "could not encode source data")
+		}
+		params["source"] = string(sourceJSON)
+	}
 
 	var err error
 	var avatar, headerImage []byte
--- a/types.go	Wed Sep 05 02:33:23 2018 +0200
+++ b/types.go	Wed Sep 05 02:33:23 2018 +0200
@@ -257,9 +257,9 @@
 
 // SourceParams is a source params structure
 type SourceParams struct { // Used for verify_credentials
-	Privacy   *string  `json:"privacy"`
-	Language  *string  `json:"language"`
-	Sensitive *bool    `json:"sensitive"`
-	Note      *string  `json:"note"`
-	Fields    *[]Field `json:"fields"`
+	Privacy   *string  `json:"privacy,omitempty"`
+	Language  *string  `json:"language,omitempty"`
+	Sensitive *bool    `json:"sensitive,omitempty"`
+	Note      *string  `json:"note,omitempty"`
+	Fields    *[]Field `json:"fields,omitempty"`
 }