[MM-15122] Migrate "WebHook.GetIncomingByTeam" to Sync by default (#10631)

This commit is contained in:
Siyuan Liu
2019-04-22 08:29:55 -07:00
committed by Jesse Hallam
parent 1e92646646
commit 434f343f69
6 changed files with 35 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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