graphql: Add channel stats under channel (#19804)
* graphql: Add channel stats under channel * add permission check
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
a9e21fcaf1
Коммит
3e8b81c0c7
@@ -29,6 +29,36 @@ func (ch *channel) Team(ctx context.Context) (*model.Team, error) {
|
|||||||
return getGraphQLTeam(ctx, ch.TeamId)
|
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 {
|
func (ch *channel) Cursor() *string {
|
||||||
cursor := string(channelCursorPrefix) + "-" + ch.Id
|
cursor := string(channelCursorPrefix) + "-" + ch.Id
|
||||||
encoded := base64.StdEncoding.EncodeToString([]byte(cursor))
|
encoded := base64.StdEncoding.EncodeToString([]byte(cursor))
|
||||||
|
|||||||
@@ -43,6 +43,12 @@ func TestGraphQLChannels(t *testing.T) {
|
|||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
DisplayName string `json:"displayName"`
|
DisplayName string `json:"displayName"`
|
||||||
} `json:"team"`
|
} `json:"team"`
|
||||||
|
Stats struct {
|
||||||
|
ChannelId string `json:"channelId"`
|
||||||
|
MemberCount float64 `json:"memberCount"`
|
||||||
|
GuestCount float64 `json:"guestCount"`
|
||||||
|
PinnedPostCount float64 `json:"pinnedpostCount"`
|
||||||
|
} `json:"stats"`
|
||||||
} `json:"channels"`
|
} `json:"channels"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,6 +372,39 @@ func TestGraphQLChannels(t *testing.T) {
|
|||||||
require.NoError(t, json.Unmarshal(resp.Data, &q))
|
require.NoError(t, json.Unmarshal(resp.Data, &q))
|
||||||
assert.Len(t, q.Channels, 5)
|
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) {
|
func TestGetPrettyDNForUsers(t *testing.T) {
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ type Channel {
|
|||||||
shared: Boolean
|
shared: Boolean
|
||||||
lastPostAt: Float!
|
lastPostAt: Float!
|
||||||
totalMsgCount: Float!
|
totalMsgCount: Float!
|
||||||
|
stats: ChannelStats
|
||||||
cursor: String
|
cursor: String
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,3 +172,10 @@ type Session {
|
|||||||
createAt: Float!
|
createAt: Float!
|
||||||
expiresAt: Float!
|
expiresAt: Float!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChannelStats {
|
||||||
|
channelId: String!
|
||||||
|
memberCount: Float!
|
||||||
|
guestCount: Float!
|
||||||
|
pinnedPostCount: Float!
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,3 +9,15 @@ type ChannelStats struct {
|
|||||||
GuestCount int64 `json:"guest_count"`
|
GuestCount int64 `json:"guest_count"`
|
||||||
PinnedPostCount int64 `json:"pinnedpost_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)
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user