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