From 707c9058002a783668ec683bb18b85b96be1b366 Mon Sep 17 00:00:00 2001 From: raghav aggarwal Date: Thu, 18 Apr 2024 17:56:37 +0530 Subject: [PATCH 1/4] MM-390: Added functionality to add user to all public channel in the team at once --- server/welcomebot.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/server/welcomebot.go b/server/welcomebot.go index 7f65ad45f..a54a40c43 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -228,12 +228,36 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A } func (p *Plugin) joinChannel(action *Action, channelName string) { - if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil { - if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil { - p.API.LogError("Couldn't add user to the channel, continuing to next channel", "user_id", action.Context.UserID, "channel_id", channel.Id) - return + if channelName == "*" { + perPage := 100 + page := 0 + for { + channels, appErr := p.API.GetPublicChannelsForTeam(action.Context.TeamID, page, perPage) + if appErr != nil { + p.API.LogError("Failed to get all the public channels for the team", "team_id", action.Context.TeamID) + return + } + + if len(channels) == 0 { + break + } + + for _, channel := range channels { + if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil { + p.API.LogError("Couldn't add user to the channel", "user_id", action.Context.UserID, "channel_id", channel.Id) + return + } + } + page++ } } else { - p.API.LogError("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID) + if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil { + if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil { + p.API.LogError("Couldn't add user to the channel, continuing to next channel", "user_id", action.Context.UserID, "channel_id", channel.Id) + return + } + } else { + p.API.LogError("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID) + } } } From 4d943f9265b3c9a98748b4d6f81a57f823136ce5 Mon Sep 17 00:00:00 2001 From: raghav aggarwal Date: Fri, 19 Apr 2024 20:36:13 +0530 Subject: [PATCH 2/4] MM-390: review fixes --- server/welcomebot.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/welcomebot.go b/server/welcomebot.go index a54a40c43..76e7c0894 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -232,6 +232,7 @@ func (p *Plugin) joinChannel(action *Action, channelName string) { perPage := 100 page := 0 for { + // Adding user to all the public channels only in case of all i.e. * channels, appErr := p.API.GetPublicChannelsForTeam(action.Context.TeamID, page, perPage) if appErr != nil { p.API.LogError("Failed to get all the public channels for the team", "team_id", action.Context.TeamID) From c7c6c0519e906189b7dfb4913ec623dc2ae077c7 Mon Sep 17 00:00:00 2001 From: raghavaggarwal2308 Date: Thu, 25 Apr 2024 16:05:37 +0530 Subject: [PATCH 3/4] [MM-414] Code refactoring --- server/welcomebot.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/server/welcomebot.go b/server/welcomebot.go index 76e7c0894..526f1a11b 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -10,6 +10,11 @@ import ( "github.com/mattermost/mattermost-server/v6/model" ) +const ( + defaultPerPage = 100 + defaultPage = 0 +) + func (p *Plugin) constructMessageTemplate(userID, teamID string) *MessageTemplate { data := &MessageTemplate{} var err *model.AppError @@ -229,13 +234,12 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A func (p *Plugin) joinChannel(action *Action, channelName string) { if channelName == "*" { - perPage := 100 - page := 0 + page := defaultPage for { - // Adding user to all the public channels only in case of all i.e. * - channels, appErr := p.API.GetPublicChannelsForTeam(action.Context.TeamID, page, perPage) - if appErr != nil { - p.API.LogError("Failed to get all the public channels for the team", "team_id", action.Context.TeamID) + // Adding user to all the public channels when channel name in '*' (i.e. all) + channels, err := p.client.Channel.ListPublicChannelsForTeam(action.Context.TeamID, page, defaultPerPage) + if err != nil { + p.client.Log.Error("Failed to get all the public channels for the team", "team_id", action.Context.TeamID, "error", err.Error()) return } @@ -244,21 +248,21 @@ func (p *Plugin) joinChannel(action *Action, channelName string) { } for _, channel := range channels { - if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil { - p.API.LogError("Couldn't add user to the channel", "user_id", action.Context.UserID, "channel_id", channel.Id) + if _, err := p.client.Channel.AddMember(channel.Id, action.Context.UserID); err != nil { + p.client.Log.Error("Couldn't add user to the channel", "user_id", action.Context.UserID, "channel_id", channel.Id, "error", err.Error()) return } } page++ } } else { - if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil { - if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil { - p.API.LogError("Couldn't add user to the channel, continuing to next channel", "user_id", action.Context.UserID, "channel_id", channel.Id) + if channel, err := p.client.Channel.GetByName(action.Context.TeamID, channelName, false); err == nil { + if _, err = p.client.Channel.AddMember(channel.Id, action.Context.UserID); err != nil { + 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()) return } } else { - p.API.LogError("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID) + p.client.Log.Error("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID, "error", err.Error()) } } } From f4b35f993edaae6ddada478bb5e715fe47faf818 Mon Sep 17 00:00:00 2001 From: raghavaggarwal2308 Date: Thu, 2 May 2024 16:58:44 +0530 Subject: [PATCH 4/4] [MM-426] Review fixes --- README.md | 2 +- server/welcomebot.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c8db5f98e..41180334f 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ where - **ActionDisplayName**: Sets the display name for the user action buttons. - **ActionName**: Sets the action name used by the plugin to identify which action is taken by a user. - **ActionSuccessfulMessage**: Message posted after the user takes this action and joins the specified channels. - - **ChannelsAddedTo**: List of channel names the user is added to. Must be the channel handle used in the URL, in lowercase. For example, in the following URL the **channel name** value is `my-channel`: https://example.com/my-team/channels/my-channel + - **ChannelsAddedTo**: List of channel names the user is added to. Must be the channel handle used in the URL, in lowercase. For example, in the following URL the **channel name** value is `my-channel`: https://example.com/my-team/channels/my-channel. If you want to add the user in the all the public channels of the team add "*" in the `ChannelsAddedTo` array. The preview of the configured messages, as well as the creation of a channel welcome message, can be done via bot commands: * `/welcomebot help` - Displays usage information. diff --git a/server/welcomebot.go b/server/welcomebot.go index 526f1a11b..1112b075a 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -236,7 +236,7 @@ func (p *Plugin) joinChannel(action *Action, channelName string) { if channelName == "*" { page := defaultPage for { - // Adding user to all the public channels when channel name in '*' (i.e. all) + // Adding user to all the public channels when channel name is '*' (i.e. all) channels, err := p.client.Channel.ListPublicChannelsForTeam(action.Context.TeamID, page, defaultPerPage) if err != nil { p.client.Log.Error("Failed to get all the public channels for the team", "team_id", action.Context.TeamID, "error", err.Error()) @@ -262,7 +262,7 @@ func (p *Plugin) joinChannel(action *Action, channelName string) { return } } else { - p.client.Log.Error("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID, "error", err.Error()) + p.client.Log.Error("Failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID, "error", err.Error()) } } }