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.

108 lines
2.2 KiB

  1. package queries
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  6. "git.aiterp.net/rpdata/api/internal/session"
  7. "git.aiterp.net/rpdata/api/model/story"
  8. )
  9. // StoriesArg is args for stories query
  10. type StoriesArg struct {
  11. Filter *struct {
  12. Author *string
  13. Tags *[]struct {
  14. Kind string
  15. Name string
  16. }
  17. EarliestFictionalDate *string
  18. LatestFictionalDate *string
  19. Category *string
  20. Limit *int32
  21. Open *bool
  22. Unlisted *bool
  23. }
  24. }
  25. // Stories resolves the stories query
  26. func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*types.StoryResolver, error) {
  27. var err error
  28. filter := args.Filter
  29. author := ""
  30. category := ""
  31. tags := make([]story.Tag, 0, 8)
  32. earliest := time.Time{}
  33. latest := time.Time{}
  34. unlisted := false
  35. open := (*bool)(nil)
  36. limit := 0
  37. if filter != nil {
  38. if filter.Author != nil {
  39. author = *filter.Author
  40. }
  41. if filter.Category != nil {
  42. category = *filter.Category
  43. }
  44. if filter.Tags != nil {
  45. for _, tagInput := range *filter.Tags {
  46. tags = append(tags, story.Tag{
  47. Kind: tagInput.Kind,
  48. Name: tagInput.Name,
  49. })
  50. }
  51. }
  52. if filter.EarliestFictionalDate != nil {
  53. earliest, err = time.Parse(time.RFC3339Nano, *filter.EarliestFictionalDate)
  54. if err != nil {
  55. return nil, err
  56. }
  57. }
  58. if filter.LatestFictionalDate != nil {
  59. latest, err = time.Parse(time.RFC3339Nano, *filter.LatestFictionalDate)
  60. if err != nil {
  61. return nil, err
  62. }
  63. }
  64. if filter.Limit != nil {
  65. limit = int(*filter.Limit)
  66. }
  67. unlisted = filter.Unlisted != nil && *filter.Unlisted == true
  68. if unlisted {
  69. user := session.FromContext(ctx).User()
  70. if user == nil {
  71. return nil, ErrUnauthorized
  72. }
  73. if author != "" && author != user.ID && !user.Permitted("story.unlisted") {
  74. return nil, ErrPermissionDenied
  75. }
  76. author = user.ID
  77. }
  78. open = filter.Open
  79. }
  80. stories, err := story.List(author, category, tags, earliest, latest, unlisted, open, limit)
  81. if err != nil {
  82. return nil, err
  83. }
  84. resolvers := make([]*types.StoryResolver, len(stories))
  85. for i, story := range stories {
  86. resolvers[i] = &types.StoryResolver{S: story}
  87. }
  88. return resolvers, nil
  89. }