github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/model/outgoing_webhook.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"io"
    10  	"net/http"
    11  	"net/url"
    12  	"strconv"
    13  	"strings"
    14  )
    15  
    16  type OutgoingWebhook struct {
    17  	Id           string      `json:"id"`
    18  	Token        string      `json:"token"`
    19  	CreateAt     int64       `json:"create_at"`
    20  	UpdateAt     int64       `json:"update_at"`
    21  	DeleteAt     int64       `json:"delete_at"`
    22  	CreatorId    string      `json:"creator_id"`
    23  	ChannelId    string      `json:"channel_id"`
    24  	TeamId       string      `json:"team_id"`
    25  	TriggerWords StringArray `json:"trigger_words"`
    26  	TriggerWhen  int         `json:"trigger_when"`
    27  	CallbackURLs StringArray `json:"callback_urls"`
    28  	DisplayName  string      `json:"display_name"`
    29  	Description  string      `json:"description"`
    30  	ContentType  string      `json:"content_type"`
    31  	Username     string      `json:"username"`
    32  	IconURL      string      `json:"icon_url"`
    33  }
    34  
    35  type OutgoingWebhookPayload struct {
    36  	Token       string `json:"token"`
    37  	TeamId      string `json:"team_id"`
    38  	TeamDomain  string `json:"team_domain"`
    39  	ChannelId   string `json:"channel_id"`
    40  	ChannelName string `json:"channel_name"`
    41  	Timestamp   int64  `json:"timestamp"`
    42  	UserId      string `json:"user_id"`
    43  	UserName    string `json:"user_name"`
    44  	PostId      string `json:"post_id"`
    45  	Text        string `json:"text"`
    46  	TriggerWord string `json:"trigger_word"`
    47  	FileIds     string `json:"file_ids"`
    48  }
    49  
    50  type OutgoingWebhookResponse struct {
    51  	Text         *string            `json:"text"`
    52  	Username     string             `json:"username"`
    53  	IconURL      string             `json:"icon_url"`
    54  	Props        StringInterface    `json:"props"`
    55  	Attachments  []*SlackAttachment `json:"attachments"`
    56  	Type         string             `json:"type"`
    57  	ResponseType string             `json:"response_type"`
    58  }
    59  
    60  const OUTGOING_HOOK_RESPONSE_TYPE_COMMENT = "comment"
    61  
    62  func (o *OutgoingWebhookPayload) ToJSON() string {
    63  	b, _ := json.Marshal(o)
    64  	return string(b)
    65  }
    66  
    67  func (o *OutgoingWebhookPayload) ToFormValues() string {
    68  	v := url.Values{}
    69  	v.Set("token", o.Token)
    70  	v.Set("team_id", o.TeamId)
    71  	v.Set("team_domain", o.TeamDomain)
    72  	v.Set("channel_id", o.ChannelId)
    73  	v.Set("channel_name", o.ChannelName)
    74  	v.Set("timestamp", strconv.FormatInt(o.Timestamp/1000, 10))
    75  	v.Set("user_id", o.UserId)
    76  	v.Set("user_name", o.UserName)
    77  	v.Set("post_id", o.PostId)
    78  	v.Set("text", o.Text)
    79  	v.Set("trigger_word", o.TriggerWord)
    80  	v.Set("file_ids", o.FileIds)
    81  
    82  	return v.Encode()
    83  }
    84  
    85  func (o *OutgoingWebhook) ToJson() string {
    86  	b, _ := json.Marshal(o)
    87  	return string(b)
    88  }
    89  
    90  func OutgoingWebhookFromJson(data io.Reader) *OutgoingWebhook {
    91  	var o *OutgoingWebhook
    92  	json.NewDecoder(data).Decode(&o)
    93  	return o
    94  }
    95  
    96  func OutgoingWebhookListToJson(l []*OutgoingWebhook) string {
    97  	b, _ := json.Marshal(l)
    98  	return string(b)
    99  }
   100  
   101  func OutgoingWebhookListFromJson(data io.Reader) []*OutgoingWebhook {
   102  	var o []*OutgoingWebhook
   103  	json.NewDecoder(data).Decode(&o)
   104  	return o
   105  }
   106  
   107  func (o *OutgoingWebhookResponse) ToJson() string {
   108  	b, _ := json.Marshal(o)
   109  	return string(b)
   110  }
   111  
   112  func OutgoingWebhookResponseFromJson(data io.Reader) (*OutgoingWebhookResponse, error) {
   113  	var o *OutgoingWebhookResponse
   114  	err := json.NewDecoder(data).Decode(&o)
   115  	return o, err
   116  }
   117  
   118  func (o *OutgoingWebhook) IsValid() *AppError {
   119  
   120  	if len(o.Id) != 26 {
   121  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.id.app_error", nil, "", http.StatusBadRequest)
   122  	}
   123  
   124  	if len(o.Token) != 26 {
   125  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.token.app_error", nil, "", http.StatusBadRequest)
   126  	}
   127  
   128  	if o.CreateAt == 0 {
   129  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.create_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   130  	}
   131  
   132  	if o.UpdateAt == 0 {
   133  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   134  	}
   135  
   136  	if len(o.CreatorId) != 26 {
   137  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.user_id.app_error", nil, "", http.StatusBadRequest)
   138  	}
   139  
   140  	if len(o.ChannelId) != 0 && len(o.ChannelId) != 26 {
   141  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.channel_id.app_error", nil, "", http.StatusBadRequest)
   142  	}
   143  
   144  	if len(o.TeamId) != 26 {
   145  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.team_id.app_error", nil, "", http.StatusBadRequest)
   146  	}
   147  
   148  	if len(fmt.Sprintf("%s", o.TriggerWords)) > 1024 {
   149  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.words.app_error", nil, "", http.StatusBadRequest)
   150  	}
   151  
   152  	if len(o.TriggerWords) != 0 {
   153  		for _, triggerWord := range o.TriggerWords {
   154  			if len(triggerWord) == 0 {
   155  				return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.trigger_words.app_error", nil, "", http.StatusBadRequest)
   156  			}
   157  		}
   158  	}
   159  
   160  	if len(o.CallbackURLs) == 0 || len(fmt.Sprintf("%s", o.CallbackURLs)) > 1024 {
   161  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.callback.app_error", nil, "", http.StatusBadRequest)
   162  	}
   163  
   164  	for _, callback := range o.CallbackURLs {
   165  		if !IsValidHttpUrl(callback) {
   166  			return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.url.app_error", nil, "", http.StatusBadRequest)
   167  		}
   168  	}
   169  
   170  	if len(o.DisplayName) > 64 {
   171  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.display_name.app_error", nil, "", http.StatusBadRequest)
   172  	}
   173  
   174  	if len(o.Description) > 500 {
   175  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.description.app_error", nil, "", http.StatusBadRequest)
   176  	}
   177  
   178  	if len(o.ContentType) > 128 {
   179  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.content_type.app_error", nil, "", http.StatusBadRequest)
   180  	}
   181  
   182  	if o.TriggerWhen > 1 {
   183  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.content_type.app_error", nil, "", http.StatusBadRequest)
   184  	}
   185  
   186  	if len(o.Username) > 64 {
   187  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.username.app_error", nil, "", http.StatusBadRequest)
   188  	}
   189  
   190  	if len(o.IconURL) > 1024 {
   191  		return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.icon_url.app_error", nil, "", http.StatusBadRequest)
   192  	}
   193  
   194  	return nil
   195  }
   196  
   197  func (o *OutgoingWebhook) PreSave() {
   198  	if o.Id == "" {
   199  		o.Id = NewId()
   200  	}
   201  
   202  	if o.Token == "" {
   203  		o.Token = NewId()
   204  	}
   205  
   206  	o.CreateAt = GetMillis()
   207  	o.UpdateAt = o.CreateAt
   208  }
   209  
   210  func (o *OutgoingWebhook) PreUpdate() {
   211  	o.UpdateAt = GetMillis()
   212  }
   213  
   214  func (o *OutgoingWebhook) TriggerWordExactMatch(word string) bool {
   215  	if len(word) == 0 {
   216  		return false
   217  	}
   218  
   219  	for _, trigger := range o.TriggerWords {
   220  		if trigger == word {
   221  			return true
   222  		}
   223  	}
   224  
   225  	return false
   226  }
   227  
   228  func (o *OutgoingWebhook) TriggerWordStartsWith(word string) bool {
   229  	if len(word) == 0 {
   230  		return false
   231  	}
   232  
   233  	for _, trigger := range o.TriggerWords {
   234  		if strings.HasPrefix(word, trigger) {
   235  			return true
   236  		}
   237  	}
   238  
   239  	return false
   240  }
   241  
   242  func (o *OutgoingWebhook) GetTriggerWord(word string, isExactMatch bool) (triggerWord string) {
   243  	if len(word) == 0 {
   244  		return
   245  	}
   246  
   247  	if isExactMatch {
   248  		for _, trigger := range o.TriggerWords {
   249  			if trigger == word {
   250  				triggerWord = trigger
   251  				break
   252  			}
   253  		}
   254  	} else {
   255  		for _, trigger := range o.TriggerWords {
   256  			if strings.HasPrefix(word, trigger) {
   257  				triggerWord = trigger
   258  				break
   259  			}
   260  		}
   261  	}
   262  
   263  	return triggerWord
   264  }