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

Этот коммит содержится в:
Rodrigo Villablanca Vásquez
2019-07-09 16:57:05 -04:00
коммит произвёл Jesús Espino
родитель ff89b2c8e1
Коммит 678c8f4f84
5 изменённых файлов: 18 добавлений и 21 удалений

Просмотреть файл

@@ -699,8 +699,8 @@ func (a *App) HandleCommandWebhook(hookId string, response *model.CommandRespons
ParentId: hook.ParentId, ParentId: hook.ParentId,
} }
if result := <-a.Srv.Store.CommandWebhook().TryUse(hook.Id, 5); result.Err != nil { if err = a.Srv.Store.CommandWebhook().TryUse(hook.Id, 5); err != nil {
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode) return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+err.Message, err.StatusCode)
} }
_, err = a.HandleCommandResponse(cmd, args, response, false) _, err = a.HandleCommandResponse(cmd, args, response, false)

Просмотреть файл

@@ -69,16 +69,14 @@ func (s SqlCommandWebhookStore) Get(id string) (*model.CommandWebhook, *model.Ap
return &webhook, nil return &webhook, nil
} }
func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel { func (s SqlCommandWebhookStore) TryUse(id string, limit int) *model.AppError {
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 { 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) 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 { } 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) 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() { func (s SqlCommandWebhookStore) Cleanup() {

Просмотреть файл

@@ -430,7 +430,7 @@ type CommandStore interface {
type CommandWebhookStore interface { type CommandWebhookStore interface {
Save(webhook *model.CommandWebhook) (*model.CommandWebhook, *model.AppError) Save(webhook *model.CommandWebhook) (*model.CommandWebhook, *model.AppError)
Get(id string) (*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() Cleanup()
} }

Просмотреть файл

@@ -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") 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") 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") t.Fatal("Should be able to use webhook once")
} }
} }

Просмотреть файл

@@ -4,9 +4,8 @@
package mocks package mocks
import mock "github.com/stretchr/testify/mock" import "github.com/stretchr/testify/mock"
import model "github.com/mattermost/mattermost-server/model" import "github.com/mattermost/mattermost-server/model"
import store "github.com/mattermost/mattermost-server/store"
// CommandWebhookStore is an autogenerated mock type for the CommandWebhookStore type // CommandWebhookStore is an autogenerated mock type for the CommandWebhookStore type
type CommandWebhookStore struct { 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 // 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) ret := _m.Called(id, limit)
var r0 store.StoreChannel var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string, int) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, int) *model.AppError); ok {
r0 = rf(id, limit) r0 = rf(id, limit)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AppError)
} }
} }