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.

103 lines
2.6 KiB

  1. package queries
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "git.aiterp.net/rpdata/api/graph2/input"
  7. "git.aiterp.net/rpdata/api/internal/auth"
  8. "git.aiterp.net/rpdata/api/internal/loader"
  9. "git.aiterp.net/rpdata/api/models"
  10. "git.aiterp.net/rpdata/api/models/logs"
  11. "github.com/99designs/gqlgen/graphql"
  12. )
  13. // Queries
  14. func (r *resolver) Log(ctx context.Context, id string) (models.Log, error) {
  15. return logs.FindID(id)
  16. }
  17. func (r *resolver) Logs(ctx context.Context, filter *logs.Filter) ([]models.Log, error) {
  18. logs, err := logs.List(filter)
  19. if err != nil {
  20. return nil, err
  21. }
  22. reqCtx := graphql.GetRequestContext(ctx)
  23. maybeCharacters := strings.Contains(reqCtx.RawQuery, "characters")
  24. maybeChannels := strings.Contains(reqCtx.RawQuery, "channels")
  25. if len(logs) >= 100 && (maybeCharacters || maybeChannels) {
  26. loader := loader.FromContext(ctx)
  27. if loader == nil {
  28. return nil, errors.New("no loader")
  29. }
  30. for _, log := range logs {
  31. if maybeChannels {
  32. loader.PrimeChannels("name", log.ChannelName)
  33. }
  34. if maybeCharacters {
  35. loader.PrimeCharacters("id", log.CharacterIDs...)
  36. }
  37. }
  38. }
  39. return logs, nil
  40. }
  41. // Mutations
  42. func (r *mutationResolver) AddLog(ctx context.Context, input input.LogAddInput) (models.Log, error) {
  43. token := auth.TokenFromContext(ctx)
  44. if !token.Authenticated() || !token.Permitted("log.add") {
  45. return models.Log{}, errors.New("You are not permitted to add logs")
  46. }
  47. open := input.Open != nil && *input.Open == true
  48. title := ""
  49. if input.Title != nil {
  50. title = *input.Title
  51. }
  52. event := ""
  53. if input.Event != nil {
  54. event = *input.Event
  55. }
  56. description := ""
  57. if input.Description != nil {
  58. description = *input.Description
  59. }
  60. return logs.Add(input.Date, input.Channel, title, event, description, open)
  61. }
  62. func (r *mutationResolver) EditLog(ctx context.Context, input input.LogEditInput) (models.Log, error) {
  63. token := auth.TokenFromContext(ctx)
  64. if !token.Authenticated() || !token.Permitted("log.edit") {
  65. return models.Log{}, errors.New("You are not permitted to edit logs")
  66. }
  67. log, err := logs.FindID(input.ID)
  68. if err != nil {
  69. return models.Log{}, errors.New("Log not found")
  70. }
  71. return logs.Edit(log, input.Title, input.Event, input.Description, input.Open)
  72. }
  73. func (r *mutationResolver) RemoveLog(ctx context.Context, input input.LogRemoveInput) (models.Log, error) {
  74. token := auth.TokenFromContext(ctx)
  75. if !token.Authenticated() || !token.Permitted("log.remove") {
  76. return models.Log{}, errors.New("You are not permitted to remove logs")
  77. }
  78. log, err := logs.FindID(input.ID)
  79. if err != nil {
  80. return models.Log{}, errors.New("Log not found")
  81. }
  82. return logs.Remove(log)
  83. }