Skip to content

Commit c7c6c05

Browse files
[MM-414] Code refactoring
1 parent 4d943f9 commit c7c6c05

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

server/welcomebot.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010
"github.com/mattermost/mattermost-server/v6/model"
1111
)
1212

13+
const (
14+
defaultPerPage = 100
15+
defaultPage = 0
16+
)
17+
1318
func (p *Plugin) constructMessageTemplate(userID, teamID string) *MessageTemplate {
1419
data := &MessageTemplate{}
1520
var err *model.AppError
@@ -229,13 +234,12 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A
229234

230235
func (p *Plugin) joinChannel(action *Action, channelName string) {
231236
if channelName == "*" {
232-
perPage := 100
233-
page := 0
237+
page := defaultPage
234238
for {
235-
// Adding user to all the public channels only in case of all i.e. *
236-
channels, appErr := p.API.GetPublicChannelsForTeam(action.Context.TeamID, page, perPage)
237-
if appErr != nil {
238-
p.API.LogError("Failed to get all the public channels for the team", "team_id", action.Context.TeamID)
239+
// Adding user to all the public channels when channel name in '*' (i.e. all)
240+
channels, err := p.client.Channel.ListPublicChannelsForTeam(action.Context.TeamID, page, defaultPerPage)
241+
if err != nil {
242+
p.client.Log.Error("Failed to get all the public channels for the team", "team_id", action.Context.TeamID, "error", err.Error())
239243
return
240244
}
241245

@@ -244,21 +248,21 @@ func (p *Plugin) joinChannel(action *Action, channelName string) {
244248
}
245249

246250
for _, channel := range channels {
247-
if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil {
248-
p.API.LogError("Couldn't add user to the channel", "user_id", action.Context.UserID, "channel_id", channel.Id)
251+
if _, err := p.client.Channel.AddMember(channel.Id, action.Context.UserID); err != nil {
252+
p.client.Log.Error("Couldn't add user to the channel", "user_id", action.Context.UserID, "channel_id", channel.Id, "error", err.Error())
249253
return
250254
}
251255
}
252256
page++
253257
}
254258
} else {
255-
if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil {
256-
if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil {
257-
p.API.LogError("Couldn't add user to the channel, continuing to next channel", "user_id", action.Context.UserID, "channel_id", channel.Id)
259+
if channel, err := p.client.Channel.GetByName(action.Context.TeamID, channelName, false); err == nil {
260+
if _, err = p.client.Channel.AddMember(channel.Id, action.Context.UserID); err != nil {
261+
p.client.Log.Error("Couldn't add user to the channel, continuing to next channel", "user_id", action.Context.UserID, "channel_id", channel.Id, "error", err.Error())
258262
return
259263
}
260264
} else {
261-
p.API.LogError("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID)
265+
p.client.Log.Error("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID, "error", err.Error())
262266
}
263267
}
264268
}

0 commit comments

Comments
 (0)