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
|
package matterclient
import (
"crypto/md5"
"encoding/json"
"fmt"
"strings"
"github.com/mattermost/mattermost-server/v6/model"
)
func (m *Client) parseResponse(rmsg *model.WebSocketResponse) {
m.logger.Debugf("getting response: %#v", rmsg)
}
func (m *Client) DeleteMessage(postID string) error {
_, err := m.Client.DeletePost(postID)
if err != nil {
return err
}
return nil
}
func (m *Client) EditMessage(postID string, text string) (string, error) {
post := &model.Post{Message: text, Id: postID}
res, _, err := m.Client.UpdatePost(postID, post)
if err != nil {
return "", err
}
return res.Id, nil
}
func (m *Client) GetFileLinks(filenames []string) []string {
uriScheme := "https://"
if m.NoTLS {
uriScheme = "http://"
}
var output []string
for _, f := range filenames {
res, _, err := m.Client.GetFileLink(f)
if err != nil {
// public links is probably disabled, create the link ourselves
output = append(output, uriScheme+m.Credentials.Server+model.APIURLSuffix+"/files/"+f)
continue
}
output = append(output, res)
}
return output
}
func (m *Client) GetPosts(channelID string, limit int) *model.PostList {
for {
res, resp, err := m.Client.GetPostsForChannel(channelID, 0, limit, "", true)
if err == nil {
return res
}
if err := m.HandleRatelimit("GetPostsForChannel", resp); err != nil {
return nil
}
}
}
func (m *Client) GetPostsSince(channelID string, time int64) *model.PostList {
for {
res, resp, err := m.Client.GetPostsSince(channelID, time, true)
if err == nil {
return res
}
if err := m.HandleRatelimit("GetPostsSince", resp); err != nil {
return nil
}
}
}
func (m *Client) GetPublicLink(filename string) string {
res, _, err := m.Client.GetFileLink(filename)
if err != nil {
return ""
}
return res
}
func (m *Client) GetPublicLinks(filenames []string) []string {
var output []string
for _, f := range filenames {
res, _, err := m.Client.GetFileLink(f)
if err != nil {
continue
}
output = append(output, res)
}
return output
}
func (m *Client) PostMessage(channelID string, text string, rootID string) (string, error) {
post := &model.Post{
ChannelId: channelID,
Message: text,
RootId: rootID,
}
for {
res, resp, err := m.Client.CreatePost(post)
if err == nil {
return res.Id, nil
}
if err := m.HandleRatelimit("CreatePost", resp); err != nil {
return "", err
}
}
}
func (m *Client) PostMessageWithFiles(channelID string, text string, rootID string, fileIds []string) (string, error) {
post := &model.Post{
ChannelId: channelID,
Message: text,
RootId: rootID,
FileIds: fileIds,
}
for {
res, resp, err := m.Client.CreatePost(post)
if err == nil {
return res.Id, nil
}
if err := m.HandleRatelimit("CreatePost", resp); err != nil {
return "", err
}
}
}
func (m *Client) SearchPosts(query string) *model.PostList {
res, _, err := m.Client.SearchPosts(m.Team.ID, query, false)
if err != nil {
return nil
}
return res
}
// SendDirectMessage sends a direct message to specified user
func (m *Client) SendDirectMessage(toUserID string, msg string, rootID string) error {
return m.SendDirectMessageProps(toUserID, msg, rootID, nil)
}
func (m *Client) SendDirectMessageProps(toUserID string, msg string, rootID string, props map[string]interface{}) error {
m.logger.Debugf("SendDirectMessage to %s, msg %s", toUserID, msg)
for {
// create DM channel (only happens on first message)
_, resp, err := m.Client.CreateDirectChannel(m.User.Id, toUserID)
if err == nil {
break
}
if err := m.HandleRatelimit("CreateDirectChannel", resp); err != nil {
m.logger.Debugf("SendDirectMessage to %#v failed: %s", toUserID, err)
return err
}
}
channelName := model.GetDMNameFromIds(toUserID, m.User.Id)
// update our channels
if err := m.UpdateChannels(); err != nil {
m.logger.Errorf("failed to update channels: %#v", err)
}
// build & send the message
msg = strings.ReplaceAll(msg, "\r", "")
post := &model.Post{
ChannelId: m.GetChannelID(channelName, m.Team.ID),
Message: msg,
RootId: rootID,
}
post.SetProps(props)
for {
_, resp, err := m.Client.CreatePost(post)
if err == nil {
return nil
}
if err := m.HandleRatelimit("CreatePost", resp); err != nil {
return err
}
}
}
func (m *Client) UploadFile(data []byte, channelID string, filename string) (string, error) {
f, _, err := m.Client.UploadFile(data, channelID, filename)
if err != nil {
return "", err
}
return f.FileInfos[0].Id, nil
}
func (m *Client) parseActionPost(rmsg *Message) {
// add post to cache, if it already exists don't relay this again.
// this should fix reposts
if ok, _ := m.lruCache.ContainsOrAdd(digestString(rmsg.Raw.GetData()["post"].(string)), true); ok {
m.logger.Debugf("message %#v in cache, not processing again", rmsg.Raw.GetData()["post"].(string))
rmsg.Text = ""
return
}
var data model.Post
if err := json.NewDecoder(strings.NewReader(rmsg.Raw.GetData()["post"].(string))).Decode(&data); err != nil {
return
}
// we don't have the user, refresh the userlist
if m.GetUser(data.UserId) == nil {
m.logger.Infof("User '%v' is not known, ignoring message '%#v'",
data.UserId, data)
return
}
rmsg.Username = m.GetUserName(data.UserId)
rmsg.Channel = m.GetChannelName(data.ChannelId)
rmsg.UserID = data.UserId
rmsg.Type = data.Type
teamid, _ := rmsg.Raw.GetData()["team_id"].(string)
// edit messsages have no team_id for some reason
if teamid == "" {
// we can find the team_id from the channelid
teamid = m.GetChannelTeamID(data.ChannelId)
rmsg.Raw.GetData()["team_id"] = teamid
}
if teamid != "" {
rmsg.Team = m.GetTeamName(teamid)
}
// direct message
if rmsg.Raw.GetData()["channel_type"] == "D" {
rmsg.Channel = m.GetUser(data.UserId).Username
}
rmsg.Text = data.Message
rmsg.Post = &data
}
func (m *Client) parseMessage(rmsg *Message) {
switch rmsg.Raw.EventType() {
case model.WebsocketEventPosted, model.WebsocketEventPostEdited, model.WebsocketEventPostDeleted:
m.parseActionPost(rmsg)
case "user_updated":
if user, ok := rmsg.Raw.GetData()["user"].(*model.User); ok {
m.UpdateUser(user.Id)
}
case "group_added":
if err := m.UpdateChannels(); err != nil {
m.logger.Errorf("failed to update channels: %#v", err)
}
}
}
func digestString(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s))) //nolint:gosec
}
|