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
…'s creator that allows the creator to add the bot to a team and a channel
  • Loading branch information
Wipeout55 committed Jun 6, 2019
1 parent 16c11d7 commit 79dbf7d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
26 changes: 26 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,31 @@ func (a *App) CreateBot(bot *model.Bot) (*model.Bot, *model.AppError) {
return nil, result.Err
}

// 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
}

// Get the owner for translation of the mobile message
ownerUser, err := a.Srv.Store.User().Get(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
44 changes: 27 additions & 17 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 Expand Up @@ -239,21 +249,21 @@ func TestGetBots(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()

OwnerId1 := model.NewId()
OwnerId2 := model.NewId()
user1 := th.CreateUser()
user2 := th.CreateUser()

bot1, err := th.App.CreateBot(&model.Bot{
Username: "username",
Description: "a bot",
OwnerId: OwnerId1,
OwnerId: user1.Id,
})
require.Nil(t, err)
defer th.App.PermanentDeleteBot(bot1.UserId)

deletedBot1, err := th.App.CreateBot(&model.Bot{
Username: "username4",
Description: "a deleted bot",
OwnerId: OwnerId1,
OwnerId: user1.Id,
})
require.Nil(t, err)
deletedBot1, err = th.App.UpdateBotActive(deletedBot1.UserId, false)
Expand All @@ -263,31 +273,31 @@ func TestGetBots(t *testing.T) {
bot2, err := th.App.CreateBot(&model.Bot{
Username: "username2",
Description: "a second bot",
OwnerId: OwnerId1,
OwnerId: user1.Id,
})
require.Nil(t, err)
defer th.App.PermanentDeleteBot(bot2.UserId)

bot3, err := th.App.CreateBot(&model.Bot{
Username: "username3",
Description: "a third bot",
OwnerId: OwnerId1,
OwnerId: user1.Id,
})
require.Nil(t, err)
defer th.App.PermanentDeleteBot(bot3.UserId)

bot4, err := th.App.CreateBot(&model.Bot{
Username: "username5",
Description: "a fourth bot",
OwnerId: OwnerId2,
OwnerId: user2.Id,
})
require.Nil(t, err)
defer th.App.PermanentDeleteBot(bot4.UserId)

deletedBot2, err := th.App.CreateBot(&model.Bot{
Username: "username6",
Description: "a deleted bot",
OwnerId: OwnerId2,
OwnerId: user2.Id,
})
require.Nil(t, err)
deletedBot2, err = th.App.UpdateBotActive(deletedBot2.UserId, false)
Expand Down Expand Up @@ -386,7 +396,7 @@ func TestGetBots(t *testing.T) {
bots, err := th.App.GetBots(&model.BotGetOptions{
Page: 0,
PerPage: 10,
OwnerId: OwnerId1,
OwnerId: user1.Id,
IncludeDeleted: false,
})
require.Nil(t, err)
Expand All @@ -397,7 +407,7 @@ func TestGetBots(t *testing.T) {
bots, err := th.App.GetBots(&model.BotGetOptions{
Page: 0,
PerPage: 10,
OwnerId: OwnerId2,
OwnerId: user2.Id,
IncludeDeleted: false,
})
require.Nil(t, err)
Expand All @@ -408,7 +418,7 @@ func TestGetBots(t *testing.T) {
bots, err := th.App.GetBots(&model.BotGetOptions{
Page: 0,
PerPage: 10,
OwnerId: OwnerId1,
OwnerId: user1.Id,
IncludeDeleted: true,
})
require.Nil(t, err)
Expand All @@ -419,7 +429,7 @@ func TestGetBots(t *testing.T) {
bots, err := th.App.GetBots(&model.BotGetOptions{
Page: 0,
PerPage: 10,
OwnerId: OwnerId2,
OwnerId: user2.Id,
IncludeDeleted: true,
})
require.Nil(t, err)
Expand Down Expand Up @@ -491,8 +501,8 @@ func TestDisableUserBots(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()

ownerId1 := model.NewId()
ownerId2 := model.NewId()
user1 := th.CreateUser()
user2 := th.CreateUser()

bots := []*model.Bot{}
defer func() {
Expand All @@ -505,7 +515,7 @@ func TestDisableUserBots(t *testing.T) {
bot, err := th.App.CreateBot(&model.Bot{
Username: fmt.Sprintf("username%v", i),
Description: "a bot",
OwnerId: ownerId1,
OwnerId: user1.Id,
})
require.Nil(t, err)
bots = append(bots, bot)
Expand All @@ -515,12 +525,12 @@ func TestDisableUserBots(t *testing.T) {
u2bot1, err := th.App.CreateBot(&model.Bot{
Username: "username_nodisable",
Description: "a bot",
OwnerId: ownerId2,
OwnerId: user2.Id,
})
require.Nil(t, err)
defer th.App.PermanentDeleteBot(u2bot1.UserId)

err = th.App.disableUserBots(ownerId1)
err = th.App.disableUserBots(user1.Id)
require.Nil(t, err)

// Check all bots and corrensponding users are disabled for creator 1
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 79dbf7d

Please sign in to comment.