GraphQL API and utilities for the rpdata project
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.

45 lines
906 B

  1. package queries
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  5. "git.aiterp.net/rpdata/api/internal/auth"
  6. "git.aiterp.net/rpdata/api/model/file"
  7. )
  8. // FilesArgs is args for files query
  9. type FilesArgs struct {
  10. Filter *struct {
  11. Public *bool
  12. MimeTypes *[]string
  13. }
  14. }
  15. // Files resolves the file query
  16. func (r *QueryResolver) Files(ctx context.Context, args *FilesArgs) ([]*types.FileResolver, error) {
  17. filter := args.Filter
  18. token := auth.TokenFromContext(ctx)
  19. author := ""
  20. if token != nil {
  21. author = token.UserID
  22. }
  23. mimeTypes := []string(nil)
  24. if filter.MimeTypes != nil {
  25. mimeTypes = *filter.MimeTypes
  26. }
  27. files, err := file.List(author, filter.Public, mimeTypes)
  28. if err != nil {
  29. return nil, err
  30. }
  31. resolvers := make([]*types.FileResolver, len(files))
  32. for i := range files {
  33. resolvers[i] = &types.FileResolver{F: files[i]}
  34. }
  35. return resolvers, nil
  36. }