Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
221 lines
5.8 KiB
Go
221 lines
5.8 KiB
Go
package pluginapi
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin"
|
|
"github.com/mattermost/mattermost/server/public/pluginapi/cluster"
|
|
)
|
|
|
|
const (
|
|
internalKeyPrefix = "mmi_"
|
|
botUserKey = internalKeyPrefix + "botid"
|
|
botEnsureMutexKey = internalKeyPrefix + "bot_ensure"
|
|
)
|
|
|
|
// BotService exposes methods to manipulate bots.
|
|
type BotService struct {
|
|
api plugin.API
|
|
}
|
|
|
|
// Get returns a bot by botUserID.
|
|
//
|
|
// Minimum server version: 5.10
|
|
func (b *BotService) Get(botUserID string, includeDeleted bool) (*model.Bot, error) {
|
|
bot, appErr := b.api.GetBot(botUserID, includeDeleted)
|
|
|
|
return bot, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// BotListOption is an option to configure a bot List() request.
|
|
type BotListOption func(*model.BotGetOptions)
|
|
|
|
// BotOwner option configures bot list request to only retrieve the bots that matches with
|
|
// owner's id.
|
|
func BotOwner(id string) BotListOption {
|
|
return func(o *model.BotGetOptions) {
|
|
o.OwnerId = id
|
|
}
|
|
}
|
|
|
|
// BotIncludeDeleted option configures bot list request to also retrieve the deleted bots.
|
|
func BotIncludeDeleted() BotListOption {
|
|
return func(o *model.BotGetOptions) {
|
|
o.IncludeDeleted = true
|
|
}
|
|
}
|
|
|
|
// BotOnlyOrphans option configures bot list request to only retrieve orphan bots.
|
|
func BotOnlyOrphans() BotListOption {
|
|
return func(o *model.BotGetOptions) {
|
|
o.OnlyOrphaned = true
|
|
}
|
|
}
|
|
|
|
// List returns a list of bots by page, count and options.
|
|
//
|
|
// Minimum server version: 5.10
|
|
func (b *BotService) List(page, perPage int, options ...BotListOption) ([]*model.Bot, error) {
|
|
opts := &model.BotGetOptions{
|
|
Page: page,
|
|
PerPage: perPage,
|
|
}
|
|
for _, o := range options {
|
|
o(opts)
|
|
}
|
|
bots, appErr := b.api.GetBots(opts)
|
|
|
|
return bots, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// Create creates the bot and corresponding user.
|
|
//
|
|
// Minimum server version: 5.10
|
|
func (b *BotService) Create(bot *model.Bot) error {
|
|
createdBot, appErr := b.api.CreateBot(bot)
|
|
if appErr != nil {
|
|
return normalizeAppErr(appErr)
|
|
}
|
|
|
|
*bot = *createdBot
|
|
|
|
return nil
|
|
}
|
|
|
|
// Patch applies the given patch to the bot and corresponding user.
|
|
//
|
|
// Minimum server version: 5.10
|
|
func (b *BotService) Patch(botUserID string, botPatch *model.BotPatch) (*model.Bot, error) {
|
|
bot, appErr := b.api.PatchBot(botUserID, botPatch)
|
|
|
|
return bot, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// UpdateActive marks a bot as active or inactive, along with its corresponding user.
|
|
//
|
|
// Minimum server version: 5.10
|
|
func (b *BotService) UpdateActive(botUserID string, isActive bool) (*model.Bot, error) {
|
|
bot, appErr := b.api.UpdateBotActive(botUserID, isActive)
|
|
|
|
return bot, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// DeletePermanently permanently deletes a bot and its corresponding user.
|
|
//
|
|
// Minimum server version: 5.10
|
|
func (b *BotService) DeletePermanently(botUserID string) error {
|
|
return normalizeAppErr(b.api.PermanentDeleteBot(botUserID))
|
|
}
|
|
|
|
type ensureBotOptions struct {
|
|
ProfileImagePath string
|
|
ProfileImageBytes []byte
|
|
}
|
|
|
|
type EnsureBotOption func(*ensureBotOptions)
|
|
|
|
// ProfileImagePath configures EnsureBot to set a profile image from the given path.
|
|
//
|
|
// Using this option overrides any previously set ProfileImageBytes option.
|
|
func ProfileImagePath(path string) EnsureBotOption {
|
|
return func(args *ensureBotOptions) {
|
|
args.ProfileImagePath = path
|
|
args.ProfileImageBytes = nil
|
|
}
|
|
}
|
|
|
|
// ProfileImageBytes configures EnsureBot to set a profile image from the given bytes.
|
|
//
|
|
// Using this option overrides any previously set ProfileImagePath option.
|
|
func ProfileImageBytes(bytes []byte) EnsureBotOption {
|
|
return func(args *ensureBotOptions) {
|
|
args.ProfileImageBytes = bytes
|
|
args.ProfileImagePath = ""
|
|
}
|
|
}
|
|
|
|
// EnsureBot either returns an existing bot user matching the given bot, or creates a bot user from the given bot.
|
|
// A profile image or icon image may be optionally passed in to be set for the existing or newly created bot.
|
|
// Returns the id of the resulting bot.
|
|
// EnsureBot can safely be called multiple instances of a plugin concurrently.
|
|
//
|
|
// Minimum server version: 5.10
|
|
func (b *BotService) EnsureBot(bot *model.Bot, options ...EnsureBotOption) (string, error) {
|
|
m, err := cluster.NewMutex(b.api, botEnsureMutexKey)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "failed to create mutex")
|
|
}
|
|
|
|
return b.ensureBot(m, bot, options...)
|
|
}
|
|
|
|
type mutex interface {
|
|
Lock()
|
|
Unlock()
|
|
}
|
|
|
|
func (b *BotService) ensureBot(m mutex, bot *model.Bot, options ...EnsureBotOption) (string, error) {
|
|
err := ensureServerVersion(b.api, "5.10.0")
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "failed to ensure bot")
|
|
}
|
|
|
|
// Default options
|
|
o := &ensureBotOptions{
|
|
ProfileImagePath: "",
|
|
}
|
|
|
|
for _, setter := range options {
|
|
setter(o)
|
|
}
|
|
|
|
botID, err := b.ensureBotUser(m, bot)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if o.ProfileImagePath != "" {
|
|
imageBytes, err := b.readFile(o.ProfileImagePath)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "failed to read profile image")
|
|
}
|
|
appErr := b.api.SetProfileImage(botID, imageBytes)
|
|
if appErr != nil {
|
|
return "", errors.Wrap(appErr, "failed to set profile image")
|
|
}
|
|
} else if len(o.ProfileImageBytes) > 0 {
|
|
appErr := b.api.SetProfileImage(botID, o.ProfileImageBytes)
|
|
if appErr != nil {
|
|
return "", errors.Wrap(appErr, "failed to set profile image")
|
|
}
|
|
}
|
|
|
|
return botID, nil
|
|
}
|
|
|
|
func (b *BotService) ensureBotUser(m mutex, bot *model.Bot) (retBotID string, retErr error) {
|
|
// Lock to prevent two plugins from racing to create the bot account
|
|
m.Lock()
|
|
defer m.Unlock()
|
|
|
|
return b.api.EnsureBotUser(bot)
|
|
}
|
|
|
|
func (b *BotService) readFile(path string) ([]byte, error) {
|
|
bundlePath, err := b.api.GetBundlePath()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to get bundle path")
|
|
}
|
|
|
|
imageBytes, err := os.ReadFile(filepath.Join(bundlePath, path))
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to read image")
|
|
}
|
|
|
|
return imageBytes, nil
|
|
}
|