diff --git a/app/channel.go b/app/channel.go index 3e64957668..6bf9083942 100644 --- a/app/channel.go +++ b/app/channel.go @@ -766,8 +766,13 @@ func (a *App) UpdateChannelMemberNotifyProps(data map[string]string, channelId s } func (a *App) DeleteChannel(channel *model.Channel, userId string) *model.AppError { - ihc := a.Srv.Store.Webhook().GetIncomingByChannel(channel.Id) + ihc := make(chan store.StoreResult, 1) ohc := a.Srv.Store.Webhook().GetOutgoingByChannel(channel.Id, -1, -1) + go func() { + webhooks, err := a.Srv.Store.Webhook().GetIncomingByChannel(channel.Id) + ihc <- store.StoreResult{Data: webhooks, Err: err} + close(ihc) + }() var user *model.User if userId != "" { diff --git a/store/sqlstore/webhook_store.go b/store/sqlstore/webhook_store.go index 7fad2a7b53..d33da92d17 100644 --- a/store/sqlstore/webhook_store.go +++ b/store/sqlstore/webhook_store.go @@ -4,9 +4,8 @@ package sqlstore import ( - "net/http" - "database/sql" + "net/http" "github.com/mattermost/mattermost-server/einterfaces" "github.com/mattermost/mattermost-server/model" @@ -201,16 +200,14 @@ func (s SqlWebhookStore) GetIncomingByTeam(teamId string, offset, limit int) sto }) } -func (s SqlWebhookStore) GetIncomingByChannel(channelId string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - var webhooks []*model.IncomingWebhook +func (s SqlWebhookStore) GetIncomingByChannel(channelId string) ([]*model.IncomingWebhook, *model.AppError) { + var webhooks []*model.IncomingWebhook - if _, err := s.GetReplica().Select(&webhooks, "SELECT * FROM IncomingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0", map[string]interface{}{"ChannelId": channelId}); err != nil { - result.Err = model.NewAppError("SqlWebhookStore.GetIncomingByChannel", "store.sql_webhooks.get_incoming_by_channel.app_error", nil, "channelId="+channelId+", err="+err.Error(), http.StatusInternalServerError) - } + if _, err := s.GetReplica().Select(&webhooks, "SELECT * FROM IncomingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0", map[string]interface{}{"ChannelId": channelId}); err != nil { + return nil, model.NewAppError("SqlWebhookStore.GetIncomingByChannel", "store.sql_webhooks.get_incoming_by_channel.app_error", nil, "channelId="+channelId+", err="+err.Error(), http.StatusInternalServerError) + } - result.Data = webhooks - }) + return webhooks, nil } func (s SqlWebhookStore) SaveOutgoing(webhook *model.OutgoingWebhook) store.StoreChannel { diff --git a/store/store.go b/store/store.go index 13b2499c16..a76450d0f8 100644 --- a/store/store.go +++ b/store/store.go @@ -382,7 +382,7 @@ type WebhookStore interface { GetIncomingList(offset, limit int) StoreChannel GetIncomingByTeam(teamId string, offset, limit int) StoreChannel UpdateIncoming(webhook *model.IncomingWebhook) (*model.IncomingWebhook, *model.AppError) - GetIncomingByChannel(channelId string) StoreChannel + GetIncomingByChannel(channelId string) ([]*model.IncomingWebhook, *model.AppError) DeleteIncoming(webhookId string, time int64) StoreChannel PermanentDeleteIncomingByChannel(channelId string) StoreChannel PermanentDeleteIncomingByUser(userId string) StoreChannel diff --git a/store/storetest/mocks/WebhookStore.go b/store/storetest/mocks/WebhookStore.go index 72e8d44fd9..a56a4f550f 100644 --- a/store/storetest/mocks/WebhookStore.go +++ b/store/storetest/mocks/WebhookStore.go @@ -108,19 +108,28 @@ func (_m *WebhookStore) GetIncoming(id string, allowFromCache bool) (*model.Inco } // GetIncomingByChannel provides a mock function with given fields: channelId -func (_m *WebhookStore) GetIncomingByChannel(channelId string) store.StoreChannel { +func (_m *WebhookStore) GetIncomingByChannel(channelId string) ([]*model.IncomingWebhook, *model.AppError) { ret := _m.Called(channelId) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 []*model.IncomingWebhook + if rf, ok := ret.Get(0).(func(string) []*model.IncomingWebhook); ok { r0 = rf(channelId) } 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) *model.AppError); ok { + r1 = rf(channelId) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 } // GetIncomingByTeam 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 ec49f52416..bb33627849 100644 --- a/store/storetest/webhook_store.go +++ b/store/storetest/webhook_store.go @@ -4,11 +4,10 @@ package storetest import ( + "net/http" "testing" "time" - "net/http" - "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/store" "github.com/stretchr/testify/require" @@ -152,6 +151,26 @@ func testWebhookStoreGetIncomingByTeam(t *testing.T, ss store.Store) { } } +func testWebhookStoreGetIncomingByChannel(t *testing.T, ss store.Store) { + o1 := buildIncomingWebhook() + + o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) + + webhooks, err := ss.Webhook().GetIncomingByChannel(o1.ChannelId) + require.Nil(t, err) + if webhooks[0].CreateAt != o1.CreateAt { + t.Fatal("invalid returned webhook") + } + + if webhooks, err = ss.Webhook().GetIncomingByChannel("123"); err != nil { + t.Fatal(err) + } else { + if len(webhooks) != 0 { + t.Fatal("no webhooks should have returned") + } + } +} + func testWebhookStoreDeleteIncoming(t *testing.T, ss store.Store) { o1 := buildIncomingWebhook()