diff --git a/app/webhook.go b/app/webhook.go index d0c102b7d8..bebe7338b2 100644 --- a/app/webhook.go +++ b/app/webhook.go @@ -388,11 +388,7 @@ func (a *App) GetIncomingWebhooksForTeamPage(teamId string, page, perPage int) ( return nil, model.NewAppError("GetIncomingWebhooksForTeamPage", "api.incoming_webhook.disabled.app_error", nil, "", http.StatusNotImplemented) } - if result := <-a.Srv.Store.Webhook().GetIncomingByTeam(teamId, page*perPage, perPage); result.Err != nil { - return nil, result.Err - } else { - return result.Data.([]*model.IncomingWebhook), nil - } + return a.Srv.Store.Webhook().GetIncomingByTeam(teamId, page*perPage, perPage) } func (a *App) GetIncomingWebhooksPage(page, perPage int) ([]*model.IncomingWebhook, *model.AppError) { diff --git a/cmd/mattermost/commands/webhook.go b/cmd/mattermost/commands/webhook.go index 6d165fef8e..92034219f0 100644 --- a/cmd/mattermost/commands/webhook.go +++ b/cmd/mattermost/commands/webhook.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/store" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -101,7 +102,12 @@ func listWebhookCmdF(command *cobra.Command, args []string) error { } // Fetch all hooks with a very large limit so we get them all. - incomingResult := app.Srv.Store.Webhook().GetIncomingByTeam(team.Id, 0, 100000000) + incomingResult := make(chan store.StoreResult, 1) + go func() { + incomingHooks, err := app.Srv.Store.Webhook().GetIncomingByTeam(team.Id, 0, 100000000) + incomingResult <- store.StoreResult{Data: incomingHooks, Err: err} + close(incomingResult) + }() outgoingResult := app.Srv.Store.Webhook().GetOutgoingByTeam(team.Id, 0, 100000000) if result := <-incomingResult; result.Err == nil { diff --git a/store/sqlstore/webhook_store.go b/store/sqlstore/webhook_store.go index d33da92d17..7db4ecb33b 100644 --- a/store/sqlstore/webhook_store.go +++ b/store/sqlstore/webhook_store.go @@ -188,16 +188,14 @@ func (s SqlWebhookStore) GetIncomingList(offset, limit int) store.StoreChannel { }) } -func (s SqlWebhookStore) GetIncomingByTeam(teamId string, offset, limit int) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - var webhooks []*model.IncomingWebhook +func (s SqlWebhookStore) GetIncomingByTeam(teamId string, offset, limit int) ([]*model.IncomingWebhook, *model.AppError) { + var webhooks []*model.IncomingWebhook - 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) - } + 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 { + return nil, model.NewAppError("SqlWebhookStore.GetIncomingByUser", "store.sql_webhooks.get_incoming_by_user.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError) + } - result.Data = webhooks - }) + return webhooks, nil } func (s SqlWebhookStore) GetIncomingByChannel(channelId string) ([]*model.IncomingWebhook, *model.AppError) { diff --git a/store/store.go b/store/store.go index a76450d0f8..0c2e302a60 100644 --- a/store/store.go +++ b/store/store.go @@ -380,7 +380,7 @@ type WebhookStore interface { SaveIncoming(webhook *model.IncomingWebhook) StoreChannel GetIncoming(id string, allowFromCache bool) (*model.IncomingWebhook, *model.AppError) GetIncomingList(offset, limit int) StoreChannel - GetIncomingByTeam(teamId string, offset, limit int) StoreChannel + GetIncomingByTeam(teamId string, offset, limit int) ([]*model.IncomingWebhook, *model.AppError) UpdateIncoming(webhook *model.IncomingWebhook) (*model.IncomingWebhook, *model.AppError) GetIncomingByChannel(channelId string) ([]*model.IncomingWebhook, *model.AppError) DeleteIncoming(webhookId string, time int64) StoreChannel diff --git a/store/storetest/mocks/WebhookStore.go b/store/storetest/mocks/WebhookStore.go index a56a4f550f..9e0821193d 100644 --- a/store/storetest/mocks/WebhookStore.go +++ b/store/storetest/mocks/WebhookStore.go @@ -133,19 +133,28 @@ func (_m *WebhookStore) GetIncomingByChannel(channelId string) ([]*model.Incomin } // GetIncomingByTeam provides a mock function with given fields: teamId, offset, limit -func (_m *WebhookStore) GetIncomingByTeam(teamId string, offset int, limit int) store.StoreChannel { +func (_m *WebhookStore) GetIncomingByTeam(teamId string, offset int, limit int) ([]*model.IncomingWebhook, *model.AppError) { ret := _m.Called(teamId, offset, limit) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, int, int) store.StoreChannel); ok { + var r0 []*model.IncomingWebhook + if rf, ok := ret.Get(0).(func(string, int, int) []*model.IncomingWebhook); ok { r0 = rf(teamId, offset, limit) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).([]*model.IncomingWebhook) } } - return r0 + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string, int, int) *model.AppError); ok { + r1 = rf(teamId, offset, limit) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 } // GetIncomingList provides a mock function with given fields: offset, limit diff --git a/store/storetest/webhook_store.go b/store/storetest/webhook_store.go index bb33627849..f80c6b968e 100644 --- a/store/storetest/webhook_store.go +++ b/store/storetest/webhook_store.go @@ -134,18 +134,18 @@ func testWebhookStoreGetIncomingByTeam(t *testing.T, ss store.Store) { o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) - if r1 := <-ss.Webhook().GetIncomingByTeam(o1.TeamId, 0, 100); r1.Err != nil { - t.Fatal(r1.Err) + if hooks, err := ss.Webhook().GetIncomingByTeam(o1.TeamId, 0, 100); err != nil { + t.Fatal(err) } else { - if r1.Data.([]*model.IncomingWebhook)[0].CreateAt != o1.CreateAt { + if hooks[0].CreateAt != o1.CreateAt { t.Fatal("invalid returned webhook") } } - if result := <-ss.Webhook().GetIncomingByTeam("123", 0, 100); result.Err != nil { - t.Fatal(result.Err) + if hooks, err := ss.Webhook().GetIncomingByTeam("123", 0, 100); err != nil { + t.Fatal(err) } else { - if len(result.Data.([]*model.IncomingWebhook)) != 0 { + if len(hooks) != 0 { t.Fatal("no webhooks should have returned") } }