[MM-37716] Drop support for LHS specific bot icons (#18087)

Этот коммит содержится в:
Ben Schumacher
2021-08-12 00:27:35 +02:00
коммит произвёл GitHub
родитель 99bb6084b3
Коммит 225565f412
13 изменённых файлов: 0 добавлений и 1013 удалений

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

@@ -108,8 +108,6 @@ type AppIface interface {
// ['town-square', 'game-of-thrones', 'wow']
//
DefaultChannelNames() []string
// DeleteBotIconImage deletes LHS icon for a bot.
DeleteBotIconImage(botUserId string) *model.AppError
// DeleteChannelScheme deletes a channels scheme and sets its SchemeId to nil.
DeleteChannelScheme(channel *model.Channel) (*model.Channel, *model.AppError)
// DeleteGroupConstrainedMemberships deletes team and channel memberships of users who aren't members of the allowed
@@ -154,8 +152,6 @@ type AppIface interface {
GetAllLdapGroupsPage(page int, perPage int, opts model.LdapGroupSearchOpts) ([]*model.Group, int, *model.AppError)
// GetBot returns the given bot.
GetBot(botUserId string, includeDeleted bool) (*model.Bot, *model.AppError)
// GetBotIconImage retrieves LHS icon for a bot.
GetBotIconImage(botUserId string) ([]byte, *model.AppError)
// GetBots returns the requested page of bots.
GetBots(options *model.BotGetOptions) (model.BotList, *model.AppError)
// GetChannelGroupUsers returns the users who are associated to the channel via GroupChannels and GroupMembers.
@@ -299,10 +295,6 @@ type AppIface interface {
SessionHasPermissionToManageBot(session model.Session, botUserId string) *model.AppError
// SessionIsRegistered determines if a specific session has been registered
SessionIsRegistered(session model.Session) bool
// SetBotIconImage sets LHS icon for a bot.
SetBotIconImage(botUserId string, file io.ReadSeeker) *model.AppError
// SetBotIconImageFromMultiPartFile sets LHS icon for a bot.
SetBotIconImageFromMultiPartFile(botUserId string, imageData *multipart.FileHeader) *model.AppError
// SetSessionExpireInDays sets the session's expiry the specified number of days
// relative to either the session creation date or the current time, depending
// on the `ExtendSessionOnActivity` config setting.

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

@@ -4,17 +4,11 @@
package app
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"path/filepath"
"github.com/mattermost/mattermost-server/v6/app/imaging"
"github.com/mattermost/mattermost-server/v6/app/request"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/i18n"
@@ -571,112 +565,3 @@ func (a *App) ConvertUserToBot(user *model.User) (*model.Bot, *model.AppError) {
}
return bot, nil
}
// SetBotIconImageFromMultiPartFile sets LHS icon for a bot.
func (a *App) SetBotIconImageFromMultiPartFile(botUserId string, imageData *multipart.FileHeader) *model.AppError {
file, err := imageData.Open()
if err != nil {
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.open.app_error", nil, err.Error(), http.StatusBadRequest)
}
defer file.Close()
file.Seek(0, 0)
return a.SetBotIconImage(botUserId, file)
}
// SetBotIconImage sets LHS icon for a bot.
func (a *App) SetBotIconImage(botUserId string, file io.ReadSeeker) *model.AppError {
bot, err := a.GetBot(botUserId, true)
if err != nil {
return err
}
if _, err := imaging.ParseSVG(file); err != nil {
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.parse.app_error", nil, err.Error(), http.StatusBadRequest)
}
file.Seek(0, 0)
data, readErr := ioutil.ReadAll(file)
if readErr != nil {
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.read.app_error", nil, readErr.Error(), http.StatusInternalServerError)
}
if storedData, readFileErr := a.ReadFile(getBotIconPath(botUserId)); readFileErr == nil && bytes.Equal(storedData, data) {
return nil
}
// Set icon
if _, err = a.WriteFile(bytes.NewReader(data), getBotIconPath(botUserId)); err != nil {
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.app_error", nil, err.Error(), http.StatusInternalServerError)
}
bot.LastIconUpdate = model.GetMillis()
if _, err := a.Srv().Store.Bot().Update(bot); err != nil {
var nfErr *store.ErrNotFound
var appErr *model.AppError
switch {
case errors.As(err, &nfErr):
return model.MakeBotNotFoundError(nfErr.ID)
case errors.As(err, &appErr): // in case we haven't converted to plain error.
return appErr
default: // last fallback in case it doesn't map to an existing app error.
return model.NewAppError("SetBotIconImage", "app.bot.patchbot.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
}
a.invalidateUserCacheAndPublish(botUserId)
return nil
}
// DeleteBotIconImage deletes LHS icon for a bot.
func (a *App) DeleteBotIconImage(botUserId string) *model.AppError {
bot, err := a.GetBot(botUserId, true)
if err != nil {
return err
}
// Delete icon
if err = a.RemoveFile(getBotIconPath(botUserId)); err != nil {
return model.NewAppError("DeleteBotIconImage", "api.bot.delete_bot_icon_image.app_error", nil, err.Error(), http.StatusInternalServerError)
}
if nErr := a.Srv().Store.User().UpdateLastPictureUpdate(botUserId); nErr != nil {
mlog.Warn(nErr.Error())
}
bot.LastIconUpdate = int64(0)
if _, err := a.Srv().Store.Bot().Update(bot); err != nil {
var nfErr *store.ErrNotFound
var appErr *model.AppError
switch {
case errors.As(err, &nfErr):
return model.MakeBotNotFoundError(nfErr.ID)
case errors.As(err, &appErr): // in case we haven't converted to plain error.
return appErr
default: // last fallback in case it doesn't map to an existing app error.
return model.NewAppError("DeleteBotIconImage", "app.bot.patchbot.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
}
a.invalidateUserCacheAndPublish(botUserId)
return nil
}
// GetBotIconImage retrieves LHS icon for a bot.
func (a *App) GetBotIconImage(botUserId string) ([]byte, *model.AppError) {
if _, err := a.GetBot(botUserId, true); err != nil {
return nil, err
}
data, err := a.ReadFile(getBotIconPath(botUserId))
if err != nil {
return nil, model.NewAppError("GetBotIconImage", "api.bot.get_bot_icon_image.read.app_error", nil, err.Error(), http.StatusNotFound)
}
return data, nil
}
func getBotIconPath(botUserId string) string {
return filepath.Join("bots", botUserId, "icon.svg")
}

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

@@ -5,9 +5,6 @@ package app
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@@ -15,7 +12,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/utils/fileutils"
)
func TestCreateBot(t *testing.T) {
@@ -753,168 +749,6 @@ func TestConvertUserToBot(t *testing.T) {
})
}
func TestSetBotIconImage(t *testing.T) {
t.Run("invalid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
path, _ := fileutils.FindDir("tests")
svgFile, fileErr := os.Open(filepath.Join(path, "test.svg"))
require.NoError(t, fileErr)
defer svgFile.Close()
err := th.App.SetBotIconImage("invalid_bot_id", svgFile)
require.NotNil(t, err)
})
t.Run("valid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
// Set an icon image
path, _ := fileutils.FindDir("tests")
svgFile, fileErr := os.Open(filepath.Join(path, "test.svg"))
require.NoError(t, fileErr)
defer svgFile.Close()
expectedData, fileErr := ioutil.ReadAll(svgFile)
require.NoError(t, fileErr)
require.NotNil(t, expectedData)
bot, err := th.App.ConvertUserToBot(&model.User{
Username: "username",
Id: th.BasicUser.Id,
})
require.Nil(t, err)
defer th.App.PermanentDeleteBot(bot.UserId)
fpath := fmt.Sprintf("/bots/%v/icon.svg", bot.UserId)
exists, err := th.App.FileExists(fpath)
require.Nil(t, err)
require.False(t, exists, "icon.svg shouldn't exist for the bot")
svgFile.Seek(0, 0)
err = th.App.SetBotIconImage(bot.UserId, svgFile)
require.Nil(t, err)
exists, err = th.App.FileExists(fpath)
require.Nil(t, err)
require.True(t, exists, "icon.svg should exist for the bot")
actualData, err := th.App.ReadFile(fpath)
require.Nil(t, err)
require.NotNil(t, actualData)
require.Equal(t, expectedData, actualData)
})
}
func TestGetBotIconImage(t *testing.T) {
t.Run("invalid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
actualData, err := th.App.GetBotIconImage("invalid_bot_id")
require.NotNil(t, err)
require.Nil(t, actualData)
})
t.Run("valid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
// Set an icon image
path, _ := fileutils.FindDir("tests")
svgFile, fileErr := os.Open(filepath.Join(path, "test.svg"))
require.NoError(t, fileErr)
defer svgFile.Close()
expectedData, fileErr := ioutil.ReadAll(svgFile)
require.NoError(t, fileErr)
require.NotNil(t, expectedData)
bot, err := th.App.ConvertUserToBot(&model.User{
Username: "username",
Id: th.BasicUser.Id,
})
require.Nil(t, err)
defer th.App.PermanentDeleteBot(bot.UserId)
svgFile.Seek(0, 0)
fpath := fmt.Sprintf("/bots/%v/icon.svg", bot.UserId)
_, err = th.App.WriteFile(svgFile, fpath)
require.Nil(t, err)
actualBytes, err := th.App.GetBotIconImage(bot.UserId)
require.Nil(t, err)
require.NotNil(t, actualBytes)
actualData, err := th.App.ReadFile(fpath)
require.Nil(t, err)
require.NotNil(t, actualData)
require.Equal(t, expectedData, actualData)
})
}
func TestDeleteBotIconImage(t *testing.T) {
t.Run("invalid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
err := th.App.DeleteBotIconImage("invalid_bot_id")
require.NotNil(t, err)
})
t.Run("valid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
// Set an icon image
path, _ := fileutils.FindDir("tests")
svgFile, fileErr := os.Open(filepath.Join(path, "test.svg"))
require.NoError(t, fileErr)
defer svgFile.Close()
expectedData, fileErr := ioutil.ReadAll(svgFile)
require.NoError(t, fileErr)
require.NotNil(t, expectedData)
bot, err := th.App.ConvertUserToBot(&model.User{
Username: "username",
Id: th.BasicUser.Id,
})
require.Nil(t, err)
defer th.App.PermanentDeleteBot(bot.UserId)
// Set icon
svgFile.Seek(0, 0)
err = th.App.SetBotIconImage(bot.UserId, svgFile)
require.Nil(t, err)
// Get icon
actualData, err := th.App.GetBotIconImage(bot.UserId)
require.Nil(t, err)
require.NotNil(t, actualData)
require.Equal(t, expectedData, actualData)
// Bot icon should exist
fpath := fmt.Sprintf("/bots/%v/icon.svg", bot.UserId)
exists, err := th.App.FileExists(fpath)
require.Nil(t, err)
require.True(t, exists, "icon.svg should exist for the bot")
// Delete icon
err = th.App.DeleteBotIconImage(bot.UserId)
require.Nil(t, err)
// Bot icon should not exist
exists, err = th.App.FileExists(fpath)
require.Nil(t, err)
require.False(t, exists, "icon.svg should be deleted for the bot")
})
}
func TestGetSystemBot(t *testing.T) {
t.Run("An error should be returned if there are no sysadmins in the instance", func(t *testing.T) {
th := Setup(t).InitBasic()

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

@@ -2785,28 +2785,6 @@ func (a *OpenTracingAppLayer) DeleteAllKeysForPlugin(pluginID string) *model.App
return resultVar0
}
func (a *OpenTracingAppLayer) DeleteBotIconImage(botUserId string) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.DeleteBotIconImage")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.DeleteBotIconImage(botUserId)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) DeleteBrandImage() *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.DeleteBrandImage")
@@ -4659,28 +4637,6 @@ func (a *OpenTracingAppLayer) GetBot(botUserId string, includeDeleted bool) (*mo
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetBotIconImage(botUserId string) ([]byte, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetBotIconImage")
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.GetBotIconImage(botUserId)
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))
ext.Error.Set(span, true)
}
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetBots(options *model.BotGetOptions) (model.BotList, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetBots")
@@ -14727,50 +14683,6 @@ func (a *OpenTracingAppLayer) SetAutoResponderStatus(user *model.User, oldNotify
a.app.SetAutoResponderStatus(user, oldNotifyProps)
}
func (a *OpenTracingAppLayer) SetBotIconImage(botUserId string, file io.ReadSeeker) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SetBotIconImage")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.SetBotIconImage(botUserId, file)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) SetBotIconImageFromMultiPartFile(botUserId string, imageData *multipart.FileHeader) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SetBotIconImageFromMultiPartFile")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.SetBotIconImageFromMultiPartFile(botUserId, imageData)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) SetCustomStatus(userID string, cs *model.CustomStatus) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SetCustomStatus")

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

@@ -939,26 +939,6 @@ func (api *PluginAPI) PermanentDeleteBot(userID string) *model.AppError {
return api.app.PermanentDeleteBot(userID)
}
func (api *PluginAPI) GetBotIconImage(userID string) ([]byte, *model.AppError) {
if _, err := api.app.GetBot(userID, true); err != nil {
return nil, err
}
return api.app.GetBotIconImage(userID)
}
func (api *PluginAPI) SetBotIconImage(userID string, data []byte) *model.AppError {
return api.app.SetBotIconImage(userID, bytes.NewReader(data))
}
func (api *PluginAPI) DeleteBotIconImage(userID string) *model.AppError {
if _, err := api.app.GetBot(userID, true); err != nil {
return err
}
return api.app.DeleteBotIconImage(userID)
}
func (api *PluginAPI) PublishUserTyping(userID, channelID, parentId string) *model.AppError {
return api.app.PublishUserTyping(userID, channelID, parentId)
}