go-mastodon/filters.go

126 lines
2.9 KiB
Go
Raw Permalink Normal View History

2022-03-10 19:23:35 +01:00
package mastodon
import (
"errors"
"fmt"
"net/http"
"net/url"
"time"
)
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"`
}
2022-11-20 21:31:59 +01:00
func (f *Filter) GetID() string {
if f != nil {
return string(f.ID)
} else {
return ""
}
}
func GetFilters() ([]*Filter, error) {
2022-03-10 19:23:35 +01:00
var filters []*Filter
err := doAPI(http.MethodGet, "/api/v1/filters", nil, &filters, nil)
2022-03-10 19:23:35 +01:00
if err != nil {
return nil, err
}
return filters, nil
}
2022-11-20 21:31:59 +01:00
func GetFilter(filterId ID) (*Filter, error) {
2022-03-10 19:23:35 +01:00
var filter Filter
2022-11-20 21:31:59 +01:00
err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(filterId))), nil, &filter, nil)
2022-03-10 19:23:35 +01:00
if err != nil {
return nil, err
}
return &filter, nil
}
2022-11-20 21:31:59 +01:00
func (f *Filter) Create() (*Filter, error) {
if f == nil {
2022-03-10 19:23:35 +01:00
return nil, errors.New("filter can't be nil")
}
2022-11-20 21:31:59 +01:00
if f.Phrase == "" {
2022-03-10 19:23:35 +01:00
return nil, errors.New("phrase can't be empty")
}
2022-11-20 21:31:59 +01:00
if len(f.Context) == 0 {
2022-03-10 19:23:35 +01:00
return nil, errors.New("context can't be empty")
}
params := url.Values{}
2022-11-20 21:31:59 +01:00
params.Set("phrase", f.Phrase)
for _, c := range f.Context {
2022-03-10 19:23:35 +01:00
params.Add("context[]", c)
}
2022-11-20 21:31:59 +01:00
if f.WholeWord {
2022-03-10 19:23:35 +01:00
params.Add("whole_word", "true")
}
2022-11-20 21:31:59 +01:00
if f.Irreversible {
2022-03-10 19:23:35 +01:00
params.Add("irreversible", "true")
}
2022-11-20 21:31:59 +01:00
if !f.ExpiresAt.IsZero() {
diff := time.Until(f.ExpiresAt)
2022-03-10 19:23:35 +01:00
params.Add("expires_in", fmt.Sprintf("%.0f", diff.Seconds()))
}
2022-11-20 21:31:59 +01:00
var ret Filter
err := doAPI(http.MethodPost, "/api/v1/filters", params, &ret, nil)
2022-03-10 19:23:35 +01:00
if err != nil {
return nil, err
}
2022-11-20 21:31:59 +01:00
return &ret, nil
2022-03-10 19:23:35 +01:00
}
2022-11-20 21:31:59 +01:00
func (f *Filter) Update(newFilter *Filter) (*Filter, error) {
if newFilter == nil {
2022-03-10 19:23:35 +01:00
return nil, errors.New("filter can't be nil")
}
2022-11-20 21:31:59 +01:00
if f.GetID() == "" {
2022-03-10 19:23:35 +01:00
return nil, errors.New("ID can't be empty")
}
2022-11-20 21:31:59 +01:00
if newFilter.Phrase == "" {
2022-03-10 19:23:35 +01:00
return nil, errors.New("phrase can't be empty")
}
2022-11-20 21:31:59 +01:00
if len(newFilter.Context) == 0 {
2022-03-10 19:23:35 +01:00
return nil, errors.New("context can't be empty")
}
params := url.Values{}
2022-11-20 21:31:59 +01:00
params.Set("phrase", newFilter.Phrase)
for _, c := range newFilter.Context {
2022-03-10 19:23:35 +01:00
params.Add("context[]", c)
}
2022-11-20 21:31:59 +01:00
if newFilter.WholeWord {
2022-03-10 19:23:35 +01:00
params.Add("whole_word", "true")
} else {
params.Add("whole_word", "false")
}
2022-11-20 21:31:59 +01:00
if newFilter.Irreversible {
2022-03-10 19:23:35 +01:00
params.Add("irreversible", "true")
} else {
params.Add("irreversible", "false")
}
2022-11-20 21:31:59 +01:00
if !newFilter.ExpiresAt.IsZero() {
diff := time.Until(newFilter.ExpiresAt)
2022-03-10 19:23:35 +01:00
params.Add("expires_in", fmt.Sprintf("%.0f", diff.Seconds()))
} else {
params.Add("expires_in", "")
}
2022-11-20 21:31:59 +01:00
var ret Filter
err := doAPI(http.MethodPut, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(f.GetID())), params, &ret, nil)
2022-03-10 19:23:35 +01:00
if err != nil {
return nil, err
}
2022-11-20 21:31:59 +01:00
return &ret, nil
2022-03-10 19:23:35 +01:00
}
2022-11-20 21:31:59 +01:00
func (f *Filter) Delete() error {
return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(f.GetID())), nil, nil, nil)
2022-03-10 19:23:35 +01:00
}