-
Notifications
You must be signed in to change notification settings - Fork 18
/
team.go
231 lines (185 loc) · 6.03 KB
/
team.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package pluginapi
import (
"bytes"
"io"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
)
// TeamService exposes methods to manipulate teams and their members.
type TeamService struct {
api plugin.API
}
// Get gets a team.
//
// Minimum server version: 5.2
func (t *TeamService) Get(teamID string) (*model.Team, error) {
team, appErr := t.api.GetTeam(teamID)
return team, normalizeAppErr(appErr)
}
// GetByName gets a team by its name.
//
// Minimum server version: 5.2
func (t *TeamService) GetByName(name string) (*model.Team, error) {
team, appErr := t.api.GetTeamByName(name)
return team, normalizeAppErr(appErr)
}
// TeamListOption is used to filter team listing.
type TeamListOption func(*ListTeamsOptions)
// ListTeamsOptions holds options about filter out team listing.
type ListTeamsOptions struct {
UserID string
}
// FilterTeamsByUser option is used to filter teams by user.
func FilterTeamsByUser(userID string) TeamListOption {
return func(o *ListTeamsOptions) {
o.UserID = userID
}
}
// List gets a list of teams by options.
//
// Minimum server version: 5.2
// Minimum server version when LimitTeamsToUser() option is used: 5.6
func (t *TeamService) List(options ...TeamListOption) ([]*model.Team, error) {
opts := ListTeamsOptions{}
for _, o := range options {
o(&opts)
}
var teams []*model.Team
var appErr *model.AppError
if opts.UserID != "" {
teams, appErr = t.api.GetTeamsForUser(opts.UserID)
} else {
teams, appErr = t.api.GetTeams()
}
return teams, normalizeAppErr(appErr)
}
// Search search a team.
//
// Minimum server version: 5.8
func (t *TeamService) Search(term string) ([]*model.Team, error) {
teams, appErr := t.api.SearchTeams(term)
return teams, normalizeAppErr(appErr)
}
// Create creates a team.
//
// Minimum server version: 5.2
func (t *TeamService) Create(team *model.Team) error {
createdTeam, appErr := t.api.CreateTeam(team)
if appErr != nil {
return normalizeAppErr(appErr)
}
*team = *createdTeam
return nil
}
// Update updates a team.
//
// Minimum server version: 5.2
func (t *TeamService) Update(team *model.Team) error {
updatedTeam, appErr := t.api.UpdateTeam(team)
if appErr != nil {
return normalizeAppErr(appErr)
}
*team = *updatedTeam
return nil
}
// Delete deletes a team.
//
// Minimum server version: 5.2
func (t *TeamService) Delete(teamID string) error {
return normalizeAppErr(t.api.DeleteTeam(teamID))
}
// GetIcon gets the team icon.
//
// Minimum server version: 5.6
func (t *TeamService) GetIcon(teamID string) (io.Reader, error) {
contentBytes, appErr := t.api.GetTeamIcon(teamID)
if appErr != nil {
return nil, normalizeAppErr(appErr)
}
return bytes.NewReader(contentBytes), nil
}
// SetIcon sets the team icon.
//
// Minimum server version: 5.6
func (t *TeamService) SetIcon(teamID string, content io.Reader) error {
contentBytes, err := io.ReadAll(content)
if err != nil {
return err
}
return normalizeAppErr(t.api.SetTeamIcon(teamID, contentBytes))
}
// DeleteIcon removes the team icon.
//
// Minimum server version: 5.6
func (t *TeamService) DeleteIcon(teamID string) error {
return normalizeAppErr(t.api.RemoveTeamIcon(teamID))
}
// GetUsers lists users of the team.
//
// Minimum server version: 5.6
func (t *TeamService) ListUsers(teamID string, page, count int) ([]*model.User, error) {
users, appErr := t.api.GetUsersInTeam(teamID, page, count)
return users, normalizeAppErr(appErr)
}
// ListUnreadForUser gets the unread message and mention counts for each team to which the given user belongs.
//
// Minimum server version: 5.6
func (t *TeamService) ListUnreadForUser(userID string) ([]*model.TeamUnread, error) {
teamUnreads, appErr := t.api.GetTeamsUnreadForUser(userID)
return teamUnreads, normalizeAppErr(appErr)
}
// GetMember returns a specific membership.
//
// Minimum server version: 5.2
func (t *TeamService) GetMember(teamID, userID string) (*model.TeamMember, error) {
teamMember, appErr := t.api.GetTeamMember(teamID, userID)
return teamMember, normalizeAppErr(appErr)
}
// ListMembers returns the memberships of a specific team.
//
// Minimum server version: 5.2
func (t *TeamService) ListMembers(teamID string, page, perPage int) ([]*model.TeamMember, error) {
teamMembers, appErr := t.api.GetTeamMembers(teamID, page, perPage)
return teamMembers, normalizeAppErr(appErr)
}
// ListMembersForUser returns all team memberships for a user.
//
// Minimum server version: 5.10
func (t *TeamService) ListMembersForUser(userID string, page, perPage int) ([]*model.TeamMember, error) {
teamMembers, appErr := t.api.GetTeamMembersForUser(userID, page, perPage)
return teamMembers, normalizeAppErr(appErr)
}
// CreateMember creates a team membership.
//
// Minimum server version: 5.2
func (t *TeamService) CreateMember(teamID, userID string) (*model.TeamMember, error) {
teamMember, appErr := t.api.CreateTeamMember(teamID, userID)
return teamMember, normalizeAppErr(appErr)
}
// CreateMembers creates a team membership for all provided user ids.
//
// Minimum server version: 5.2
func (t *TeamService) CreateMembers(teamID string, userIDs []string, requestorID string) ([]*model.TeamMember, error) {
teamMembers, appErr := t.api.CreateTeamMembers(teamID, userIDs, requestorID)
return teamMembers, normalizeAppErr(appErr)
}
// DeleteMember deletes a team membership.
//
// Minimum server version: 5.2
func (t *TeamService) DeleteMember(teamID, userID, requestorID string) error {
return normalizeAppErr(t.api.DeleteTeamMember(teamID, userID, requestorID))
}
// UpdateMemberRoles updates the role for a team membership.
//
// Minimum server version: 5.2
func (t *TeamService) UpdateMemberRoles(teamID, userID, newRoles string) (*model.TeamMember, error) {
teamMember, appErr := t.api.UpdateTeamMemberRoles(teamID, userID, newRoles)
return teamMember, normalizeAppErr(appErr)
}
// GetStats gets a team's statistics
//
// Minimum server version: 5.8
func (t *TeamService) GetStats(teamID string) (*model.TeamStats, error) {
teamStats, appErr := t.api.GetTeamStats(teamID)
return teamStats, normalizeAppErr(appErr)
}