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.

49 lines
962 B

  1. package resolvers
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/internal/auth"
  5. "git.aiterp.net/rpdata/api/models"
  6. "git.aiterp.net/rpdata/api/models/files"
  7. )
  8. func (r *queryResolver) File(ctx context.Context, id string) (*models.File, error) {
  9. file, err := files.FindID(id)
  10. if err != nil {
  11. return nil, err
  12. }
  13. return &file, nil
  14. }
  15. func (r *queryResolver) Files(ctx context.Context, filter *files.Filter) ([]*models.File, error) {
  16. token := auth.TokenFromContext(ctx)
  17. if filter == nil {
  18. filter = &files.Filter{}
  19. }
  20. // Only allow users to view public files that are not their own.
  21. if token != nil {
  22. if filter.Public == nil || *filter.Public == false {
  23. filter.Author = &token.UserID
  24. }
  25. } else {
  26. filter.Public = &trueValue
  27. }
  28. files, err := files.List(filter)
  29. if err != nil {
  30. return nil, err
  31. }
  32. files2 := make([]*models.File, len(files))
  33. for i := range files {
  34. files2[i] = &files[i]
  35. }
  36. return files2, nil
  37. }
  38. var trueValue = true