From 4f7f7070c068bf7f6667ef985c1067e116bdaf1d Mon Sep 17 00:00:00 2001 From: Puneeth Reddy <45575072+therealpuneeth20@users.noreply.github.com> Date: Tue, 7 May 2019 04:50:03 -0700 Subject: [PATCH] MM 15185 - Migrate "WebHook.GetOutgoingByChannel" to Sync by default (#10704) --- app/channel.go | 9 ++++++++- app/webhook.go | 6 +----- store/sqlstore/webhook_store.go | 26 ++++++++++++-------------- store/store.go | 2 +- store/storetest/mocks/WebhookStore.go | 20 ++++++++++++++------ store/storetest/webhook_store.go | 12 ++++++------ 6 files changed, 42 insertions(+), 33 deletions(-) diff --git a/app/channel.go b/app/channel.go index 8b6d0cd020..4f74352b2b 100644 --- a/app/channel.go +++ b/app/channel.go @@ -793,13 +793,20 @@ func (a *App) UpdateChannelMemberNotifyProps(data map[string]string, channelId s func (a *App) DeleteChannel(channel *model.Channel, userId string) *model.AppError { ihc := make(chan store.StoreResult, 1) - ohc := a.Srv.Store.Webhook().GetOutgoingByChannel(channel.Id, -1, -1) + ohc := make(chan store.StoreResult, 1) + go func() { webhooks, err := a.Srv.Store.Webhook().GetIncomingByChannel(channel.Id) ihc <- store.StoreResult{Data: webhooks, Err: err} close(ihc) }() + go func() { + outgoingHooks, err := a.Srv.Store.Webhook().GetOutgoingByChannel(channel.Id, -1, -1) + ohc <- store.StoreResult{Data: outgoingHooks, Err: err} + close(ohc) + }() + var user *model.User if userId != "" { var err *model.AppError diff --git a/app/webhook.go b/app/webhook.go index f9a276e830..b4dc945b51 100644 --- a/app/webhook.go +++ b/app/webhook.go @@ -503,11 +503,7 @@ func (a *App) GetOutgoingWebhooksForChannelPage(channelId string, page, perPage return nil, model.NewAppError("GetOutgoingWebhooksForChannelPage", "api.outgoing_webhook.disabled.app_error", nil, "", http.StatusNotImplemented) } - if result := <-a.Srv.Store.Webhook().GetOutgoingByChannel(channelId, page*perPage, perPage); result.Err != nil { - return nil, result.Err - } else { - return result.Data.([]*model.OutgoingWebhook), nil - } + return a.Srv.Store.Webhook().GetOutgoingByChannel(channelId, page*perPage, perPage) } func (a *App) GetOutgoingWebhooksForTeamPage(teamId string, page, perPage int) ([]*model.OutgoingWebhook, *model.AppError) { diff --git a/store/sqlstore/webhook_store.go b/store/sqlstore/webhook_store.go index fd12cd037e..200dfb7384 100644 --- a/store/sqlstore/webhook_store.go +++ b/store/sqlstore/webhook_store.go @@ -242,23 +242,21 @@ func (s SqlWebhookStore) GetOutgoingList(offset, limit int) ([]*model.OutgoingWe return webhooks, nil } -func (s SqlWebhookStore) GetOutgoingByChannel(channelId string, offset, limit int) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - var webhooks []*model.OutgoingWebhook +func (s SqlWebhookStore) GetOutgoingByChannel(channelId string, offset, limit int) ([]*model.OutgoingWebhook, *model.AppError) { + var webhooks []*model.OutgoingWebhook - query := "" - if limit < 0 || offset < 0 { - query = "SELECT * FROM OutgoingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0" - } else { - query = "SELECT * FROM OutgoingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0 LIMIT :Limit OFFSET :Offset" - } + query := "" + if limit < 0 || offset < 0 { + query = "SELECT * FROM OutgoingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0" + } else { + query = "SELECT * FROM OutgoingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0 LIMIT :Limit OFFSET :Offset" + } - if _, err := s.GetReplica().Select(&webhooks, query, map[string]interface{}{"ChannelId": channelId, "Offset": offset, "Limit": limit}); err != nil { - result.Err = model.NewAppError("SqlWebhookStore.GetOutgoingByChannel", "store.sql_webhooks.get_outgoing_by_channel.app_error", nil, "channelId="+channelId+", err="+err.Error(), http.StatusInternalServerError) - } + if _, err := s.GetReplica().Select(&webhooks, query, map[string]interface{}{"ChannelId": channelId, "Offset": offset, "Limit": limit}); err != nil { + return nil, model.NewAppError("SqlWebhookStore.GetOutgoingByChannel", "store.sql_webhooks.get_outgoing_by_channel.app_error", nil, "channelId="+channelId+", err="+err.Error(), http.StatusInternalServerError) + } - result.Data = webhooks - }) + return webhooks, nil } func (s SqlWebhookStore) GetOutgoingByTeam(teamId string, offset, limit int) ([]*model.OutgoingWebhook, *model.AppError) { diff --git a/store/store.go b/store/store.go index 485f1bb94e..0ddf26b420 100644 --- a/store/store.go +++ b/store/store.go @@ -395,8 +395,8 @@ type WebhookStore interface { SaveOutgoing(webhook *model.OutgoingWebhook) (*model.OutgoingWebhook, *model.AppError) GetOutgoing(id string) (*model.OutgoingWebhook, *model.AppError) + GetOutgoingByChannel(channelId string, offset, limit int) ([]*model.OutgoingWebhook, *model.AppError) GetOutgoingList(offset, limit int) ([]*model.OutgoingWebhook, *model.AppError) - GetOutgoingByChannel(channelId string, offset, limit int) StoreChannel GetOutgoingByTeam(teamId string, offset, limit int) ([]*model.OutgoingWebhook, *model.AppError) DeleteOutgoing(webhookId string, time int64) *model.AppError PermanentDeleteOutgoingByChannel(channelId string) *model.AppError diff --git a/store/storetest/mocks/WebhookStore.go b/store/storetest/mocks/WebhookStore.go index 4b0569d86a..039b6f19dd 100644 --- a/store/storetest/mocks/WebhookStore.go +++ b/store/storetest/mocks/WebhookStore.go @@ -6,7 +6,6 @@ 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" // WebhookStore is an autogenerated mock type for the WebhookStore type type WebhookStore struct { @@ -222,19 +221,28 @@ func (_m *WebhookStore) GetOutgoing(id string) (*model.OutgoingWebhook, *model.A } // GetOutgoingByChannel provides a mock function with given fields: channelId, offset, limit -func (_m *WebhookStore) GetOutgoingByChannel(channelId string, offset int, limit int) store.StoreChannel { +func (_m *WebhookStore) GetOutgoingByChannel(channelId string, offset int, limit int) ([]*model.OutgoingWebhook, *model.AppError) { ret := _m.Called(channelId, offset, limit) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, int, int) store.StoreChannel); ok { + var r0 []*model.OutgoingWebhook + if rf, ok := ret.Get(0).(func(string, int, int) []*model.OutgoingWebhook); ok { r0 = rf(channelId, offset, limit) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).([]*model.OutgoingWebhook) } } - return r0 + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(string, int, int) *model.AppError); ok { + r1 = rf(channelId, offset, limit) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 } // GetOutgoingByTeam provides a mock function with given fields: teamId, offset, limit diff --git a/store/storetest/webhook_store.go b/store/storetest/webhook_store.go index 577e5f3310..8dff9d1860 100644 --- a/store/storetest/webhook_store.go +++ b/store/storetest/webhook_store.go @@ -371,18 +371,18 @@ func testWebhookStoreGetOutgoingByChannel(t *testing.T, ss store.Store) { o1, _ = ss.Webhook().SaveOutgoing(o1) - if r1 := <-ss.Webhook().GetOutgoingByChannel(o1.ChannelId, 0, 100); r1.Err != nil { - t.Fatal(r1.Err) + if r1, err := ss.Webhook().GetOutgoingByChannel(o1.ChannelId, 0, 100); err != nil { + t.Fatal(err) } else { - if r1.Data.([]*model.OutgoingWebhook)[0].CreateAt != o1.CreateAt { + if r1[0].CreateAt != o1.CreateAt { t.Fatal("invalid returned webhook") } } - if result := <-ss.Webhook().GetOutgoingByChannel("123", -1, -1); result.Err != nil { - t.Fatal(result.Err) + if result, err := ss.Webhook().GetOutgoingByChannel("123", -1, -1); err != nil { + t.Fatal(err) } else { - if len(result.Data.([]*model.OutgoingWebhook)) != 0 { + if len(result) != 0 { t.Fatal("no webhooks should have returned") } }