diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-01-12 15:08:48 -0500 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2017-01-12 15:08:48 -0500 |
commit | 046d2c61a5fb88742ba232f7d541d9ba9ed42891 (patch) | |
tree | eb02bb7ddac1a58d9f24127be3e8b2ca1e5e2b0a | |
parent | a5dd7e6d63a9be8a11fdcebdf22b98d131abc673 (diff) | |
download | chat-046d2c61a5fb88742ba232f7d541d9ba9ed42891.tar.gz chat-046d2c61a5fb88742ba232f7d541d9ba9ed42891.tar.bz2 chat-046d2c61a5fb88742ba232f7d541d9ba9ed42891.zip |
Fix slack link parsing for integrations without attachments (#5050)
-rw-r--r-- | api/post.go | 20 |
1 files changed, 12 insertions, 8 deletions
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) |