From 3e8b81c0c7b33c816aaac1dbab7fa6012521b67a Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Fri, 25 Mar 2022 11:59:57 +0300 Subject: [PATCH] graphql: Add channel stats under channel (#19804) * graphql: Add channel stats under channel * add permission check --- api4/resolver_channel.go | 30 +++++++++++++++++++++++++++ api4/resolver_channel_test.go | 39 +++++++++++++++++++++++++++++++++++ api4/schema.graphqls | 10 ++++++++- model/channel_stats.go | 12 +++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/api4/resolver_channel.go b/api4/resolver_channel.go index 4196dba231..69a2545d05 100644 --- a/api4/resolver_channel.go +++ b/api4/resolver_channel.go @@ -29,6 +29,36 @@ func (ch *channel) Team(ctx context.Context) (*model.Team, error) { return getGraphQLTeam(ctx, ch.TeamId) } +// match with api4.getChannelStats +func (ch *channel) Stats(ctx context.Context) (*model.ChannelStats, error) { + c, err := getCtx(ctx) + if err != nil { + return nil, err + } + + if !c.App.SessionHasPermissionToChannel(*c.AppContext.Session(), ch.Id, model.PermissionReadChannel) { + c.SetPermissionError(model.PermissionReadChannel) + return nil, c.Err + } + + memberCount, appErr := c.App.GetChannelMemberCount(ch.Id) + if appErr != nil { + return nil, appErr + } + + guestCount, appErr := c.App.GetChannelGuestCount(ch.Id) + if appErr != nil { + return nil, appErr + } + + pinnedPostCount, appErr := c.App.GetChannelPinnedPostCount(ch.Id) + if appErr != nil { + return nil, appErr + } + + return &model.ChannelStats{ChannelId: ch.Id, MemberCount: memberCount, GuestCount: guestCount, PinnedPostCount: pinnedPostCount}, nil +} + func (ch *channel) Cursor() *string { cursor := string(channelCursorPrefix) + "-" + ch.Id encoded := base64.StdEncoding.EncodeToString([]byte(cursor)) diff --git a/api4/resolver_channel_test.go b/api4/resolver_channel_test.go index 42e7a95ce7..717002949f 100644 --- a/api4/resolver_channel_test.go +++ b/api4/resolver_channel_test.go @@ -43,6 +43,12 @@ func TestGraphQLChannels(t *testing.T) { ID string `json:"id"` DisplayName string `json:"displayName"` } `json:"team"` + Stats struct { + ChannelId string `json:"channelId"` + MemberCount float64 `json:"memberCount"` + GuestCount float64 `json:"guestCount"` + PinnedPostCount float64 `json:"pinnedpostCount"` + } `json:"stats"` } `json:"channels"` } @@ -366,6 +372,39 @@ func TestGraphQLChannels(t *testing.T) { require.NoError(t, json.Unmarshal(resp.Data, &q)) assert.Len(t, q.Channels, 5) }) + + t.Run("stats", func(t *testing.T) { + query := `query channels($teamId: String, $first: Int) { + channels(userId: "me", teamId: $teamId, first: $first) { + id + stats { + channelId + memberCount + } + } + } + ` + input := graphQLInput{ + OperationName: "channels", + Query: query, + Variables: map[string]interface{}{ + "first": 10, + "teamId": myTeam.Id, + }, + } + + resp, err := th.MakeGraphQLRequest(&input) + require.NoError(t, err) + require.Len(t, resp.Errors, 0) + require.NoError(t, json.Unmarshal(resp.Data, &q)) + require.Len(t, q.Channels, 2) + for _, ch := range q.Channels { + require.Equal(t, ch.ID, ch.Stats.ChannelId) + count, appErr := th.App.GetChannelMemberCount(ch.Stats.ChannelId) + require.Nil(t, appErr) + require.Equal(t, float64(count), ch.Stats.MemberCount) + } + }) } func TestGetPrettyDNForUsers(t *testing.T) { diff --git a/api4/schema.graphqls b/api4/schema.graphqls index e4f2c0be0b..39e91574fd 100644 --- a/api4/schema.graphqls +++ b/api4/schema.graphqls @@ -52,6 +52,7 @@ type Channel { shared: Boolean lastPostAt: Float! totalMsgCount: Float! + stats: ChannelStats cursor: String } @@ -170,4 +171,11 @@ type Session { token: String! createAt: Float! expiresAt: Float! -} \ No newline at end of file +} + +type ChannelStats { + channelId: String! + memberCount: Float! + guestCount: Float! + pinnedPostCount: Float! +} diff --git a/model/channel_stats.go b/model/channel_stats.go index cf44d5416c..05ec3bd77a 100644 --- a/model/channel_stats.go +++ b/model/channel_stats.go @@ -9,3 +9,15 @@ type ChannelStats struct { GuestCount int64 `json:"guest_count"` PinnedPostCount int64 `json:"pinnedpost_count"` } + +func (o *ChannelStats) MemberCount_() float64 { + return float64(o.MemberCount) +} + +func (o *ChannelStats) GuestCount_() float64 { + return float64(o.GuestCount) +} + +func (o *ChannelStats) PinnedPostCount_() float64 { + return float64(o.PinnedPostCount) +}