-
Notifications
You must be signed in to change notification settings - Fork 18
/
channel.go
285 lines (226 loc) · 8.87 KB
/
channel.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package pluginapi
import (
"net/http"
"time"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
"github.com/pkg/errors"
)
// ChannelService exposes methods to manipulate channels.
type ChannelService struct {
api plugin.API
}
// Get gets a channel.
//
// Minimum server version: 5.2
func (c *ChannelService) Get(channelID string) (*model.Channel, error) {
channel, appErr := c.api.GetChannel(channelID)
return channel, normalizeAppErr(appErr)
}
// GetByName gets a channel by its name, given a team id.
//
// Minimum server version: 5.2
func (c *ChannelService) GetByName(teamID, channelName string, includeDeleted bool) (*model.Channel, error) {
channel, appErr := c.api.GetChannelByName(teamID, channelName, includeDeleted)
return channel, normalizeAppErr(appErr)
}
// GetDirect gets a direct message channel.
//
// Note that if the channel does not exist it will create it.
//
// Minimum server version: 5.2
func (c *ChannelService) GetDirect(userID1, userID2 string) (*model.Channel, error) {
channel, appErr := c.api.GetDirectChannel(userID1, userID2)
return channel, normalizeAppErr(appErr)
}
// GetGroup gets a group message channel.
//
// Note that if the channel does not exist it will create it.
//
// Minimum server version: 5.2
func (c *ChannelService) GetGroup(userIDs []string) (*model.Channel, error) {
channel, appErr := c.api.GetGroupChannel(userIDs)
return channel, normalizeAppErr(appErr)
}
// GetByNameForTeamName gets a channel by its name, given a team name.
//
// Minimum server version: 5.2
func (c *ChannelService) GetByNameForTeamName(teamName, channelName string, includeDeleted bool) (*model.Channel, error) {
channel, appErr := c.api.GetChannelByNameForTeamName(teamName, channelName, includeDeleted)
return channel, normalizeAppErr(appErr)
}
// ListForTeamForUser gets a list of channels for given user ID in given team ID.
//
// Minimum server version: 5.6
func (c *ChannelService) ListForTeamForUser(teamID, userID string, includeDeleted bool) ([]*model.Channel, error) {
channels, appErr := c.api.GetChannelsForTeamForUser(teamID, userID, includeDeleted)
return channels, normalizeAppErr(appErr)
}
// ListPublicChannelsForTeam gets a list of all channels.
//
// Minimum server version: 5.2
func (c *ChannelService) ListPublicChannelsForTeam(teamID string, page, perPage int) ([]*model.Channel, error) {
channels, appErr := c.api.GetPublicChannelsForTeam(teamID, page, perPage)
return channels, normalizeAppErr(appErr)
}
// Search returns the channels on a team matching the provided search term.
//
// Minimum server version: 5.6
func (c *ChannelService) Search(teamID, term string) ([]*model.Channel, error) {
channels, appErr := c.api.SearchChannels(teamID, term)
return channels, normalizeAppErr(appErr)
}
// Create creates a channel.
//
// Minimum server version: 5.2
func (c *ChannelService) Create(channel *model.Channel) error {
createdChannel, appErr := c.api.CreateChannel(channel)
if appErr != nil {
return normalizeAppErr(appErr)
}
*channel = *createdChannel
return c.waitForChannelCreation(channel.Id)
}
// Update updates a channel.
//
// Minimum server version: 5.2
func (c *ChannelService) Update(channel *model.Channel) error {
updatedChannel, appErr := c.api.UpdateChannel(channel)
if appErr != nil {
return normalizeAppErr(appErr)
}
*channel = *updatedChannel
return nil
}
// Delete deletes a channel.
//
// Minimum server version: 5.2
func (c *ChannelService) Delete(channelID string) error {
return normalizeAppErr(c.api.DeleteChannel(channelID))
}
// GetChannelStats gets statistics for a channel.
//
// Minimum server version: 5.6
func (c *ChannelService) GetChannelStats(channelID string) (*model.ChannelStats, error) {
channelStats, appErr := c.api.GetChannelStats(channelID)
return channelStats, normalizeAppErr(appErr)
}
// GetMember gets a channel membership for a user.
//
// Minimum server version: 5.2
func (c *ChannelService) GetMember(channelID, userID string) (*model.ChannelMember, error) {
channelMember, appErr := c.api.GetChannelMember(channelID, userID)
return channelMember, normalizeAppErr(appErr)
}
// ListMembers gets a channel membership for all users.
//
// Minimum server version: 5.6
func (c *ChannelService) ListMembers(channelID string, page, perPage int) ([]*model.ChannelMember, error) {
channelMembers, appErr := c.api.GetChannelMembers(channelID, page, perPage)
return channelMembersToChannelMemberSlice(channelMembers), normalizeAppErr(appErr)
}
// ListMembersByIDs gets a channel membership for a particular User
//
// Minimum server version: 5.6
func (c *ChannelService) ListMembersByIDs(channelID string, userIDs []string) ([]*model.ChannelMember, error) {
channelMembers, appErr := c.api.GetChannelMembersByIds(channelID, userIDs)
return channelMembersToChannelMemberSlice(channelMembers), normalizeAppErr(appErr)
}
// ListMembersForUser returns all channel memberships on a team for a user.
//
// Minimum server version: 5.10
func (c *ChannelService) ListMembersForUser(teamID, userID string, page, perPage int) ([]*model.ChannelMember, error) {
channelMembers, appErr := c.api.GetChannelMembersForUser(teamID, userID, page, perPage)
return channelMembers, normalizeAppErr(appErr)
}
// AddMember joins a user to a channel (as if they joined themselves).
// This means the user will not receive notifications for joining the channel.
//
// Minimum server version: 5.2
func (c *ChannelService) AddMember(channelID, userID string) (*model.ChannelMember, error) {
channelMember, appErr := c.api.AddChannelMember(channelID, userID)
return channelMember, normalizeAppErr(appErr)
}
// AddUser adds a user to a channel as if the specified user had invited them.
// This means the user will receive the regular notifications for being added to the channel.
//
// Minimum server version: 5.18
func (c *ChannelService) AddUser(channelID, userID, asUserID string) (*model.ChannelMember, error) {
channelMember, appErr := c.api.AddUserToChannel(channelID, userID, asUserID)
return channelMember, normalizeAppErr(appErr)
}
// DeleteMember deletes a channel membership for a user.
//
// Minimum server version: 5.2
func (c *ChannelService) DeleteMember(channelID, userID string) error {
appErr := c.api.DeleteChannelMember(channelID, userID)
return normalizeAppErr(appErr)
}
// UpdateChannelMemberRoles updates a user's roles for a channel.
//
// Minimum server version: 5.2
func (c *ChannelService) UpdateChannelMemberRoles(channelID, userID, newRoles string) (*model.ChannelMember, error) {
channelMember, appErr := c.api.UpdateChannelMemberRoles(channelID, userID, newRoles)
return channelMember, normalizeAppErr(appErr)
}
// UpdateChannelMemberNotifications updates a user's notification properties for a channel.
//
// Minimum server version: 5.2
func (c *ChannelService) UpdateChannelMemberNotifications(channelID, userID string, notifications map[string]string) (*model.ChannelMember, error) {
channelMember, appErr := c.api.UpdateChannelMemberNotifications(channelID, userID, notifications)
return channelMember, normalizeAppErr(appErr)
}
// CreateSidebarCategory creates a new sidebar category for a set of channels.
//
// Minimum server version: 5.38
func (c *ChannelService) CreateSidebarCategory(
userID, teamID string, newCategory *model.SidebarCategoryWithChannels) error {
category, appErr := c.api.CreateChannelSidebarCategory(userID, teamID, newCategory)
if appErr != nil {
return normalizeAppErr(appErr)
}
*newCategory = *category
return nil
}
// GetSidebarCategories returns sidebar categories.
//
// Minimum server version: 5.38
func (c *ChannelService) GetSidebarCategories(userID, teamID string) (*model.OrderedSidebarCategories, error) {
categories, appErr := c.api.GetChannelSidebarCategories(userID, teamID)
return categories, normalizeAppErr(appErr)
}
// UpdateSidebarCategories updates the channel sidebar categories.
//
// Minimum server version: 5.38
func (c *ChannelService) UpdateSidebarCategories(
userID, teamID string, categories []*model.SidebarCategoryWithChannels) error {
updatedCategories, appErr := c.api.UpdateChannelSidebarCategories(userID, teamID, categories)
if appErr != nil {
return normalizeAppErr(appErr)
}
copy(categories, updatedCategories)
return nil
}
func (c *ChannelService) waitForChannelCreation(channelID string) error {
if len(c.api.GetConfig().SqlSettings.DataSourceReplicas) == 0 {
return nil
}
now := time.Now()
for time.Since(now) < 1500*time.Millisecond {
time.Sleep(100 * time.Millisecond)
if _, err := c.api.GetChannel(channelID); err == nil {
// Channel found
return nil
} else if err.StatusCode != http.StatusNotFound {
return err
}
}
return errors.Errorf("giving up waiting for channel creation, channelID=%s", channelID)
}
func channelMembersToChannelMemberSlice(cm model.ChannelMembers) []*model.ChannelMember {
cmp := make([]*model.ChannelMember, len(cm))
for i := 0; i < len(cm); i++ {
cmp[i] = &(cm)[i]
}
return cmp
}