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
260 lines
7.4 KiB
Go
260 lines
7.4 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
const (
|
|
BotDisplayNameMaxRunes = UserFirstNameMaxRunes
|
|
BotDescriptionMaxRunes = 1024
|
|
BotCreatorIdMaxRunes = KeyValuePluginIdMaxRunes // UserId or PluginId
|
|
BotSystemBotUsername = "system-bot"
|
|
)
|
|
|
|
// ProtectedBotUsernames is the set of system-owned bots that must not be
|
|
// disabled. These bots back core functionality (post reminders, reports,
|
|
// channel notifications, content review), so disabling them silently breaks
|
|
// features for the lifetime of the installation.
|
|
var ProtectedBotUsernames = map[string]struct{}{
|
|
BotSystemBotUsername: {},
|
|
ContentFlaggingBotUsername: {},
|
|
}
|
|
|
|
// Bot is a special type of User meant for programmatic interactions.
|
|
// Note that the primary key of a bot is the UserId, and matches the primary key of the
|
|
// corresponding user.
|
|
type Bot struct {
|
|
UserId string `json:"user_id"`
|
|
Username string `json:"username"`
|
|
DisplayName string `json:"display_name,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
OwnerId string `json:"owner_id"`
|
|
LastIconUpdate int64 `json:"last_icon_update,omitempty"`
|
|
CreateAt int64 `json:"create_at"`
|
|
UpdateAt int64 `json:"update_at"`
|
|
DeleteAt int64 `json:"delete_at"`
|
|
}
|
|
|
|
// IsSystemOwned returns whether the bot is one of the protected, system-owned
|
|
// bots that must not be disabled. See ProtectedBotUsernames.
|
|
func (b *Bot) IsSystemOwned() bool {
|
|
_, ok := ProtectedBotUsernames[b.Username]
|
|
return ok
|
|
}
|
|
|
|
// MarshalJSON adds the computed system_owned field to the bot's JSON
|
|
// representation without persisting it as a stored field.
|
|
func (b *Bot) MarshalJSON() ([]byte, error) {
|
|
type Alias Bot
|
|
return json.Marshal(&struct {
|
|
SystemOwned bool `json:"system_owned"`
|
|
*Alias
|
|
}{
|
|
SystemOwned: b.IsSystemOwned(),
|
|
Alias: (*Alias)(b),
|
|
})
|
|
}
|
|
|
|
func (b *Bot) Auditable() map[string]any {
|
|
return map[string]any{
|
|
"user_id": b.UserId,
|
|
"username": b.Username,
|
|
"display_name": b.DisplayName,
|
|
"description": b.Description,
|
|
"owner_id": b.OwnerId,
|
|
"last_icon_update": b.LastIconUpdate,
|
|
"create_at": b.CreateAt,
|
|
"update_at": b.UpdateAt,
|
|
"delete_at": b.DeleteAt,
|
|
}
|
|
}
|
|
|
|
// BotPatch is a description of what fields to update on an existing bot.
|
|
type BotPatch struct {
|
|
Username *string `json:"username"`
|
|
DisplayName *string `json:"display_name"`
|
|
Description *string `json:"description"`
|
|
}
|
|
|
|
func (b *BotPatch) Auditable() map[string]any {
|
|
return map[string]any{
|
|
"username": b.Username,
|
|
"display_name": b.DisplayName,
|
|
"description": b.Description,
|
|
}
|
|
}
|
|
|
|
// BotGetOptions acts as a filter on bulk bot fetching queries.
|
|
type BotGetOptions struct {
|
|
OwnerId string
|
|
IncludeDeleted bool
|
|
OnlyOrphaned bool
|
|
Page int
|
|
PerPage int
|
|
}
|
|
|
|
// BotList is a list of bots.
|
|
type BotList []*Bot
|
|
|
|
// Trace describes the minimum information required to identify a bot for the purpose of logging.
|
|
func (b *Bot) Trace() map[string]any {
|
|
return map[string]any{"user_id": b.UserId}
|
|
}
|
|
|
|
// Clone returns a shallow copy of the bot.
|
|
func (b *Bot) Clone() *Bot {
|
|
bCopy := *b
|
|
return &bCopy
|
|
}
|
|
|
|
// IsValidCreate validates bot for Create call. This skips validations of fields that are auto-filled on Create
|
|
func (b *Bot) IsValidCreate() *AppError {
|
|
if !IsValidUsername(b.Username) {
|
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.username.app_error", b.Trace(), "", http.StatusBadRequest)
|
|
}
|
|
|
|
if utf8.RuneCountInString(b.DisplayName) > BotDisplayNameMaxRunes {
|
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.user_id.app_error", b.Trace(), "", http.StatusBadRequest)
|
|
}
|
|
|
|
if utf8.RuneCountInString(b.Description) > BotDescriptionMaxRunes {
|
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.description.app_error", b.Trace(), "", http.StatusBadRequest)
|
|
}
|
|
|
|
if b.OwnerId == "" || utf8.RuneCountInString(b.OwnerId) > BotCreatorIdMaxRunes {
|
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.creator_id.app_error", b.Trace(), "", http.StatusBadRequest)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// IsValid validates the bot and returns an error if it isn't configured correctly.
|
|
func (b *Bot) IsValid() *AppError {
|
|
if !IsValidId(b.UserId) {
|
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.user_id.app_error", b.Trace(), "", http.StatusBadRequest)
|
|
}
|
|
|
|
if b.CreateAt == 0 {
|
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.create_at.app_error", b.Trace(), "", http.StatusBadRequest)
|
|
}
|
|
|
|
if b.UpdateAt == 0 {
|
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.update_at.app_error", b.Trace(), "", http.StatusBadRequest)
|
|
}
|
|
return b.IsValidCreate()
|
|
}
|
|
|
|
// PreSave should be run before saving a new bot to the database.
|
|
func (b *Bot) PreSave() {
|
|
b.CreateAt = GetMillis()
|
|
b.UpdateAt = b.CreateAt
|
|
b.DeleteAt = 0
|
|
b.Username = NormalizeUsername(b.Username)
|
|
}
|
|
|
|
// PreUpdate should be run before saving an updated bot to the database.
|
|
func (b *Bot) PreUpdate() {
|
|
b.UpdateAt = GetMillis()
|
|
}
|
|
|
|
// Etag generates an etag for caching.
|
|
func (b *Bot) Etag() string {
|
|
return Etag(b.UserId, b.UpdateAt)
|
|
}
|
|
|
|
// Patch modifies an existing bot with optional fields from the given patch.
|
|
// TODO 6.0: consider returning a boolean to indicate whether or not the patch
|
|
// applied any changes.
|
|
func (b *Bot) Patch(patch *BotPatch) {
|
|
if patch.Username != nil {
|
|
b.Username = *patch.Username
|
|
}
|
|
|
|
if patch.DisplayName != nil {
|
|
b.DisplayName = *patch.DisplayName
|
|
}
|
|
|
|
if patch.Description != nil {
|
|
b.Description = *patch.Description
|
|
}
|
|
}
|
|
|
|
// WouldPatch returns whether or not the given patch would be applied or not.
|
|
func (b *Bot) WouldPatch(patch *BotPatch) bool {
|
|
if patch == nil {
|
|
return false
|
|
}
|
|
if patch.Username != nil && *patch.Username != b.Username {
|
|
return true
|
|
}
|
|
if patch.DisplayName != nil && *patch.DisplayName != b.DisplayName {
|
|
return true
|
|
}
|
|
if patch.Description != nil && *patch.Description != b.Description {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// UserFromBot returns a user model describing the bot fields stored in the User store.
|
|
func UserFromBot(b *Bot) *User {
|
|
return &User{
|
|
Id: b.UserId,
|
|
Username: b.Username,
|
|
Email: NormalizeEmail(fmt.Sprintf("%s@localhost", b.Username)),
|
|
FirstName: b.DisplayName,
|
|
Roles: SystemUserRoleId,
|
|
}
|
|
}
|
|
|
|
// BotFromUser returns a bot model given a user model
|
|
func BotFromUser(u *User) *Bot {
|
|
return &Bot{
|
|
OwnerId: u.Id,
|
|
UserId: u.Id,
|
|
Username: u.Username,
|
|
DisplayName: u.GetDisplayName(ShowUsername),
|
|
}
|
|
}
|
|
|
|
// Etag computes the etag for a list of bots.
|
|
func (l *BotList) Etag() string {
|
|
id := "0"
|
|
var t int64
|
|
var delta int64
|
|
|
|
for _, v := range *l {
|
|
if v.UpdateAt > t {
|
|
t = v.UpdateAt
|
|
id = v.UserId
|
|
}
|
|
}
|
|
|
|
return Etag(id, t, delta, len(*l))
|
|
}
|
|
|
|
// MakeBotNotFoundError creates the error returned when a bot does not exist, or when the user isn't allowed to query the bot.
|
|
// The errors must the same in both cases to avoid leaking that a user is a bot.
|
|
func MakeBotNotFoundError(where, userId string) *AppError {
|
|
return NewAppError(where, "store.sql_bot.get.missing.app_error", map[string]any{"user_id": userId}, "", http.StatusNotFound)
|
|
}
|
|
|
|
func IsBotDMChannel(channel *Channel, botUserID string) bool {
|
|
if channel.Type != ChannelTypeDirect {
|
|
return false
|
|
}
|
|
|
|
if !strings.HasPrefix(channel.Name, botUserID+"__") && !strings.HasSuffix(channel.Name, "__"+botUserID) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|