From a755bcdde63b2f27866d49be97bb82e4cdb0e893 Mon Sep 17 00:00:00 2001 From: Charles Birk Date: Mon, 17 Sep 2018 10:15:28 -0400 Subject: [MM-11860]: Expose slack attachment parsing functions in the model package (#9351) Refactored parseSlackAttachment functions from https://github.com/mattermost/mattermost-server/blob/master/app/post.go#L312 into model/slack_attachments.go so that plugins have access to them. --- app/post.go | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) (limited to 'app/post.go') diff --git a/app/post.go b/app/post.go index 114029f44..a36e9709f 100644 --- a/app/post.go +++ b/app/post.go @@ -13,7 +13,6 @@ import ( "net/http" "net/url" "path" - "regexp" "strings" "github.com/dyatlov/go-opengraph/opengraph" @@ -25,8 +24,6 @@ import ( "golang.org/x/net/html/charset" ) -var linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) - func (a *App) CreatePostAsUser(post *model.Post) (*model.Post, *model.AppError) { // Check that channel has not been deleted var channel *model.Channel @@ -308,28 +305,6 @@ func (a *App) handlePostEvents(post *model.Post, user *model.User, channel *mode return nil } -// This method only parses and processes the attachments, -// all else should be set in the post which is passed -func parseSlackAttachment(post *model.Post, attachments []*model.SlackAttachment) { - post.Type = model.POST_SLACK_ATTACHMENT - - for _, attachment := range attachments { - attachment.Text = parseSlackLinksToMarkdown(attachment.Text) - attachment.Pretext = parseSlackLinksToMarkdown(attachment.Pretext) - - for _, field := range attachment.Fields { - if value, ok := field.Value.(string); ok { - field.Value = parseSlackLinksToMarkdown(value) - } - } - } - post.AddProp("attachments", attachments) -} - -func parseSlackLinksToMarkdown(text string) string { - return linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})") -} - func (a *App) SendEphemeralPost(userId string, post *model.Post) *model.Post { post.Type = model.POST_EPHEMERAL @@ -931,7 +906,7 @@ func (a *App) DoPostAction(postId, actionId, userId, selectedOption string) *mod if response.EphemeralText != "" { ephemeralPost := &model.Post{} - ephemeralPost.Message = parseSlackLinksToMarkdown(response.EphemeralText) + ephemeralPost.Message = model.ParseSlackLinksToMarkdown(response.EphemeralText) ephemeralPost.ChannelId = post.ChannelId ephemeralPost.RootId = post.RootId if ephemeralPost.RootId == "" { -- cgit v1.2.3-1-g7c22