Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MM-128]: Added functionality to add user to all public channel in the team at once #133

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 34 additions & 5 deletions server/welcomebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -228,12 +233,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 == "*" {
page := defaultPage
for {
// 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())
return
}

if len(channels) == 0 {
break
}

for _, channel := range channels {
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 {
p.API.LogError("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID)
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.client.Log.Error("Failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID, "error", err.Error())
}
}
}