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.

139 lines
3.6 KiB

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