From 0e140dad2faefb9cb9a7fd8c03106d25d5574230 Mon Sep 17 00:00:00 2001 From: Shobhit Gupta Date: Wed, 7 Oct 2020 02:59:34 -0700 Subject: [PATCH] =?UTF-8?q?Remove=20direct=20Store=20calls=20from=20the=20?= =?UTF-8?q?Api=20layer=20for=20the=20file=20api4/channe=E2=80=A6=20(#15754?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Agniva De Sarker --- api4/channel.go | 4 ++-- app/app_iface.go | 1 + app/channel.go | 9 +++++++++ app/channel_test.go | 21 +++++++++++++++++++++ app/opentracing/opentracing_layer.go | 22 ++++++++++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/api4/channel.go b/api4/channel.go index e4f16e7ff2..0d533d01a5 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -1736,9 +1736,9 @@ func channelMemberCountsByGroup(c *Context, w http.ResponseWriter, r *http.Reque includeTimezones := r.URL.Query().Get("include_timezones") == "true" - channelMemberCounts, err := c.App.Srv().Store.Channel().GetMemberCountsByGroup(c.Params.ChannelId, includeTimezones) + channelMemberCounts, err := c.App.GetMemberCountsByGroup(c.Params.ChannelId, includeTimezones) if err != nil { - c.Err = model.NewAppError("Api4.channelMemberCountsByGroup", "app.channel.get_member_count.app_error", nil, err.Error(), http.StatusInternalServerError) + c.Err = err return } diff --git a/app/app_iface.go b/app/app_iface.go index 37edc24aac..019c2cfb6d 100644 --- a/app/app_iface.go +++ b/app/app_iface.go @@ -582,6 +582,7 @@ type AppIface interface { GetLatestTermsOfService() (*model.TermsOfService, *model.AppError) GetLogs(page, perPage int) ([]string, *model.AppError) GetLogsSkipSend(page, perPage int) ([]string, *model.AppError) + GetMemberCountsByGroup(channelID string, includeTimezones bool) ([]*model.ChannelMemberCountByGroup, *model.AppError) GetMessageForNotification(post *model.Post, translateFunc i18n.TranslateFunc) string GetMultipleEmojiByName(names []string) ([]*model.Emoji, *model.AppError) GetNewUsersForTeamPage(teamId string, page, perPage int, asAdmin bool, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) diff --git a/app/channel.go b/app/channel.go index c074064c10..f4b12e62f1 100644 --- a/app/channel.go +++ b/app/channel.go @@ -2854,3 +2854,12 @@ func (a *App) ClearChannelMembersCache(channelID string) { page++ } } + +func (a *App) GetMemberCountsByGroup(channelID string, includeTimezones bool) ([]*model.ChannelMemberCountByGroup, *model.AppError) { + channelMemberCounts, err := a.Srv().Store.Channel().GetMemberCountsByGroup(channelID, includeTimezones) + if err != nil { + return nil, model.NewAppError("GetMemberCountsByGroup", "app.channel.get_member_count.app_error", nil, err.Error(), http.StatusInternalServerError) + } + + return channelMemberCounts, nil +} diff --git a/app/channel_test.go b/app/channel_test.go index c597e50f3f..965344a5f8 100644 --- a/app/channel_test.go +++ b/app/channel_test.go @@ -1898,3 +1898,24 @@ func TestClearChannelMembersCache(t *testing.T) { th.App.ClearChannelMembersCache("channelID") } + +func TestGetMemberCountsByGroup(t *testing.T) { + th := SetupWithStoreMock(t) + defer th.TearDown() + + mockStore := th.App.Srv().Store.(*mocks.Store) + mockChannelStore := mocks.ChannelStore{} + cmc := []*model.ChannelMemberCountByGroup{} + for i := 0; i < 5; i++ { + cmc = append(cmc, &model.ChannelMemberCountByGroup{ + GroupId: model.NewId(), + ChannelMemberCount: int64(i), + ChannelMemberTimezonesCount: int64(i), + }) + } + mockChannelStore.On("GetMemberCountsByGroup", "channelID", true).Return(cmc, nil) + mockStore.On("Channel").Return(&mockChannelStore) + resp, err := th.App.GetMemberCountsByGroup("channelID", true) + require.Nil(t, err) + require.ElementsMatch(t, cmc, resp) +} diff --git a/app/opentracing/opentracing_layer.go b/app/opentracing/opentracing_layer.go index 71b62e584d..d000fa2365 100644 --- a/app/opentracing/opentracing_layer.go +++ b/app/opentracing/opentracing_layer.go @@ -6157,6 +6157,28 @@ func (a *OpenTracingAppLayer) GetMarketplacePlugins(filter *model.MarketplacePlu return resultVar0, resultVar1 } +func (a *OpenTracingAppLayer) GetMemberCountsByGroup(channelID string, includeTimezones bool) ([]*model.ChannelMemberCountByGroup, *model.AppError) { + origCtx := a.ctx + span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetMemberCountsByGroup") + + a.ctx = newCtx + a.app.Srv().Store.SetContext(newCtx) + defer func() { + a.app.Srv().Store.SetContext(origCtx) + a.ctx = origCtx + }() + + defer span.Finish() + resultVar0, resultVar1 := a.app.GetMemberCountsByGroup(channelID, includeTimezones) + + if resultVar1 != nil { + span.LogFields(spanlog.Error(resultVar1)) + ext.Error.Set(span, true) + } + + return resultVar0, resultVar1 +} + func (a *OpenTracingAppLayer) GetMessageForNotification(post *model.Post, translateFunc i18n.TranslateFunc) string { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetMessageForNotification")