summaryrefslogtreecommitdiffstats
path: root/matterclient/channels.go
blob: ddd4d006774d1f80428e3519c8b2aaaf5338759b (plain) (blame)
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
package matterclient

import (
	"errors"
	"strings"

	"github.com/mattermost/mattermost-server/model"
	"github.com/sirupsen/logrus"
)

// GetChannels returns all channels we're members off
func (m *MMClient) 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 *MMClient) GetChannelHeader(channelId string) string { //nolint:golint
	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 (m *MMClient) GetChannelId(name string, teamId string) string { //nolint:golint
	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 channel.Type == model.CHANNEL_GROUP {
				res := strings.Replace(channel.DisplayName, ", ", "-", -1)
				res = strings.Replace(res, " ", "_", -1)
				if res == name {
					return channel.Id
				}
			}

		}
	}
	return ""
}

func (m *MMClient) getChannelIdTeam(name string, teamId string) string { //nolint:golint
	for _, t := range m.OtherTeams {
		if t.Id == teamId {
			for _, channel := range append(t.Channels, t.MoreChannels...) {
				if channel.Name == name {
					return channel.Id
				}
			}
		}
	}
	return ""
}

func (m *MMClient) GetChannelName(channelId string) string { //nolint:golint
	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 {
				if channel.Type == model.CHANNEL_GROUP {
					res := strings.Replace(channel.DisplayName, ", ", "-", -1)
					res = strings.Replace(res, " ", "_", -1)
					return res
				}
				return channel.Name
			}
		}
	}
	return ""
}

func (m *MMClient) GetChannelTeamId(id string) string { //nolint:golint
	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 *MMClient) GetLastViewedAt(channelId string) int64 { //nolint:golint
	m.RLock()
	defer m.RUnlock()
	res, resp := m.Client.GetChannelMember(channelId, m.User.Id, "")
	if resp.Error != nil {
		return model.GetMillis()
	}
	return res.LastViewedAt
}

// GetMoreChannels returns existing channels where we're not a member off.
func (m *MMClient) 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 *MMClient) GetTeamFromChannel(channelId string) string { //nolint:golint
	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.CHANNEL_GROUP {
					return "G"
				}
				return t.Id
			}
		}
		channels = nil
	}
	return ""
}

func (m *MMClient) JoinChannel(channelId string) error { //nolint:golint
	m.RLock()
	defer m.RUnlock()
	for _, c := range m.Team.Channels {
		if c.Id == channelId {
			m.log.Debug("Not joining ", channelId, " already joined.")
			return nil
		}
	}
	m.log.Debug("Joining ", channelId)
	_, resp := m.Client.AddChannelMember(channelId, m.User.Id)
	if resp.Error != nil {
		return resp.Error
	}
	return nil
}

func (m *MMClient) UpdateChannels() error {
	mmchannels, resp := m.Client.GetChannelsForTeamForUser(m.Team.Id, m.User.Id, "")
	if resp.Error != nil {
		return errors.New(resp.Error.DetailedError)
	}
	m.Lock()
	m.Team.Channels = mmchannels
	m.Unlock()

	mmchannels, resp = m.Client.GetPublicChannelsForTeam(m.Team.Id, 0, 5000, "")
	if resp.Error != nil {
		return errors.New(resp.Error.DetailedError)
	}

	m.Lock()
	m.Team.MoreChannels = mmchannels
	m.Unlock()
	return nil
}

func (m *MMClient) UpdateChannelHeader(channelId string, header string) { //nolint:golint
	channel := &model.Channel{Id: channelId, Header: header}
	m.log.Debugf("updating channelheader %#v, %#v", channelId, header)
	_, resp := m.Client.UpdateChannel(channel)
	if resp.Error != nil {
		logrus.Error(resp.Error)
	}
}

func (m *MMClient) UpdateLastViewed(channelId string) error { //nolint:golint
	m.log.Debugf("posting lastview %#v", channelId)
	view := &model.ChannelView{ChannelId: channelId}
	_, resp := m.Client.ViewChannel(m.User.Id, view)
	if resp.Error != nil {
		m.log.Errorf("ChannelView update for %s failed: %s", channelId, resp.Error)
		return resp.Error
	}
	return nil
}