70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package moderation
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"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 broadcaster’s 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 broadcaster’s updated Shield Mode status.
|
||
Data []ShieldModeStatus `json:"data"`
|
||
}
|
||
|
||
// Activates or deactivates the broadcaster’s Shield Mode.
|
||
//
|
||
// Twitch’s 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 hasn’t 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()
|
||
|
||
var data UpdateShieldModeStatusResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|