2024-03-03 15:14:59 -05:00
|
|
|
|
package moderation
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-03-03 15:14:59 -05:00
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-03-03 15:14:59 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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 broadcaster’s 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 broadcaster’s 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 broadcaster’s list of blocked terms.
|
|
|
|
|
// These are the terms that the broadcaster doesn’t 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)
|
|
|
|
|
|
|
|
|
|
r, w := io.Pipe()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
if err := json.NewEncoder(w).Encode(body); err != nil {
|
|
|
|
|
w.CloseWithError(err)
|
|
|
|
|
} else {
|
|
|
|
|
w.Close()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Make(m.baseUrl, "moderation/blocked_terms", v), r)
|
2024-03-03 15:14:59 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := m.client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to add blocked term (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 15:14:59 -05:00
|
|
|
|
var data AddBlockedTermResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|