From 8945bd6cd0ae9ac558c74955a913d7fd738d5116 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Tue, 3 Jan 2017 10:09:19 -0500 Subject: Fix msg command so that it doesn't try to pull all user profiles at once (#4936) --- api/command_msg.go | 93 +++++++++++++++++++++++-------------------------- api/command_msg_test.go | 3 ++ 2 files changed, 46 insertions(+), 50 deletions(-) (limited to 'api') diff --git a/api/command_msg.go b/api/command_msg.go index 8a9949544..f2d06824d 100644 --- a/api/command_msg.go +++ b/api/command_msg.go @@ -39,63 +39,56 @@ func (me *msgProvider) DoCommand(c *Context, args *model.CommandArgs, message st splitMessage := strings.SplitN(message, " ", 2) parsedMessage := "" - targetUser := "" + targetUsername := "" if len(splitMessage) > 1 { parsedMessage = strings.SplitN(message, " ", 2)[1] } - targetUser = strings.SplitN(message, " ", 2)[0] - targetUser = strings.TrimPrefix(targetUser, "@") - - // FIX ME - // Why isn't this selecting by username since we have that? - if profileList := <-Srv.Store.User().GetAll(); profileList.Err != nil { - c.Err = profileList.Err - return &model.CommandResponse{Text: c.T("api.command_msg.list.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + targetUsername = strings.SplitN(message, " ", 2)[0] + targetUsername = strings.TrimPrefix(targetUsername, "@") + + var userProfile *model.User + if result := <-Srv.Store.User().GetByUsername(targetUsername); result.Err != nil { + c.Err = result.Err + return &model.CommandResponse{Text: c.T("api.command_msg.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } else { - profileUsers := profileList.Data.([]*model.User) - for _, userProfile := range profileUsers { - // Don't let users open DMs with themselves. It probably won't work out well. - if userProfile.Id == c.Session.UserId { - continue - } - if userProfile.Username == targetUser { - targetChannelId := "" - - // Find the channel based on this user - channelName := model.GetDMNameFromIds(c.Session.UserId, userProfile.Id) - - if channel := <-Srv.Store.Channel().GetByName(c.TeamId, channelName); channel.Err != nil { - if channel.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { - if directChannel, err := CreateDirectChannel(c.Session.UserId, userProfile.Id); err != nil { - c.Err = err - return &model.CommandResponse{Text: c.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} - } else { - targetChannelId = directChannel.Id - } - } else { - c.Err = channel.Err - return &model.CommandResponse{Text: c.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} - } - } else { - targetChannelId = channel.Data.(*model.Channel).Id - } - - makeDirectChannelVisible(targetChannelId) - if len(parsedMessage) > 0 { - post := &model.Post{} - post.Message = parsedMessage - post.ChannelId = targetChannelId - post.UserId = c.Session.UserId - if _, err := CreatePost(c, post, true); err != nil { - return &model.CommandResponse{Text: c.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} - } - } - - return &model.CommandResponse{GotoLocation: c.GetTeamURL() + "/channels/" + channelName, Text: "", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + userProfile = result.Data.(*model.User) + } + + if userProfile.Id == c.Session.UserId { + return &model.CommandResponse{Text: c.T("api.command_msg.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + } + + // Find the channel based on this user + channelName := model.GetDMNameFromIds(c.Session.UserId, userProfile.Id) + + targetChannelId := "" + if channel := <-Srv.Store.Channel().GetByName(c.TeamId, channelName); channel.Err != nil { + if channel.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { + if directChannel, err := CreateDirectChannel(c.Session.UserId, userProfile.Id); err != nil { + c.Err = err + return &model.CommandResponse{Text: c.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + } else { + targetChannelId = directChannel.Id } + } else { + c.Err = channel.Err + return &model.CommandResponse{Text: c.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + } + } else { + targetChannelId = channel.Data.(*model.Channel).Id + } + + makeDirectChannelVisible(targetChannelId) + if len(parsedMessage) > 0 { + post := &model.Post{} + post.Message = parsedMessage + post.ChannelId = targetChannelId + post.UserId = c.Session.UserId + if _, err := CreatePost(c, post, true); err != nil { + return &model.CommandResponse{Text: c.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } } - return &model.CommandResponse{Text: c.T("api.command_msg.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + return &model.CommandResponse{GotoLocation: c.GetTeamURL() + "/channels/" + channelName, Text: "", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } diff --git a/api/command_msg_test.go b/api/command_msg_test.go index ad1c8d5ce..4fe28fdba 100644 --- a/api/command_msg_test.go +++ b/api/command_msg_test.go @@ -38,4 +38,7 @@ func TestMsgCommands(t *testing.T) { if !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) && !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id) { t.Fatal("failed to go back to existing direct channel") } + + Client.Must(Client.Command("", "/msg "+th.BasicUser.Username+" foobar")) + Client.Must(Client.Command("", "/msg junk foobar")) } -- cgit v1.2.3-1-g7c22