go-twitch/api/moderation/add_blocked_term.go

81 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package moderation
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
type AddBlockedTermParams struct {
// The ID of the broadcaster that owns the list of blocked terms.
BroadcasterID string `url:"broadcaster_id"`
// The ID of the broadcaster or a user that has permission to moderate the broadcasters chat room.
// This ID must match the user ID in the user access token.
ModeratorID string `url:"moderator_id"`
}
type AddBlockedTermRequest struct {
// The word or phrase to block from being used in the broadcasters chat room.
// The term must contain a minimum of 2 characters and may contain up to a maximum of 500 characters.
//
// Terms may include a wildcard character (*).
// The wildcard character must appear at the beginning or end of a word or set of characters.
// For example, *foo or foo*.
//
// If the blocked term already exists, the response contains the existing blocked term.
Text string `json:"text"`
}
type AddBlockedTermResponse struct {
// A list that contains the single blocked term that the broadcaster added.
Data []BlockedTerm `json:"data"`
}
// Adds a word or phrase to the broadcasters list of blocked terms.
// These are the terms that the broadcaster doesnt want used in their chat room.
//
// Requires a user access token that includes the moderator:manage:blocked_terms scope.
func (m *Moderation) AddBlockedTerm(ctx context.Context, params *AddBlockedTermParams, body *AddBlockedTermRequest) (*AddBlockedTermResponse, error) {
v, _ := query.Values(params)
endpoint := m.baseUrl.ResolveReference(&url.URL{Path: "moderation/blocked_terms", RawQuery: v.Encode()})
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(body); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), r)
if err != nil {
return nil, err
}
res, err := m.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to add blocked term (%d)", res.StatusCode)
}
var data AddBlockedTermResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}