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.

167 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. // FileEditArgs is args for the editFile mutation
  50. type FileEditArgs struct {
  51. Input *struct {
  52. ID string
  53. Name *string
  54. Public *bool
  55. }
  56. }
  57. // EditFile resolves the editFile mutation
  58. func (r *MutationResolver) EditFile(ctx context.Context, args *FileEditArgs) (*FileResolver, error) {
  59. input := args.Input
  60. user := session.FromContext(ctx).User()
  61. if user == nil || !user.Permitted("member") {
  62. return nil, ErrUnauthorized
  63. }
  64. file, err := file.FindID(input.ID)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if file.Author != user.ID && !user.Permitted("file.edit") {
  69. return nil, ErrUnauthorized
  70. }
  71. err = file.Edit(input.Name, input.Public)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return &FileResolver{F: file}, nil
  76. }
  77. // RemoveFile resolves the removeFIle mutation
  78. func (r *MutationResolver) RemoveFile(ctx context.Context, args *FileArgs) (*FileResolver, error) {
  79. user := session.FromContext(ctx).User()
  80. if user == nil || !user.Permitted("member") {
  81. return nil, ErrUnauthorized
  82. }
  83. file, err := file.FindID(args.ID)
  84. if err != nil {
  85. return nil, err
  86. }
  87. if file.Author != user.ID && !user.Permitted("file.remove") {
  88. return nil, ErrUnauthorized
  89. }
  90. err = file.Delete()
  91. if err != nil {
  92. return nil, err
  93. }
  94. return &FileResolver{F: file}, nil
  95. }
  96. // ID resolves File.id
  97. func (f *FileResolver) ID() string {
  98. return f.F.ID
  99. }
  100. // Author resolves File.author
  101. func (f *FileResolver) Author() string {
  102. return f.F.Author
  103. }
  104. // Kind resolves File.kind
  105. func (f *FileResolver) Kind() string {
  106. return f.F.Kind
  107. }
  108. // Time resolves File.time
  109. func (f *FileResolver) Time() string {
  110. return f.F.Time.Format(time.RFC3339Nano)
  111. }
  112. // Public resolves File.public
  113. func (f *FileResolver) Public() bool {
  114. return f.F.Public
  115. }
  116. // Name resolves File.name
  117. func (f *FileResolver) Name() string {
  118. return f.F.Name
  119. }
  120. // MimeType resolves File.mimeType
  121. func (f *FileResolver) MimeType() string {
  122. return f.F.MimeType
  123. }
  124. // Size resolves File.size
  125. func (f *FileResolver) Size() int32 {
  126. return int32(f.F.Size)
  127. }
  128. // URL resolves File.url
  129. func (f *FileResolver) URL() *string {
  130. if f.F.URL == "" {
  131. return nil
  132. }
  133. return &f.F.URL
  134. }