From 564dffec35d2ab6772c2561f28d9ad9a2bcbeb40 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Tue, 26 Jul 2016 14:04:28 +0200 Subject: Add option to trigger outgoing webhook if first word starts with trigger word (#3611) --- model/outgoing_webhook.go | 20 ++++++++++++++++++++ model/outgoing_webhook_test.go | 11 +++++++++++ 2 files changed, 31 insertions(+) (limited to 'model') diff --git a/model/outgoing_webhook.go b/model/outgoing_webhook.go index ee7a32f1f..ec2ed75c7 100644 --- a/model/outgoing_webhook.go +++ b/model/outgoing_webhook.go @@ -9,6 +9,7 @@ import ( "io" "net/url" "strconv" + "strings" ) type OutgoingWebhook struct { @@ -21,6 +22,7 @@ type OutgoingWebhook struct { ChannelId string `json:"channel_id"` TeamId string `json:"team_id"` TriggerWords StringArray `json:"trigger_words"` + TriggerWhen int `json:"trigger_when"` CallbackURLs StringArray `json:"callback_urls"` DisplayName string `json:"display_name"` Description string `json:"description"` @@ -171,6 +173,10 @@ func (o *OutgoingWebhook) IsValid() *AppError { return NewLocAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.content_type.app_error", nil, "") } + if o.TriggerWhen > 1 { + return NewLocAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.content_type.app_error", nil, "") + } + return nil } @@ -204,3 +210,17 @@ func (o *OutgoingWebhook) HasTriggerWord(word string) bool { return false } + +func (o *OutgoingWebhook) TriggerWordStartsWith(word string) bool { + if len(o.TriggerWords) == 0 || len(word) == 0 { + return false + } + + for _, trigger := range o.TriggerWords { + if strings.HasPrefix(word, trigger) { + return true + } + } + + return false +} diff --git a/model/outgoing_webhook_test.go b/model/outgoing_webhook_test.go index 24b81d221..2458c62e4 100644 --- a/model/outgoing_webhook_test.go +++ b/model/outgoing_webhook_test.go @@ -163,3 +163,14 @@ func TestOutgoingWebhookPreUpdate(t *testing.T) { o := OutgoingWebhook{} o.PreUpdate() } + +func TestOutgoingWebhookTriggerWordStartsWith(t *testing.T) { + o := OutgoingWebhook{Id: NewId()} + o.TriggerWords = append(o.TriggerWords, "foo") + if !o.TriggerWordStartsWith("foobar") { + t.Fatal("Should return true") + } + if o.TriggerWordStartsWith("barfoo") { + t.Fatal("Should return false") + } +} -- cgit v1.2.3-1-g7c22