Skip to content

Commit

Permalink
[MM-14727] Modified App.Bot.CreateBot to create a new post to the bot… (
Browse files Browse the repository at this point in the history
  • Loading branch information
Wipeout55 authored and Inconnu08 committed Jun 21, 2019
1 parent cbd57bd commit adf3c40
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
28 changes: 28 additions & 0 deletions app/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package app
import (
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/utils"
)

// CreateBot creates the given bot and corresponding user.
Expand All @@ -22,6 +24,32 @@ 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, err := a.Srv.Store.User().Get(bot.OwnerId)
if err != nil && err.Id != store.MISSING_ACCOUNT_ERROR {
mlog.Error(err.Error())
return nil, err
} else 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

0 comments on commit adf3c40

Please sign in to comment.