[MM-14727] Modified App.Bot.CreateBot to create a new post to the bot… (#11062)
Этот коммит содержится в:
коммит произвёл
Hanzei
родитель
539de0d593
Коммит
c524ee544f
28
app/bot.go
28
app/bot.go
@@ -6,6 +6,8 @@ package app
|
|||||||
import (
|
import (
|
||||||
"github.com/mattermost/mattermost-server/mlog"
|
"github.com/mattermost/mattermost-server/mlog"
|
||||||
"github.com/mattermost/mattermost-server/model"
|
"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.
|
// 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
|
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
|
return result.Data.(*model.Bot), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,6 +57,16 @@ func TestCreateBot(t *testing.T) {
|
|||||||
assert.Equal(t, "username", bot.Username)
|
assert.Equal(t, "username", bot.Username)
|
||||||
assert.Equal(t, "a bot", bot.Description)
|
assert.Equal(t, "a bot", bot.Description)
|
||||||
assert.Equal(t, th.BasicUser.Id, bot.OwnerId)
|
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) {
|
t.Run("create bot, username already used by a non-bot user", func(t *testing.T) {
|
||||||
|
|||||||
@@ -135,6 +135,10 @@
|
|||||||
"id": "api.bot.create_disabled",
|
"id": "api.bot.create_disabled",
|
||||||
"translation": "Bot creation has been 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",
|
"id": "api.channel.add_member.added",
|
||||||
"translation": "%v added to the channel by %v."
|
"translation": "%v added to the channel by %v."
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ const (
|
|||||||
POST_CHANNEL_DELETED = "system_channel_deleted"
|
POST_CHANNEL_DELETED = "system_channel_deleted"
|
||||||
POST_EPHEMERAL = "system_ephemeral"
|
POST_EPHEMERAL = "system_ephemeral"
|
||||||
POST_CHANGE_CHANNEL_PRIVACY = "system_change_chan_privacy"
|
POST_CHANGE_CHANNEL_PRIVACY = "system_change_chan_privacy"
|
||||||
|
POST_ADD_BOT_TEAMS_CHANNELS = "add_bot_teams_channels"
|
||||||
POST_FILEIDS_MAX_RUNES = 150
|
POST_FILEIDS_MAX_RUNES = 150
|
||||||
POST_FILENAMES_MAX_RUNES = 4000
|
POST_FILENAMES_MAX_RUNES = 4000
|
||||||
POST_HASHTAGS_MAX_RUNES = 1000
|
POST_HASHTAGS_MAX_RUNES = 1000
|
||||||
@@ -234,7 +235,8 @@ func (o *Post) IsValid(maxPostSize int) *AppError {
|
|||||||
POST_DISPLAYNAME_CHANGE,
|
POST_DISPLAYNAME_CHANGE,
|
||||||
POST_CONVERT_CHANNEL,
|
POST_CONVERT_CHANNEL,
|
||||||
POST_CHANNEL_DELETED,
|
POST_CHANNEL_DELETED,
|
||||||
POST_CHANGE_CHANNEL_PRIVACY:
|
POST_CHANGE_CHANNEL_PRIVACY,
|
||||||
|
POST_ADD_BOT_TEAMS_CHANNELS:
|
||||||
default:
|
default:
|
||||||
if !strings.HasPrefix(o.Type, POST_CUSTOM_TYPE_PREFIX) {
|
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)
|
return NewAppError("Post.IsValid", "model.post.is_valid.type.app_error", nil, "id="+o.Type, http.StatusBadRequest)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user