124 lines
3.1 KiB
Go
124 lines
3.1 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"`
|
|
}
|
|
|
|
// GetFilters returns all the filters on the current account.
|
|
func (c *Client) GetFilters() ([]*Filter, error) {
|
|
var filters []*Filter
|
|
err := c.doAPI(http.MethodGet, "/api/v1/filters", nil, &filters, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return filters, nil
|
|
}
|
|
|
|
// GetFilter retrieves a filter by ID.
|
|
func (c *Client) GetFilter(id ID) (*Filter, error) {
|
|
var filter Filter
|
|
err := c.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
|
|
}
|
|
|
|
// CreateFilter creates a new filter.
|
|
func (c *Client) 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 := c.doAPI(http.MethodPost, "/api/v1/filters", params, &f, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &f, nil
|
|
}
|
|
|
|
// UpdateFilter updates a filter.
|
|
func (c *Client) 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 := c.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
|
|
}
|
|
|
|
// DeleteFilter removes a filter.
|
|
func (c *Client) DeleteFilter(id ID) error {
|
|
return c.doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), nil, nil, nil)
|
|
}
|