Debian Codegraph @

usergroups.go

raw symbols SCIP
1 package slack
2
3 import (
4 "context"
5 "net/url"
6 "strings"
7 )
8
9 // UserGroup contains all the information of a user group
10 type UserGroup struct {
11 ID string `json:"id"`
12 TeamID string `json:"team_id"`
13 IsUserGroup bool `json:"is_usergroup"`
14 Name string `json:"name"`
15 Description string `json:"description"`
16 Handle string `json:"handle"`
17 IsExternal bool `json:"is_external"`
18 DateCreate JSONTime `json:"date_create"`
19 DateUpdate JSONTime `json:"date_update"`
20 DateDelete JSONTime `json:"date_delete"`
21 AutoType string `json:"auto_type"`
22 CreatedBy string `json:"created_by"`
23 UpdatedBy string `json:"updated_by"`
24 DeletedBy string `json:"deleted_by"`
25 Prefs UserGroupPrefs `json:"prefs"`
26 UserCount int `json:"user_count"`
27 Users []string `json:"users"`
28 }
29
30 // UserGroupPrefs contains default channels and groups (private channels)
31 type UserGroupPrefs struct {
32 Channels []string `json:"channels"`
33 Groups []string `json:"groups"`
34 }
35
36 type userGroupResponseFull struct {
37 UserGroups []UserGroup `json:"usergroups"`
38 UserGroup UserGroup `json:"usergroup"`
39 Users []string `json:"users"`
40 SlackResponse
41 }
42
43 func (api *Client) userGroupRequest(ctx context.Context, path string, values url.Values) (*userGroupResponseFull, error) {
44 response := &userGroupResponseFull{}
45 err := api.postMethod(ctx, path, values, response)
46 if err != nil {
47 return nil, err
48 }
49
50 return response, response.Err()
51 }
52
53 // CreateUserGroup creates a new user group
54 func (api *Client) CreateUserGroup(userGroup UserGroup) (UserGroup, error) {
55 return api.CreateUserGroupContext(context.Background(), userGroup)
56 }
57
58 // CreateUserGroupContext creates a new user group with a custom context
59 func (api *Client) CreateUserGroupContext(ctx context.Context, userGroup UserGroup) (UserGroup, error) {
60 values := url.Values{
61 "token": {api.token},
62 "name": {userGroup.Name},
63 }
64
65 if userGroup.Handle != "" {
66 values["handle"] = []string{userGroup.Handle}
67 }
68
69 if userGroup.Description != "" {
70 values["description"] = []string{userGroup.Description}
71 }
72
73 if len(userGroup.Prefs.Channels) > 0 {
74 values["channels"] = []string{strings.Join(userGroup.Prefs.Channels, ",")}
75 }
76
77 response, err := api.userGroupRequest(ctx, "usergroups.create", values)
78 if err != nil {
79 return UserGroup{}, err
80 }
81 return response.UserGroup, nil
82 }
83
84 // DisableUserGroup disables an existing user group
85 func (api *Client) DisableUserGroup(userGroup string) (UserGroup, error) {
86 return api.DisableUserGroupContext(context.Background(), userGroup)
87 }
88
89 // DisableUserGroupContext disables an existing user group with a custom context
90 func (api *Client) DisableUserGroupContext(ctx context.Context, userGroup string) (UserGroup, error) {
91 values := url.Values{
92 "token": {api.token},
93 "usergroup": {userGroup},
94 }
95
96 response, err := api.userGroupRequest(ctx, "usergroups.disable", values)
97 if err != nil {
98 return UserGroup{}, err
99 }
100 return response.UserGroup, nil
101 }
102
103 // EnableUserGroup enables an existing user group
104 func (api *Client) EnableUserGroup(userGroup string) (UserGroup, error) {
105 return api.EnableUserGroupContext(context.Background(), userGroup)
106 }
107
108 // EnableUserGroupContext enables an existing user group with a custom context
109 func (api *Client) EnableUserGroupContext(ctx context.Context, userGroup string) (UserGroup, error) {
110 values := url.Values{
111 "token": {api.token},
112 "usergroup": {userGroup},
113 }
114
115 response, err := api.userGroupRequest(ctx, "usergroups.enable", values)
116 if err != nil {
117 return UserGroup{}, err
118 }
119 return response.UserGroup, nil
120 }
121
122 // GetUserGroupsOption options for the GetUserGroups method call.
123 type GetUserGroupsOption func(*GetUserGroupsParams)
124
125 // GetUserGroupsOptionIncludeCount include the number of users in each User Group (default: false)
126 func GetUserGroupsOptionIncludeCount(b bool) GetUserGroupsOption {
127 return func(params *GetUserGroupsParams) {
128 params.IncludeCount = b
129 }
130 }
131
132 // GetUserGroupsOptionIncludeDisabled include disabled User Groups (default: false)
133 func GetUserGroupsOptionIncludeDisabled(b bool) GetUserGroupsOption {
134 return func(params *GetUserGroupsParams) {
135 params.IncludeDisabled = b
136 }
137 }
138
139 // GetUserGroupsOptionIncludeUsers include the list of users for each User Group (default: false)
140 func GetUserGroupsOptionIncludeUsers(b bool) GetUserGroupsOption {
141 return func(params *GetUserGroupsParams) {
142 params.IncludeUsers = b
143 }
144 }
145
146 // GetUserGroupsParams contains arguments for GetUserGroups method call
147 type GetUserGroupsParams struct {
148 IncludeCount bool
149 IncludeDisabled bool
150 IncludeUsers bool
151 }
152
153 // GetUserGroups returns a list of user groups for the team
154 func (api *Client) GetUserGroups(options ...GetUserGroupsOption) ([]UserGroup, error) {
155 return api.GetUserGroupsContext(context.Background(), options...)
156 }
157
158 // GetUserGroupsContext returns a list of user groups for the team with a custom context
159 func (api *Client) GetUserGroupsContext(ctx context.Context, options ...GetUserGroupsOption) ([]UserGroup, error) {
160 params := GetUserGroupsParams{}
161
162 for _, opt := range options {
163 opt(&params)
164 }
165
166 values := url.Values{
167 "token": {api.token},
168 }
169 if params.IncludeCount {
170 values.Add("include_count", "true")
171 }
172 if params.IncludeDisabled {
173 values.Add("include_disabled", "true")
174 }
175 if params.IncludeUsers {
176 values.Add("include_users", "true")
177 }
178
179 response, err := api.userGroupRequest(ctx, "usergroups.list", values)
180 if err != nil {
181 return nil, err
182 }
183 return response.UserGroups, nil
184 }
185
186 // UpdateUserGroupsOption options for the UpdateUserGroup method call.
187 type UpdateUserGroupsOption func(*UpdateUserGroupsParams)
188
189 // UpdateUserGroupsOptionName change the name of the User Group (default: empty, so it's no-op)
190 func UpdateUserGroupsOptionName(name string) UpdateUserGroupsOption {
191 return func(params *UpdateUserGroupsParams) {
192 params.Name = name
193 }
194 }
195
196 // UpdateUserGroupsOptionHandle change the handle of the User Group (default: empty, so it's no-op)
197 func UpdateUserGroupsOptionHandle(handle string) UpdateUserGroupsOption {
198 return func(params *UpdateUserGroupsParams) {
199 params.Handle = handle
200 }
201 }
202
203 // UpdateUserGroupsOptionDescription change the description of the User Group. (default: nil, so it's no-op)
204 func UpdateUserGroupsOptionDescription(description *string) UpdateUserGroupsOption {
205 return func(params *UpdateUserGroupsParams) {
206 params.Description = description
207 }
208 }
209
210 // UpdateUserGroupsOptionChannels change the default channels of the User Group. (default: unspecified, so it's no-op)
211 func UpdateUserGroupsOptionChannels(channels []string) UpdateUserGroupsOption {
212 return func(params *UpdateUserGroupsParams) {
213 params.Channels = &channels
214 }
215 }
216
217 // UpdateUserGroupsParams contains arguments for UpdateUserGroup method call
218 type UpdateUserGroupsParams struct {
219 Name string
220 Handle string
221 Description *string
222 Channels *[]string
223 }
224
225 // UpdateUserGroup will update an existing user group
226 func (api *Client) UpdateUserGroup(userGroupID string, options ...UpdateUserGroupsOption) (UserGroup, error) {
227 return api.UpdateUserGroupContext(context.Background(), userGroupID, options...)
228 }
229
230 // UpdateUserGroupContext will update an existing user group with a custom context
231 func (api *Client) UpdateUserGroupContext(ctx context.Context, userGroupID string, options ...UpdateUserGroupsOption) (UserGroup, error) {
232 params := UpdateUserGroupsParams{}
233
234 for _, opt := range options {
235 opt(&params)
236 }
237
238 values := url.Values{
239 "token": {api.token},
240 "usergroup": {userGroupID},
241 }
242
243 if params.Name != "" {
244 values["name"] = []string{params.Name}
245 }
246
247 if params.Handle != "" {
248 values["handle"] = []string{params.Handle}
249 }
250
251 if params.Description != nil {
252 values["description"] = []string{*params.Description}
253 }
254
255 if params.Channels != nil {
256 values["channels"] = []string{strings.Join(*params.Channels, ",")}
257 }
258
259 response, err := api.userGroupRequest(ctx, "usergroups.update", values)
260 if err != nil {
261 return UserGroup{}, err
262 }
263 return response.UserGroup, nil
264 }
265
266 // GetUserGroupMembers will retrieve the current list of users in a group
267 func (api *Client) GetUserGroupMembers(userGroup string) ([]string, error) {
268 return api.GetUserGroupMembersContext(context.Background(), userGroup)
269 }
270
271 // GetUserGroupMembersContext will retrieve the current list of users in a group with a custom context
272 func (api *Client) GetUserGroupMembersContext(ctx context.Context, userGroup string) ([]string, error) {
273 values := url.Values{
274 "token": {api.token},
275 "usergroup": {userGroup},
276 }
277
278 response, err := api.userGroupRequest(ctx, "usergroups.users.list", values)
279 if err != nil {
280 return []string{}, err
281 }
282 return response.Users, nil
283 }
284
285 // UpdateUserGroupMembers will update the members of an existing user group
286 func (api *Client) UpdateUserGroupMembers(userGroup string, members string) (UserGroup, error) {
287 return api.UpdateUserGroupMembersContext(context.Background(), userGroup, members)
288 }
289
290 // UpdateUserGroupMembersContext will update the members of an existing user group with a custom context
291 func (api *Client) UpdateUserGroupMembersContext(ctx context.Context, userGroup string, members string) (UserGroup, error) {
292 values := url.Values{
293 "token": {api.token},
294 "usergroup": {userGroup},
295 "users": {members},
296 }
297
298 response, err := api.userGroupRequest(ctx, "usergroups.users.update", values)
299 if err != nil {
300 return UserGroup{}, err
301 }
302 return response.UserGroup, nil
303 }