From 046d2c61a5fb88742ba232f7d541d9ba9ed42891 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 12 Jan 2017 15:08:48 -0500 Subject: Fix slack link parsing for integrations without attachments (#5050) --- api/post.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'api/post.go') diff --git a/api/post.go b/api/post.go index 8b5a33224..27a610199 100644 --- a/api/post.go +++ b/api/post.go @@ -174,6 +174,8 @@ func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post return rpost, nil } +var linkWithTextRegex *regexp.Regexp = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) + func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIconUrl string, props model.StringInterface, postType string) (*model.Post, *model.AppError) { post := &model.Post{UserId: c.Session.UserId, ChannelId: channelId, Message: text, Type: postType} post.AddProp("from_webhook", "true") @@ -192,10 +194,12 @@ func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIc } } + post.Message = parseSlackLinksToMarkdown(post.Message) + if len(props) > 0 { for key, val := range props { if key == "attachments" { - createSlackPost(post, val) + parseSlackAttachment(post, val) } else if key != "override_icon_url" && key != "override_username" && key != "from_webhook" { post.AddProp(key, val) } @@ -210,12 +214,12 @@ func CreateWebhookPost(c *Context, channelId, text, overrideUsername, overrideIc } func CreateCommandPost(c *Context, post *model.Post, response *model.CommandResponse) { - post.Message = response.Text + post.Message = parseSlackLinksToMarkdown(response.Text) post.UserId = c.Session.UserId post.CreateAt = model.GetMillis() if response.Attachments != nil { - createSlackPost(post, response.Attachments) + parseSlackAttachment(post, response.Attachments) } switch response.ResponseType { @@ -235,13 +239,9 @@ func CreateCommandPost(c *Context, post *model.Post, response *model.CommandResp // This method only parses and processes the attachments, // all else should be set in the post which is passed -func createSlackPost(post *model.Post, attachments interface{}) { +func parseSlackAttachment(post *model.Post, attachments interface{}) { post.Type = model.POST_SLACK_ATTACHMENT - // parse links into Markdown format - linkWithTextRegex := regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) - post.Message = linkWithTextRegex.ReplaceAllString(post.Message, "[${2}](${1})") - if list, success := attachments.([]interface{}); success { for i, aInt := range list { attachment := aInt.(map[string]interface{}) @@ -275,6 +275,10 @@ func createSlackPost(post *model.Post, attachments interface{}) { } } +func parseSlackLinksToMarkdown(text string) string { + return linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})") +} + func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) { tchan := Srv.Store.Team().Get(c.TeamId) cchan := Srv.Store.Channel().Get(post.ChannelId, true) -- cgit v1.2.3-1-g7c22