MM-43144: Add teams loader (#19960)
```release-note NONE ``` Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
16b1cbd6e3
Коммит
7f0d1cf0dd
@@ -65,6 +65,7 @@ const (
|
||||
webCtx ctxKey = 0
|
||||
rolesLoaderCtx ctxKey = 1
|
||||
channelsLoaderCtx ctxKey = 2
|
||||
teamsLoaderCtx ctxKey = 3
|
||||
)
|
||||
|
||||
const loaderBatchCapacity = 200
|
||||
@@ -107,6 +108,9 @@ func (api *API) graphQL(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
channelsLoader := dataloader.NewBatchedLoader(graphQLChannelsLoader, dataloader.WithBatchCapacity(loaderBatchCapacity))
|
||||
reqCtx = context.WithValue(reqCtx, channelsLoaderCtx, channelsLoader)
|
||||
|
||||
teamsLoader := dataloader.NewBatchedLoader(graphQLTeamsLoader, dataloader.WithBatchCapacity(loaderBatchCapacity))
|
||||
reqCtx = context.WithValue(reqCtx, teamsLoaderCtx, teamsLoader)
|
||||
|
||||
response = api.schema.Exec(reqCtx,
|
||||
params.Query,
|
||||
params.OperationName,
|
||||
|
||||
@@ -303,3 +303,12 @@ func getChannelsLoader(ctx context.Context) (*dataloader.Loader, error) {
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
// getTeamsLoader returns the teams loader out of the context.
|
||||
func getTeamsLoader(ctx context.Context) (*dataloader.Loader, error) {
|
||||
l, ok := ctx.Value(teamsLoaderCtx).(*dataloader.Loader)
|
||||
if !ok {
|
||||
return nil, errors.New("no dataloader.Loader found in context")
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
@@ -5,8 +5,12 @@ package api4
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/graph-gophers/dataloader/v6"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/web"
|
||||
)
|
||||
|
||||
func getGraphQLTeam(ctx context.Context, id string) (*model.Team, error) {
|
||||
@@ -15,11 +19,18 @@ func getGraphQLTeam(ctx context.Context, id string) (*model.Team, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
team, appErr := c.App.GetTeam(id)
|
||||
if appErr != nil {
|
||||
return nil, appErr
|
||||
loader, err := getTeamsLoader(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
thunk := loader.Load(ctx, dataloader.StringKey(id))
|
||||
result, err := thunk()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
team := result.(*model.Team)
|
||||
|
||||
if (!team.AllowOpenInvite || team.Type != model.TeamOpen) &&
|
||||
!c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), team.Id, model.PermissionViewTeam) {
|
||||
c.SetPermissionError(model.PermissionViewTeam)
|
||||
@@ -29,3 +40,53 @@ func getGraphQLTeam(ctx context.Context, id string) (*model.Team, error) {
|
||||
team = c.App.SanitizeTeam(*c.AppContext.Session(), team)
|
||||
return team, nil
|
||||
}
|
||||
|
||||
func graphQLTeamsLoader(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
|
||||
stringKeys := keys.Keys()
|
||||
result := make([]*dataloader.Result, len(stringKeys))
|
||||
|
||||
c, err := getCtx(ctx)
|
||||
if err != nil {
|
||||
for i := range result {
|
||||
result[i] = &dataloader.Result{Error: err}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
teams, err := getGraphQLTeams(c, stringKeys)
|
||||
if err != nil {
|
||||
for i := range result {
|
||||
result[i] = &dataloader.Result{Error: err}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
for i, ch := range teams {
|
||||
result[i] = &dataloader.Result{Data: ch}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func getGraphQLTeams(c *web.Context, teamIDs []string) ([]*model.Team, error) {
|
||||
teams, appErr := c.App.GetTeams(teamIDs)
|
||||
if appErr != nil {
|
||||
return nil, appErr
|
||||
}
|
||||
|
||||
if len(teams) != len(teamIDs) {
|
||||
return nil, fmt.Errorf("All teams were not found. Requested %d; Found %d", len(teamIDs), len(teams))
|
||||
}
|
||||
|
||||
// The teams need to be in the exact same order as the input slice.
|
||||
tmp := make(map[string]*model.Team)
|
||||
for _, ch := range teams {
|
||||
tmp[ch.Id] = ch
|
||||
}
|
||||
|
||||
// We reuse the same slice and just rewrite the teams.
|
||||
for i, id := range teamIDs {
|
||||
teams[i] = tmp[id]
|
||||
}
|
||||
|
||||
return teams, nil
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user