GH-10617 SyncStore: GetIncomingByChannel to sync (#10633)

* GH-10617 GetIncomingByChannel to sync

* GH-10617 using async calls explicitly

* GH-10617 order of assignment change

* Update store.go

* Update store.go

* Update store.go

* GH-10617 fmt fix
This commit is contained in:
Syerikjan Kh
2019-04-22 23:28:08 +08:00
committed by Jesse Hallam
parent 99a8370742
commit 1e92646646
5 changed files with 49 additions and 19 deletions

View File

@@ -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 != "" {

View File

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

View File

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

View File

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

View File

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