Migrate CommandWebhook.TryUse to sync by default (#11593)

This commit is contained in:
Rodrigo Villablanca Vásquez
2019-07-09 16:57:05 -04:00
committed by Jesús Espino
parent ff89b2c8e1
commit 678c8f4f84
5 changed files with 18 additions and 21 deletions

View File

@@ -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)

View File

@@ -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() {

View File

@@ -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()
}

View File

@@ -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")
}
}

View File

@@ -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)
}
}