Base for usergroup management
Sam Rudge authored 7 years ago
Matt Whiteley committed 7 years ago
0 | package slack | |
1 | ||
2 | import ( | |
3 | "errors" | |
4 | "net/url" | |
5 | ) | |
6 | ||
7 | ||
8 | // User contains all the information of a user group | |
9 | type UserGroup struct { | |
10 | ID string `json:"id"` | |
11 | Name string `json:"name"` | |
12 | TeamId string `json:"team_id"` | |
13 | IsUserGroup bool `json:"is_usergroup"` | |
14 | Description string `json:"description"` | |
15 | Handle string `json:"handle"` | |
16 | IsExternal bool `json:"is_external"` | |
17 | AutoType string `json:"auto_type"` | |
18 | CreatedBy string `json:"created_by"` | |
19 | UpdatedBy string `json:"updated_by"` | |
20 | DeletedBy string `json:"deleted_by"` | |
21 | Prefs struct{ | |
22 | Channels []string `json:"channels"` | |
23 | Groups []string `json:"groups"` | |
24 | } `json:"prefs"` | |
25 | Users []string | |
26 | UserCount int `json:"user_count"` | |
27 | } | |
28 | ||
29 | type userGroupResponseFull struct { | |
30 | UserGroups []UserGroup `json:"usergroups"` | |
31 | UserGroup UserGroup `json:"usergroup"` | |
32 | SlackResponse | |
33 | } | |
34 | ||
35 | func userGroupRequest(path string, values url.Values, debug bool) (*userGroupResponseFull, error) { | |
36 | response := &userGroupResponseFull{} | |
37 | err := post(path, values, response, debug) | |
38 | if err != nil { | |
39 | return nil, err | |
40 | } | |
41 | if !response.Ok { | |
42 | return nil, errors.New(response.Error) | |
43 | } | |
44 | return response, nil | |
45 | } | |
46 | ||
47 | // GetUserGroups returns a list of user groups for the team | |
48 | func (api *Client) GetUserGroups() ([]UserGroup, error){ | |
49 | values := url.Values{ | |
50 | "token": {api.config.token}, | |
51 | } | |
52 | response, err := userGroupRequest("usergroups.list", values, api.debug) | |
53 | if err != nil { | |
54 | return nil, err | |
55 | } | |
56 | return response.UserGroups, nil | |
57 | } | |
58 | ||
59 | func (api *Client) UpdateUserGroup(userGroup UserGroup) (UserGroup, error){ | |
60 | values := url.Values{ | |
61 | "token": {api.config.token}, | |
62 | "usergroup": {userGroup.ID}, | |
63 | } | |
64 | ||
65 | if userGroup.Name != ""{ | |
66 | values["name"] = []string{userGroup.Name} | |
67 | } | |
68 | ||
69 | if userGroup.Handle != ""{ | |
70 | values["handle"] = []string{userGroup.Handle} | |
71 | } | |
72 | ||
73 | if userGroup.Description != ""{ | |
74 | values["description"] = []string{userGroup.Description} | |
75 | } | |
76 | ||
77 | response, err := userGroupRequest("usergroups.update", values, api.debug) | |
78 | if err != nil { | |
79 | return UserGroup{}, err | |
80 | } | |
81 | return response.UserGroup, nil | |
82 | } |