120 lines
2.8 KiB
Go
120 lines
2.8 KiB
Go
package mastodon
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
// Filter is metadata for a filter of users.
|
|
type Filter struct {
|
|
ID ID `json:"id"`
|
|
Phrase string `json:"phrase"`
|
|
Context []string `json:"context"`
|
|
WholeWord bool `json:"whole_word"`
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
Irreversible bool `json:"irreversible"`
|
|
}
|
|
|
|
func GetFilters() ([]*Filter, error) {
|
|
var filters []*Filter
|
|
err := doAPI(http.MethodGet, "/api/v1/filters", nil, &filters, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return filters, nil
|
|
}
|
|
|
|
// was ist id ?
|
|
func GetFilter(id ID) (*Filter, error) {
|
|
var filter Filter
|
|
err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), nil, &filter, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &filter, nil
|
|
}
|
|
|
|
func CreateFilter(filter *Filter) (*Filter, error) {
|
|
if filter == nil {
|
|
return nil, errors.New("filter can't be nil")
|
|
}
|
|
if filter.Phrase == "" {
|
|
return nil, errors.New("phrase can't be empty")
|
|
}
|
|
if len(filter.Context) == 0 {
|
|
return nil, errors.New("context can't be empty")
|
|
}
|
|
params := url.Values{}
|
|
params.Set("phrase", filter.Phrase)
|
|
for _, c := range filter.Context {
|
|
params.Add("context[]", c)
|
|
}
|
|
if filter.WholeWord {
|
|
params.Add("whole_word", "true")
|
|
}
|
|
if filter.Irreversible {
|
|
params.Add("irreversible", "true")
|
|
}
|
|
if !filter.ExpiresAt.IsZero() {
|
|
diff := time.Until(filter.ExpiresAt)
|
|
params.Add("expires_in", fmt.Sprintf("%.0f", diff.Seconds()))
|
|
}
|
|
|
|
var f Filter
|
|
err := doAPI(http.MethodPost, "/api/v1/filters", params, &f, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &f, nil
|
|
}
|
|
|
|
func UpdateFilter(id ID, filter *Filter) (*Filter, error) {
|
|
if filter == nil {
|
|
return nil, errors.New("filter can't be nil")
|
|
}
|
|
if id == ID("") {
|
|
return nil, errors.New("ID can't be empty")
|
|
}
|
|
if filter.Phrase == "" {
|
|
return nil, errors.New("phrase can't be empty")
|
|
}
|
|
if len(filter.Context) == 0 {
|
|
return nil, errors.New("context can't be empty")
|
|
}
|
|
params := url.Values{}
|
|
params.Set("phrase", filter.Phrase)
|
|
for _, c := range filter.Context {
|
|
params.Add("context[]", c)
|
|
}
|
|
if filter.WholeWord {
|
|
params.Add("whole_word", "true")
|
|
} else {
|
|
params.Add("whole_word", "false")
|
|
}
|
|
if filter.Irreversible {
|
|
params.Add("irreversible", "true")
|
|
} else {
|
|
params.Add("irreversible", "false")
|
|
}
|
|
if !filter.ExpiresAt.IsZero() {
|
|
diff := time.Until(filter.ExpiresAt)
|
|
params.Add("expires_in", fmt.Sprintf("%.0f", diff.Seconds()))
|
|
} else {
|
|
params.Add("expires_in", "")
|
|
}
|
|
|
|
var f Filter
|
|
err := doAPI(http.MethodPut, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), params, &f, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &f, nil
|
|
}
|
|
|
|
func DeleteFilter(id ID) error {
|
|
return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), nil, nil, nil)
|
|
}
|