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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
package matterclient
import (
"strings"
"github.com/mattermost/mattermost-server/v6/model"
)
// GetChannels returns all channels we're members off
func (m *Client) GetChannels() []*model.Channel {
m.RLock()
defer m.RUnlock()
var channels []*model.Channel
// our primary team channels first
channels = append(channels, m.Team.Channels...)
for _, t := range m.OtherTeams {
if t.ID != m.Team.ID {
channels = append(channels, t.Channels...)
}
}
return channels
}
func (m *Client) GetChannelHeader(channelID string) string {
m.RLock()
defer m.RUnlock()
for _, t := range m.OtherTeams {
for _, channel := range append(t.Channels, t.MoreChannels...) {
if channel.Id == channelID {
return channel.Header
}
}
}
return ""
}
func getNormalisedName(channel *model.Channel) string {
if channel.Type == model.ChannelTypeGroup {
res := strings.ReplaceAll(channel.DisplayName, ", ", "-")
res = strings.ReplaceAll(res, " ", "_")
return res
}
return channel.Name
}
func (m *Client) GetChannelID(name string, teamID string) string {
m.RLock()
defer m.RUnlock()
if teamID != "" {
return m.getChannelIDTeam(name, teamID)
}
for _, t := range m.OtherTeams {
for _, channel := range append(t.Channels, t.MoreChannels...) {
if getNormalisedName(channel) == name {
return channel.Id
}
}
}
return ""
}
func (m *Client) getChannelIDTeam(name string, teamID string) string {
for _, t := range m.OtherTeams {
if t.ID == teamID {
for _, channel := range append(t.Channels, t.MoreChannels...) {
if getNormalisedName(channel) == name {
return channel.Id
}
}
}
}
// Fallback if it's not found in the t.Channels or t.MoreChannels cache.
// This also let's us join private channels.
channel, _, err := m.Client.GetChannelByName(name, teamID, "")
if err != nil {
return ""
}
return channel.Id
}
func (m *Client) GetChannelName(channelID string) string {
m.RLock()
defer m.RUnlock()
for _, t := range m.OtherTeams {
if t == nil {
continue
}
for _, channel := range append(t.Channels, t.MoreChannels...) {
if channel.Id == channelID {
return getNormalisedName(channel)
}
}
}
return ""
}
func (m *Client) GetChannelTeamID(id string) string {
m.RLock()
defer m.RUnlock()
for _, t := range append(m.OtherTeams, m.Team) {
for _, channel := range append(t.Channels, t.MoreChannels...) {
if channel.Id == id {
return channel.TeamId
}
}
}
return ""
}
func (m *Client) GetLastViewedAt(channelID string) int64 {
m.RLock()
defer m.RUnlock()
for {
res, resp, err := m.Client.GetChannelMember(channelID, m.User.Id, "")
if err == nil {
return res.LastViewedAt
}
if err := m.HandleRatelimit("GetChannelMember", resp); err != nil {
return model.GetMillis()
}
}
}
// GetMoreChannels returns existing channels where we're not a member off.
func (m *Client) GetMoreChannels() []*model.Channel {
m.RLock()
defer m.RUnlock()
var channels []*model.Channel
for _, t := range m.OtherTeams {
channels = append(channels, t.MoreChannels...)
}
return channels
}
// GetTeamFromChannel returns teamId belonging to channel (DM channels have no teamId).
func (m *Client) GetTeamFromChannel(channelID string) string {
m.RLock()
defer m.RUnlock()
var channels []*model.Channel
for _, t := range m.OtherTeams {
channels = append(channels, t.Channels...)
if t.MoreChannels != nil {
channels = append(channels, t.MoreChannels...)
}
for _, c := range channels {
if c.Id == channelID {
if c.Type == model.ChannelTypeGroup {
return "G"
}
return t.ID
}
}
channels = nil
}
return ""
}
func (m *Client) JoinChannel(channelID string) error {
m.RLock()
defer m.RUnlock()
for _, c := range m.Team.Channels {
if c.Id == channelID {
m.logger.Debug("Not joining ", channelID, " already joined.")
return nil
}
}
m.logger.Debug("Joining ", channelID)
_, _, err := m.Client.AddChannelMember(channelID, m.User.Id)
if err != nil {
return err
}
return nil
}
func (m *Client) UpdateChannelsTeam(teamID string) error {
var (
mmchannels []*model.Channel
resp *model.Response
err error
)
for {
mmchannels, resp, err = m.Client.GetChannelsForTeamForUser(teamID, m.User.Id, false, "")
if err == nil {
break
}
if err = m.HandleRatelimit("GetChannelsForTeamForUser", resp); err != nil {
return err
}
}
for idx, t := range m.OtherTeams {
if t.ID == teamID {
m.Lock()
m.OtherTeams[idx].Channels = mmchannels
m.Unlock()
}
}
idx := 0
max := 200
var moreChannels []*model.Channel
for {
mmchannels, resp, err = m.Client.GetPublicChannelsForTeam(teamID, idx, max, "")
if err == nil {
break
}
if err := m.HandleRatelimit("GetPublicChannelsForTeam", resp); err != nil {
return err
}
}
for len(mmchannels) > 0 {
moreChannels = append(moreChannels, mmchannels...)
for {
mmchannels, resp, err = m.Client.GetPublicChannelsForTeam(teamID, idx, max, "")
if err == nil {
idx++
break
}
if err := m.HandleRatelimit("GetPublicChannelsForTeam", resp); err != nil {
return err
}
}
}
for idx, t := range m.OtherTeams {
if t.ID == teamID {
m.Lock()
m.OtherTeams[idx].MoreChannels = moreChannels
m.Unlock()
}
}
return nil
}
func (m *Client) UpdateChannels() error {
if err := m.UpdateChannelsTeam(m.Team.ID); err != nil {
return err
}
for _, t := range m.OtherTeams {
// We've already populated users/channels for team in the above.
if t.ID == m.Team.ID {
continue
}
if err := m.UpdateChannelsTeam(t.ID); err != nil {
return err
}
}
return nil
}
func (m *Client) UpdateChannelHeader(channelID string, header string) {
channel := &model.Channel{Id: channelID, Header: header}
m.logger.Debugf("updating channelheader %#v, %#v", channelID, header)
_, _, err := m.Client.UpdateChannel(channel)
if err != nil {
m.logger.Error(err)
}
}
func (m *Client) UpdateLastViewed(channelID string) error {
m.logger.Debugf("posting lastview %#v", channelID)
view := &model.ChannelView{ChannelId: channelID}
for {
_, resp, err := m.Client.ViewChannel(m.User.Id, view)
if err == nil {
return nil
}
if err := m.HandleRatelimit("ViewChannel", resp); err != nil {
m.logger.Errorf("ChannelView update for %s failed: %s", channelID, err)
return err
}
}
}
|