Fix (hopefully) handling of arrays in API calls
authorMikael Berthe <mikael@lilotux.net>
Wed, 05 Sep 2018 02:33:23 +0200
changeset 231 741291bb4772
parent 230 d13e9a780d91
child 232 de13b1d39099
Fix (hopefully) handling of arrays in API calls Item numbers should not be sent, except for two-dimension arrays.
account.go
api.go
lists.go
notifications.go
report.go
status.go
--- a/account.go	Wed Sep 05 02:33:23 2018 +0200
+++ b/account.go	Wed Sep 05 02:33:23 2018 +0200
@@ -362,7 +362,7 @@
 		if id < 1 {
 			return nil, ErrInvalidID
 		}
-		qID := fmt.Sprintf("id[%d]", i+1)
+		qID := fmt.Sprintf("id[%d]", i)
 		params[qID] = strconv.FormatInt(id, 10)
 	}
 
--- a/api.go	Wed Sep 05 02:33:23 2018 +0200
+++ b/api.go	Wed Sep 05 02:33:23 2018 +0200
@@ -94,12 +94,13 @@
 			// It seems Mastodon doesn't like parameters with index
 			// numbers, but it needs the brackets.
 			// Let's check if the key matches '^.+\[.*\]$'
+			// Do not proceed if there's another bracket pair.
 			klen := len(key)
 			if klen == 0 {
 				continue
 			}
 			i := strings.Index(key, "[")
-			if key[klen-1] == ']' && i > 0 {
+			if i > 0 && key[klen-1] == ']' && strings.Index(key[i+1:], "[") < 0 {
 				// This is an array, let's remove the index number
 				key = key[:i] + "[]"
 			}
--- a/lists.go	Wed Sep 05 02:33:23 2018 +0200
+++ b/lists.go	Wed Sep 05 02:33:23 2018 +0200
@@ -100,7 +100,7 @@
 		if id < 1 {
 			return ErrInvalidID
 		}
-		qID := fmt.Sprintf("account_ids[%d]", i+1)
+		qID := fmt.Sprintf("account_ids[%d]", i)
 		params[qID] = strconv.FormatInt(id, 10)
 	}
 	return mc.apiCall(endPoint, method, params, nil, nil, nil)
@@ -115,7 +115,7 @@
 		if id < 1 {
 			return ErrInvalidID
 		}
-		qID := fmt.Sprintf("account_ids[%d]", i+1)
+		qID := fmt.Sprintf("account_ids[%d]", i)
 		params[qID] = strconv.FormatInt(id, 10)
 	}
 	return mc.apiCall(endPoint, method, params, nil, nil, nil)
--- a/notifications.go	Wed Sep 05 02:33:23 2018 +0200
+++ b/notifications.go	Wed Sep 05 02:33:23 2018 +0200
@@ -28,7 +28,7 @@
 	if len(excludeTypes) > 0 {
 		params = make(apiCallParams)
 		for i, eType := range excludeTypes {
-			qID := fmt.Sprintf("exclude_types[%d]", i+1)
+			qID := fmt.Sprintf("exclude_types[%d]", i)
 			params[qID] = eType
 		}
 	}
--- a/report.go	Wed Sep 05 02:33:23 2018 +0200
+++ b/report.go	Wed Sep 05 02:33:23 2018 +0200
@@ -36,7 +36,7 @@
 		if id < 1 {
 			return nil, ErrInvalidID
 		}
-		qID := fmt.Sprintf("status_ids[%d]", i+1)
+		qID := fmt.Sprintf("status_ids[%d]", i)
 		params[qID] = strconv.FormatInt(id, 10)
 	}
 
--- a/status.go	Wed Sep 05 02:33:23 2018 +0200
+++ b/status.go	Wed Sep 05 02:33:23 2018 +0200
@@ -141,7 +141,7 @@
 			if id < 1 {
 				return ErrInvalidID
 			}
-			qID := fmt.Sprintf("media_ids[%d]", i+1)
+			qID := fmt.Sprintf("media_ids[%d]", i)
 			params[qID] = strconv.FormatInt(id, 10)
 		}
 		if opts.Sensitive {