From 7b03ae607ff52fe48dc4adff9edea4f5cd9add34 Mon Sep 17 00:00:00 2001 From: ayusht2810 Date: Thu, 14 Dec 2023 17:57:36 +0530 Subject: [PATCH] [MI-3832] Avoid publishing websocket event when user is not connected --- server/command.go | 12 ++++++------ server/command_test.go | 9 ++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/server/command.go b/server/command.go index d8ef1f878..0eae58480 100644 --- a/server/command.go +++ b/server/command.go @@ -147,6 +147,12 @@ func (p *Plugin) ExecuteCommand(_ *plugin.Context, args *model.CommandArgs) (*mo } func (p *Plugin) executeLinkCommand(args *model.CommandArgs, parameters []string) (*model.CommandResponse, *model.AppError) { + client, err := p.GetClientForUser(args.UserId) + if err != nil { + p.API.LogError("Unable to get the client for user", "MMUserID", args.UserId, "Error", err.Error()) + return p.cmdError(args.UserId, args.ChannelId, "Unable to link the channel, looks like your account is not connected to MS Teams") + } + if len(parameters) < 2 { p.API.PublishWebSocketEvent( "link_channels", @@ -157,12 +163,6 @@ func (p *Plugin) executeLinkCommand(args *model.CommandArgs, parameters []string return &model.CommandResponse{}, nil } - client, err := p.GetClientForUser(args.UserId) - if err != nil { - p.API.LogError("Unable to get the client for user", "MMUserID", args.UserId, "Error", err.Error()) - return p.cmdError(args.UserId, args.ChannelId, "Unable to link the channel, looks like your account is not connected to MS Teams") - } - p.sendBotEphemeralPost(args.UserId, args.ChannelId, commandWaitingMessage) if errMsg, _ := p.LinkChannels(args.UserId, args.TeamId, args.ChannelId, parameters[0], parameters[1], client); errMsg != "" { return p.cmdError(args.UserId, args.ChannelId, errMsg) diff --git a/server/command_test.go b/server/command_test.go index d4abb9269..c4f8c66f8 100644 --- a/server/command_test.go +++ b/server/command_test.go @@ -725,11 +725,18 @@ func TestExecuteLinkCommand(t *testing.T) { api.On("GetChannel", testutils.GetChannelID()).Return(&model.Channel{ Type: model.ChannelTypeOpen, }, nil).Times(1) + api.On("GetConfig").Return(&model.Config{ + ServiceSettings: model.ServiceSettings{ + SiteURL: model.NewString("/"), + }, + }, nil).Times(1) api.On("HasPermissionToChannel", testutils.GetUserID(), testutils.GetChannelID(), model.PermissionManageChannelRoles).Return(true).Times(1) api.On("PublishWebSocketEvent", "link_channels", (map[string]interface{})(nil), &model.WebsocketBroadcast{UserId: testutils.GetUserID()}).Return().Once() api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost("bot-user-id", testutils.GetChannelID(), "Invalid link command, please pass the MS Teams team id and channel id as parameters.")).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID(), time.Now().UnixMicro())).Times(1) }, - setupStore: func(s *mockStore.Store) {}, + setupStore: func(s *mockStore.Store) { + s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&oauth2.Token{}, nil).Times(1) + }, setupClient: func(c *mockClient.Client, uc *mockClient.Client) {}, setupMetrics: func(mockmetrics *mockMetrics.Metrics) {}, },