From 21a3219b9b1df033635631afa751742bd4c56ea0 Mon Sep 17 00:00:00 2001 From: Saturnino Abril Date: Wed, 19 Jul 2017 03:43:31 +0800 Subject: [PLT-6676] Make OutgoingWebhook to fire when post has no text content but only attachment (#6935) * make OutgoingWebhook to fire when post has no text content but only attachment * update per comment and modify payload & test --- model/outgoing_webhook.go | 32 +++++++++++++++++++++++++++++--- model/outgoing_webhook_test.go | 2 ++ 2 files changed, 31 insertions(+), 3 deletions(-) (limited to 'model') diff --git a/model/outgoing_webhook.go b/model/outgoing_webhook.go index 70c65bec7..59408c24e 100644 --- a/model/outgoing_webhook.go +++ b/model/outgoing_webhook.go @@ -42,6 +42,7 @@ type OutgoingWebhookPayload struct { PostId string `json:"post_id"` Text string `json:"text"` TriggerWord string `json:"trigger_word"` + FileIds string `json:"file_ids"` } func (o *OutgoingWebhookPayload) ToJSON() string { @@ -66,6 +67,7 @@ func (o *OutgoingWebhookPayload) ToFormValues() string { v.Set("post_id", o.PostId) v.Set("text", o.Text) v.Set("trigger_word", o.TriggerWord) + v.Set("file_ids", o.FileIds) return v.Encode() } @@ -198,8 +200,8 @@ func (o *OutgoingWebhook) PreUpdate() { o.UpdateAt = GetMillis() } -func (o *OutgoingWebhook) HasTriggerWord(word string) bool { - if len(o.TriggerWords) == 0 || len(word) == 0 { +func (o *OutgoingWebhook) TriggerWordExactMatch(word string) bool { + if len(word) == 0 { return false } @@ -213,7 +215,7 @@ func (o *OutgoingWebhook) HasTriggerWord(word string) bool { } func (o *OutgoingWebhook) TriggerWordStartsWith(word string) bool { - if len(o.TriggerWords) == 0 || len(word) == 0 { + if len(word) == 0 { return false } @@ -225,3 +227,27 @@ func (o *OutgoingWebhook) TriggerWordStartsWith(word string) bool { return false } + +func (o *OutgoingWebhook) GetTriggerWord(word string, isExactMatch bool) (triggerWord string) { + if len(word) == 0 { + return + } + + if isExactMatch { + for _, trigger := range o.TriggerWords { + if trigger == word { + triggerWord = trigger + break + } + } + } else { + for _, trigger := range o.TriggerWords { + if strings.HasPrefix(word, trigger) { + triggerWord = trigger + break + } + } + } + + return triggerWord +} diff --git a/model/outgoing_webhook_test.go b/model/outgoing_webhook_test.go index 725423cdf..431b1f6c1 100644 --- a/model/outgoing_webhook_test.go +++ b/model/outgoing_webhook_test.go @@ -136,6 +136,7 @@ func TestOutgoingWebhookPayloadToFormValues(t *testing.T) { PostId: "PostId", Text: "Text", TriggerWord: "TriggerWord", + FileIds: "FileIds", } v := url.Values{} v.Set("token", "Token") @@ -149,6 +150,7 @@ func TestOutgoingWebhookPayloadToFormValues(t *testing.T) { v.Set("post_id", "PostId") v.Set("text", "Text") v.Set("trigger_word", "TriggerWord") + v.Set("file_ids", "FileIds") if got, want := p.ToFormValues(), v.Encode(); !reflect.DeepEqual(got, want) { t.Fatalf("Got %+v, wanted %+v", got, want) } -- cgit v1.2.3-1-g7c22