diff --git a/app/plugin_api.go b/app/plugin_api.go index 6d6de42f84..0f86c80b05 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -466,6 +466,20 @@ func (api *PluginAPI) GetTeamIcon(teamId string) ([]byte, *model.AppError) { return data, nil } +func (api *PluginAPI) SetTeamIcon(teamId string, data []byte) *model.AppError { + team, err := api.app.GetTeam(teamId) + if err != nil { + return err + } + + fileReader := bytes.NewReader(data) + err = api.app.SetTeamIconFromFile(team, fileReader) + if err != nil { + return err + } + return nil +} + // Plugin Section func (api *PluginAPI) GetPlugins() ([]*model.Manifest, *model.AppError) { diff --git a/app/plugin_api_test.go b/app/plugin_api_test.go index e195718bf8..b239cb07a2 100644 --- a/app/plugin_api_test.go +++ b/app/plugin_api_test.go @@ -332,12 +332,42 @@ func TestPluginAPIGetTeamIcon(t *testing.T) { require.Nil(t, err) // Get the team icon to check - imageProfile, err := api.GetTeamIcon(th.BasicTeam.Id) + teamIcon, err := api.GetTeamIcon(th.BasicTeam.Id) require.Nil(t, err) - require.NotEmpty(t, imageProfile) + require.NotEmpty(t, teamIcon) colorful := color.NRGBA{255, 0, 0, 255} - byteReader := bytes.NewReader(imageProfile) + byteReader := bytes.NewReader(teamIcon) + img2, _, err2 := image.Decode(byteReader) + require.Nil(t, err2) + require.Equal(t, img2.At(2, 3), colorful) +} + +func TestPluginAPISetTeamIcon(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + api := th.SetupPluginAPI() + + // Create an 128 x 128 image + img := image.NewRGBA(image.Rect(0, 0, 128, 128)) + // Draw a red dot at (2, 3) + img.Set(2, 3, color.RGBA{255, 0, 0, 255}) + buf := new(bytes.Buffer) + err := png.Encode(buf, img) + require.Nil(t, err) + dataBytes := buf.Bytes() + + // Set the user profile image + err = api.SetTeamIcon(th.BasicTeam.Id, dataBytes) + require.Nil(t, err) + + // Get the user profile image to check + teamIcon, err := api.GetTeamIcon(th.BasicTeam.Id) + require.Nil(t, err) + require.NotEmpty(t, teamIcon) + + colorful := color.NRGBA{255, 0, 0, 255} + byteReader := bytes.NewReader(teamIcon) img2, _, err2 := image.Decode(byteReader) require.Nil(t, err2) require.Equal(t, img2.At(2, 3), colorful) diff --git a/plugin/api.go b/plugin/api.go index 0aa8c6771a..d71a9f61fc 100644 --- a/plugin/api.go +++ b/plugin/api.go @@ -69,6 +69,11 @@ type API interface { // Minimum server version: 5.6 GetTeamIcon(teamId string) ([]byte, *model.AppError) + // SetTeamIcon sets the Team Icon. + // + // Minimum server version: 5.6 + SetTeamIcon(teamId string, data []byte) *model.AppError + // UpdateUser updates a user. UpdateUser(user *model.User) (*model.User, *model.AppError) diff --git a/plugin/client_rpc_generated.go b/plugin/client_rpc_generated.go index 46b0c159fc..f0c28ec641 100644 --- a/plugin/client_rpc_generated.go +++ b/plugin/client_rpc_generated.go @@ -916,6 +916,35 @@ func (s *apiRPCServer) GetTeamIcon(args *Z_GetTeamIconArgs, returns *Z_GetTeamIc return nil } +type Z_SetTeamIconArgs struct { + A string + B []byte +} + +type Z_SetTeamIconReturns struct { + A *model.AppError +} + +func (g *apiRPCClient) SetTeamIcon(teamId string, data []byte) *model.AppError { + _args := &Z_SetTeamIconArgs{teamId, data} + _returns := &Z_SetTeamIconReturns{} + if err := g.client.Call("Plugin.SetTeamIcon", _args, _returns); err != nil { + log.Printf("RPC call to SetTeamIcon API failed: %s", err.Error()) + } + return _returns.A +} + +func (s *apiRPCServer) SetTeamIcon(args *Z_SetTeamIconArgs, returns *Z_SetTeamIconReturns) error { + if hook, ok := s.impl.(interface { + SetTeamIcon(teamId string, data []byte) *model.AppError + }); ok { + returns.A = hook.SetTeamIcon(args.A, args.B) + } else { + return encodableError(fmt.Errorf("API SetTeamIcon called but not implemented.")) + } + return nil +} + type Z_UpdateUserArgs struct { A *model.User } diff --git a/plugin/plugintest/api.go b/plugin/plugintest/api.go index 2100da958c..758395d75f 100644 --- a/plugin/plugintest/api.go +++ b/plugin/plugintest/api.go @@ -1876,6 +1876,22 @@ func (_m *API) SetProfileImage(userId string, data []byte) *model.AppError { return r0 } +// SetTeamIcon provides a mock function with given fields: teamId, data +func (_m *API) SetTeamIcon(teamId string, data []byte) *model.AppError { + ret := _m.Called(teamId, data) + + var r0 *model.AppError + if rf, ok := ret.Get(0).(func(string, []byte) *model.AppError); ok { + r0 = rf(teamId, data) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*model.AppError) + } + } + + return r0 +} + // UnregisterCommand provides a mock function with given fields: teamId, trigger func (_m *API) UnregisterCommand(teamId string, trigger string) error { ret := _m.Called(teamId, trigger)