groupme-lib/blocks_api.go

116 lines
2.9 KiB
Go
Raw Normal View History

// Package groupme defines a client capable of executing API commands for the GroupMe chat service
package groupme
import (
"context"
"net/http"
)
// GroupMe documentation: https://dev.groupme.com/docs/v3#blocks
/*//////// Endpoints ////////*/
const (
// Used to build other endpoints
blocksEndpointRoot = "/blocks"
// Actual Endpoints
indexBlocksEndpoint = blocksEndpointRoot // GET
blockBetweenEndpoint = blocksEndpointRoot + "/between" // GET
createBlockEndpoint = blocksEndpointRoot // POST
unblockEndpoint = blocksEndpointRoot // DELETE
)
/*//////// API Requests ////////*/
// IndexBlock - A list of contacts you have blocked. These people cannot DM you
func (c *Client) IndexBlock(ctx context.Context, userID string, authToken string) ([]*Block, error) {
httpReq, err := http.NewRequest("GET", c.endpointBase+indexBlocksEndpoint, nil)
if err != nil {
return nil, err
}
URL := httpReq.URL
query := URL.Query()
query.Set("user", userID)
URL.RawQuery = query.Encode()
var resp struct {
Blocks []*Block `json:"blocks"`
}
err = c.doWithAuthToken(ctx, httpReq, &resp, authToken)
if err != nil {
return nil, err
}
return resp.Blocks, nil
}
// BlockBetween - Asks if a block exists between you and another user id
func (c *Client) BlockBetween(ctx context.Context, userID, otherUserID string, authToken string) (bool, error) {
httpReq, err := http.NewRequest("GET", c.endpointBase+blockBetweenEndpoint, nil)
if err != nil {
return false, err
}
URL := httpReq.URL
query := URL.Query()
query.Set("user", userID)
query.Set("otherUser", otherUserID)
URL.RawQuery = query.Encode()
var resp struct {
Between bool `json:"between"`
}
err = c.doWithAuthToken(ctx, httpReq, &resp, authToken)
if err != nil {
return false, err
}
return resp.Between, nil
}
// CreateBlock - Creates a block between you and the contact
func (c *Client) CreateBlock(ctx context.Context, userID, otherUserID string, authToken string) (*Block, error) {
httpReq, err := http.NewRequest("POST", c.endpointBase+createBlockEndpoint, nil)
if err != nil {
return nil, err
}
URL := httpReq.URL
query := URL.Query()
query.Set("user", userID)
query.Set("otherUser", otherUserID)
URL.RawQuery = query.Encode()
var resp struct {
Block *Block `json:"block"`
}
err = c.doWithAuthToken(ctx, httpReq, &resp, authToken)
if err != nil {
return nil, err
}
return resp.Block, nil
}
// Unblock - Removes block between you and other user
func (c *Client) Unblock(ctx context.Context, userID, otherUserID string, authToken string) error {
httpReq, err := http.NewRequest("DELETE", c.endpointBase+unblockEndpoint, nil)
if err != nil {
return err
}
URL := httpReq.URL
query := URL.Query()
query.Set("user", userID)
query.Set("otherUser", otherUserID)
URL.RawQuery = query.Encode()
err = c.doWithAuthToken(ctx, httpReq, nil, authToken)
if err != nil {
return err
}
return nil
}