MM-16872 - Extend Plugin API to set LHS bot icon (#11601)

* MM-16872 - Extend Plugin API to set LHS bot icon

* MM-16872 - Using ReadSeeker as opposed to Reader for reading svg image file

* MM-16872 - PR feedback

* MM-16872 - Using userId rather than bot.UserId

* MM-16872 - Minor stylistic changes

* MM-16872 - Removing DriverName check
Этот коммит содержится в:
Ali Farooq
2019-07-11 12:00:12 -04:00
коммит произвёл GitHub
родитель 76f4fccf8a
Коммит 5ed40a48c8
10 изменённых файлов: 388 добавлений и 57 удалений

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

@@ -5,6 +5,7 @@ package app
import (
"fmt"
"io"
"mime/multipart"
"net/http"
@@ -197,19 +198,25 @@ func (a *App) ConvertUserToBot(user *model.User) (*model.Bot, *model.AppError) {
return a.Srv.Store.Bot().Save(model.BotFromUser(user))
}
// SetBotIconImage sets LHS icon for a bot.
func (a *App) SetBotIconImage(botUserId string, imageData *multipart.FileHeader) *model.AppError {
if len(*a.Config().FileSettings.DriverName) == 0 {
return model.NewAppError("SetBotIconImage", "api.bot.icon_image.storage.app_error", nil, "", http.StatusNotImplemented)
}
// 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()
if _, err = parseSVG(file); err != nil {
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 {
if _, err := a.GetBot(botUserId, true); err != nil {
return err
}
if _, err := parseSVG(file); err != nil {
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.parse.app_error", nil, err.Error(), http.StatusBadRequest)
}
@@ -229,8 +236,8 @@ func (a *App) SetBotIconImage(botUserId string, imageData *multipart.FileHeader)
// DeleteBotIconImage deletes LHS icon for a bot.
func (a *App) DeleteBotIconImage(botUserId string) *model.AppError {
if len(*a.Config().FileSettings.DriverName) == 0 {
return model.NewAppError("DeleteBotIconImage", "api.bot.icon_image.storage.app_error", nil, "", http.StatusNotImplemented)
if _, err := a.GetBot(botUserId, true); err != nil {
return err
}
// Delete icon
@@ -247,17 +254,17 @@ func (a *App) DeleteBotIconImage(botUserId string) *model.AppError {
}
// GetBotIconImage retrieves LHS icon for a bot.
func (a *App) GetBotIconImage(botUserId string) ([]byte, bool, *model.AppError) {
if len(*a.Config().FileSettings.DriverName) == 0 {
return nil, false, model.NewAppError("GetBotIconImage", "api.bot.icon_image.storage.app_error", nil, "", http.StatusNotImplemented)
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, false, model.NewAppError("GetBotIconImage", "api.bot.get_bot_icon_image.read.app_error", nil, err.Error(), http.StatusNotFound)
return nil, model.NewAppError("GetBotIconImage", "api.bot.get_bot_icon_image.read.app_error", nil, err.Error(), http.StatusNotFound)
}
return data, false, nil
return data, nil
}
func getBotIconPath(botUserId string) string {

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

@@ -5,6 +5,9 @@ package app
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@@ -12,6 +15,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
func TestCreateBot(t *testing.T) {
@@ -596,6 +600,165 @@ 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.Nil(t, fileErr)
require.NotNil(t, expectedData)
bot, err := th.App.ConvertUserToBot(&model.User{
Username: "username",
Id: th.BasicUser.Id,
})
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.Nil(t, fileErr)
require.NotNil(t, expectedData)
bot, err := th.App.ConvertUserToBot(&model.User{
Username: "username",
Id: th.BasicUser.Id,
})
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.Nil(t, fileErr)
require.NotNil(t, expectedData)
bot, err := th.App.ConvertUserToBot(&model.User{
Username: "username",
Id: th.BasicUser.Id,
})
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 sToP(s string) *string {
return &s
}

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

@@ -500,12 +500,7 @@ func (api *PluginAPI) SetProfileImage(userId string, data []byte) *model.AppErro
return err
}
fileReader := bytes.NewReader(data)
err = api.app.SetProfileImageFromFile(userId, fileReader)
if err != nil {
return err
}
return nil
return api.app.SetProfileImageFromFile(userId, bytes.NewReader(data))
}
func (api *PluginAPI) GetEmojiList(sortBy string, page, perPage int) ([]*model.Emoji, *model.AppError) {
@@ -580,12 +575,7 @@ func (api *PluginAPI) SetTeamIcon(teamId string, data []byte) *model.AppError {
return err
}
fileReader := bytes.NewReader(data)
err = api.app.SetTeamIconFromFile(team, fileReader)
if err != nil {
return err
}
return nil
return api.app.SetTeamIconFromFile(team, bytes.NewReader(data))
}
func (api *PluginAPI) OpenInteractiveDialog(dialog model.OpenDialogRequest) *model.AppError {
@@ -756,3 +746,27 @@ func (api *PluginAPI) UpdateBotActive(userId string, active bool) (*model.Bot, *
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 {
if _, err := api.app.GetBot(userId, true); err != nil {
return err
}
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)
}