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.

37 lines
792 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. // RemoveFileArgs is an arg
  9. type RemoveFileArgs struct {
  10. ID string
  11. }
  12. // RemoveFile resolves the removeFIle mutation
  13. func (r *MutationResolver) RemoveFile(ctx context.Context, args *RemoveFileArgs) (*types.FileResolver, error) {
  14. token := auth.TokenFromContext(ctx)
  15. if !token.Permitted("member") {
  16. return nil, ErrUnauthorized
  17. }
  18. file, err := file.FindID(args.ID)
  19. if err != nil {
  20. return nil, err
  21. }
  22. if file.Author != token.UserID && !token.Permitted("file.remove") {
  23. return nil, ErrUnauthorized
  24. }
  25. err = file.Delete()
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &types.FileResolver{F: file}, nil
  30. }