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.

43 lines
904 B

  1. package mutations
  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. // FileEditArgs is args for the editFile mutation
  9. type FileEditArgs struct {
  10. Input *struct {
  11. ID string
  12. Name *string
  13. Public *bool
  14. }
  15. }
  16. // EditFile resolves the editFile mutation
  17. func (r *MutationResolver) EditFile(ctx context.Context, args *FileEditArgs) (*types.FileResolver, error) {
  18. input := args.Input
  19. token := auth.TokenFromContext(ctx)
  20. if !token.Permitted("member") {
  21. return nil, ErrUnauthorized
  22. }
  23. file, err := file.FindID(input.ID)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if file.Author != token.UserID && !token.Permitted("file.edit") {
  28. return nil, ErrUnauthorized
  29. }
  30. err = file.Edit(input.Name, input.Public)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return &types.FileResolver{F: file}, nil
  35. }