[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.
This commit is contained in:
Charles Birk
2018-09-17 10:15:28 -04:00
committed by Joram Wilander
parent 7226ea7dfb
commit a755bcdde6
5 changed files with 30 additions and 30 deletions

View File

@@ -5,8 +5,11 @@ package model
import (
"fmt"
"regexp"
)
var linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
type SlackAttachment struct {
Id int64 `json:"id"`
Fallback string `json:"fallback"`
@@ -57,3 +60,25 @@ func StringifySlackFieldValue(a []*SlackAttachment) []*SlackAttachment {
}
return nonNilAttachments
}
// This method only parses and processes the attachments,
// all else should be set in the post which is passed
func ParseSlackAttachment(post *Post, attachments []*SlackAttachment) {
post.Type = 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})")
}