github.com/jfrerich/mattermost-server@v5.8.0-rc2+incompatible/model/slack_attachment.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"fmt"
     8  	"regexp"
     9  )
    10  
    11  var linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
    12  
    13  type SlackAttachment struct {
    14  	Id         int64                   `json:"id"`
    15  	Fallback   string                  `json:"fallback"`
    16  	Color      string                  `json:"color"`
    17  	Pretext    string                  `json:"pretext"`
    18  	AuthorName string                  `json:"author_name"`
    19  	AuthorLink string                  `json:"author_link"`
    20  	AuthorIcon string                  `json:"author_icon"`
    21  	Title      string                  `json:"title"`
    22  	TitleLink  string                  `json:"title_link"`
    23  	Text       string                  `json:"text"`
    24  	Fields     []*SlackAttachmentField `json:"fields"`
    25  	ImageURL   string                  `json:"image_url"`
    26  	ThumbURL   string                  `json:"thumb_url"`
    27  	Footer     string                  `json:"footer"`
    28  	FooterIcon string                  `json:"footer_icon"`
    29  	Timestamp  interface{}             `json:"ts"` // This is either a string or an int64
    30  	Actions    []*PostAction           `json:"actions,omitempty"`
    31  }
    32  
    33  type SlackAttachmentField struct {
    34  	Title string              `json:"title"`
    35  	Value interface{}         `json:"value"`
    36  	Short SlackCompatibleBool `json:"short"`
    37  }
    38  
    39  func StringifySlackFieldValue(a []*SlackAttachment) []*SlackAttachment {
    40  	var nonNilAttachments []*SlackAttachment
    41  	for _, attachment := range a {
    42  		if attachment == nil {
    43  			continue
    44  		}
    45  		nonNilAttachments = append(nonNilAttachments, attachment)
    46  
    47  		var nonNilFields []*SlackAttachmentField
    48  		for _, field := range attachment.Fields {
    49  			if field == nil {
    50  				continue
    51  			}
    52  			nonNilFields = append(nonNilFields, field)
    53  
    54  			if field.Value != nil {
    55  				// Ensure the value is set to a string if it is set
    56  				field.Value = fmt.Sprintf("%v", field.Value)
    57  			}
    58  		}
    59  		attachment.Fields = nonNilFields
    60  	}
    61  	return nonNilAttachments
    62  }
    63  
    64  // This method only parses and processes the attachments,
    65  // all else should be set in the post which is passed
    66  func ParseSlackAttachment(post *Post, attachments []*SlackAttachment) {
    67  	if post.Type == "" {
    68  		post.Type = POST_SLACK_ATTACHMENT
    69  	}
    70  
    71  	postAttachments := []*SlackAttachment{}
    72  
    73  	for _, attachment := range attachments {
    74  		if attachment == nil {
    75  			continue
    76  		}
    77  
    78  		attachment.Text = ParseSlackLinksToMarkdown(attachment.Text)
    79  		attachment.Pretext = ParseSlackLinksToMarkdown(attachment.Pretext)
    80  
    81  		for _, field := range attachment.Fields {
    82  			if value, ok := field.Value.(string); ok {
    83  				field.Value = ParseSlackLinksToMarkdown(value)
    84  			}
    85  		}
    86  		postAttachments = append(postAttachments, attachment)
    87  	}
    88  	post.AddProp("attachments", postAttachments)
    89  }
    90  
    91  func ParseSlackLinksToMarkdown(text string) string {
    92  	return linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})")
    93  }