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.

115 lines
2.4 KiB

  1. package resolver
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/internal/session"
  5. "git.aiterp.net/rpdata/api/model/file"
  6. "git.aiterp.net/rpdata/api/resolver/types"
  7. )
  8. // FileArgs is an arg
  9. type FileArgs struct {
  10. ID string
  11. }
  12. // File implements the file query
  13. func (r *QueryResolver) File(ctx context.Context, args *FileArgs) (*types.FileResolver, error) {
  14. file, err := file.FindID(args.ID)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return &types.FileResolver{F: file}, nil
  19. }
  20. // FilesArgs is an arg
  21. type FilesArgs struct {
  22. IncludePublic *bool
  23. MimeTypes *[]string
  24. }
  25. // Files implements the file query
  26. func (r *QueryResolver) Files(ctx context.Context, args *FilesArgs) ([]*types.FileResolver, error) {
  27. user := session.FromContext(ctx).User()
  28. author := ""
  29. if user != nil {
  30. author = user.ID
  31. }
  32. public := args.IncludePublic != nil && *args.IncludePublic == true
  33. mimeTypes := []string(nil)
  34. if args.MimeTypes != nil {
  35. mimeTypes = *args.MimeTypes
  36. }
  37. files, err := file.List(author, public, mimeTypes)
  38. if err != nil {
  39. return nil, err
  40. }
  41. resolvers := make([]*types.FileResolver, len(files))
  42. for i := range files {
  43. resolvers[i] = &types.FileResolver{F: files[i]}
  44. }
  45. return resolvers, nil
  46. }
  47. // FileEditArgs is args for the editFile mutation
  48. type FileEditArgs struct {
  49. Input *struct {
  50. ID string
  51. Name *string
  52. Public *bool
  53. }
  54. }
  55. // EditFile resolves the editFile mutation
  56. func (r *MutationResolver) EditFile(ctx context.Context, args *FileEditArgs) (*types.FileResolver, error) {
  57. input := args.Input
  58. user := session.FromContext(ctx).User()
  59. if user == nil || !user.Permitted("member") {
  60. return nil, ErrUnauthorized
  61. }
  62. file, err := file.FindID(input.ID)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if file.Author != user.ID && !user.Permitted("file.edit") {
  67. return nil, ErrUnauthorized
  68. }
  69. err = file.Edit(input.Name, input.Public)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return &types.FileResolver{F: file}, nil
  74. }
  75. // RemoveFile resolves the removeFIle mutation
  76. func (r *MutationResolver) RemoveFile(ctx context.Context, args *FileArgs) (*types.FileResolver, error) {
  77. user := session.FromContext(ctx).User()
  78. if user == nil || !user.Permitted("member") {
  79. return nil, ErrUnauthorized
  80. }
  81. file, err := file.FindID(args.ID)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if file.Author != user.ID && !user.Permitted("file.remove") {
  86. return nil, ErrUnauthorized
  87. }
  88. err = file.Delete()
  89. if err != nil {
  90. return nil, err
  91. }
  92. return &types.FileResolver{F: file}, nil
  93. }