github.com/nhannv/mattermost-server@v5.11.1+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  func (s *SlackAttachment) Equals(input *SlackAttachment) bool {
    34  	// Direct comparison of simple types
    35  
    36  	if s.Id != input.Id {
    37  		return false
    38  	}
    39  
    40  	if s.Fallback != input.Fallback {
    41  		return false
    42  	}
    43  
    44  	if s.Color != input.Color {
    45  		return false
    46  	}
    47  
    48  	if s.Pretext != input.Pretext {
    49  		return false
    50  	}
    51  
    52  	if s.AuthorName != input.AuthorName {
    53  		return false
    54  	}
    55  
    56  	if s.AuthorLink != input.AuthorLink {
    57  		return false
    58  	}
    59  
    60  	if s.AuthorIcon != input.AuthorIcon {
    61  		return false
    62  	}
    63  
    64  	if s.Title != input.Title {
    65  		return false
    66  	}
    67  
    68  	if s.TitleLink != input.TitleLink {
    69  		return false
    70  	}
    71  
    72  	if s.Text != input.Text {
    73  		return false
    74  	}
    75  
    76  	if s.ImageURL != input.ImageURL {
    77  		return false
    78  	}
    79  
    80  	if s.ThumbURL != input.ThumbURL {
    81  		return false
    82  	}
    83  
    84  	if s.Footer != input.Footer {
    85  		return false
    86  	}
    87  
    88  	if s.FooterIcon != input.FooterIcon {
    89  		return false
    90  	}
    91  
    92  	// Compare length & slice values of fields
    93  	if len(s.Fields) != len(input.Fields) {
    94  		return false
    95  	}
    96  
    97  	for j := range s.Fields {
    98  		if !s.Fields[j].Equals(input.Fields[j]) {
    99  			return false
   100  		}
   101  	}
   102  
   103  	// Compare length & slice values of actions
   104  	if len(s.Actions) != len(input.Actions) {
   105  		return false
   106  	}
   107  
   108  	for j := range s.Actions {
   109  		if !s.Actions[j].Equals(input.Actions[j]) {
   110  			return false
   111  		}
   112  	}
   113  
   114  	if s.Timestamp != input.Timestamp {
   115  		return false
   116  	}
   117  
   118  	return true
   119  }
   120  
   121  type SlackAttachmentField struct {
   122  	Title string              `json:"title"`
   123  	Value interface{}         `json:"value"`
   124  	Short SlackCompatibleBool `json:"short"`
   125  }
   126  
   127  func (s *SlackAttachmentField) Equals(input *SlackAttachmentField) bool {
   128  	if s.Title != input.Title {
   129  		return false
   130  	}
   131  
   132  	if s.Value != input.Value {
   133  		return false
   134  	}
   135  
   136  	if s.Short != input.Short {
   137  		return false
   138  	}
   139  
   140  	return true
   141  }
   142  
   143  func StringifySlackFieldValue(a []*SlackAttachment) []*SlackAttachment {
   144  	var nonNilAttachments []*SlackAttachment
   145  	for _, attachment := range a {
   146  		if attachment == nil {
   147  			continue
   148  		}
   149  		nonNilAttachments = append(nonNilAttachments, attachment)
   150  
   151  		var nonNilFields []*SlackAttachmentField
   152  		for _, field := range attachment.Fields {
   153  			if field == nil {
   154  				continue
   155  			}
   156  			nonNilFields = append(nonNilFields, field)
   157  
   158  			if field.Value != nil {
   159  				// Ensure the value is set to a string if it is set
   160  				field.Value = fmt.Sprintf("%v", field.Value)
   161  			}
   162  		}
   163  		attachment.Fields = nonNilFields
   164  	}
   165  	return nonNilAttachments
   166  }
   167  
   168  // This method only parses and processes the attachments,
   169  // all else should be set in the post which is passed
   170  func ParseSlackAttachment(post *Post, attachments []*SlackAttachment) {
   171  	if post.Type == "" {
   172  		post.Type = POST_SLACK_ATTACHMENT
   173  	}
   174  
   175  	postAttachments := []*SlackAttachment{}
   176  
   177  	for _, attachment := range attachments {
   178  		if attachment == nil {
   179  			continue
   180  		}
   181  
   182  		attachment.Text = ParseSlackLinksToMarkdown(attachment.Text)
   183  		attachment.Pretext = ParseSlackLinksToMarkdown(attachment.Pretext)
   184  
   185  		for _, field := range attachment.Fields {
   186  			if value, ok := field.Value.(string); ok {
   187  				field.Value = ParseSlackLinksToMarkdown(value)
   188  			}
   189  		}
   190  		postAttachments = append(postAttachments, attachment)
   191  	}
   192  	post.AddProp("attachments", postAttachments)
   193  }
   194  
   195  func ParseSlackLinksToMarkdown(text string) string {
   196  	return linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})")
   197  }