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.

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