go-twitch/api/users/block_user.go

56 lines
1.6 KiB
Go

package users
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
type BlockUserParams struct {
// The ID of the user to block. The API ignores the request if the broadcaster has already blocked the user.
TargetUserID string `url:"target_user_id"`
// The location where the harassment took place that is causing the brodcaster to block the user. Possible values are:
//
// chat, whisper
SourceContext *string `url:"source_context,omitempty"`
// The reason that the broadcaster is blocking the user. Possible values are:
//
// harassment, spam, other
Reason *string `url:"reason,omitempty"`
}
// Blocks the specified user from interacting with or having contact with the broadcaster.
// The user ID in the OAuth token identifies the broadcaster who is blocking the user.
//
// To learn more about blocking users,
// see Block Other Users on Twitch: https://help.twitch.tv/s/article/how-to-manage-harassment-in-chat?language=en_US#BlockWhispersandMessagesfromStrangers
//
// Requires a user access token that includes the user:manage:blocked_users scope.
func (u *Users) BlockUser(ctx context.Context, params *BlockUserParams) error {
v, _ := query.Values(params)
endpoint := u.baseUrl.ResolveReference(&url.URL{Path: "users/blocks", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodPut, endpoint.String(), nil)
if err != nil {
return err
}
res, err := u.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return fmt.Errorf("failed to block user (%d)", res.StatusCode)
}
return nil
}