From 4111ffdb4db7b5a277567a6548931530f90e368e Mon Sep 17 00:00:00 2001 From: Romain FOYARD Date: Wed, 30 Oct 2019 18:36:14 +0100 Subject: [PATCH] Include extra metadata when clicking an interactive button (#12697) * Include user_name, team_domain and channel_name when clicking an interactive button * Fixed api4's TestPostActionCookies test * Moved team database request to improve concurrency. Renamed TeamDomain to TeamName. * Further optimizations to database requests * Removed useless goroutine when fetching channel --- api4/integration_action_test.go | 5 ++++- app/integration_action.go | 36 +++++++++++++++++++++++++++++++++ app/integration_action_test.go | 3 +++ model/integration_action.go | 19 +++++++++-------- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/api4/integration_action_test.go b/api4/integration_action_test.go index 7b629f4a04..c543d851d2 100644 --- a/api4/integration_action_test.go +++ b/api4/integration_action_test.go @@ -27,8 +27,11 @@ func (th *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { assert.NotEmpty(th.t, string(bb)) poir := model.PostActionIntegrationRequestFromJson(bytes.NewReader(bb)) assert.NotEmpty(th.t, poir.UserId) + assert.NotEmpty(th.t, poir.UserName) assert.NotEmpty(th.t, poir.ChannelId) - assert.Empty(th.t, poir.TeamId) + assert.NotEmpty(th.t, poir.ChannelName) + assert.NotEmpty(th.t, poir.TeamId) + assert.NotEmpty(th.t, poir.TeamName) assert.NotEmpty(th.t, poir.PostId) assert.NotEmpty(th.t, poir.TriggerId) assert.Equal(th.t, "button", poir.Type) diff --git a/app/integration_action.go b/app/integration_action.go index 98309bfe34..5c95cf6377 100644 --- a/app/integration_action.go +++ b/app/integration_action.go @@ -76,6 +76,13 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st close(cchan) }() + userChan := make(chan store.StoreResult, 1) + go func() { + user, err := a.Srv.Store.User().Get(upstreamRequest.UserId) + userChan <- store.StoreResult{Data: user, Err: err} + close(userChan) + }() + result := <-pchan if result.Err != nil { if cookie == nil { @@ -89,7 +96,14 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st return "", model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, "postId doesn't match", http.StatusBadRequest) } + channel, err := a.Srv.Store.Channel().Get(cookie.ChannelId, true) + if err != nil { + return "", err + } + upstreamRequest.ChannelId = cookie.ChannelId + upstreamRequest.ChannelName = channel.Name + upstreamRequest.TeamId = channel.TeamId upstreamRequest.Type = cookie.Type upstreamRequest.Context = cookie.Integration.Context datasource = cookie.DataSource @@ -112,6 +126,7 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st } upstreamRequest.ChannelId = post.ChannelId + upstreamRequest.ChannelName = channel.Name upstreamRequest.TeamId = channel.TeamId upstreamRequest.Type = action.Type upstreamRequest.Context = action.Integration.Context @@ -140,6 +155,27 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st upstreamURL = action.Integration.URL } + teamChan := make(chan store.StoreResult, 1) + go func() { + team, err := a.Srv.Store.Team().Get(upstreamRequest.TeamId) + teamChan <- store.StoreResult{Data: team, Err: err} + close(teamChan) + }() + + ur := <-userChan + if ur.Err != nil { + return "", ur.Err + } + user := ur.Data.(*model.User) + upstreamRequest.UserName = user.Username + + tr := <-teamChan + if tr.Err != nil { + return "", tr.Err + } + team := tr.Data.(*model.Team) + upstreamRequest.TeamName = team.Name + if upstreamRequest.Type == model.POST_ACTION_TYPE_SELECT { if selectedOption != "" { if upstreamRequest.Context == nil { diff --git a/app/integration_action_test.go b/app/integration_action_test.go index 6474a16b29..fb5727f7ae 100644 --- a/app/integration_action_test.go +++ b/app/integration_action_test.go @@ -80,8 +80,11 @@ func TestPostAction(t *testing.T) { assert.NotNil(t, request) assert.Equal(t, request.UserId, th.BasicUser.Id) + assert.Equal(t, request.UserName, th.BasicUser.Username) assert.Equal(t, request.ChannelId, th.BasicChannel.Id) + assert.Equal(t, request.ChannelName, th.BasicChannel.Name) assert.Equal(t, request.TeamId, th.BasicTeam.Id) + assert.Equal(t, request.TeamName, th.BasicTeam.Name) assert.True(t, len(request.TriggerId) > 0) if request.Type == model.POST_ACTION_TYPE_SELECT { assert.Equal(t, request.DataSource, "some_source") diff --git a/model/integration_action.go b/model/integration_action.go index cef7256abb..64898e242b 100644 --- a/model/integration_action.go +++ b/model/integration_action.go @@ -157,14 +157,17 @@ type PostActionIntegration struct { } type PostActionIntegrationRequest struct { - UserId string `json:"user_id"` - ChannelId string `json:"channel_id"` - TeamId string `json:"team_id"` - PostId string `json:"post_id"` - TriggerId string `json:"trigger_id"` - Type string `json:"type"` - DataSource string `json:"data_source"` - Context map[string]interface{} `json:"context,omitempty"` + UserId string `json:"user_id"` + UserName string `json:"user_name"` + ChannelId string `json:"channel_id"` + ChannelName string `json:"channel_name"` + TeamId string `json:"team_id"` + TeamName string `json:"team_domain"` + PostId string `json:"post_id"` + TriggerId string `json:"trigger_id"` + Type string `json:"type"` + DataSource string `json:"data_source"` + Context map[string]interface{} `json:"context,omitempty"` } type PostActionIntegrationResponse struct {