-
Notifications
You must be signed in to change notification settings - Fork 1
/
group.go
135 lines (111 loc) · 4.13 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package pkgo
import (
"net/url"
"github.com/google/uuid"
)
// Group is a member group.
type Group struct {
ID string `json:"id"`
UUID uuid.UUID `json:"uuid"`
Name string `json:"name"`
DisplayName string `json:"display_name,omitempty"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
Banner string `json:"banner,omitempty"`
Color Color `json:"color"`
Privacy *GroupPrivacy `json:"privacy,omitempty"`
// Only returned in GroupsWithMembers
Members []uuid.UUID `json:"members,omitempty"`
}
// GroupPrivacy is a group privacy object.
type GroupPrivacy struct {
DescriptionPrivacy Privacy `json:"description_privacy,omitempty"`
IconPrivacy Privacy `json:"icon_privacy,omitempty"`
ListPrivacy Privacy `json:"list_privacy,omitempty"`
Visibility Privacy `json:"visibility,omitempty"`
}
// Groups gets a system's groups.
func (s *Session) Groups(systemID string) (g []Group, err error) {
err = s.RequestJSON("GET", "/systems/"+systemID+"/groups", &g)
return
}
// GroupsWithMembers gets a system's groups, with the Members field filled.
func (s *Session) GroupsWithMembers(systemID string) (g []Group, err error) {
err = s.RequestJSON("GET", "/systems/"+systemID+"/groups", &g, WithURLValues(url.Values{
"with_members": {"true"},
}))
return
}
// CreateGroupData is the data for s.CreateGroup.
type CreateGroupData struct {
Name string `json:"name"`
DisplayName string `json:"display_name,omitempty"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
Banner string `json:"banner,omitempty"`
Color Color `json:"color"`
Privacy *GroupPrivacy `json:"privacy,omitempty"`
}
// CreateGroup creates a group.
func (s *Session) CreateGroup(data CreateGroupData) (g Group, err error) {
err = s.RequestJSON("POST", "/groups", &g, WithJSONBody(data))
return
}
// Group returns a group by its ID or UUID.
func (s *Session) Group(id string) (g Group, err error) {
err = s.RequestJSON("GET", "/groups/"+id, &g)
return
}
// GroupByUUID returns a group by its UUID.
func (s *Session) GroupByUUID(id uuid.UUID) (g Group, err error) {
return s.Group(id.String())
}
// MemberGroups returns the groups the given member is in.
func (s *Session) MemberGroups(id string) (g []Group, err error) {
err = s.RequestJSON("GET", "/members/"+id+"/groups", &g)
return
}
// EditGroupData is the data for s.EditGroup.
type EditGroupData struct {
Name NullableString `json:"name,omitempty"`
DisplayName NullableString `json:"display_name,omitempty"`
Description NullableString `json:"description,omitempty"`
Icon NullableString `json:"icon,omitempty"`
Banner NullableString `json:"banner,omitempty"`
Color *Color `json:"color"`
Privacy *GroupPrivacy `json:"privacy,omitempty"`
}
// EditGroup edits the given group.
func (s *Session) EditGroup(id string, dat EditGroupData) (g Group, err error) {
err = s.RequestJSON("PATCH", "/groups/"+id, &g, WithJSONBody(dat))
return g, err
}
// DeleteGroup deletes a group. Requires authentication.
func (s *Session) DeleteGroup(id string) (err error) {
_, err = s.Request("DELETE", "/groups/"+id)
return
}
// AddGroupMembers adds the given member IDs to the given group.
func (s *Session) AddGroupMembers(groupID string, memberIDs []string) (err error) {
if memberIDs == nil {
memberIDs = []string{}
}
_, err = s.Request("POST", "/groups/"+groupID+"/members/add", WithJSONBody(memberIDs))
return
}
// RemoveGroupMembers removes the given member IDs from the given group.
func (s *Session) RemoveGroupMembers(groupID string, memberIDs []string) (err error) {
if memberIDs == nil {
memberIDs = []string{}
}
_, err = s.Request("POST", "/groups/"+groupID+"/members/remove", WithJSONBody(memberIDs))
return
}
// OverwriteGroupMembers overwrites a group's members with the given member IDs.
func (s *Session) OverwriteGroupMembers(groupID string, memberIDs []string) (err error) {
if memberIDs == nil {
memberIDs = []string{}
}
_, err = s.Request("POST", "/groups/"+groupID+"/members/overwrite", WithJSONBody(memberIDs))
return
}