From c524ee544fb6cc89014c83849994ec2c553c7cc8 Mon Sep 17 00:00:00 2001 From: Devin Binnie Date: Thu, 13 Jun 2019 07:36:13 -0400 Subject: [PATCH] =?UTF-8?q?[MM-14727]=20Modified=20App.Bot.CreateBot=20to?= =?UTF-8?q?=20create=20a=20new=20post=20to=20the=20bot=E2=80=A6=20(#11062)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/bot.go | 28 ++++++++++++++++++++++++++++ app/bot_test.go | 10 ++++++++++ i18n/en.json | 4 ++++ model/post.go | 4 +++- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/bot.go b/app/bot.go index 72db58ed7c..2f57a10ce6 100644 --- a/app/bot.go +++ b/app/bot.go @@ -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. @@ -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 } diff --git a/app/bot_test.go b/app/bot_test.go index ded8b3411f..3c955356b1 100644 --- a/app/bot_test.go +++ b/app/bot_test.go @@ -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) { diff --git a/i18n/en.json b/i18n/en.json index 1e51ad1430..34ce359b1b 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -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." diff --git a/model/post.go b/model/post.go index 6b405f6859..7155e0f6dc 100644 --- a/model/post.go +++ b/model/post.go @@ -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 @@ -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)