go-twitch/api/moderation/update_shield_mode_status.go

76 lines
2.3 KiB
Go
Raw Normal View History

2024-03-03 15:14:59 -05:00
package moderation
import (
"context"
"encoding/json"
"fmt"
2024-03-03 15:14:59 -05:00
"io"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
type UpdateShieldModeStatusParams struct {
// The ID of the broadcaster whose Shield Mode you want to activate or deactivate.
BroadcasterID string `url:"broadcaster_id"`
// The ID of the broadcaster or a user that is one of the broadcasters moderators. This ID must match the user ID in the access token.
ModeratorID string `url:"moderator_id"`
}
type UpdateShieldModeStatusRequest struct {
// A Boolean value that determines whether to activate Shield Mode. Set to true to activate Shield Mode; otherwise, false to deactivate Shield Mode.
IsActive bool `json:"is_active"`
}
type UpdateShieldModeStatusResponse struct {
// A list that contains a single object with the broadcasters updated Shield Mode status.
Data []ShieldModeStatus `json:"data"`
}
// Activates or deactivates the broadcasters Shield Mode.
//
// Twitchs Shield Mode feature is like a panic button that broadcasters can push to protect themselves from chat abuse coming from one or more accounts.
// When activated, Shield Mode applies the overrides that the broadcaster configured in the Twitch UX.
// If the broadcaster hasnt configured Shield Mode, it applies default overrides.
//
// Requires a user access token that includes the moderator:manage:shield_mode scope.
func (m *Moderation) UpdateShieldModeStatus(ctx context.Context, params *UpdateShieldModeStatusParams) (*UpdateShieldModeStatusResponse, error) {
v, _ := query.Values(params)
endpoint := m.baseUrl.ResolveReference(&url.URL{Path: "moderation/shield_mode", RawQuery: v.Encode()})
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(UpdateShieldModeStatusRequest{IsActive: true}); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, 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 update shield mode status (%d)", res.StatusCode)
}
2024-03-03 15:14:59 -05:00
var data UpdateShieldModeStatusResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}