From 566f28be0a203edf1ecb021cb8673575b31956dd Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 17 Jan 2020 08:34:11 +0100 Subject: [PATCH] =?UTF-8?q?GH-12702=20v2:=20Add=20new=20command=20response?= =?UTF-8?q?=20parameter:=20"skip=5Fslack=5Fp=E2=80=A6=20(#13420)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add new command response parameter: "skip_slack_parsing". Skips Slack magic if set to "true". (#12702) --- app/command.go | 9 +++------ app/command_code.go | 2 +- app/command_test.go | 4 ++++ model/command_response.go | 23 ++++++++++++----------- model/command_response_test.go | 3 +++ 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/app/command.go b/app/command.go index 01d847021a..05f91351af 100644 --- a/app/command.go +++ b/app/command.go @@ -434,16 +434,13 @@ func (a *App) HandleCommandResponsePost(command *model.Command, args *model.Comm post.AddProp("from_webhook", "true") } - // Do not process text if this is a code block - skipSlackParsing := command.Trigger == "code" - - // Process Slack text replacements - if !skipSlackParsing { + // Process Slack text replacements if the response does not contain "skip_slack_parsing": true. + if !response.SkipSlackParsing { response.Text = a.ProcessSlackText(response.Text) response.Attachments = a.ProcessSlackAttachments(response.Attachments) } - if _, err := a.CreateCommandPost(post, args.TeamId, response, skipSlackParsing); err != nil { + if _, err := a.CreateCommandPost(post, args.TeamId, response, response.SkipSlackParsing); err != nil { return post, err } diff --git a/app/command_code.go b/app/command_code.go index 04ac57dd00..db44a74ed2 100644 --- a/app/command_code.go +++ b/app/command_code.go @@ -40,5 +40,5 @@ func (me *CodeProvider) DoCommand(a *App, args *model.CommandArgs, message strin return &model.CommandResponse{Text: args.T("api.command_code.message.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } rmsg := " " + strings.Join(strings.Split(message, "\n"), "\n ") - return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL, Text: rmsg} + return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL, Text: rmsg, SkipSlackParsing: true} } diff --git a/app/command_test.go b/app/command_test.go index 554b94a1ba..652778a856 100644 --- a/app/command_test.go +++ b/app/command_test.go @@ -221,7 +221,11 @@ func TestHandleCommandResponsePost(t *testing.T) { Text: "", }, } + + // set and unset SkipSlackParsing here seems the nicest way as no separate response objects are created for every testcase. + resp.SkipSlackParsing = true post, err = th.App.HandleCommandResponsePost(command, args, resp, builtIn) + resp.SkipSlackParsing = false assert.Nil(t, err) assert.Equal(t, resp.Text, post.Message, "/code text should not be converted to Slack links") diff --git a/model/command_response.go b/model/command_response.go index fb2a67603e..26b6cceba4 100644 --- a/model/command_response.go +++ b/model/command_response.go @@ -18,17 +18,18 @@ const ( ) type CommandResponse struct { - ResponseType string `json:"response_type"` - Text string `json:"text"` - Username string `json:"username"` - ChannelId string `json:"channel_id"` - IconURL string `json:"icon_url"` - Type string `json:"type"` - Props StringInterface `json:"props"` - GotoLocation string `json:"goto_location"` - TriggerId string `json:"trigger_id"` - Attachments []*SlackAttachment `json:"attachments"` - ExtraResponses []*CommandResponse `json:"extra_responses"` + ResponseType string `json:"response_type"` + Text string `json:"text"` + Username string `json:"username"` + ChannelId string `json:"channel_id"` + IconURL string `json:"icon_url"` + Type string `json:"type"` + Props StringInterface `json:"props"` + GotoLocation string `json:"goto_location"` + TriggerId string `json:"trigger_id"` + SkipSlackParsing bool `json:"skip_slack_parsing"` // Set to `true` to skip the Slack-compatibility handling of Text. + Attachments []*SlackAttachment `json:"attachments"` + ExtraResponses []*CommandResponse `json:"extra_responses"` } func (o *CommandResponse) ToJson() string { diff --git a/model/command_response_test.go b/model/command_response_test.go index 31a7c2735e..5c470e649d 100644 --- a/model/command_response_test.go +++ b/model/command_response_test.go @@ -20,6 +20,9 @@ func TestCommandResponseFromHTTPBody(t *testing.T) { {"text/plain", "foo", "foo"}, {"application/json", `{"text": "foo"}`, "foo"}, {"application/json; charset=utf-8", `{"text": "foo"}`, "foo"}, + {"application/json", `{"text": "` + "```" + `haskell\nlet\n\nf1 = [ 3 | a <- [1]]\nf2 = [ 4 | b <- [2]]\nf3 = \\p -> 5\n\nin 1\n` + "```" + `", "skip_slack_parsing": true}`, + "```haskell\nlet\n\nf1 = [ 3 | a <- [1]]\nf2 = [ 4 | b <- [2]]\nf3 = \\p -> 5\n\nin 1\n```", + }, } { response, err := CommandResponseFromHTTPBody(test.ContentType, strings.NewReader(test.Body)) assert.NoError(t, err)