From e62471dff6082c4e3efb37ea27125ee182083980 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Thu, 17 Oct 2019 16:11:26 -0700 Subject: [PATCH] MM-18816 Adding ability to add users as another user to the plugin API. (#12562) * Adding ability to add users as anouther user to the plugin API. * Documentation feedback. --- app/plugin_api.go | 11 +++++++++++ app/plugin_api_test.go | 12 ++++++++++++ plugin/api.go | 9 ++++++++- plugin/client_rpc_generated.go | 31 +++++++++++++++++++++++++++++++ plugin/plugintest/api.go | 25 +++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 1 deletion(-) diff --git a/app/plugin_api.go b/app/plugin_api.go index 790a14da21..dda5e1ae10 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -406,6 +406,17 @@ func (api *PluginAPI) AddChannelMember(channelId, userId string) (*model.Channel return api.app.AddChannelMember(userId, channel, userRequestorId, postRootId) } +func (api *PluginAPI) AddUserToChannel(channelId, userId, asUserId string) (*model.ChannelMember, *model.AppError) { + postRootId := "" + + channel, err := api.GetChannel(channelId) + if err != nil { + return nil, err + } + + return api.app.AddChannelMember(userId, channel, asUserId, postRootId) +} + func (api *PluginAPI) GetChannelMember(channelId, userId string) (*model.ChannelMember, *model.AppError) { return api.app.GetChannelMember(channelId, userId) } diff --git a/app/plugin_api_test.go b/app/plugin_api_test.go index 34ee7af700..dc5fab7d3e 100644 --- a/app/plugin_api_test.go +++ b/app/plugin_api_test.go @@ -1439,3 +1439,15 @@ func TestPluginAPIGetUnsanitizedConfig(t *testing.T) { assert.NotEqual(t, config.SqlSettings.DataSourceSearchReplicas[i], model.FAKE_SETTING) } } + +func TestPluginAddUserToChannel(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + api := th.SetupPluginAPI() + + member, err := api.AddUserToChannel(th.BasicChannel.Id, th.BasicUser.Id, th.BasicUser2.Id) + require.Nil(t, err) + require.NotNil(t, member) + require.Equal(t, th.BasicChannel.Id, member.ChannelId) + require.Equal(t, th.BasicUser.Id, member.UserId) +} diff --git a/plugin/api.go b/plugin/api.go index 4a21a331d4..1afadc2b89 100644 --- a/plugin/api.go +++ b/plugin/api.go @@ -336,11 +336,18 @@ type API interface { // Minimum server version: 5.10 SearchPostsInTeam(teamId string, paramsList []*model.SearchParams) ([]*model.Post, *model.AppError) - // AddChannelMember creates a channel membership for a user. + // AddChannelMember joins a user to a channel (as if they joined themselves) + // This means the user will not receive notifications for joining the channel. // // Minimum server version: 5.2 AddChannelMember(channelId, userId string) (*model.ChannelMember, *model.AppError) + // AddUserToChannel adds a user to a channel as if the specified user had invited them. + // This means the user will receive the regular notifications for being added to the channel. + // + // Minimum server version: 5.18 + AddUserToChannel(channelId, userId, asUserId string) (*model.ChannelMember, *model.AppError) + // GetChannelMember gets a channel membership for a user. // // Minimum server version: 5.2 diff --git a/plugin/client_rpc_generated.go b/plugin/client_rpc_generated.go index 43ba11b2f0..c50730fbef 100644 --- a/plugin/client_rpc_generated.go +++ b/plugin/client_rpc_generated.go @@ -2282,6 +2282,37 @@ func (s *apiRPCServer) AddChannelMember(args *Z_AddChannelMemberArgs, returns *Z return nil } +type Z_AddUserToChannelArgs struct { + A string + B string + C string +} + +type Z_AddUserToChannelReturns struct { + A *model.ChannelMember + B *model.AppError +} + +func (g *apiRPCClient) AddUserToChannel(channelId, userId, asUserId string) (*model.ChannelMember, *model.AppError) { + _args := &Z_AddUserToChannelArgs{channelId, userId, asUserId} + _returns := &Z_AddUserToChannelReturns{} + if err := g.client.Call("Plugin.AddUserToChannel", _args, _returns); err != nil { + log.Printf("RPC call to AddUserToChannel API failed: %s", err.Error()) + } + return _returns.A, _returns.B +} + +func (s *apiRPCServer) AddUserToChannel(args *Z_AddUserToChannelArgs, returns *Z_AddUserToChannelReturns) error { + if hook, ok := s.impl.(interface { + AddUserToChannel(channelId, userId, asUserId string) (*model.ChannelMember, *model.AppError) + }); ok { + returns.A, returns.B = hook.AddUserToChannel(args.A, args.B, args.C) + } else { + return encodableError(fmt.Errorf("API AddUserToChannel called but not implemented.")) + } + return nil +} + type Z_GetChannelMemberArgs struct { A string B string diff --git a/plugin/plugintest/api.go b/plugin/plugintest/api.go index 0ce1dda159..f308746374 100644 --- a/plugin/plugintest/api.go +++ b/plugin/plugintest/api.go @@ -66,6 +66,31 @@ func (_m *API) AddReaction(reaction *model.Reaction) (*model.Reaction, *model.Ap return r0, r1 } +// AddUserToChannel provides a mock function with given fields: channelId, userId, asUserId +func (_m *API) AddUserToChannel(channelId string, userId string, asUserId string) (*model.ChannelMember, *model.AppError) { + ret := _m.Called(channelId, userId, asUserId) + + var r0 *model.ChannelMember + if rf, ok := ret.Get(0).(func(string, string, string) *model.ChannelMember); ok { + r0 = rf(channelId, userId, asUserId) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*model.ChannelMember) + } + } + + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string, string, string) *model.AppError); ok { + r1 = rf(channelId, userId, asUserId) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 +} + // CopyFileInfos provides a mock function with given fields: userId, fileIds func (_m *API) CopyFileInfos(userId string, fileIds []string) ([]string, *model.AppError) { ret := _m.Called(userId, fileIds)