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

374 lines
12 KiB
Go
Raw Normal View History

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"
"strconv"
)
// Group contains all the information for a group
type Group struct {
groupConversation
IsGroup bool `json:"is_group"`
}
type groupResponseFull struct {
Group Group `json:"group"`
Groups []Group `json:"groups"`
Purpose string `json:"purpose"`
Topic string `json:"topic"`
NotInGroup bool `json:"not_in_group"`
NoOp bool `json:"no_op"`
AlreadyClosed bool `json:"already_closed"`
AlreadyOpen bool `json:"already_open"`
AlreadyInGroup bool `json:"already_in_group"`
Channel Channel `json:"channel"`
History
SlackResponse
}
2018-12-01 18:55:35 +00:00
func groupRequest(ctx context.Context, client httpClient, path string, values url.Values, d debug) (*groupResponseFull, error) {
2016-09-05 14:34:37 +00:00
response := &groupResponseFull{}
2018-12-01 18:55:35 +00:00
err := postForm(ctx, client, APIURL+path, values, response, d)
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
}
// ArchiveGroup archives a private group
func (api *Client) ArchiveGroup(group string) error {
2017-07-16 12:29:46 +00:00
return api.ArchiveGroupContext(context.Background(), group)
}
2018-08-09 22:38:19 +00:00
// ArchiveGroupContext archives a private group
2017-07-16 12:29:46 +00:00
func (api *Client) ArchiveGroupContext(ctx context.Context, group string) 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
"channel": {group},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
_, err := groupRequest(ctx, api.httpclient, "groups.archive", values, api)
2018-08-09 22:38:19 +00:00
return err
2016-09-05 14:34:37 +00:00
}
// UnarchiveGroup unarchives a private group
func (api *Client) UnarchiveGroup(group string) error {
2017-07-16 12:29:46 +00:00
return api.UnarchiveGroupContext(context.Background(), group)
}
2018-08-09 22:38:19 +00:00
// UnarchiveGroupContext unarchives a private group
2017-07-16 12:29:46 +00:00
func (api *Client) UnarchiveGroupContext(ctx context.Context, group string) 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
"channel": {group},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
_, err := groupRequest(ctx, api.httpclient, "groups.unarchive", values, api)
2018-08-09 22:38:19 +00:00
return err
2016-09-05 14:34:37 +00:00
}
// CreateGroup creates a private group
func (api *Client) CreateGroup(group string) (*Group, error) {
2017-07-16 12:29:46 +00:00
return api.CreateGroupContext(context.Background(), group)
}
2018-08-09 22:38:19 +00:00
// CreateGroupContext creates a private group
2017-07-16 12:29:46 +00:00
func (api *Client) CreateGroupContext(ctx context.Context, group string) (*Group, 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
"name": {group},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.create", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
return &response.Group, nil
}
// CreateChildGroup creates a new private group archiving the old one
// This method takes an existing private group and performs the following steps:
// 1. Renames the existing group (from "example" to "example-archived").
// 2. Archives the existing group.
// 3. Creates a new group with the name of the existing group.
// 4. Adds all members of the existing group to the new group.
func (api *Client) CreateChildGroup(group string) (*Group, error) {
2017-07-16 12:29:46 +00:00
return api.CreateChildGroupContext(context.Background(), group)
}
2018-08-09 22:38:19 +00:00
// CreateChildGroupContext creates a new private group archiving the old one with a custom context
2017-07-16 12:29:46 +00:00
// For more information see CreateChildGroup
func (api *Client) CreateChildGroupContext(ctx context.Context, group string) (*Group, 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
"channel": {group},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.createChild", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
return &response.Group, nil
}
// CloseGroup closes a private group
func (api *Client) CloseGroup(group string) (bool, bool, error) {
2017-07-16 12:29:46 +00:00
return api.CloseGroupContext(context.Background(), group)
}
// CloseGroupContext closes a private group with a custom context
func (api *Client) CloseGroupContext(ctx context.Context, group string) (bool, bool, 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
"channel": {group},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := imRequest(ctx, api.httpclient, "groups.close", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return false, false, err
}
return response.NoOp, response.AlreadyClosed, nil
}
// GetGroupHistory fetches all the history for a private group
func (api *Client) GetGroupHistory(group string, params HistoryParameters) (*History, error) {
2017-07-16 12:29:46 +00:00
return api.GetGroupHistoryContext(context.Background(), group, params)
}
// GetGroupHistoryContext fetches all the history for a private group with a custom context
func (api *Client) GetGroupHistoryContext(ctx context.Context, group string, params HistoryParameters) (*History, 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
"channel": {group},
}
if params.Latest != DEFAULT_HISTORY_LATEST {
values.Add("latest", params.Latest)
}
if params.Oldest != DEFAULT_HISTORY_OLDEST {
values.Add("oldest", params.Oldest)
}
if params.Count != DEFAULT_HISTORY_COUNT {
values.Add("count", strconv.Itoa(params.Count))
}
if params.Inclusive != DEFAULT_HISTORY_INCLUSIVE {
if params.Inclusive {
values.Add("inclusive", "1")
} else {
values.Add("inclusive", "0")
}
}
if params.Unreads != DEFAULT_HISTORY_UNREADS {
if params.Unreads {
values.Add("unreads", "1")
} else {
values.Add("unreads", "0")
}
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.history", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
return &response.History, nil
}
// InviteUserToGroup invites a specific user to a private group
func (api *Client) InviteUserToGroup(group, user string) (*Group, bool, error) {
2017-07-16 12:29:46 +00:00
return api.InviteUserToGroupContext(context.Background(), group, user)
}
// InviteUserToGroupContext invites a specific user to a private group with a custom context
func (api *Client) InviteUserToGroupContext(ctx context.Context, group, user string) (*Group, bool, 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
"channel": {group},
"user": {user},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.invite", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, false, err
}
return &response.Group, response.AlreadyInGroup, nil
}
// LeaveGroup makes authenticated user leave the group
func (api *Client) LeaveGroup(group string) error {
2017-07-16 12:29:46 +00:00
return api.LeaveGroupContext(context.Background(), group)
}
// LeaveGroupContext makes authenticated user leave the group with a custom context
2018-08-09 22:38:19 +00:00
func (api *Client) LeaveGroupContext(ctx context.Context, group string) (err 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
"channel": {group},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
_, err = groupRequest(ctx, api.httpclient, "groups.leave", values, api)
2018-01-08 21:41:38 +00:00
return err
2016-09-05 14:34:37 +00:00
}
// KickUserFromGroup kicks a user from a group
func (api *Client) KickUserFromGroup(group, user string) error {
2017-07-16 12:29:46 +00:00
return api.KickUserFromGroupContext(context.Background(), group, user)
}
// KickUserFromGroupContext kicks a user from a group with a custom context
2018-08-09 22:38:19 +00:00
func (api *Client) KickUserFromGroupContext(ctx context.Context, group, user string) (err 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
"channel": {group},
"user": {user},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
_, err = groupRequest(ctx, api.httpclient, "groups.kick", values, api)
2018-01-08 21:41:38 +00:00
return err
2016-09-05 14:34:37 +00:00
}
// GetGroups retrieves all groups
func (api *Client) GetGroups(excludeArchived bool) ([]Group, error) {
2017-07-16 12:29:46 +00:00
return api.GetGroupsContext(context.Background(), excludeArchived)
}
// GetGroupsContext retrieves all groups with a custom context
func (api *Client) GetGroupsContext(ctx context.Context, excludeArchived bool) ([]Group, 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
}
if excludeArchived {
values.Add("exclude_archived", "1")
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.list", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
return response.Groups, nil
}
// GetGroupInfo retrieves the given group
func (api *Client) GetGroupInfo(group string) (*Group, error) {
2017-07-16 12:29:46 +00:00
return api.GetGroupInfoContext(context.Background(), group)
}
// GetGroupInfoContext retrieves the given group with a custom context
func (api *Client) GetGroupInfoContext(ctx context.Context, group string) (*Group, 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
"channel": {group},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.info", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
return &response.Group, nil
}
// SetGroupReadMark sets the read mark on a private group
// Clients should try to avoid making this call too often. When needing to mark a read position, a client should set a
// timer before making the call. In this way, any further updates needed during the timeout will not generate extra
// calls (just one per channel). This is useful for when reading scroll-back history, or following a busy live
// channel. A timeout of 5 seconds is a good starting point. Be sure to flush these calls on shutdown/logout.
func (api *Client) SetGroupReadMark(group, ts string) error {
2017-07-16 12:29:46 +00:00
return api.SetGroupReadMarkContext(context.Background(), group, ts)
}
// SetGroupReadMarkContext sets the read mark on a private group with a custom context
// For more details see SetGroupReadMark
2018-08-09 22:38:19 +00:00
func (api *Client) SetGroupReadMarkContext(ctx context.Context, group, ts string) (err 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
"channel": {group},
"ts": {ts},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
_, err = groupRequest(ctx, api.httpclient, "groups.mark", values, api)
2018-01-08 21:41:38 +00:00
return err
2016-09-05 14:34:37 +00:00
}
// OpenGroup opens a private group
func (api *Client) OpenGroup(group string) (bool, bool, error) {
2017-07-16 12:29:46 +00:00
return api.OpenGroupContext(context.Background(), group)
}
// OpenGroupContext opens a private group with a custom context
func (api *Client) OpenGroupContext(ctx context.Context, group string) (bool, bool, 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
"channel": {group},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.open", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return false, false, err
}
return response.NoOp, response.AlreadyOpen, nil
}
// RenameGroup renames a group
// XXX: They return a channel, not a group. What is this crap? :(
// Inconsistent api it seems.
func (api *Client) RenameGroup(group, name string) (*Channel, error) {
2017-07-16 12:29:46 +00:00
return api.RenameGroupContext(context.Background(), group, name)
}
// RenameGroupContext renames a group with a custom context
func (api *Client) RenameGroupContext(ctx context.Context, group, name string) (*Channel, 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
"channel": {group},
"name": {name},
}
2018-08-09 22:38:19 +00:00
2016-09-05 14:34:37 +00:00
// XXX: the created entry in this call returns a string instead of a number
// so I may have to do some workaround to solve it.
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.rename", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
return &response.Channel, nil
}
// SetGroupPurpose sets the group purpose
func (api *Client) SetGroupPurpose(group, purpose string) (string, error) {
2017-07-16 12:29:46 +00:00
return api.SetGroupPurposeContext(context.Background(), group, purpose)
}
// SetGroupPurposeContext sets the group purpose with a custom context
func (api *Client) SetGroupPurposeContext(ctx context.Context, group, purpose string) (string, 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
"channel": {group},
"purpose": {purpose},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.setPurpose", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return "", err
}
return response.Purpose, nil
}
// SetGroupTopic sets the group topic
func (api *Client) SetGroupTopic(group, topic string) (string, error) {
2017-07-16 12:29:46 +00:00
return api.SetGroupTopicContext(context.Background(), group, topic)
}
// SetGroupTopicContext sets the group topic with a custom context
func (api *Client) SetGroupTopicContext(ctx context.Context, group, topic string) (string, 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
"channel": {group},
"topic": {topic},
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
response, err := groupRequest(ctx, api.httpclient, "groups.setTopic", values, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return "", err
}
return response.Topic, nil
}