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.

163 lines
3.3 KiB

  1. package resolver
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/internal/session"
  6. "git.aiterp.net/rpdata/api/model/file"
  7. )
  8. // FileResolver for the File graphql type
  9. type FileResolver struct{ F file.File }
  10. // FileArgs is an arg
  11. type FileArgs struct {
  12. ID string
  13. }
  14. // File implements the file query
  15. func (r *QueryResolver) File(ctx context.Context, args *FileArgs) (*FileResolver, error) {
  16. file, err := file.FindID(args.ID)
  17. if err != nil {
  18. return nil, err
  19. }
  20. return &FileResolver{F: file}, nil
  21. }
  22. // FilesArgs is an arg
  23. type FilesArgs struct {
  24. IncludePublic *bool
  25. MimeTypes *[]string
  26. }
  27. // Files implements the file query
  28. func (r *QueryResolver) Files(ctx context.Context, args *FilesArgs) ([]*FileResolver, error) {
  29. user := session.FromContext(ctx).User()
  30. author := ""
  31. if user != nil {
  32. author = user.ID
  33. }
  34. public := args.IncludePublic != nil && *args.IncludePublic == true
  35. mimeTypes := []string(nil)
  36. if args.MimeTypes != nil {
  37. mimeTypes = *args.MimeTypes
  38. }
  39. files, err := file.List(author, public, mimeTypes)
  40. if err != nil {
  41. return nil, err
  42. }
  43. resolvers := make([]*FileResolver, len(files))
  44. for i := range files {
  45. resolvers[i] = &FileResolver{F: files[i]}
  46. }
  47. return resolvers, nil
  48. }
  49. // FileEditInput is an input for the editFile mutation
  50. type FileEditInput struct {
  51. ID string
  52. Name *string
  53. Public *bool
  54. }
  55. // EditFile resolves the editFile mutation
  56. func (r *MutationResolver) EditFile(ctx context.Context, args *struct{ Input FileEditInput }) (*FileResolver, error) {
  57. user := session.FromContext(ctx).User()
  58. if user == nil || !user.Permitted("member") {
  59. return nil, ErrUnauthorized
  60. }
  61. file, err := file.FindID(args.Input.ID)
  62. if err != nil {
  63. return nil, err
  64. }
  65. if file.Author != user.ID && !user.Permitted("file.edit") {
  66. return nil, ErrUnauthorized
  67. }
  68. err = file.Edit(args.Input.Name, args.Input.Public)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return &FileResolver{F: file}, nil
  73. }
  74. // RemoveFile resolves the removeFIle mutation
  75. func (r *MutationResolver) RemoveFile(ctx context.Context, args *FileArgs) (*FileResolver, error) {
  76. user := session.FromContext(ctx).User()
  77. if user == nil || !user.Permitted("member") {
  78. return nil, ErrUnauthorized
  79. }
  80. file, err := file.FindID(args.ID)
  81. if err != nil {
  82. return nil, err
  83. }
  84. if file.Author != user.ID && !user.Permitted("file.remove") {
  85. return nil, ErrUnauthorized
  86. }
  87. err = file.Delete()
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &FileResolver{F: file}, nil
  92. }
  93. // ID resolves File.id
  94. func (f *FileResolver) ID() string {
  95. return f.F.ID
  96. }
  97. // Author resolves File.author
  98. func (f *FileResolver) Author() string {
  99. return f.F.Author
  100. }
  101. // Kind resolves File.kind
  102. func (f *FileResolver) Kind() string {
  103. return f.F.Kind
  104. }
  105. // Time resolves File.time
  106. func (f *FileResolver) Time() string {
  107. return f.F.Time.Format(time.RFC3339Nano)
  108. }
  109. // Public resolves File.public
  110. func (f *FileResolver) Public() bool {
  111. return f.F.Public
  112. }
  113. // Name resolves File.name
  114. func (f *FileResolver) Name() string {
  115. return f.F.Name
  116. }
  117. // MimeType resolves File.mimeType
  118. func (f *FileResolver) MimeType() string {
  119. return f.F.MimeType
  120. }
  121. // Size resolves File.size
  122. func (f *FileResolver) Size() int32 {
  123. return int32(f.F.Size)
  124. }
  125. // URL resolves File.url
  126. func (f *FileResolver) URL() *string {
  127. if f.F.URL == "" {
  128. return nil
  129. }
  130. return &f.F.URL
  131. }