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
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GroupMe documentation: https://dev.groupme.com/docs/v3#leaderboard
|
|
|
|
|
2021-01-22 21:47:22 +00:00
|
|
|
/*//////// Endpoints ////////*/
|
2020-07-30 02:20:57 +00:00
|
|
|
const (
|
|
|
|
// Used to build other endpoints
|
|
|
|
leaderboardEndpointRoot = groupEndpointRoot + "/likes"
|
|
|
|
|
|
|
|
// Actual Endpoints
|
|
|
|
indexLeaderboardEndpoint = leaderboardEndpointRoot // GET
|
|
|
|
myLikesLeaderboardEndpoint = leaderboardEndpointRoot + "/mine" // GET
|
|
|
|
myHitsLeaderboardEndpoint = leaderboardEndpointRoot + "/for_me" // GET
|
|
|
|
)
|
|
|
|
|
2021-01-22 21:47:22 +00:00
|
|
|
/*//////// API Requests ////////*/
|
2020-07-30 02:20:57 +00:00
|
|
|
|
|
|
|
// Index
|
|
|
|
|
|
|
|
type period string
|
|
|
|
|
|
|
|
// Define acceptable period values
|
|
|
|
const (
|
2021-01-22 21:47:22 +00:00
|
|
|
PeriodDay = "day"
|
|
|
|
PeriodWeek = "week"
|
|
|
|
PeriodMonth = "month"
|
2020-07-30 02:20:57 +00:00
|
|
|
)
|
|
|
|
|
2021-01-22 21:47:22 +00:00
|
|
|
// IndexLeaderboard - A list of the liked messages in the group for a given period of
|
|
|
|
// time. Messages are ranked in order of number of likes.
|
2023-09-20 02:08:46 +00:00
|
|
|
func (c *Client) IndexLeaderboard(ctx context.Context, groupID ID, p period, authToken string) ([]*Message, error) {
|
2020-07-30 02:20:57 +00:00
|
|
|
url := fmt.Sprintf(c.endpointBase+indexLeaderboardEndpoint, groupID)
|
|
|
|
httpReq, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
URL := httpReq.URL
|
|
|
|
query := URL.Query()
|
2021-01-22 21:47:22 +00:00
|
|
|
query.Set("period", string(p))
|
2020-07-30 02:20:57 +00:00
|
|
|
URL.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Messages []*Message `json:"messages"`
|
|
|
|
}
|
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.Messages, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// My Likes
|
|
|
|
|
|
|
|
/*
|
|
|
|
MyLikesLeaderboard -
|
|
|
|
|
|
|
|
A list of messages you have liked. Messages are returned in
|
|
|
|
reverse chrono-order. Note that the payload includes a liked_at
|
|
|
|
timestamp in ISO-8601 format.
|
|
|
|
|
|
|
|
Parameters:
|
2023-09-20 02:08:46 +00:00
|
|
|
|
2020-07-30 02:20:57 +00:00
|
|
|
groupID - required, ID(string)
|
|
|
|
*/
|
2023-09-20 02:08:46 +00:00
|
|
|
func (c *Client) MyLikesLeaderboard(ctx context.Context, groupID ID, authToken string) ([]*Message, error) {
|
2020-07-30 02:20:57 +00:00
|
|
|
url := fmt.Sprintf(c.endpointBase+myLikesLeaderboardEndpoint, groupID)
|
|
|
|
httpReq, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Messages []*Message `json:"messages"`
|
|
|
|
}
|
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.Messages, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// My Hits
|
|
|
|
|
|
|
|
/*
|
|
|
|
MyHitsLeaderboard -
|
|
|
|
|
|
|
|
A list of messages you have liked. Messages are returned in
|
|
|
|
reverse chrono-order. Note that the payload includes a liked_at
|
|
|
|
timestamp in ISO-8601 format.
|
|
|
|
|
|
|
|
Parameters:
|
2023-09-20 02:08:46 +00:00
|
|
|
|
2020-07-30 02:20:57 +00:00
|
|
|
groupID - required, ID(string)
|
|
|
|
*/
|
2023-09-20 02:08:46 +00:00
|
|
|
func (c *Client) MyHitsLeaderboard(ctx context.Context, groupID ID, authToken string) ([]*Message, error) {
|
2020-07-30 02:20:57 +00:00
|
|
|
url := fmt.Sprintf(c.endpointBase+myHitsLeaderboardEndpoint, groupID)
|
|
|
|
httpReq, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Messages []*Message `json:"messages"`
|
|
|
|
}
|
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.Messages, nil
|
|
|
|
}
|