matterbridge/vendor/github.com/nlopes/slack/team.go

166 lines
4.8 KiB
Go
Raw Normal View History

2016-09-05 07:34:37 -07:00
package slack
import (
2017-07-16 05:29:46 -07:00
"context"
2016-09-05 07:34:37 -07:00
"net/url"
2016-11-05 16:07:24 -07:00
"strconv"
)
const (
2017-07-16 05:29:46 -07:00
DEFAULT_LOGINS_COUNT = 100
DEFAULT_LOGINS_PAGE = 1
2016-09-05 07:34:37 -07:00
)
type TeamResponse struct {
Team TeamInfo `json:"team"`
SlackResponse
}
type TeamInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Domain string `json:"domain"`
EmailDomain string `json:"email_domain"`
Icon map[string]interface{} `json:"icon"`
}
2016-11-05 16:07:24 -07:00
type LoginResponse struct {
Logins []Login `json:"logins"`
2017-07-16 05:29:46 -07:00
Paging `json:"paging"`
2016-11-05 16:07:24 -07:00
SlackResponse
}
type Login struct {
UserID string `json:"user_id"`
Username string `json:"username"`
DateFirst int `json:"date_first"`
DateLast int `json:"date_last"`
Count int `json:"count"`
IP string `json:"ip"`
UserAgent string `json:"user_agent"`
ISP string `json:"isp"`
Country string `json:"country"`
Region string `json:"region"`
}
2017-01-27 15:36:22 -08:00
type BillableInfoResponse struct {
BillableInfo map[string]BillingActive `json:"billable_info"`
SlackResponse
}
type BillingActive struct {
BillingActive bool `json:"billing_active"`
}
2016-11-05 16:07:24 -07:00
// AccessLogParameters contains all the parameters necessary (including the optional ones) for a GetAccessLogs() request
type AccessLogParameters struct {
2017-07-16 05:29:46 -07:00
Count int
Page int
2016-11-05 16:07:24 -07:00
}
// NewAccessLogParameters provides an instance of AccessLogParameters with all the sane default values set
func NewAccessLogParameters() AccessLogParameters {
return AccessLogParameters{
Count: DEFAULT_LOGINS_COUNT,
Page: DEFAULT_LOGINS_PAGE,
}
}
2018-12-01 10:55:35 -08:00
func teamRequest(ctx context.Context, client httpClient, path string, values url.Values, d debug) (*TeamResponse, error) {
2016-09-05 07:34:37 -07:00
response := &TeamResponse{}
2018-12-01 10:55:35 -08:00
err := postSlackMethod(ctx, client, path, values, response, d)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
2018-12-01 10:55:35 -08:00
return response, response.Err()
2016-09-05 07:34:37 -07:00
}
2018-12-01 10:55:35 -08:00
func billableInfoRequest(ctx context.Context, client httpClient, path string, values url.Values, d debug) (map[string]BillingActive, error) {
2017-01-27 15:36:22 -08:00
response := &BillableInfoResponse{}
2018-12-01 10:55:35 -08:00
err := postSlackMethod(ctx, client, path, values, response, d)
2017-01-27 15:36:22 -08:00
if err != nil {
return nil, err
}
2018-12-01 10:55:35 -08:00
return response.BillableInfo, response.Err()
2017-01-27 15:36:22 -08:00
}
2018-12-01 10:55:35 -08:00
func accessLogsRequest(ctx context.Context, client httpClient, path string, values url.Values, d debug) (*LoginResponse, error) {
2016-11-05 16:07:24 -07:00
response := &LoginResponse{}
2018-12-01 10:55:35 -08:00
err := postSlackMethod(ctx, client, path, values, response, d)
2016-11-05 16:07:24 -07:00
if err != nil {
return nil, err
}
2018-12-01 10:55:35 -08:00
return response, response.Err()
2016-11-05 16:07:24 -07:00
}
2016-09-05 07:34:37 -07:00
// GetTeamInfo gets the Team Information of the user
func (api *Client) GetTeamInfo() (*TeamInfo, error) {
2017-07-16 05:29:46 -07:00
return api.GetTeamInfoContext(context.Background())
}
// GetTeamInfoContext gets the Team Information of the user with a custom context
func (api *Client) GetTeamInfoContext(ctx context.Context) (*TeamInfo, error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2016-09-05 07:34:37 -07:00
}
2018-12-01 10:55:35 -08:00
response, err := teamRequest(ctx, api.httpclient, "team.info", values, api)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
return &response.Team, nil
}
2016-11-05 16:07:24 -07:00
// GetAccessLogs retrieves a page of logins according to the parameters given
func (api *Client) GetAccessLogs(params AccessLogParameters) ([]Login, *Paging, error) {
2017-07-16 05:29:46 -07:00
return api.GetAccessLogsContext(context.Background(), params)
}
// GetAccessLogsContext retrieves a page of logins according to the parameters given with a custom context
func (api *Client) GetAccessLogsContext(ctx context.Context, params AccessLogParameters) ([]Login, *Paging, error) {
2016-11-05 16:07:24 -07:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2016-11-05 16:07:24 -07:00
}
if params.Count != DEFAULT_LOGINS_COUNT {
values.Add("count", strconv.Itoa(params.Count))
}
if params.Page != DEFAULT_LOGINS_PAGE {
values.Add("page", strconv.Itoa(params.Page))
}
2018-08-09 15:38:19 -07:00
2018-12-01 10:55:35 -08:00
response, err := accessLogsRequest(ctx, api.httpclient, "team.accessLogs", values, api)
2016-11-05 16:07:24 -07:00
if err != nil {
return nil, nil, err
}
return response.Logins, &response.Paging, nil
}
2017-01-27 15:36:22 -08:00
func (api *Client) GetBillableInfo(user string) (map[string]BillingActive, error) {
2017-07-16 05:29:46 -07:00
return api.GetBillableInfoContext(context.Background(), user)
}
func (api *Client) GetBillableInfoContext(ctx context.Context, user string) (map[string]BillingActive, error) {
2017-01-27 15:36:22 -08:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2017-07-16 05:29:46 -07:00
"user": {user},
2017-01-27 15:36:22 -08:00
}
2018-12-01 10:55:35 -08:00
return billableInfoRequest(ctx, api.httpclient, "team.billableInfo", values, api)
2017-01-27 15:36:22 -08:00
}
// GetBillableInfoForTeam returns the billing_active status of all users on the team.
func (api *Client) GetBillableInfoForTeam() (map[string]BillingActive, error) {
2017-07-16 05:29:46 -07:00
return api.GetBillableInfoForTeamContext(context.Background())
}
// GetBillableInfoForTeamContext returns the billing_active status of all users on the team with a custom context
func (api *Client) GetBillableInfoForTeamContext(ctx context.Context) (map[string]BillingActive, error) {
2017-01-27 15:36:22 -08:00
values := url.Values{
2018-08-09 15:38:19 -07:00
"token": {api.token},
2017-01-27 15:36:22 -08:00
}
2018-12-01 10:55:35 -08:00
return billableInfoRequest(ctx, api.httpclient, "team.billableInfo", values, api)
2017-01-27 15:36:22 -08:00
}