2016-09-05 14:34:37 +00:00
|
|
|
package slack
|
|
|
|
|
|
|
|
import (
|
2017-07-16 12:29:46 +00:00
|
|
|
"context"
|
2016-09-05 14:34:37 +00:00
|
|
|
"net/url"
|
2016-11-05 23:07:24 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-07-16 12:29:46 +00:00
|
|
|
DEFAULT_LOGINS_COUNT = 100
|
|
|
|
DEFAULT_LOGINS_PAGE = 1
|
2016-09-05 14:34:37 +00: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"`
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
type TeamProfileResponse struct {
|
|
|
|
Profile TeamProfile `json:"profile"`
|
|
|
|
SlackResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
type TeamProfile struct {
|
|
|
|
Fields []TeamProfileField `json:"fields"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TeamProfileField struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Ordering int `json:"ordering"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
Hint string `json:"hint"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
PossibleValues []string `json:"possible_values"`
|
|
|
|
IsHidden bool `json:"is_hidden"`
|
|
|
|
Options map[string]bool `json:"options"`
|
|
|
|
}
|
|
|
|
|
2016-11-05 23:07:24 +00:00
|
|
|
type LoginResponse struct {
|
|
|
|
Logins []Login `json:"logins"`
|
2017-07-16 12:29:46 +00:00
|
|
|
Paging `json:"paging"`
|
2016-11-05 23:07:24 +00: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 23:36:22 +00:00
|
|
|
type BillableInfoResponse struct {
|
|
|
|
BillableInfo map[string]BillingActive `json:"billable_info"`
|
|
|
|
SlackResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
type BillingActive struct {
|
|
|
|
BillingActive bool `json:"billing_active"`
|
|
|
|
}
|
|
|
|
|
2016-11-05 23:07:24 +00:00
|
|
|
// AccessLogParameters contains all the parameters necessary (including the optional ones) for a GetAccessLogs() request
|
|
|
|
type AccessLogParameters struct {
|
2024-05-23 21:44:31 +00:00
|
|
|
TeamID string
|
|
|
|
Count int
|
|
|
|
Page int
|
2016-11-05 23:07:24 +00: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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 20:46:58 +00:00
|
|
|
func (api *Client) teamRequest(ctx context.Context, path string, values url.Values) (*TeamResponse, error) {
|
2016-09-05 14:34:37 +00:00
|
|
|
response := &TeamResponse{}
|
2019-09-07 20:46:58 +00:00
|
|
|
err := api.postMethod(ctx, path, values, response)
|
2016-09-05 14:34:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-01 18:55:35 +00:00
|
|
|
return response, response.Err()
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 20:46:58 +00:00
|
|
|
func (api *Client) billableInfoRequest(ctx context.Context, path string, values url.Values) (map[string]BillingActive, error) {
|
2017-01-27 23:36:22 +00:00
|
|
|
response := &BillableInfoResponse{}
|
2019-09-07 20:46:58 +00:00
|
|
|
err := api.postMethod(ctx, path, values, response)
|
2017-01-27 23:36:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-01 18:55:35 +00:00
|
|
|
return response.BillableInfo, response.Err()
|
2017-01-27 23:36:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 20:46:58 +00:00
|
|
|
func (api *Client) accessLogsRequest(ctx context.Context, path string, values url.Values) (*LoginResponse, error) {
|
2016-11-05 23:07:24 +00:00
|
|
|
response := &LoginResponse{}
|
2019-09-07 20:46:58 +00:00
|
|
|
err := api.postMethod(ctx, path, values, response)
|
2016-11-05 23:07:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-01 18:55:35 +00:00
|
|
|
return response, response.Err()
|
2016-11-05 23:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
func (api *Client) teamProfileRequest(ctx context.Context, client httpClient, path string, values url.Values) (*TeamProfileResponse, error) {
|
|
|
|
response := &TeamProfileResponse{}
|
|
|
|
err := api.postMethod(ctx, path, values, response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response, response.Err()
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:34:37 +00:00
|
|
|
// GetTeamInfo gets the Team Information of the user
|
|
|
|
func (api *Client) GetTeamInfo() (*TeamInfo, error) {
|
2017-07-16 12:29:46 +00:00
|
|
|
return api.GetTeamInfoContext(context.Background())
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
// GetOtherTeamInfoContext gets Team information for any team with a custom context
|
|
|
|
func (api *Client) GetOtherTeamInfoContext(ctx context.Context, team string) (*TeamInfo, error) {
|
|
|
|
if team == "" {
|
|
|
|
return api.GetTeamInfoContext(ctx)
|
|
|
|
}
|
|
|
|
values := url.Values{
|
|
|
|
"token": {api.token},
|
|
|
|
}
|
|
|
|
values.Add("team", team)
|
|
|
|
response, err := api.teamRequest(ctx, "team.info", values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &response.Team, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOtherTeamInfo gets Team information for any team
|
|
|
|
func (api *Client) GetOtherTeamInfo(team string) (*TeamInfo, error) {
|
|
|
|
return api.GetOtherTeamInfoContext(context.Background(), team)
|
|
|
|
}
|
|
|
|
|
2017-07-16 12:29:46 +00:00
|
|
|
// GetTeamInfoContext gets the Team Information of the user with a custom context
|
|
|
|
func (api *Client) GetTeamInfoContext(ctx context.Context) (*TeamInfo, error) {
|
2016-09-05 14:34:37 +00:00
|
|
|
values := url.Values{
|
2018-08-09 22:38:19 +00:00
|
|
|
"token": {api.token},
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 20:46:58 +00:00
|
|
|
response, err := api.teamRequest(ctx, "team.info", values)
|
2016-09-05 14:34:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &response.Team, nil
|
|
|
|
}
|
2016-11-05 23:07:24 +00:00
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
// GetTeamProfile gets the Team Profile settings of the user
|
2024-05-23 21:44:31 +00:00
|
|
|
func (api *Client) GetTeamProfile(teamID ...string) (*TeamProfile, error) {
|
|
|
|
return api.GetTeamProfileContext(context.Background(), teamID...)
|
2023-01-28 21:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeamProfileContext gets the Team Profile settings of the user with a custom context
|
2024-05-23 21:44:31 +00:00
|
|
|
func (api *Client) GetTeamProfileContext(ctx context.Context, teamID ...string) (*TeamProfile, error) {
|
2023-01-28 21:57:53 +00:00
|
|
|
values := url.Values{
|
|
|
|
"token": {api.token},
|
|
|
|
}
|
2024-05-23 21:44:31 +00:00
|
|
|
if len(teamID) > 0 {
|
|
|
|
values["team_id"] = teamID
|
|
|
|
}
|
2023-01-28 21:57:53 +00:00
|
|
|
|
|
|
|
response, err := api.teamProfileRequest(ctx, api.httpclient, "team.profile.get", values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &response.Profile, nil
|
|
|
|
}
|
|
|
|
|
2016-11-05 23:07:24 +00:00
|
|
|
// GetAccessLogs retrieves a page of logins according to the parameters given
|
|
|
|
func (api *Client) GetAccessLogs(params AccessLogParameters) ([]Login, *Paging, error) {
|
2017-07-16 12:29:46 +00: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 23:07:24 +00:00
|
|
|
values := url.Values{
|
2018-08-09 22:38:19 +00:00
|
|
|
"token": {api.token},
|
2016-11-05 23:07:24 +00:00
|
|
|
}
|
2024-05-23 21:44:31 +00:00
|
|
|
if params.TeamID != "" {
|
|
|
|
values.Add("team_id", params.TeamID)
|
|
|
|
}
|
2016-11-05 23:07:24 +00: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 22:38:19 +00:00
|
|
|
|
2019-09-07 20:46:58 +00:00
|
|
|
response, err := api.accessLogsRequest(ctx, "team.accessLogs", values)
|
2016-11-05 23:07:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return response.Logins, &response.Paging, nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 21:44:31 +00:00
|
|
|
type GetBillableInfoParams struct {
|
|
|
|
User string
|
|
|
|
TeamID string
|
|
|
|
}
|
|
|
|
|
2019-09-07 20:46:58 +00:00
|
|
|
// GetBillableInfo ...
|
2024-05-23 21:44:31 +00:00
|
|
|
func (api *Client) GetBillableInfo(params GetBillableInfoParams) (map[string]BillingActive, error) {
|
|
|
|
return api.GetBillableInfoContext(context.Background(), params)
|
2017-07-16 12:29:46 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 20:46:58 +00:00
|
|
|
// GetBillableInfoContext ...
|
2024-05-23 21:44:31 +00:00
|
|
|
func (api *Client) GetBillableInfoContext(ctx context.Context, params GetBillableInfoParams) (map[string]BillingActive, error) {
|
2017-01-27 23:36:22 +00:00
|
|
|
values := url.Values{
|
2018-08-09 22:38:19 +00:00
|
|
|
"token": {api.token},
|
2017-01-27 23:36:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 21:44:31 +00:00
|
|
|
if params.TeamID != "" {
|
|
|
|
values.Add("team_id", params.TeamID)
|
|
|
|
}
|
2017-07-16 12:29:46 +00:00
|
|
|
|
2024-05-23 21:44:31 +00:00
|
|
|
if params.User != "" {
|
|
|
|
values.Add("user", params.User)
|
2017-01-27 23:36:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 20:46:58 +00:00
|
|
|
return api.billableInfoRequest(ctx, "team.billableInfo", values)
|
2017-01-27 23:36:22 +00:00
|
|
|
}
|