stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

277 lines
6.3 KiB

package resolvers
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
"context"
"errors"
"git.aiterp.net/stufflog/server/graph/loaders"
"git.aiterp.net/stufflog/server/graph/graphcore"
"git.aiterp.net/stufflog/server/internal/slerrors"
"git.aiterp.net/stufflog/server/models"
)
func (r *queryResolver) Issue(ctx context.Context, id string) (*models.Issue, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
issue, err := loaders.IssueLoaderFromContext(ctx).Load(id)
if err != nil {
return nil, err
}
_, err = r.Auth.IssuePermission(ctx, *issue)
if err != nil {
return nil, err
}
return issue, nil
}
func (r *queryResolver) Issues(ctx context.Context, filter *models.IssueFilter) ([]*models.Issue, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
if filter == nil {
filter = &models.IssueFilter{}
}
issues, err := r.Database.Issues().List(ctx, *filter)
if err != nil {
return nil, err
}
loader := loaders.IssueLoaderFromContext(ctx)
for _, issue := range issues {
loader.Prime(issue.ID, issue)
}
deleteList := make([]int, 0, len(issues))
for i, issue := range issues {
_, err := r.Auth.IssuePermission(ctx, *issue)
if err != nil {
deleteList = append(deleteList, i-len(deleteList))
}
}
for _, index := range deleteList {
issues = append(issues[:index], issues[index+1:]...)
}
return issues, nil
}
func (r *queryResolver) Item(ctx context.Context, id string) (*models.Item, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
return r.Database.Items().Find(ctx, id)
}
func (r *queryResolver) Items(ctx context.Context, filter *models.ItemFilter) ([]*models.Item, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
if filter == nil {
filter = &models.ItemFilter{}
}
return r.Database.Items().List(ctx, *filter)
}
func (r *queryResolver) ItemTags(ctx context.Context) ([]string, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
return r.Database.Items().GetTags(ctx)
}
func (r *queryResolver) IssueItem(ctx context.Context, id string) (*models.IssueItem, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
item, err := r.Database.IssueItems().Find(ctx, id)
if err != nil {
return nil, err
}
issue, err := loaders.IssueLoaderFromContext(ctx).Load(id)
if err != nil {
return nil, err
}
_, err = r.Auth.IssuePermission(ctx, *issue)
if err != nil {
return nil, err
}
return item, nil
}
func (r *queryResolver) IssueItems(ctx context.Context, filter *models.IssueItemFilter) ([]*models.IssueItem, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
if filter == nil {
filter = &models.IssueItemFilter{}
}
items, err := r.Database.IssueItems().List(ctx, *filter)
if err != nil {
return nil, err
}
accessMap := make(map[string]bool)
deleteList := make([]int, 0, len(items))
for i, item := range items {
if access, ok := accessMap[item.IssueID]; ok && access {
continue
} else if ok && !access {
deleteList = append(deleteList, i-len(deleteList))
continue
}
issue, err := loaders.IssueLoaderFromContext(ctx).Load(item.IssueID)
if err != nil {
deleteList = append(deleteList, i-len(deleteList))
accessMap[item.IssueID] = true
continue
}
_, err = r.Auth.IssuePermission(ctx, *issue)
if err != nil {
deleteList = append(deleteList, i-len(deleteList))
}
accessMap[issue.ID] = err != nil
}
for _, index := range deleteList {
items = append(items[:index], items[index+1:]...)
}
return items, nil
}
func (r *queryResolver) Project(ctx context.Context, id string) (*models.Project, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
project, err := r.Database.Projects().Find(ctx, id)
if err != nil {
return nil, err
}
_, err = r.Auth.ProjectPermission(ctx, project.ID)
if err != nil {
return nil, err
}
return project, nil
}
func (r *queryResolver) Projects(ctx context.Context, filter *models.ProjectFilter) ([]*models.Project, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
skipCheck := false
if filter == nil {
filter = &models.ProjectFilter{}
}
if filter.Permission == nil {
skipCheck = true
filter.Permission = &models.ProjectFilterPermission{
UserID: user.ID,
MinLevel: models.ProjectPermissionLevelObserver,
}
}
projects, err := r.Database.Projects().List(ctx, *filter)
if err != nil {
return nil, err
}
if !skipCheck && len(projects) > 0 {
deleteList := make([]int, 0, 4)
for i, project := range projects {
if _, err := r.Auth.ProjectPermission(ctx, project.ID); err != nil {
deleteList = append(deleteList, i-len(deleteList))
}
}
for _, di := range deleteList {
projects = append(projects[:di], projects[di:]...)
}
}
return projects, nil
}
func (r *queryResolver) Log(ctx context.Context, id string) (*models.Log, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
log, err := r.Database.Logs().Find(ctx, id)
if err != nil {
return nil, err
}
r.Auth.FilterLog(ctx, log)
if log.Empty() {
return nil, slerrors.NotFound("Log")
}
return log, nil
}
func (r *queryResolver) Logs(ctx context.Context, filter *models.LogFilter) ([]*models.Log, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, slerrors.PermissionDenied
}
if filter == nil {
filter = &models.LogFilter{
UserIDs: []string{user.ID},
}
}
logs, err := r.Database.Logs().List(ctx, *filter)
if err != nil {
return nil, err
}
r.Auth.FilterLogList(ctx, &logs)
return logs, nil
}
func (r *queryResolver) Session(ctx context.Context) (*models.User, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, errors.New("not logged in")
}
return user, nil
}
// Query returns graphcore.QueryResolver implementation.
func (r *Resolver) Query() graphcore.QueryResolver { return &queryResolver{r} }
type queryResolver struct{ *Resolver }