mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
[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:
committed by
Joram Wilander
parent
7226ea7dfb
commit
a755bcdde6
@@ -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})")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user