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