Skip to content

Commit

Permalink
[PR-70]: Fixed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Kshitij-Katiyar committed Apr 15, 2024
1 parent ddff18a commit 77baa81
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions server/welcomebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/pkg/errors"
)

func (p *Plugin) constructMessageTemplate(userID, teamID string) *MessageTemplate {
Expand Down Expand Up @@ -232,44 +233,10 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A
func (p *Plugin) joinChannel(action *Action, channelName string) {
// If it begins with @ create a DM channel
if strings.HasPrefix(channelName, "@") {
r := []rune(channelName)
dmUser, userErr := p.API.GetUserByUsername(string(r[1:]))

if userErr != nil {
p.API.LogError("Couldn't find DM user, continuing to next channel", "channelName", channelName)
return
}

if !dmUser.IsBot {
p.API.LogError("Specified DM user is not a bot, continuing to next channel", "channelName", channelName)
return
}

dmChannel, err := p.API.GetDirectChannel(dmUser.Id, action.Context.UserID)

if err != nil {
p.API.LogError("Couldn't create or get DM channel, continuing to next channel", "user_id", action.Context.UserID, "channelName", channelName, "channel_id", dmChannel.Id)
return
}

dmMessage := "Welcome to the team!"
if len(action.Context.DirectMessagePost) != 0 {
dmMessage = action.Context.DirectMessagePost
}

post := &model.Post{
Message: dmMessage,
ChannelId: dmChannel.Id,
UserId: dmUser.Id,
if err := p.handleDMs(action,channelName); err != nil {
p.API.LogError("failed to handle DM channel, continuing to next channel. " + err.Error())
}

if _, err := p.API.CreatePost(post); err != nil {
p.API.LogError(
"Could not create direct message post",
"user_id", post.UserId,
"err", err.Error(),
)
}
} else { // Otherwise treat it like a normal channel
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 {
Expand All @@ -282,3 +249,37 @@ func (p *Plugin) joinChannel(action *Action, channelName string) {
}
}
}

func (p *Plugin) handleDMs(action *Action, channelName string) error {
username := channelName[1:]
dmUser, userErr := p.API.GetUserByUsername(username)
if userErr != nil {
return errors.Wrapf(userErr, "couldn't find DM channel for username %s", username)
}

if !dmUser.IsBot {
return errors.Wrapf(userErr, "Specified DM user is not a bot for username %s", username)
}

dmChannel, err := p.API.GetDirectChannel(dmUser.Id, action.Context.UserID)
if err != nil {
return errors.Wrapf(err, "Couldn't create or get DM channel for user_id %s and channel_id %s" , action.Context.UserID, dmChannel.Id)
}

dmMessage := "Welcome to the team!"
if len(action.Context.DirectMessagePost) != 0 {
dmMessage = action.Context.DirectMessagePost
}

post := &model.Post{
Message: dmMessage,
ChannelId: dmChannel.Id,
UserId: dmUser.Id,
}

if _, err := p.API.CreatePost(post); err != nil {
return errors.Wrapf(err, "Could not create direct message for user_id %s", post.UserId)
}

return nil
}

0 comments on commit 77baa81

Please sign in to comment.