Set OwnerId for bots created via EnsureBotUser (#21560)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
a541cda9d0
Коммит
a4103f8c77
@@ -1018,6 +1018,9 @@ func (api *PluginAPI) PermanentDeleteBot(userID string) *model.AppError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (api *PluginAPI) EnsureBotUser(bot *model.Bot) (string, error) {
|
func (api *PluginAPI) EnsureBotUser(bot *model.Bot) (string, error) {
|
||||||
|
// Bots created by a plugin should use the plugin's ID for the creator field.
|
||||||
|
bot.OwnerId = api.id
|
||||||
|
|
||||||
return api.app.EnsureBot(api.ctx, api.id, bot)
|
return api.app.EnsureBot(api.ctx, api.id, bot)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,18 +22,18 @@ func (p *MyPlugin) OnConfigurationChange() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *MyPlugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model.Post, string) {
|
func (p *MyPlugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model.Post, string) {
|
||||||
createdBot, err := p.API.CreateBot(&model.Bot{
|
createdBot, appErr := p.API.CreateBot(&model.Bot{
|
||||||
Username: "bot",
|
Username: "bot",
|
||||||
Description: "a plugin bot",
|
Description: "a plugin bot",
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if appErr != nil {
|
||||||
return nil, err.Error() + "failed to create bot"
|
return nil, appErr.Error() + "failed to create bot"
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchedBot, err := p.API.GetBot(createdBot.UserId, false)
|
fetchedBot, appErr := p.API.GetBot(createdBot.UserId, false)
|
||||||
if err != nil {
|
if appErr != nil {
|
||||||
return nil, err.Error() + "failed to get bot"
|
return nil, appErr.Error() + "failed to get bot"
|
||||||
}
|
}
|
||||||
if fetchedBot.Description != "a plugin bot" {
|
if fetchedBot.Description != "a plugin bot" {
|
||||||
return nil, "GetBot did not return the expected bot Description"
|
return nil, "GetBot did not return the expected bot Description"
|
||||||
@@ -43,16 +43,16 @@ func (p *MyPlugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model
|
|||||||
}
|
}
|
||||||
|
|
||||||
updatedDescription := createdBot.Description + ", updated"
|
updatedDescription := createdBot.Description + ", updated"
|
||||||
patchedBot, err := p.API.PatchBot(createdBot.UserId, &model.BotPatch{
|
patchedBot, appErr := p.API.PatchBot(createdBot.UserId, &model.BotPatch{
|
||||||
Description: &updatedDescription,
|
Description: &updatedDescription,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if appErr != nil {
|
||||||
return nil, err.Error() + "failed to patch bot"
|
return nil, appErr.Error() + "failed to patch bot"
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchedBot, err = p.API.GetBot(patchedBot.UserId, false)
|
fetchedBot, appErr = p.API.GetBot(patchedBot.UserId, false)
|
||||||
if err != nil {
|
if appErr != nil {
|
||||||
return nil, err.Error() + "failed to get bot"
|
return nil, appErr.Error() + "failed to get bot"
|
||||||
}
|
}
|
||||||
|
|
||||||
if fetchedBot.UserId != patchedBot.UserId {
|
if fetchedBot.UserId != patchedBot.UserId {
|
||||||
@@ -62,14 +62,14 @@ func (p *MyPlugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model
|
|||||||
return nil, "GetBot did not return the updated bot Description"
|
return nil, "GetBot did not return the updated bot Description"
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchedBots, err := p.API.GetBots(&model.BotGetOptions{
|
fetchedBots, appErr := p.API.GetBots(&model.BotGetOptions{
|
||||||
Page: 0,
|
Page: 0,
|
||||||
PerPage: 1,
|
PerPage: 1,
|
||||||
OwnerId: "",
|
OwnerId: "",
|
||||||
IncludeDeleted: false,
|
IncludeDeleted: false,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if appErr != nil {
|
||||||
return nil, err.Error() + "failed to get bots"
|
return nil, appErr.Error() + "failed to get bots"
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(fetchedBots) != 1 {
|
if len(fetchedBots) != 1 {
|
||||||
@@ -79,8 +79,16 @@ func (p *MyPlugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model
|
|||||||
if fetchedBot.UserId != fetchedBots[0].UserId {
|
if fetchedBot.UserId != fetchedBots[0].UserId {
|
||||||
return nil, "GetBots did not return the expected bot"
|
return nil, "GetBots did not return the expected bot"
|
||||||
}
|
}
|
||||||
if _, err = p.API.UpdateBotActive(fetchedBot.UserId, false); err != nil {
|
if _, appErr = p.API.UpdateBotActive(fetchedBot.UserId, false); appErr != nil {
|
||||||
return nil, err.Error() + "failed to disable bot"
|
return nil, appErr.Error() + "failed to disable bot"
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := p.API.EnsureBotUser(&model.Bot{
|
||||||
|
Username: "bot2",
|
||||||
|
Description: "another plugin bot",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err.Error() + "failed to create bot"
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: investigate why the following code panics
|
// TODO: investigate why the following code panics
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user