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.

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