diff --git a/app/plugin_api.go b/app/plugin_api.go index d6b601332e..8a3f73fdf4 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -523,6 +523,25 @@ func (api *PluginAPI) RemoveTeamIcon(teamId string) *model.AppError { return nil } +func (api *PluginAPI) CreateDirectChannel(userId1 string, userId2 string) (*model.Channel, *model.AppError) { + _, err := api.app.GetUser(userId1) + if err != nil { + return nil, err + } + + _, err = api.app.GetUser(userId2) + if err != nil { + return nil, err + } + + dm, err := api.app.CreateDirectChannel(userId1, userId2) + if err != nil { + return nil, err + } + + return dm, 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 e17dda9763..24766f511e 100644 --- a/app/plugin_api_test.go +++ b/app/plugin_api_test.go @@ -509,3 +509,21 @@ func TestPluginAPIRemoveTeamIcon(t *testing.T) { err = api.RemoveTeamIcon(th.BasicTeam.Id) require.Nil(t, err) } + +func TestPluginAPICreateDirectChannel(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + api := th.SetupPluginAPI() + + dm1, err := api.CreateDirectChannel(th.BasicUser.Id, th.BasicUser2.Id) + require.Nil(t, err) + require.NotEmpty(t, dm1) + + dm2, err := api.CreateDirectChannel(th.BasicUser.Id, th.BasicUser.Id) + require.Nil(t, err) + require.NotEmpty(t, dm2) + + dm3, err := api.CreateDirectChannel(th.BasicUser.Id, model.NewId()) + require.NotNil(t, err) + require.Empty(t, dm3) +} diff --git a/plugin/api.go b/plugin/api.go index 93f73ff08d..f288486253 100644 --- a/plugin/api.go +++ b/plugin/api.go @@ -52,6 +52,11 @@ type API interface { // CreateUser creates a user. CreateUser(user *model.User) (*model.User, *model.AppError) + // CreateDirectChannel creates a Direct channel. + // + // Minimum server version: 5.6 + CreateDirectChannel(userId1 string, userId2 string) (*model.Channel, *model.AppError) + // DeleteUser deletes a user. DeleteUser(userId string) *model.AppError diff --git a/plugin/client_rpc_generated.go b/plugin/client_rpc_generated.go index a352074845..7bb1e02c27 100644 --- a/plugin/client_rpc_generated.go +++ b/plugin/client_rpc_generated.go @@ -767,6 +767,36 @@ func (s *apiRPCServer) CreateUser(args *Z_CreateUserArgs, returns *Z_CreateUserR return nil } +type Z_CreateDirectChannelArgs struct { + A string + B string +} + +type Z_CreateDirectChannelReturns struct { + A *model.Channel + B *model.AppError +} + +func (g *apiRPCClient) CreateDirectChannel(userId1 string, userId2 string) (*model.Channel, *model.AppError) { + _args := &Z_CreateDirectChannelArgs{userId1, userId2} + _returns := &Z_CreateDirectChannelReturns{} + if err := g.client.Call("Plugin.CreateDirectChannel", _args, _returns); err != nil { + log.Printf("RPC call to CreateDirectChannel API failed: %s", err.Error()) + } + return _returns.A, _returns.B +} + +func (s *apiRPCServer) CreateDirectChannel(args *Z_CreateDirectChannelArgs, returns *Z_CreateDirectChannelReturns) error { + if hook, ok := s.impl.(interface { + CreateDirectChannel(userId1 string, userId2 string) (*model.Channel, *model.AppError) + }); ok { + returns.A, returns.B = hook.CreateDirectChannel(args.A, args.B) + } else { + return encodableError(fmt.Errorf("API CreateDirectChannel called but not implemented.")) + } + return nil +} + type Z_DeleteUserArgs struct { A string } diff --git a/plugin/plugintest/api.go b/plugin/plugintest/api.go index 5abde3308d..2f5b48beb7 100644 --- a/plugin/plugintest/api.go +++ b/plugin/plugintest/api.go @@ -112,6 +112,31 @@ func (_m *API) CreateChannel(channel *model.Channel) (*model.Channel, *model.App return r0, r1 } +// CreateDirectChannel provides a mock function with given fields: userId1, userId2 +func (_m *API) CreateDirectChannel(userId1 string, userId2 string) (*model.Channel, *model.AppError) { + ret := _m.Called(userId1, userId2) + + var r0 *model.Channel + if rf, ok := ret.Get(0).(func(string, string) *model.Channel); ok { + r0 = rf(userId1, userId2) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*model.Channel) + } + } + + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok { + r1 = rf(userId1, userId2) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 +} + // CreatePost provides a mock function with given fields: post func (_m *API) CreatePost(post *model.Post) (*model.Post, *model.AppError) { ret := _m.Called(post)