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-14727] Modified App.Bot.CreateBot to create a new post to the bot… #11062

Merged
merged 3 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package app
import (
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)

// CreateBot creates the given bot and corresponding user.
Expand All @@ -22,6 +23,29 @@ func (a *App) CreateBot(bot *model.Bot) (*model.Bot, *model.AppError) {
return nil, result.Err
}

// Get the owner of the bot, if one exists. If not, don't send a message
ownerUser, _ := a.Srv.Store.User().Get(bot.OwnerId)
jwilander marked this conversation as resolved.
Show resolved Hide resolved
if ownerUser != nil {
// Send a message to the bot's creator to inform them that the bot needs to be added
// to a team and channel after it's created
channel, err := a.GetOrCreateDirectChannel(result.Data.(*model.Bot).UserId, bot.OwnerId)
if err != nil {
return nil, err
}

T := utils.GetUserTranslations(ownerUser.Locale)
botAddPost := &model.Post{
Type: model.POST_ADD_BOT_TEAMS_CHANNELS,
UserId: result.Data.(*model.Bot).UserId,
ChannelId: channel.Id,
Message: T("api.bot.teams_channels.add_message_mobile"),
}

if _, err := a.CreatePostAsUser(botAddPost, a.Session.Id); err != nil {
return nil, err
}
}

return result.Data.(*model.Bot), nil
}

Expand Down
10 changes: 10 additions & 0 deletions app/bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func TestCreateBot(t *testing.T) {
assert.Equal(t, "username", bot.Username)
assert.Equal(t, "a bot", bot.Description)
assert.Equal(t, th.BasicUser.Id, bot.OwnerId)

// Check that a post was created to add bot to team and channels
channel, err := th.App.GetOrCreateDirectChannel(bot.UserId, th.BasicUser.Id)
require.Nil(t, err)
posts, err := th.App.GetPosts(channel.Id, 0, 1)
require.Nil(t, err)

postArray := posts.ToSlice()
assert.Len(t, postArray, 1)
assert.Equal(t, postArray[0].Type, model.POST_ADD_BOT_TEAMS_CHANNELS)
})

t.Run("create bot, username already used by a non-bot user", func(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
"id": "api.bot.create_disabled",
"translation": "Bot creation has been disabled."
},
{
"id": "api.bot.teams_channels.add_message_mobile",
"translation": "Please add me to teams and channels you want me to interact in. To do this, use the browser or Mattermost Desktop App."
},
{
"id": "api.channel.add_member.added",
"translation": "%v added to the channel by %v."
Expand Down
4 changes: 3 additions & 1 deletion model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
POST_CHANNEL_DELETED = "system_channel_deleted"
POST_EPHEMERAL = "system_ephemeral"
POST_CHANGE_CHANNEL_PRIVACY = "system_change_chan_privacy"
POST_ADD_BOT_TEAMS_CHANNELS = "add_bot_teams_channels"
POST_FILEIDS_MAX_RUNES = 150
POST_FILENAMES_MAX_RUNES = 4000
POST_HASHTAGS_MAX_RUNES = 1000
Expand Down Expand Up @@ -234,7 +235,8 @@ func (o *Post) IsValid(maxPostSize int) *AppError {
POST_DISPLAYNAME_CHANGE,
POST_CONVERT_CHANNEL,
POST_CHANNEL_DELETED,
POST_CHANGE_CHANNEL_PRIVACY:
POST_CHANGE_CHANNEL_PRIVACY,
POST_ADD_BOT_TEAMS_CHANNELS:
default:
if !strings.HasPrefix(o.Type, POST_CUSTOM_TYPE_PREFIX) {
return NewAppError("Post.IsValid", "model.post.is_valid.type.app_error", nil, "id="+o.Type, http.StatusBadRequest)
Expand Down