-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmclient.go
180 lines (163 loc) · 4.94 KB
/
mmclient.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
package main
import (
"fmt"
"strings"
"github.com/mattermost/mattermost-server/v5/model"
)
// MMClient wraps a mattermost client and some data
type MMClient struct {
client *model.Client4
user *model.User
teams []*model.Team
teamsEtag string
channelsInTeam map[string][]*model.Channel
teamsToChannelsEtag map[string]string // map[teamId]etag
}
// NewMMClient Returns a new MMClient
func NewMMClient(server, username, password, caCertPath string) (*MMClient, error) {
client := model.NewAPIv4Client(server)
if caCertPath != "" {
fixedClient := loadCert(caCertPath)
client.HttpClient = fixedClient
}
var resp *model.Response
user, resp := client.Login(username, password)
if resp.StatusCode != 200 {
return nil, resp.Error
}
return &MMClient{
client: client,
user: user,
channelsInTeam: make(map[string][]*model.Channel),
teamsToChannelsEtag: make(map[string]string),
}, nil
}
// GetTeams returns a list of mattermost teams the user belongs to
func (mc *MMClient) GetTeams() ([]*model.Team, error) {
teams, resp := mc.client.GetTeamsForUser(mc.user.Id, mc.teamsEtag)
if resp.StatusCode != 200 {
return nil, resp.Error
}
if resp.Etag != "" && resp.Etag == mc.teamsEtag {
return mc.teams, nil
}
mc.teams = teams
mc.teamsEtag = resp.Etag
return teams, nil
}
// GetChannels returns a list of mattermost channels in a given team the user belongs to
func (mc *MMClient) GetChannels(teamId string) ([]*model.Channel, error) {
channels, resp := mc.client.GetChannelsForTeamForUser(
teamId, "me", false, mc.teamsToChannelsEtag[teamId])
if resp.StatusCode != 200 {
return nil, resp.Error
}
if resp.Etag != "" && resp.Etag == mc.teamsToChannelsEtag[teamId] {
return mc.channelsInTeam[teamId], nil
}
mc.channelsInTeam[teamId] = channels
mc.teamsToChannelsEtag[teamId] = resp.Etag
return channels, nil
}
// NormalizedChannel models a mattermost channel with normalized names.
// A mattermost channel can be a public channel, a private channel, a group
// or a direct message. This object tries to model a simple abstraction.
type NormalizedChannel struct {
Id, Name string
}
// GetNormalizedChannels returns a list of NormalizedChannel in the given team
// that the user belongs to
func (mc *MMClient) GetNormalizedChannels(teamId string) ([]*NormalizedChannel, error) {
channels, err := mc.GetChannels(teamId)
if err != nil {
return nil, err
}
normalizedChannels := make([]*NormalizedChannel, 0, len(channels))
for _, c := range channels {
name, err := mc.normalizeChannelName(c)
if err != nil {
return nil, err
}
normalizedChannels = append(normalizedChannels,
&NormalizedChannel{
Id: c.Id,
Name: name,
},
)
}
return normalizedChannels, nil
}
func (mc *MMClient) normalizeChannelName(c *model.Channel) (string, error) {
name := c.DisplayName
switch c.Type {
case model.CHANNEL_DIRECT:
otherUserID := c.GetOtherUserIdForDM(mc.user.Id)
user, resp := mc.client.GetUser(otherUserID, "")
switch resp.StatusCode {
case 200:
name = user.Username
case 404:
name = "Missing_User_" + otherUserID
default:
return "", resp.Error
}
name = "[D]" + name
case model.CHANNEL_GROUP:
users, resp := mc.client.GetUsersInChannel(c.Id, 0, 100, "")
if resp.StatusCode != 200 {
return "", resp.Error
}
name = "[G]" + model.GetGroupDisplayNameFromUsers(users, true)
case model.CHANNEL_PRIVATE:
name = "[P]" + c.DisplayName
}
return name, nil
}
// GetChannelUnread returns a string containing unread messages in the given channel
func (mc *MMClient) GetChannelUnread(channelId string) (*model.PostList, error) {
postList, resp := mc.client.GetPostsAroundLastUnread(mc.user.Id, channelId, 0, 200)
if resp.StatusCode != 200 {
return nil, resp.Error
}
return postList, nil
}
// MarkChannelAsRead marks the given channel as read
func (mc *MMClient) MarkChannelAsRead(channelId string) error {
_, resp := mc.client.ViewChannel(mc.user.Id, &model.ChannelView{PrevChannelId: channelId})
if resp.StatusCode != 200 {
return resp.Error
}
return nil
}
// FormatPostsForDisplay formosts a mattermost post for display
func (mc *MMClient) FormatPostsForDisplay(postList *model.PostList) (string, error) {
var text strings.Builder
order := postList.Order
for i := len(order) - 1; i >= 0; i-- {
post := postList.Posts[order[i]]
user, resp := mc.client.GetUser(post.UserId, "")
if resp.StatusCode != 200 {
return "", resp.Error
}
id := post.Id
if post.ParentId != "" {
id = post.ParentId
}
fmt.Fprintf(&text, "%s <%s> %s: %s\n",
humanTime(post.CreateAt), id[len(id)-6:], user.Username, post.Message)
}
return text.String(), nil
}
// CreatePost sends a text post to the given channel
func (mc *MMClient) CreatePost(channelId string, content []byte) error {
post := model.Post{
UserId: mc.user.Id,
ChannelId: channelId,
Message: string(content),
}
_, resp := mc.client.CreatePost(&post)
if resp.StatusCode != 201 {
return resp.Error
}
return nil
}