From dfb9241e8201a4299879ff6cfb935beb3812119d Mon Sep 17 00:00:00 2001 From: Adzim Zul Fahmi Date: Wed, 16 Jan 2019 15:13:15 +0700 Subject: [PATCH] [MM-13750] Add ability to search teams to plugin API (#10116) Add SearchTeams in plugin/api, --- app/plugin_api.go | 4 ++++ app/plugin_api_test.go | 27 +++++++++++++++++++++++++++ plugin/api.go | 5 +++++ plugin/client_rpc_generated.go | 29 +++++++++++++++++++++++++++++ plugin/plugintest/api.go | 25 +++++++++++++++++++++++++ 5 files changed, 90 insertions(+) diff --git a/app/plugin_api.go b/app/plugin_api.go index bee7321c8d..2ec57bc91d 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -118,6 +118,10 @@ func (api *PluginAPI) GetTeam(teamId string) (*model.Team, *model.AppError) { return api.app.GetTeam(teamId) } +func (api *PluginAPI) SearchTeams(term string) ([]*model.Team, *model.AppError) { + return api.app.SearchAllTeams(term) +} + func (api *PluginAPI) GetTeamByName(name string) (*model.Team, *model.AppError) { return api.app.GetTeamByName(name) } diff --git a/app/plugin_api_test.go b/app/plugin_api_test.go index 025306ad41..b7d0c0a03a 100644 --- a/app/plugin_api_test.go +++ b/app/plugin_api_test.go @@ -721,3 +721,30 @@ func TestPluginAPISendMail(t *testing.T) { require.Equal(t, resultsEmail.Body.Text, body) } + +func TestPluginAPI_SearchTeams(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + api := th.SetupPluginAPI() + + t.Run("all fine", func(t *testing.T) { + teams, err := api.SearchTeams(th.BasicTeam.Name) + assert.Nil(t, err) + assert.Len(t, teams, 1) + + teams, err = api.SearchTeams(th.BasicTeam.DisplayName) + assert.Nil(t, err) + assert.Len(t, teams, 1) + + teams, err = api.SearchTeams(th.BasicTeam.Name[:3]) + assert.Nil(t, err) + assert.Len(t, teams, 1) + }) + + t.Run("invalid team name", func(t *testing.T) { + teams, err := api.SearchTeams("not found") + assert.Nil(t, err) + assert.Empty(t, teams) + }) +} diff --git a/plugin/api.go b/plugin/api.go index 2e8bbcb4e6..b49413e1b5 100644 --- a/plugin/api.go +++ b/plugin/api.go @@ -144,6 +144,11 @@ type API interface { // UpdateTeam updates a team. UpdateTeam(team *model.Team) (*model.Team, *model.AppError) + // SearchTeams search a team. + // + // Minimum server version: 5.8 + SearchTeams(term string) ([]*model.Team, *model.AppError) + // GetTeamsForUser returns list of teams of given user ID. // // Minimum server version: 5.6 diff --git a/plugin/client_rpc_generated.go b/plugin/client_rpc_generated.go index 383cd6af1d..b88cb36ee3 100644 --- a/plugin/client_rpc_generated.go +++ b/plugin/client_rpc_generated.go @@ -1362,6 +1362,35 @@ func (s *apiRPCServer) UpdateTeam(args *Z_UpdateTeamArgs, returns *Z_UpdateTeamR return nil } +type Z_SearchTeamsArgs struct { + A string +} + +type Z_SearchTeamsReturns struct { + A []*model.Team + B *model.AppError +} + +func (g *apiRPCClient) SearchTeams(term string) ([]*model.Team, *model.AppError) { + _args := &Z_SearchTeamsArgs{term} + _returns := &Z_SearchTeamsReturns{} + if err := g.client.Call("Plugin.SearchTeams", _args, _returns); err != nil { + log.Printf("RPC call to SearchTeams API failed: %s", err.Error()) + } + return _returns.A, _returns.B +} + +func (s *apiRPCServer) SearchTeams(args *Z_SearchTeamsArgs, returns *Z_SearchTeamsReturns) error { + if hook, ok := s.impl.(interface { + SearchTeams(term string) ([]*model.Team, *model.AppError) + }); ok { + returns.A, returns.B = hook.SearchTeams(args.A) + } else { + return encodableError(fmt.Errorf("API SearchTeams called but not implemented.")) + } + return nil +} + type Z_GetTeamsForUserArgs struct { A string } diff --git a/plugin/plugintest/api.go b/plugin/plugintest/api.go index 78fcb2d466..2d1bd3a357 100644 --- a/plugin/plugintest/api.go +++ b/plugin/plugintest/api.go @@ -1958,6 +1958,31 @@ func (_m *API) SearchChannels(teamId string, term string) ([]*model.Channel, *mo return r0, r1 } +// SearchTeams provides a mock function with given fields: term +func (_m *API) SearchTeams(term string) ([]*model.Team, *model.AppError) { + ret := _m.Called(term) + + var r0 []*model.Team + if rf, ok := ret.Get(0).(func(string) []*model.Team); ok { + r0 = rf(term) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*model.Team) + } + } + + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string) *model.AppError); ok { + r1 = rf(term) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 +} + // SearchUsers provides a mock function with given fields: search func (_m *API) SearchUsers(search *model.UserSearch) ([]*model.User, *model.AppError) { ret := _m.Called(search)