MM-44953: Refactor EnsureBot to be used from plugin.API (#20521)

The mattermost-plugin-api code also needs to use this.
Hence we make it available from plugin.API.

https://mattermost.atlassian.net/browse/MM-44953

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2022-06-22 14:06:00 +05:30
коммит произвёл GitHub
родитель b5dec4f4d9
Коммит 4dd2b5f039
8 изменённых файлов: 108 добавлений и 10 удалений

Просмотреть файл

@@ -125,6 +125,11 @@ type AppIface interface {
// activation if inactive anywhere in the cluster.
// Notifies cluster peers through config change.
EnablePlugin(id string) *model.AppError
// EnsureBot provides similar functionality with the plugin-api BotService. It doesn't accept
// any ensureBotOptions hence it is not required for now.
// TODO: Once the focalboard migration completed, we should add this logic to the app and
// let plugin-api use the same code
EnsureBot(c *request.Context, productID string, bot *model.Bot) (string, error)
// Expand announcements in incoming webhooks from Slack. Those announcements
// can be found in the text attribute, or in the pretext, text, title and value
// attributes of the attachment structure. The Slack attachment structure is

Просмотреть файл

@@ -25,11 +25,15 @@ type botServiceWrapper struct {
app AppIface
}
func (w *botServiceWrapper) EnsureBot(c *request.Context, productID string, bot *model.Bot) (string, error) {
return w.app.EnsureBot(c, productID, bot)
}
// EnsureBot provides similar functionality with the plugin-api BotService. It doesn't accept
// any ensureBotOptions hence it is not required for now.
// TODO: Once the focalboard migration completed, we should add this logic to the app and
// let plugin-api use the same code
func (w *botServiceWrapper) EnsureBot(c *request.Context, productID string, bot *model.Bot) (string, error) {
func (a *App) EnsureBot(c *request.Context, productID string, bot *model.Bot) (string, error) {
if bot == nil {
return "", errors.New("passed a nil bot")
}
@@ -38,7 +42,7 @@ func (w *botServiceWrapper) EnsureBot(c *request.Context, productID string, bot
return "", errors.New("passed a bot with no username")
}
botIDBytes, err := w.app.GetPluginKey(productID, botUserKey)
botIDBytes, err := a.GetPluginKey(productID, botUserKey)
if err != nil {
return "", err
}
@@ -54,7 +58,7 @@ func (w *botServiceWrapper) EnsureBot(c *request.Context, productID string, bot
Description: &bot.Description,
}
if _, err = w.app.PatchBot(botID, botPatch); err != nil {
if _, err = a.PatchBot(botID, botPatch); err != nil {
return "", fmt.Errorf("failed to patch bot: %w", err)
}
@@ -62,13 +66,13 @@ func (w *botServiceWrapper) EnsureBot(c *request.Context, productID string, bot
}
// Check for an existing bot user with that username. If one exists, then use that.
if user, appErr := w.app.GetUserByUsername(bot.Username); appErr == nil && user != nil {
if user, appErr := a.GetUserByUsername(bot.Username); appErr == nil && user != nil {
if user.IsBot {
if appErr := w.app.SetPluginKey(productID, botUserKey, []byte(user.Id)); appErr != nil {
w.app.Srv().Log.Warn("Failed to set claimed bot user id.", mlog.String("userid", user.Id), mlog.Err(appErr))
if appErr := a.SetPluginKey(productID, botUserKey, []byte(user.Id)); appErr != nil {
return "", fmt.Errorf("failed to set plugin key: %w", err)
}
} else {
w.app.Srv().Log.Error("Product attempted to use an account that already exists. Convert user to a bot "+
a.Srv().Log.Error("Product attempted to use an account that already exists. Convert user to a bot "+
"account in the CLI by running 'mattermost user convert <username> --bot'. If the user is an "+
"existing user account you want to preserve, change its username and restart the Mattermost server, "+
"after which the plugin will create a bot account with that name. For more information about bot "+
@@ -81,13 +85,13 @@ func (w *botServiceWrapper) EnsureBot(c *request.Context, productID string, bot
return user.Id, nil
}
createdBot, err := w.app.CreateBot(c, bot)
createdBot, err := a.CreateBot(c, bot)
if err != nil {
return "", fmt.Errorf("failed to create bot: %w", err)
}
if appErr := w.app.SetPluginKey(productID, botUserKey, []byte(createdBot.UserId)); appErr != nil {
w.app.Srv().Log.Warn("Failed to set created bot user id.", mlog.String("userid", createdBot.UserId), mlog.Err(appErr))
if appErr := a.SetPluginKey(productID, botUserKey, []byte(createdBot.UserId)); appErr != nil {
return "", fmt.Errorf("failed to set plugin key: %w", err)
}
return createdBot.UserId, nil

Просмотреть файл

@@ -3911,6 +3911,28 @@ func (a *OpenTracingAppLayer) EnableUserAccessToken(token *model.UserAccessToken
return resultVar0
}
func (a *OpenTracingAppLayer) EnsureBot(c *request.Context, productID string, bot *model.Bot) (string, error) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.EnsureBot")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0, resultVar1 := a.app.EnsureBot(c, productID, bot)
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))
ext.Error.Set(span, true)
}
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) EnvironmentConfig(filter func(reflect.StructField) bool) map[string]interface{} {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.EnvironmentConfig")

Просмотреть файл

@@ -981,6 +981,10 @@ func (api *PluginAPI) PermanentDeleteBot(userID string) *model.AppError {
return api.app.PermanentDeleteBot(userID)
}
func (api *PluginAPI) EnsureBotUser(bot *model.Bot) (string, error) {
return api.app.EnsureBot(api.ctx, api.id, bot)
}
func (api *PluginAPI) PublishUserTyping(userID, channelID, parentId string) *model.AppError {
return api.app.PublishUserTyping(userID, channelID, parentId)
}