Skip to content

Commit

Permalink
[MI-3832] Avoid publishing websocket event when user is not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
ayusht2810 committed Dec 14, 2023
1 parent effe67f commit 7b03ae6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
12 changes: 6 additions & 6 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {},
},
Expand Down

0 comments on commit 7b03ae6

Please sign in to comment.