diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-02-21 19:42:34 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-21 19:42:34 -0500 |
commit | 69cac604e09c139845d2f63ac95fb702fb5a9fe1 (patch) | |
tree | 611920333a5d55f5db0cca43bd3cb5cf1bee30e8 /store | |
parent | b61115df55ea6eba9976e561a8b39bf5a297fcc9 (diff) | |
download | chat-69cac604e09c139845d2f63ac95fb702fb5a9fe1.tar.gz chat-69cac604e09c139845d2f63ac95fb702fb5a9fe1.tar.bz2 chat-69cac604e09c139845d2f63ac95fb702fb5a9fe1.zip |
Implement create and get incoming webhook endpoints for APIv4 (#5407)
* Implement POST /hooks/incoming endpoint for APIv4
* Implement GET /hooks/incoming endpoint for APIv4
* Updates per feedback
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_channel_store.go | 2 | ||||
-rw-r--r-- | store/sql_webhook_store.go | 29 | ||||
-rw-r--r-- | store/sql_webhook_store_test.go | 38 | ||||
-rw-r--r-- | store/store.go | 3 |
4 files changed, 65 insertions, 7 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 503b646f6..03bc70c75 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -357,7 +357,7 @@ func (s SqlChannelStore) get(id string, master bool, allowFromCache bool) StoreC if obj, err := db.Get(model.Channel{}, id); err != nil { result.Err = model.NewLocAppError("SqlChannelStore.Get", "store.sql_channel.get.find.app_error", nil, "id="+id+", "+err.Error()) } else if obj == nil { - result.Err = model.NewLocAppError("SqlChannelStore.Get", "store.sql_channel.get.existing.app_error", nil, "id="+id) + result.Err = model.NewAppError("SqlChannelStore.Get", "store.sql_channel.get.existing.app_error", nil, "id="+id, http.StatusBadRequest) } else { result.Data = obj.(*model.Channel) channelCache.AddWithExpiresInSecs(id, obj.(*model.Channel), CHANNEL_CACHE_SEC) diff --git a/store/sql_webhook_store.go b/store/sql_webhook_store.go index 4022aff7f..0e61130ad 100644 --- a/store/sql_webhook_store.go +++ b/store/sql_webhook_store.go @@ -4,6 +4,8 @@ package store import ( + "net/http" + "github.com/mattermost/platform/einterfaces" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" @@ -183,7 +185,28 @@ func (s SqlWebhookStore) PermanentDeleteIncomingByUser(userId string) StoreChann return storeChannel } -func (s SqlWebhookStore) GetIncomingByTeam(teamId string) StoreChannel { +func (s SqlWebhookStore) GetIncomingList(offset, limit int) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + var webhooks []*model.IncomingWebhook + + if _, err := s.GetReplica().Select(&webhooks, "SELECT * FROM IncomingWebhooks WHERE DeleteAt = 0 LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Limit": limit, "Offset": offset}); err != nil { + result.Err = model.NewAppError("SqlWebhookStore.GetIncomingList", "store.sql_webhooks.get_incoming_by_user.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) + } + + result.Data = webhooks + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (s SqlWebhookStore) GetIncomingByTeam(teamId string, offset, limit int) StoreChannel { storeChannel := make(StoreChannel, 1) go func() { @@ -191,8 +214,8 @@ func (s SqlWebhookStore) GetIncomingByTeam(teamId string) StoreChannel { var webhooks []*model.IncomingWebhook - if _, err := s.GetReplica().Select(&webhooks, "SELECT * FROM IncomingWebhooks WHERE TeamId = :TeamId AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId}); err != nil { - result.Err = model.NewLocAppError("SqlWebhookStore.GetIncomingByUser", "store.sql_webhooks.get_incoming_by_user.app_error", nil, "teamId="+teamId+", err="+err.Error()) + if _, err := s.GetReplica().Select(&webhooks, "SELECT * FROM IncomingWebhooks WHERE TeamId = :TeamId AND DeleteAt = 0 LIMIT :Limit OFFSET :Offset", map[string]interface{}{"TeamId": teamId, "Limit": limit, "Offset": offset}); err != nil { + result.Err = model.NewAppError("SqlWebhookStore.GetIncomingByUser", "store.sql_webhooks.get_incoming_by_user.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError) } result.Data = webhooks diff --git a/store/sql_webhook_store_test.go b/store/sql_webhook_store_test.go index 401b0f904..3d79d9ad3 100644 --- a/store/sql_webhook_store_test.go +++ b/store/sql_webhook_store_test.go @@ -60,6 +60,40 @@ func TestWebhookStoreGetIncoming(t *testing.T) { } } +func TestWebhookStoreGetIncomingList(t *testing.T) { + Setup() + + o1 := &model.IncomingWebhook{} + o1.ChannelId = model.NewId() + o1.UserId = model.NewId() + o1.TeamId = model.NewId() + + o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) + + if r1 := <-store.Webhook().GetIncomingList(0, 1000); r1.Err != nil { + t.Fatal(r1.Err) + } else { + found := false + hooks := r1.Data.([]*model.IncomingWebhook) + for _, hook := range hooks { + if hook.Id == o1.Id { + found = true + } + } + if !found { + t.Fatal("missing webhook") + } + } + + if result := <-store.Webhook().GetIncomingList(0, 1); result.Err != nil { + t.Fatal(result.Err) + } else { + if len(result.Data.([]*model.IncomingWebhook)) != 1 { + t.Fatal("only 1 should be returned") + } + } +} + func TestWebhookStoreGetIncomingByTeam(t *testing.T) { Setup() @@ -70,7 +104,7 @@ func TestWebhookStoreGetIncomingByTeam(t *testing.T) { o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) - if r1 := <-store.Webhook().GetIncomingByTeam(o1.TeamId); r1.Err != nil { + if r1 := <-store.Webhook().GetIncomingByTeam(o1.TeamId, 0, 100); r1.Err != nil { t.Fatal(r1.Err) } else { if r1.Data.([]*model.IncomingWebhook)[0].CreateAt != o1.CreateAt { @@ -78,7 +112,7 @@ func TestWebhookStoreGetIncomingByTeam(t *testing.T) { } } - if result := <-store.Webhook().GetIncomingByTeam("123"); result.Err != nil { + if result := <-store.Webhook().GetIncomingByTeam("123", 0, 100); result.Err != nil { t.Fatal(result.Err) } else { if len(result.Data.([]*model.IncomingWebhook)) != 0 { diff --git a/store/store.go b/store/store.go index 6c78a235d..d0c403460 100644 --- a/store/store.go +++ b/store/store.go @@ -252,7 +252,8 @@ type SystemStore interface { type WebhookStore interface { SaveIncoming(webhook *model.IncomingWebhook) StoreChannel GetIncoming(id string, allowFromCache bool) StoreChannel - GetIncomingByTeam(teamId string) StoreChannel + GetIncomingList(offset, limit int) StoreChannel + GetIncomingByTeam(teamId string, offset, limit int) StoreChannel GetIncomingByChannel(channelId string) StoreChannel DeleteIncoming(webhookId string, time int64) StoreChannel PermanentDeleteIncomingByUser(userId string) StoreChannel |