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
|
package matterclient
import (
"github.com/mattermost/mattermost-server/v6/model"
)
func (m *Client) GetNickName(userID string) string {
if user := m.GetUser(userID); user != nil {
return user.Nickname
}
return ""
}
func (m *Client) GetStatus(userID string) string {
res, _, err := m.Client.GetUserStatus(userID, "")
if err != nil {
return ""
}
if res.Status == model.StatusAway {
return "away"
}
if res.Status == model.StatusOnline {
return "online"
}
return "offline"
}
func (m *Client) GetStatuses() map[string]string {
var ids []string
statuses := make(map[string]string)
for id := range m.Users {
ids = append(ids, id)
}
res, _, err := m.Client.GetUsersStatusesByIds(ids)
if err != nil {
return statuses
}
for _, status := range res {
statuses[status.UserId] = "offline"
if status.Status == model.StatusAway {
statuses[status.UserId] = "away"
}
if status.Status == model.StatusOnline {
statuses[status.UserId] = "online"
}
}
return statuses
}
func (m *Client) GetTeamID() string {
return m.Team.ID
}
// GetTeamName returns the name of the specified teamId
func (m *Client) GetTeamName(teamID string) string {
m.RLock()
defer m.RUnlock()
for _, t := range m.OtherTeams {
if t.ID == teamID {
return t.Team.Name
}
}
return ""
}
func (m *Client) GetUser(userID string) *model.User {
m.Lock()
defer m.Unlock()
_, ok := m.Users[userID]
if !ok {
res, _, err := m.Client.GetUser(userID, "")
if err != nil {
return nil
}
m.Users[userID] = res
}
return m.Users[userID]
}
func (m *Client) GetUserName(userID string) string {
if user := m.GetUser(userID); user != nil {
return user.Username
}
return ""
}
func (m *Client) GetUsers() map[string]*model.User {
users := make(map[string]*model.User)
m.RLock()
defer m.RUnlock()
for k, v := range m.Users {
users[k] = v
}
return users
}
func (m *Client) UpdateUsers() error {
idx := 0
max := 200
var (
mmusers []*model.User
resp *model.Response
err error
)
for {
mmusers, resp, err = m.Client.GetUsers(idx, max, "")
if err == nil {
break
}
if err = m.HandleRatelimit("GetUsers", resp); err != nil {
return err
}
}
for len(mmusers) > 0 {
m.Lock()
for _, user := range mmusers {
m.Users[user.Id] = user
}
m.Unlock()
for {
mmusers, resp, err = m.Client.GetUsers(idx, max, "")
if err == nil {
idx++
break
}
if err := m.HandleRatelimit("GetUsers", resp); err != nil {
return err
}
}
}
return nil
}
func (m *Client) UpdateUserNick(nick string) error {
user := m.User
user.Nickname = nick
_, _, err := m.Client.UpdateUser(user)
if err != nil {
return err
}
return nil
}
func (m *Client) UsernamesInChannel(channelID string) []string {
res, _, err := m.Client.GetChannelMembers(channelID, 0, 50000, "")
if err != nil {
m.logger.Errorf("UsernamesInChannel(%s) failed: %s", channelID, err)
return []string{}
}
allusers := m.GetUsers()
result := []string{}
for _, member := range res {
result = append(result, allusers[member.UserId].Nickname)
}
return result
}
func (m *Client) UpdateStatus(userID string, status string) error {
_, _, err := m.Client.UpdateUserStatus(userID, &model.Status{Status: status})
if err != nil {
return err
}
return nil
}
func (m *Client) UpdateUser(userID string) {
m.Lock()
defer m.Unlock()
res, _, err := m.Client.GetUser(userID, "")
if err != nil {
return
}
m.Users[userID] = res
}
|