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.

102 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. filter := args.Filter
  28. author := ""
  29. if filter != nil && filter.Author != nil {
  30. author = *filter.Author
  31. }
  32. category := ""
  33. if filter != nil && filter.Category != nil {
  34. category = *filter.Category
  35. }
  36. tags := make([]story.Tag, 0, 8)
  37. if filter != nil && filter.Tags != nil {
  38. for _, tagInput := range *filter.Tags {
  39. tags = append(tags, story.Tag{
  40. Kind: tagInput.Kind,
  41. Name: tagInput.Name,
  42. })
  43. }
  44. }
  45. earliest := time.Time{}
  46. err := error(nil)
  47. if filter != nil && filter.EarliestFictionalDate != nil {
  48. earliest, err = time.Parse(time.RFC3339Nano, *filter.EarliestFictionalDate)
  49. if err != nil {
  50. return nil, err
  51. }
  52. }
  53. latest := time.Time{}
  54. if filter != nil && filter.LatestFictionalDate != nil {
  55. latest, err = time.Parse(time.RFC3339Nano, *filter.LatestFictionalDate)
  56. if err != nil {
  57. return nil, err
  58. }
  59. }
  60. unlisted := filter.Unlisted != nil && *filter.Unlisted == true
  61. if unlisted {
  62. user := session.FromContext(ctx).User()
  63. if user == nil {
  64. return nil, ErrUnauthorized
  65. }
  66. if author != "" && author != user.ID && !user.Permitted("story.unlisted") {
  67. return nil, ErrPermissionDenied
  68. }
  69. author = user.ID
  70. }
  71. open := filter.Open
  72. limit := 0
  73. if filter != nil && filter.Limit != nil {
  74. limit = int(*filter.Limit)
  75. }
  76. stories, err := story.List(author, category, tags, earliest, latest, unlisted, open, limit)
  77. if err != nil {
  78. return nil, err
  79. }
  80. resolvers := make([]*types.StoryResolver, len(stories))
  81. for i, story := range stories {
  82. resolvers[i] = &types.StoryResolver{S: story}
  83. }
  84. return resolvers, nil
  85. }