diff --git a/app/webhook.go b/app/webhook.go index e169af9d79..bd5a49ff7f 100644 --- a/app/webhook.go +++ b/app/webhook.go @@ -699,8 +699,8 @@ func (a *App) HandleCommandWebhook(hookId string, response *model.CommandRespons ParentId: hook.ParentId, } - if result := <-a.Srv.Store.CommandWebhook().TryUse(hook.Id, 5); result.Err != nil { - return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode) + if err = a.Srv.Store.CommandWebhook().TryUse(hook.Id, 5); err != nil { + return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+err.Message, err.StatusCode) } _, err = a.HandleCommandResponse(cmd, args, response, false) diff --git a/store/sqlstore/command_webhook_store.go b/store/sqlstore/command_webhook_store.go index 2fe28c0007..82d4c86105 100644 --- a/store/sqlstore/command_webhook_store.go +++ b/store/sqlstore/command_webhook_store.go @@ -69,16 +69,14 @@ func (s SqlCommandWebhookStore) Get(id string) (*model.CommandWebhook, *model.Ap return &webhook, nil } -func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - if sqlResult, err := s.GetMaster().Exec("UPDATE CommandWebhooks SET UseCount = UseCount + 1 WHERE Id = :Id AND UseCount < :UseLimit", map[string]interface{}{"Id": id, "UseLimit": limit}); err != nil { - result.Err = model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError) - } else if rows, _ := sqlResult.RowsAffected(); rows == 0 { - result.Err = model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.invalid.app_error", nil, "id="+id, http.StatusBadRequest) - } +func (s SqlCommandWebhookStore) TryUse(id string, limit int) *model.AppError { + if sqlResult, err := s.GetMaster().Exec("UPDATE CommandWebhooks SET UseCount = UseCount + 1 WHERE Id = :Id AND UseCount < :UseLimit", map[string]interface{}{"Id": id, "UseLimit": limit}); err != nil { + return model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError) + } else if rows, _ := sqlResult.RowsAffected(); rows == 0 { + return model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.invalid.app_error", nil, "id="+id, http.StatusBadRequest) + } - result.Data = id - }) + return nil } func (s SqlCommandWebhookStore) Cleanup() { diff --git a/store/store.go b/store/store.go index db8ae3d8a9..6ac9e276fc 100644 --- a/store/store.go +++ b/store/store.go @@ -430,7 +430,7 @@ type CommandStore interface { type CommandWebhookStore interface { Save(webhook *model.CommandWebhook) (*model.CommandWebhook, *model.AppError) Get(id string) (*model.CommandWebhook, *model.AppError) - TryUse(id string, limit int) StoreChannel + TryUse(id string, limit int) *model.AppError Cleanup() } diff --git a/store/storetest/command_webhook_store.go b/store/storetest/command_webhook_store.go index 8748aed597..9fdcb319fe 100644 --- a/store/storetest/command_webhook_store.go +++ b/store/storetest/command_webhook_store.go @@ -63,11 +63,11 @@ func testCommandWebhookStore(t *testing.T, ss store.Store) { t.Fatal("Should have set the status as not found for expired webhook") } - if err := (<-cws.TryUse(h1.Id, 1)).Err; err != nil { + if err := cws.TryUse(h1.Id, 1); err != nil { t.Fatal("Should be able to use webhook once") } - if err := (<-cws.TryUse(h1.Id, 1)).Err; err == nil || err.StatusCode != http.StatusBadRequest { + if err := cws.TryUse(h1.Id, 1); err == nil || err.StatusCode != http.StatusBadRequest { t.Fatal("Should be able to use webhook once") } } diff --git a/store/storetest/mocks/CommandWebhookStore.go b/store/storetest/mocks/CommandWebhookStore.go index 1c35cb4f09..80b9530a63 100644 --- a/store/storetest/mocks/CommandWebhookStore.go +++ b/store/storetest/mocks/CommandWebhookStore.go @@ -4,9 +4,8 @@ package mocks -import mock "github.com/stretchr/testify/mock" -import model "github.com/mattermost/mattermost-server/model" -import store "github.com/mattermost/mattermost-server/store" +import "github.com/stretchr/testify/mock" +import "github.com/mattermost/mattermost-server/model" // CommandWebhookStore is an autogenerated mock type for the CommandWebhookStore type type CommandWebhookStore struct { @@ -69,15 +68,15 @@ func (_m *CommandWebhookStore) Save(webhook *model.CommandWebhook) (*model.Comma } // TryUse provides a mock function with given fields: id, limit -func (_m *CommandWebhookStore) TryUse(id string, limit int) store.StoreChannel { +func (_m *CommandWebhookStore) TryUse(id string, limit int) *model.AppError { ret := _m.Called(id, limit) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, int) store.StoreChannel); ok { + var r0 *model.AppError + if rf, ok := ret.Get(0).(func(string, int) *model.AppError); ok { r0 = rf(id, limit) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(*model.AppError) } }