mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Migrate CommandWebhook.TryUse to sync by default (#11593)
This commit is contained in:
committed by
Jesús Espino
parent
ff89b2c8e1
commit
678c8f4f84
@@ -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() {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user